blob: 76a6044a81b1a0538e79c363b8ade8a7cef9a3f9 [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
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
129/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
132 */
133static int current_copyID = 0;
134
135/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000136 * Array to hold the hashtab with variables local to each sourced script.
137 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000139typedef struct
140{
141 dictitem_T sv_var;
142 dict_T sv_dict;
143} scriptvar_T;
144
145static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
146#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
147#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149static int echo_attr = 0; /* attributes used for ":echo" */
150
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000151/* Values for trans_function_name() argument: */
152#define TFN_INT 1 /* internal function name OK */
153#define TFN_QUIET 2 /* no error messages */
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Structure to hold info for a user function.
157 */
158typedef struct ufunc ufunc_T;
159
160struct ufunc
161{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000162 int uf_varargs; /* variable nr of arguments */
163 int uf_flags;
164 int uf_calls; /* nr of active calls */
165 garray_T uf_args; /* arguments */
166 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000167#ifdef FEAT_PROFILE
168 int uf_profiling; /* TRUE when func is being profiled */
169 /* profiling the function as a whole */
170 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000171 proftime_T uf_tm_total; /* time spent in function + children */
172 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173 proftime_T uf_tm_children; /* time spent in children this call */
174 /* profiling the function per line */
175 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T *uf_tml_total; /* time spent in a line + children */
177 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tml_start; /* start time for current line */
179 proftime_T uf_tml_children; /* time spent in children for this line */
180 proftime_T uf_tml_wait; /* start wait time for current line */
181 int uf_tml_idx; /* index of line being timed; -1 if none */
182 int uf_tml_execed; /* line being timed was executed */
183#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000184 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000186 int uf_refcount; /* for numbered function: reference count */
187 char_u uf_name[1]; /* name of function (actually longer); can
188 start with <SNR>123_ (<SNR> is K_SPECIAL
189 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190};
191
192/* function flags */
193#define FC_ABORT 1 /* abort function on error */
194#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000195#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000198 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000200static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000202/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000203static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
204
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205/* list heads for garbage collection */
206static dict_T *first_dict = NULL; /* list of all dicts */
207static list_T *first_list = NULL; /* list of all lists */
208
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000209/* From user function to hashitem and back. */
210static ufunc_T dumuf;
211#define UF2HIKEY(fp) ((fp)->uf_name)
212#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
213#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
214
215#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
216#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaar33570922005-01-25 22:26:29 +0000218#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
219#define VAR_SHORT_LEN 20 /* short variable name length */
220#define FIXVAR_CNT 12 /* number of fixed variables */
221
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000223typedef struct funccall_S funccall_T;
224
225struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226{
227 ufunc_T *func; /* function being called */
228 int linenr; /* next line to be executed */
229 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000230 struct /* fixed variables for arguments */
231 {
232 dictitem_T var; /* variable (without room for name) */
233 char_u room[VAR_SHORT_LEN]; /* room for the name */
234 } fixvar[FIXVAR_CNT];
235 dict_T l_vars; /* l: local function variables */
236 dictitem_T l_vars_var; /* variable for l: scope */
237 dict_T l_avars; /* a: argument variables */
238 dictitem_T l_avars_var; /* variable for a: scope */
239 list_T l_varlist; /* list for a:000 */
240 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
241 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 linenr_T breakpoint; /* next line with breakpoint or zero */
243 int dbg_tick; /* debug_tick when breakpoint was set */
244 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000245#ifdef FEAT_PROFILE
246 proftime_T prof_child; /* time spent in a child */
247#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000248 funccall_T *caller; /* calling function or NULL */
249};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252 * Info used by a ":for" loop.
253 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000255{
256 int fi_semicolon; /* TRUE if ending in '; var]' */
257 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000258 listwatch_T fi_lw; /* keep an eye on the item used. */
259 list_T *fi_list; /* list being used */
260} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000263 * Struct used by trans_function_name()
264 */
265typedef struct
266{
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000268 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 dictitem_T *fd_di; /* Dictionary item used */
270} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000271
Bram Moolenaara7043832005-01-21 11:56:39 +0000272
273/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 * Array to hold the value of v: variables.
275 * The value is in a dictitem, so that it can also be used in the v: scope.
276 * The reason to use this table anyway is for very quick access to the
277 * variables with the VV_ defines.
278 */
279#include "version.h"
280
281/* values for vv_flags: */
282#define VV_COMPAT 1 /* compatible, also used without "v:" */
283#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000284#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000286#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000287
288static struct vimvar
289{
290 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291 dictitem_T vv_di; /* value and name for key */
292 char vv_filler[16]; /* space for LONGEST name below!!! */
293 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
294} vimvars[VV_LEN] =
295{
296 /*
297 * The order here must match the VV_ defines in vim.h!
298 * Initializing a union does not work, leave tv.vval empty to get zero's.
299 */
300 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
301 {VV_NAME("count1", VAR_NUMBER), VV_RO},
302 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
303 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
304 {VV_NAME("warningmsg", VAR_STRING), 0},
305 {VV_NAME("statusmsg", VAR_STRING), 0},
306 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
308 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
310 {VV_NAME("termresponse", VAR_STRING), VV_RO},
311 {VV_NAME("fname", VAR_STRING), VV_RO},
312 {VV_NAME("lang", VAR_STRING), VV_RO},
313 {VV_NAME("lc_time", VAR_STRING), VV_RO},
314 {VV_NAME("ctype", VAR_STRING), VV_RO},
315 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
316 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
317 {VV_NAME("fname_in", VAR_STRING), VV_RO},
318 {VV_NAME("fname_out", VAR_STRING), VV_RO},
319 {VV_NAME("fname_new", VAR_STRING), VV_RO},
320 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
321 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
322 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
324 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
325 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("progname", VAR_STRING), VV_RO},
327 {VV_NAME("servername", VAR_STRING), VV_RO},
328 {VV_NAME("dying", VAR_NUMBER), VV_RO},
329 {VV_NAME("exception", VAR_STRING), VV_RO},
330 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
331 {VV_NAME("register", VAR_STRING), VV_RO},
332 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
333 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000334 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
335 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000336 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000337 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
338 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000339 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
341 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000344 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000345 {VV_NAME("swapname", VAR_STRING), VV_RO},
346 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000347 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000348 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000349 {VV_NAME("mouse_win", VAR_NUMBER), 0},
350 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
351 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000352 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000353 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000354 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000355};
356
357/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358#define vv_type vv_di.di_tv.v_type
359#define vv_nr vv_di.di_tv.vval.v_number
360#define vv_float vv_di.di_tv.vval.v_float
361#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000362#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000364
365/*
366 * The v: variables are stored in dictionary "vimvardict".
367 * "vimvars_var" is the variable that is used for the "l:" scope.
368 */
369static dict_T vimvardict;
370static dictitem_T vimvars_var;
371#define vimvarht vimvardict.dv_hashtab
372
Bram Moolenaara40058a2005-07-11 22:42:07 +0000373static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
374static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
375#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
376static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
377#endif
378static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
379static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
380static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
382static void list_glob_vars __ARGS((int *first));
383static void list_buf_vars __ARGS((int *first));
384static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_vim_vars __ARGS((int *first));
389static void list_script_vars __ARGS((int *first));
390static void list_func_vars __ARGS((int *first));
391static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000392static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
393static int check_changedtick __ARGS((char_u *arg));
394static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
395static void clear_lval __ARGS((lval_T *lp));
396static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
397static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
398static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
399static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static listitem_T *listitem_alloc __ARGS((void));
423static void listitem_free __ARGS((listitem_T *item));
424static void listitem_remove __ARGS((list_T *l, listitem_T *item));
425static long list_len __ARGS((list_T *l));
426static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
427static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
428static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000430static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_append __ARGS((list_T *l, listitem_T *item));
433static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000442static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
443static void set_ref_in_list __ARGS((list_T *l, int copyID));
444static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000446static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static dictitem_T *dictitem_alloc __ARGS((char_u *key));
448static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
449static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
450static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000451static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int dict_add __ARGS((dict_T *d, dictitem_T *item));
453static long dict_len __ARGS((dict_T *d));
454static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static 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));
467static int call_func __ARGS((char_u *name, 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 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
473#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
510#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
514static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000537static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000546static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000548static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000554static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000562static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000563static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000564static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000565static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000568static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000576static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000590static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
609static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
610#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000615static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000616static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000619static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000623#ifdef vim_mkdir
624static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000630#ifdef FEAT_FLOAT
631static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000634static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000635static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000638static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000650#ifdef FEAT_FLOAT
651static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000654static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000663static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000664static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000665static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000666static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000668static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000670static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000672#ifdef FEAT_FLOAT
673static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
674#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000676static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000677static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000680#ifdef FEAT_FLOAT
681static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000684static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685#ifdef HAVE_STRFTIME
686static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
687#endif
688static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000699static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000701static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000702static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000703static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000704static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000705static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000707static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
713#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000724static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000727static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000729static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000730static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000731static int get_env_len __ARGS((char_u **arg));
732static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000733static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000734static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
735#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
736#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
737 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000738static 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 +0000739static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000740static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000741static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
742static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static typval_T *alloc_tv __ARGS((void));
744static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void init_tv __ARGS((typval_T *varp));
746static long get_tv_number __ARGS((typval_T *varp));
747static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000748static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static char_u *get_tv_string __ARGS((typval_T *varp));
750static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000751static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000753static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
755static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
756static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000757static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
758static 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 +0000759static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
760static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000761static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000762static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000764static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
766static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
767static int eval_fname_script __ARGS((char_u *p));
768static int eval_fname_sid __ARGS((char_u *p));
769static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static ufunc_T *find_func __ARGS((char_u *name));
771static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000772static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773#ifdef FEAT_PROFILE
774static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000775static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
776static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
777static int
778# ifdef __BORLANDC__
779 _RTLENTRYF
780# endif
781 prof_total_cmp __ARGS((const void *s1, const void *s2));
782static int
783# ifdef __BORLANDC__
784 _RTLENTRYF
785# endif
786 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000788static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000789static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000790static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static void func_free __ARGS((ufunc_T *fp));
792static void func_unref __ARGS((char_u *name));
793static void func_ref __ARGS((char_u *name));
794static 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 +0000795static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
796static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000798static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
799static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000800static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000801static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000802static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000804/* Character used as separated in autoload function/variable names. */
805#define AUTOLOAD_CHAR '#'
806
Bram Moolenaar33570922005-01-25 22:26:29 +0000807/*
808 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000809 */
810 void
811eval_init()
812{
Bram Moolenaar33570922005-01-25 22:26:29 +0000813 int i;
814 struct vimvar *p;
815
816 init_var_dict(&globvardict, &globvars_var);
817 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000818 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000820
821 for (i = 0; i < VV_LEN; ++i)
822 {
823 p = &vimvars[i];
824 STRCPY(p->vv_di.di_key, p->vv_name);
825 if (p->vv_flags & VV_RO)
826 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
827 else if (p->vv_flags & VV_RO_SBX)
828 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
829 else
830 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000831
832 /* add to v: scope dict, unless the value is not always available */
833 if (p->vv_type != VAR_UNKNOWN)
834 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000836 /* add to compat scope dict */
837 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000839 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000840}
841
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000842#if defined(EXITFREE) || defined(PROTO)
843 void
844eval_clear()
845{
846 int i;
847 struct vimvar *p;
848
849 for (i = 0; i < VV_LEN; ++i)
850 {
851 p = &vimvars[i];
852 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000853 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000854 vim_free(p->vv_str);
855 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000856 }
857 else if (p->vv_di.di_tv.v_type == VAR_LIST)
858 {
859 list_unref(p->vv_list);
860 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000861 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000862 }
863 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000864 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000865 hash_clear(&compat_hashtab);
866
867 /* script-local variables */
868 for (i = 1; i <= ga_scripts.ga_len; ++i)
869 vars_clear(&SCRIPT_VARS(i));
870 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000871 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000872
873 /* global variables */
874 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000875
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000876 /* autoloaded script names */
877 ga_clear_strings(&ga_loaded);
878
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000879 /* unreferenced lists and dicts */
880 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000881
882 /* functions */
883 free_all_functions();
884 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000885}
886#endif
887
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 * Return the name of the executed function.
890 */
891 char_u *
892func_name(cookie)
893 void *cookie;
894{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000895 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896}
897
898/*
899 * Return the address holding the next breakpoint line for a funccall cookie.
900 */
901 linenr_T *
902func_breakpoint(cookie)
903 void *cookie;
904{
Bram Moolenaar33570922005-01-25 22:26:29 +0000905 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906}
907
908/*
909 * Return the address holding the debug tick for a funccall cookie.
910 */
911 int *
912func_dbg_tick(cookie)
913 void *cookie;
914{
Bram Moolenaar33570922005-01-25 22:26:29 +0000915 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916}
917
918/*
919 * Return the nesting level for a funccall cookie.
920 */
921 int
922func_level(cookie)
923 void *cookie;
924{
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926}
927
928/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000929funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000931/* pointer to list of previously used funccal, still around because some
932 * item in it is still being used. */
933funccall_T *previous_funccal = NULL;
934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935/*
936 * Return TRUE when a function was ended by a ":return" command.
937 */
938 int
939current_func_returned()
940{
941 return current_funccal->returned;
942}
943
944
945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 * Set an internal variable to a string value. Creates the variable if it does
947 * not already exist.
948 */
949 void
950set_internal_string_var(name, value)
951 char_u *name;
952 char_u *value;
953{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000954 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 val = vim_strsave(value);
958 if (val != NULL)
959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000961 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000963 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 }
967}
968
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000969static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000970static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000971static char_u *redir_endp = NULL;
972static char_u *redir_varname = NULL;
973
974/*
975 * Start recording command output to a variable
976 * Returns OK if successfully completed the setup. FAIL otherwise.
977 */
978 int
979var_redir_start(name, append)
980 char_u *name;
981 int append; /* append to an existing variable */
982{
983 int save_emsg;
984 int err;
985 typval_T tv;
986
987 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000988 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000989 {
990 EMSG(_(e_invarg));
991 return FAIL;
992 }
993
994 redir_varname = vim_strsave(name);
995 if (redir_varname == NULL)
996 return FAIL;
997
998 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
999 if (redir_lval == NULL)
1000 {
1001 var_redir_stop();
1002 return FAIL;
1003 }
1004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 /* The output is stored in growarray "redir_ga" until redirection ends. */
1006 ga_init2(&redir_ga, (int)sizeof(char), 500);
1007
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001009 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1010 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1012 {
1013 if (redir_endp != NULL && *redir_endp != NUL)
1014 /* Trailing characters are present after the variable name */
1015 EMSG(_(e_trailing));
1016 else
1017 EMSG(_(e_invarg));
1018 var_redir_stop();
1019 return FAIL;
1020 }
1021
1022 /* check if we can write to the variable: set it to or append an empty
1023 * string */
1024 save_emsg = did_emsg;
1025 did_emsg = FALSE;
1026 tv.v_type = VAR_STRING;
1027 tv.vval.v_string = (char_u *)"";
1028 if (append)
1029 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1030 else
1031 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1032 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001033 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 if (err)
1035 {
1036 var_redir_stop();
1037 return FAIL;
1038 }
1039 if (redir_lval->ll_newkey != NULL)
1040 {
1041 /* Dictionary item was created, don't do it again. */
1042 vim_free(redir_lval->ll_newkey);
1043 redir_lval->ll_newkey = NULL;
1044 }
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001050 * Append "value[value_len]" to the variable set by var_redir_start().
1051 * The actual appending is postponed until redirection ends, because the value
1052 * appended may in fact be the string we write to, changing it may cause freed
1053 * memory to be used:
1054 * :redir => foo
1055 * :let foo
1056 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 */
1058 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001059var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001061 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001063 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064
1065 if (redir_lval == NULL)
1066 return;
1067
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001068 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001069 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001071 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001073 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001074 {
1075 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001076 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001077 }
1078 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080}
1081
1082/*
1083 * Stop redirecting command output to a variable.
1084 */
1085 void
1086var_redir_stop()
1087{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001088 typval_T tv;
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_lval != NULL)
1091 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001092 /* Append the trailing NUL. */
1093 ga_append(&redir_ga, NUL);
1094
1095 /* Assign the text to the variable. */
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = redir_ga.ga_data;
1098 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1099 vim_free(tv.vval.v_string);
1100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 clear_lval(redir_lval);
1102 vim_free(redir_lval);
1103 redir_lval = NULL;
1104 }
1105 vim_free(redir_varname);
1106 redir_varname = NULL;
1107}
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109# if defined(FEAT_MBYTE) || defined(PROTO)
1110 int
1111eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1112 char_u *enc_from;
1113 char_u *enc_to;
1114 char_u *fname_from;
1115 char_u *fname_to;
1116{
1117 int err = FALSE;
1118
1119 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1120 set_vim_var_string(VV_CC_TO, enc_to, -1);
1121 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1122 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1123 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1124 err = TRUE;
1125 set_vim_var_string(VV_CC_FROM, NULL, -1);
1126 set_vim_var_string(VV_CC_TO, NULL, -1);
1127 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1128 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1129
1130 if (err)
1131 return FAIL;
1132 return OK;
1133}
1134# endif
1135
1136# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1137 int
1138eval_printexpr(fname, args)
1139 char_u *fname;
1140 char_u *args;
1141{
1142 int err = FALSE;
1143
1144 set_vim_var_string(VV_FNAME_IN, fname, -1);
1145 set_vim_var_string(VV_CMDARG, args, -1);
1146 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1147 err = TRUE;
1148 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1149 set_vim_var_string(VV_CMDARG, NULL, -1);
1150
1151 if (err)
1152 {
1153 mch_remove(fname);
1154 return FAIL;
1155 }
1156 return OK;
1157}
1158# endif
1159
1160# if defined(FEAT_DIFF) || defined(PROTO)
1161 void
1162eval_diff(origfile, newfile, outfile)
1163 char_u *origfile;
1164 char_u *newfile;
1165 char_u *outfile;
1166{
1167 int err = FALSE;
1168
1169 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1170 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1171 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1172 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1173 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1174 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1175 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1176}
1177
1178 void
1179eval_patch(origfile, difffile, outfile)
1180 char_u *origfile;
1181 char_u *difffile;
1182 char_u *outfile;
1183{
1184 int err;
1185
1186 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1187 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1188 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1189 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1192 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1193}
1194# endif
1195
1196/*
1197 * Top level evaluation function, returning a boolean.
1198 * Sets "error" to TRUE if there was an error.
1199 * Return TRUE or FALSE.
1200 */
1201 int
1202eval_to_bool(arg, error, nextcmd, skip)
1203 char_u *arg;
1204 int *error;
1205 char_u **nextcmd;
1206 int skip; /* only parse, don't execute */
1207{
Bram Moolenaar33570922005-01-25 22:26:29 +00001208 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 int retval = FALSE;
1210
1211 if (skip)
1212 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else
1216 {
1217 *error = FALSE;
1218 if (!skip)
1219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001220 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
1224 if (skip)
1225 --emsg_skip;
1226
1227 return retval;
1228}
1229
1230/*
1231 * Top level evaluation function, returning a string. If "skip" is TRUE,
1232 * only parsing to "nextcmd" is done, without reporting errors. Return
1233 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1234 */
1235 char_u *
1236eval_to_string_skip(arg, nextcmd, skip)
1237 char_u *arg;
1238 char_u **nextcmd;
1239 int skip; /* only parse, don't execute */
1240{
Bram Moolenaar33570922005-01-25 22:26:29 +00001241 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 char_u *retval;
1243
1244 if (skip)
1245 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 retval = NULL;
1248 else
1249 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001250 retval = vim_strsave(get_tv_string(&tv));
1251 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 if (skip)
1254 --emsg_skip;
1255
1256 return retval;
1257}
1258
1259/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001260 * Skip over an expression at "*pp".
1261 * Return FAIL for an error, OK otherwise.
1262 */
1263 int
1264skip_expr(pp)
1265 char_u **pp;
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001268
1269 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001271}
1272
1273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001275 * When "convert" is TRUE convert a List into a sequence of lines and convert
1276 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 * Return pointer to allocated memory, or NULL for failure.
1278 */
1279 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001280eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 char_u *arg;
1282 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001283 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
Bram Moolenaar33570922005-01-25 22:26:29 +00001285 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001287 garray_T ga;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001288 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 retval = NULL;
1292 else
1293 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001294 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001295 {
1296 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001297 if (tv.vval.v_list != NULL)
1298 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001299 ga_append(&ga, NUL);
1300 retval = (char_u *)ga.ga_data;
1301 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001302#ifdef FEAT_FLOAT
1303 else if (convert && tv.v_type == VAR_FLOAT)
1304 {
1305 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1306 retval = vim_strsave(numbuf);
1307 }
1308#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001309 else
1310 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313
1314 return retval;
1315}
1316
1317/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001318 * Call eval_to_string() without using current local variables and using
1319 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 */
1321 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001322eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 char_u *arg;
1324 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001325 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326{
1327 char_u *retval;
1328 void *save_funccalp;
1329
1330 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001331 if (use_sandbox)
1332 ++sandbox;
1333 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001334 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001335 if (use_sandbox)
1336 --sandbox;
1337 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 restore_funccal(save_funccalp);
1339 return retval;
1340}
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342/*
1343 * Top level evaluation function, returning a number.
1344 * Evaluates "expr" silently.
1345 * Returns -1 for an error.
1346 */
1347 int
1348eval_to_number(expr)
1349 char_u *expr;
1350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001353 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
1355 ++emsg_off;
1356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 retval = -1;
1359 else
1360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001361 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 --emsg_off;
1365
1366 return retval;
1367}
1368
Bram Moolenaara40058a2005-07-11 22:42:07 +00001369/*
1370 * Prepare v: variable "idx" to be used.
1371 * Save the current typeval in "save_tv".
1372 * When not used yet add the variable to the v: hashtable.
1373 */
1374 static void
1375prepare_vimvar(idx, save_tv)
1376 int idx;
1377 typval_T *save_tv;
1378{
1379 *save_tv = vimvars[idx].vv_tv;
1380 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1381 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1382}
1383
1384/*
1385 * Restore v: variable "idx" to typeval "save_tv".
1386 * When no longer defined, remove the variable from the v: hashtable.
1387 */
1388 static void
1389restore_vimvar(idx, save_tv)
1390 int idx;
1391 typval_T *save_tv;
1392{
1393 hashitem_T *hi;
1394
Bram Moolenaara40058a2005-07-11 22:42:07 +00001395 vimvars[idx].vv_tv = *save_tv;
1396 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1397 {
1398 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1399 if (HASHITEM_EMPTY(hi))
1400 EMSG2(_(e_intern2), "restore_vimvar()");
1401 else
1402 hash_remove(&vimvarht, hi);
1403 }
1404}
1405
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001406#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001407/*
1408 * Evaluate an expression to a list with suggestions.
1409 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001410 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001411 */
1412 list_T *
1413eval_spell_expr(badword, expr)
1414 char_u *badword;
1415 char_u *expr;
1416{
1417 typval_T save_val;
1418 typval_T rettv;
1419 list_T *list = NULL;
1420 char_u *p = skipwhite(expr);
1421
1422 /* Set "v:val" to the bad word. */
1423 prepare_vimvar(VV_VAL, &save_val);
1424 vimvars[VV_VAL].vv_type = VAR_STRING;
1425 vimvars[VV_VAL].vv_str = badword;
1426 if (p_verbose == 0)
1427 ++emsg_off;
1428
1429 if (eval1(&p, &rettv, TRUE) == OK)
1430 {
1431 if (rettv.v_type != VAR_LIST)
1432 clear_tv(&rettv);
1433 else
1434 list = rettv.vval.v_list;
1435 }
1436
1437 if (p_verbose == 0)
1438 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001439 restore_vimvar(VV_VAL, &save_val);
1440
1441 return list;
1442}
1443
1444/*
1445 * "list" is supposed to contain two items: a word and a number. Return the
1446 * word in "pp" and the number as the return value.
1447 * Return -1 if anything isn't right.
1448 * Used to get the good word and score from the eval_spell_expr() result.
1449 */
1450 int
1451get_spellword(list, pp)
1452 list_T *list;
1453 char_u **pp;
1454{
1455 listitem_T *li;
1456
1457 li = list->lv_first;
1458 if (li == NULL)
1459 return -1;
1460 *pp = get_tv_string(&li->li_tv);
1461
1462 li = li->li_next;
1463 if (li == NULL)
1464 return -1;
1465 return get_tv_number(&li->li_tv);
1466}
1467#endif
1468
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001469/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001470 * Top level evaluation function.
1471 * Returns an allocated typval_T with the result.
1472 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001473 */
1474 typval_T *
1475eval_expr(arg, nextcmd)
1476 char_u *arg;
1477 char_u **nextcmd;
1478{
1479 typval_T *tv;
1480
1481 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001482 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001483 {
1484 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001485 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001486 }
1487
1488 return tv;
1489}
1490
1491
Bram Moolenaar4f688582007-07-24 12:34:30 +00001492#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1493 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001495 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001496 * Uses argv[argc] for the function arguments. Only Number and String
1497 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001498 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500 static int
1501call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 char_u *func;
1503 int argc;
1504 char_u **argv;
1505 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
Bram Moolenaar33570922005-01-25 22:26:29 +00001508 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 long n;
1510 int len;
1511 int i;
1512 int doesrange;
1513 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001514 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001516 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001518 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 for (i = 0; i < argc; i++)
1521 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001522 /* Pass a NULL or empty argument as an empty string */
1523 if (argv[i] == NULL || *argv[i] == NUL)
1524 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001525 argvars[i].v_type = VAR_STRING;
1526 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001527 continue;
1528 }
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 /* Recognize a number argument, the others must be strings. */
1531 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1532 if (len != 0 && len == (int)STRLEN(argv[i]))
1533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001534 argvars[i].v_type = VAR_NUMBER;
1535 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537 else
1538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001539 argvars[i].v_type = VAR_STRING;
1540 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 }
1542 }
1543
1544 if (safe)
1545 {
1546 save_funccalp = save_funccal();
1547 ++sandbox;
1548 }
1549
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1551 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (safe)
1555 {
1556 --sandbox;
1557 restore_funccal(save_funccalp);
1558 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 vim_free(argvars);
1560
1561 if (ret == FAIL)
1562 clear_tv(rettv);
1563
1564 return ret;
1565}
1566
Bram Moolenaar4f688582007-07-24 12:34:30 +00001567# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001569 * Call vimL function "func" and return the result as a string.
1570 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Uses argv[argc] for the function arguments.
1572 */
1573 void *
1574call_func_retstr(func, argc, argv, safe)
1575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
1579{
1580 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001581 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582
1583 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1584 return NULL;
1585
1586 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 return retval;
1589}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001590# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591
Bram Moolenaar4f688582007-07-24 12:34:30 +00001592# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001594 * Call vimL function "func" and return the result as a number.
1595 * Returns -1 when calling the function fails.
1596 * Uses argv[argc] for the function arguments.
1597 */
1598 long
1599call_func_retnr(func, argc, argv, safe)
1600 char_u *func;
1601 int argc;
1602 char_u **argv;
1603 int safe; /* use the sandbox */
1604{
1605 typval_T rettv;
1606 long retval;
1607
1608 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1609 return -1;
1610
1611 retval = get_tv_number_chk(&rettv, NULL);
1612 clear_tv(&rettv);
1613 return retval;
1614}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001615# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001616
1617/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001618 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001620 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 */
1622 void *
1623call_func_retlist(func, argc, argv, safe)
1624 char_u *func;
1625 int argc;
1626 char_u **argv;
1627 int safe; /* use the sandbox */
1628{
1629 typval_T rettv;
1630
1631 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1632 return NULL;
1633
1634 if (rettv.v_type != VAR_LIST)
1635 {
1636 clear_tv(&rettv);
1637 return NULL;
1638 }
1639
1640 return rettv.vval.v_list;
1641}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643
Bram Moolenaar4f688582007-07-24 12:34:30 +00001644
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645/*
1646 * Save the current function call pointer, and set it to NULL.
1647 * Used when executing autocommands and for ":source".
1648 */
1649 void *
1650save_funccal()
1651{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001652 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 current_funccal = NULL;
1655 return (void *)fc;
1656}
1657
1658 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001659restore_funccal(vfc)
1660 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001662 funccall_T *fc = (funccall_T *)vfc;
1663
1664 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665}
1666
Bram Moolenaar05159a02005-02-26 23:04:13 +00001667#if defined(FEAT_PROFILE) || defined(PROTO)
1668/*
1669 * Prepare profiling for entering a child or something else that is not
1670 * counted for the script/function itself.
1671 * Should always be called in pair with prof_child_exit().
1672 */
1673 void
1674prof_child_enter(tm)
1675 proftime_T *tm; /* place to store waittime */
1676{
1677 funccall_T *fc = current_funccal;
1678
1679 if (fc != NULL && fc->func->uf_profiling)
1680 profile_start(&fc->prof_child);
1681 script_prof_save(tm);
1682}
1683
1684/*
1685 * Take care of time spent in a child.
1686 * Should always be called after prof_child_enter().
1687 */
1688 void
1689prof_child_exit(tm)
1690 proftime_T *tm; /* where waittime was stored */
1691{
1692 funccall_T *fc = current_funccal;
1693
1694 if (fc != NULL && fc->func->uf_profiling)
1695 {
1696 profile_end(&fc->prof_child);
1697 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1698 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1699 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1700 }
1701 script_prof_restore(tm);
1702}
1703#endif
1704
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#ifdef FEAT_FOLDING
1707/*
1708 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1709 * it in "*cp". Doesn't give error messages.
1710 */
1711 int
1712eval_foldexpr(arg, cp)
1713 char_u *arg;
1714 int *cp;
1715{
Bram Moolenaar33570922005-01-25 22:26:29 +00001716 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 int retval;
1718 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001719 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1720 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001723 if (use_sandbox)
1724 ++sandbox;
1725 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 retval = 0;
1729 else
1730 {
1731 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 if (tv.v_type == VAR_NUMBER)
1733 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001734 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 retval = 0;
1736 else
1737 {
1738 /* If the result is a string, check if there is a non-digit before
1739 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (!VIM_ISDIGIT(*s) && *s != '-')
1742 *cp = *s++;
1743 retval = atol((char *)s);
1744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001745 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001748 if (use_sandbox)
1749 --sandbox;
1750 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752 return retval;
1753}
1754#endif
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757 * ":let" list all variable values
1758 * ":let var1 var2" list variable values
1759 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 * ":let var += expr" assignment command.
1761 * ":let var -= expr" assignment command.
1762 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 */
1765 void
1766ex_let(eap)
1767 exarg_T *eap;
1768{
1769 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001771 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 int var_count = 0;
1774 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001776 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001777 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaardb552d602006-03-23 22:59:57 +00001779 argend = skip_var_list(arg, &var_count, &semicolon);
1780 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001782 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1783 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001784 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001787 /*
1788 * ":let" without "=": list variables
1789 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 if (*arg == '[')
1791 EMSG(_(e_invarg));
1792 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001794 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001798 list_glob_vars(&first);
1799 list_buf_vars(&first);
1800 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001801#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001802 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001803#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001804 list_script_vars(&first);
1805 list_func_vars(&first);
1806 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 eap->nextcmd = check_nextcmd(arg);
1809 }
1810 else
1811 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 op[0] = '=';
1813 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001814 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 {
1816 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1817 op[0] = expr[-1]; /* +=, -= or .= */
1818 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (eap->skip)
1822 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (eap->skip)
1825 {
1826 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 --emsg_skip;
1829 }
1830 else if (i != FAIL)
1831 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001833 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836 }
1837}
1838
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839/*
1840 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1841 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 * When "nextchars" is not NULL it points to a string with characters that
1843 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1844 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 * Returns OK or FAIL;
1846 */
1847 static int
1848ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1849 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 int copy; /* copy values from "tv", don't move */
1852 int semicolon; /* from skip_var_list() */
1853 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001854 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855{
1856 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001859 listitem_T *item;
1860 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861
1862 if (*arg != '[')
1863 {
1864 /*
1865 * ":let var = expr" or ":for var in list"
1866 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return FAIL;
1869 return OK;
1870 }
1871
1872 /*
1873 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1874 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001875 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 {
1877 EMSG(_(e_listreq));
1878 return FAIL;
1879 }
1880
1881 i = list_len(l);
1882 if (semicolon == 0 && var_count < i)
1883 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001884 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 return FAIL;
1886 }
1887 if (var_count - semicolon > i)
1888 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001889 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 return FAIL;
1891 }
1892
1893 item = l->lv_first;
1894 while (*arg != ']')
1895 {
1896 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 item = item->li_next;
1899 if (arg == NULL)
1900 return FAIL;
1901
1902 arg = skipwhite(arg);
1903 if (*arg == ';')
1904 {
1905 /* Put the rest of the list (may be empty) in the var after ';'.
1906 * Create a new list for this. */
1907 l = list_alloc();
1908 if (l == NULL)
1909 return FAIL;
1910 while (item != NULL)
1911 {
1912 list_append_tv(l, &item->li_tv);
1913 item = item->li_next;
1914 }
1915
1916 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001917 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 ltv.vval.v_list = l;
1919 l->lv_refcount = 1;
1920
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1922 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 clear_tv(&ltv);
1924 if (arg == NULL)
1925 return FAIL;
1926 break;
1927 }
1928 else if (*arg != ',' && *arg != ']')
1929 {
1930 EMSG2(_(e_intern2), "ex_let_vars()");
1931 return FAIL;
1932 }
1933 }
1934
1935 return OK;
1936}
1937
1938/*
1939 * Skip over assignable variable "var" or list of variables "[var, var]".
1940 * Used for ":let varvar = expr" and ":for varvar in expr".
1941 * For "[var, var]" increment "*var_count" for each variable.
1942 * for "[var, var; var]" set "semicolon".
1943 * Return NULL for an error.
1944 */
1945 static char_u *
1946skip_var_list(arg, var_count, semicolon)
1947 char_u *arg;
1948 int *var_count;
1949 int *semicolon;
1950{
1951 char_u *p, *s;
1952
1953 if (*arg == '[')
1954 {
1955 /* "[var, var]": find the matching ']'. */
1956 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001957 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 {
1959 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1960 s = skip_var_one(p);
1961 if (s == p)
1962 {
1963 EMSG2(_(e_invarg2), p);
1964 return NULL;
1965 }
1966 ++*var_count;
1967
1968 p = skipwhite(s);
1969 if (*p == ']')
1970 break;
1971 else if (*p == ';')
1972 {
1973 if (*semicolon == 1)
1974 {
1975 EMSG(_("Double ; in list of variables"));
1976 return NULL;
1977 }
1978 *semicolon = 1;
1979 }
1980 else if (*p != ',')
1981 {
1982 EMSG2(_(e_invarg2), p);
1983 return NULL;
1984 }
1985 }
1986 return p + 1;
1987 }
1988 else
1989 return skip_var_one(arg);
1990}
1991
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001993 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001994 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001995 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 static char_u *
1997skip_var_one(arg)
1998 char_u *arg;
1999{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002000 if (*arg == '@' && arg[1] != NUL)
2001 return arg + 2;
2002 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2003 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004}
2005
Bram Moolenaara7043832005-01-21 11:56:39 +00002006/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002007 * List variables for hashtab "ht" with prefix "prefix".
2008 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002011list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002012 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002013 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002014 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002015 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002016{
Bram Moolenaar33570922005-01-25 22:26:29 +00002017 hashitem_T *hi;
2018 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002019 int todo;
2020
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002021 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002022 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2023 {
2024 if (!HASHITEM_EMPTY(hi))
2025 {
2026 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002027 di = HI2DI(hi);
2028 if (empty || di->di_tv.v_type != VAR_STRING
2029 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002030 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002031 }
2032 }
2033}
2034
2035/*
2036 * List global variables.
2037 */
2038 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002039list_glob_vars(first)
2040 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002041{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002042 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002043}
2044
2045/*
2046 * List buffer variables.
2047 */
2048 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002049list_buf_vars(first)
2050 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002051{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 char_u numbuf[NUMBUFLEN];
2053
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002054 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2055 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056
2057 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002058 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2059 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002060}
2061
2062/*
2063 * List window variables.
2064 */
2065 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002066list_win_vars(first)
2067 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002068{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002069 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2070 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002071}
2072
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002073#ifdef FEAT_WINDOWS
2074/*
2075 * List tab page variables.
2076 */
2077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_tab_vars(first)
2079 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002080{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2082 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002083}
2084#endif
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
2087 * List Vim variables.
2088 */
2089 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090list_vim_vars(first)
2091 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002093 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094}
2095
2096/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002097 * List script-local variables, if there is a script.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_script_vars(first)
2101 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002102{
2103 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2105 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002106}
2107
2108/*
2109 * List function variables, if there is a function.
2110 */
2111 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112list_func_vars(first)
2113 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002114{
2115 if (current_funccal != NULL)
2116 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002118}
2119
2120/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 * List variables in "arg".
2122 */
2123 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 exarg_T *eap;
2126 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128{
2129 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u *name_start;
2133 char_u *arg_subsc;
2134 char_u *tofree;
2135 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136
2137 while (!ends_excmd(*arg) && !got_int)
2138 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002141 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2143 {
2144 emsg_severe = TRUE;
2145 EMSG(_(e_trailing));
2146 break;
2147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002151 /* get_name_len() takes care of expanding curly braces */
2152 name_start = name = arg;
2153 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2154 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156 /* This is mainly to keep test 49 working: when expanding
2157 * curly braces fails overrule the exception error message. */
2158 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160 emsg_severe = TRUE;
2161 EMSG2(_(e_invarg2), arg);
2162 break;
2163 }
2164 error = TRUE;
2165 }
2166 else
2167 {
2168 if (tofree != NULL)
2169 name = tofree;
2170 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 else
2173 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 /* handle d.key, l[idx], f(expr) */
2175 arg_subsc = arg;
2176 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002177 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002179 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 case 'g': list_glob_vars(first); break;
2185 case 'b': list_buf_vars(first); break;
2186 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002187#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 case 'v': list_vim_vars(first); break;
2191 case 's': list_script_vars(first); break;
2192 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 default:
2194 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002196 }
2197 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 {
2199 char_u numbuf[NUMBUFLEN];
2200 char_u *tf;
2201 int c;
2202 char_u *s;
2203
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002204 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 c = *arg;
2206 *arg = NUL;
2207 list_one_var_a((char_u *)"",
2208 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 tv.v_type,
2210 s == NULL ? (char_u *)"" : s,
2211 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 *arg = c;
2213 vim_free(tf);
2214 }
2215 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 }
2218 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219
2220 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222
2223 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 }
2225
2226 return arg;
2227}
2228
2229/*
2230 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2231 * Returns a pointer to the char just after the var name.
2232 * Returns NULL if there is an error.
2233 */
2234 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002235ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002237 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002239 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241{
2242 int c1;
2243 char_u *name;
2244 char_u *p;
2245 char_u *arg_end = NULL;
2246 int len;
2247 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249
2250 /*
2251 * ":let $VAR = expr": Set environment variable.
2252 */
2253 if (*arg == '$')
2254 {
2255 /* Find the end of the name. */
2256 ++arg;
2257 name = arg;
2258 len = get_env_len(&arg);
2259 if (len == 0)
2260 EMSG2(_(e_invarg2), name - 1);
2261 else
2262 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002263 if (op != NULL && (*op == '+' || *op == '-'))
2264 EMSG2(_(e_letwrong), op);
2265 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002266 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 EMSG(_(e_letunexp));
2268 else
2269 {
2270 c1 = name[len];
2271 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 p = get_tv_string_chk(tv);
2273 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002274 {
2275 int mustfree = FALSE;
2276 char_u *s = vim_getenv(name, &mustfree);
2277
2278 if (s != NULL)
2279 {
2280 p = tofree = concat_str(s, p);
2281 if (mustfree)
2282 vim_free(s);
2283 }
2284 }
2285 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002287 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 if (STRICMP(name, "HOME") == 0)
2289 init_homedir();
2290 else if (didset_vim && STRICMP(name, "VIM") == 0)
2291 didset_vim = FALSE;
2292 else if (didset_vimruntime
2293 && STRICMP(name, "VIMRUNTIME") == 0)
2294 didset_vimruntime = FALSE;
2295 arg_end = arg;
2296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 }
2300 }
2301 }
2302
2303 /*
2304 * ":let &option = expr": Set option value.
2305 * ":let &l:option = expr": Set local option value.
2306 * ":let &g:option = expr": Set global option value.
2307 */
2308 else if (*arg == '&')
2309 {
2310 /* Find the end of the name. */
2311 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002312 if (p == NULL || (endchars != NULL
2313 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 EMSG(_(e_letunexp));
2315 else
2316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 long n;
2318 int opt_type;
2319 long numval;
2320 char_u *stringval = NULL;
2321 char_u *s;
2322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 c1 = *p;
2324 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325
2326 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002327 s = get_tv_string_chk(tv); /* != NULL if number or string */
2328 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 {
2330 opt_type = get_option_value(arg, &numval,
2331 &stringval, opt_flags);
2332 if ((opt_type == 1 && *op == '.')
2333 || (opt_type == 0 && *op != '.'))
2334 EMSG2(_(e_letwrong), op);
2335 else
2336 {
2337 if (opt_type == 1) /* number */
2338 {
2339 if (*op == '+')
2340 n = numval + n;
2341 else
2342 n = numval - n;
2343 }
2344 else if (opt_type == 0 && stringval != NULL) /* string */
2345 {
2346 s = concat_str(stringval, s);
2347 vim_free(stringval);
2348 stringval = s;
2349 }
2350 }
2351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 if (s != NULL)
2353 {
2354 set_option_value(arg, n, s, opt_flags);
2355 arg_end = p;
2356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 }
2360 }
2361
2362 /*
2363 * ":let @r = expr": Set register contents.
2364 */
2365 else if (*arg == '@')
2366 {
2367 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 if (op != NULL && (*op == '+' || *op == '-'))
2369 EMSG2(_(e_letwrong), op);
2370 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002371 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 EMSG(_(e_letunexp));
2373 else
2374 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002375 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 char_u *s;
2377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 p = get_tv_string_chk(tv);
2379 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002381 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 if (s != NULL)
2383 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002384 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 vim_free(s);
2386 }
2387 }
2388 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 arg_end = arg + 1;
2392 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002393 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 }
2395 }
2396
2397 /*
2398 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002401 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002403 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002405 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2409 EMSG(_(e_letunexp));
2410 else
2411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 arg_end = p;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418
2419 else
2420 EMSG2(_(e_invarg2), arg);
2421
2422 return arg_end;
2423}
2424
2425/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2427 */
2428 static int
2429check_changedtick(arg)
2430 char_u *arg;
2431{
2432 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2433 {
2434 EMSG2(_(e_readonlyvar), arg);
2435 return TRUE;
2436 }
2437 return FALSE;
2438}
2439
2440/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 * Get an lval: variable, Dict item or List item that can be assigned a value
2442 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2443 * "name.key", "name.key[expr]" etc.
2444 * Indexing only works if "name" is an existing List or Dictionary.
2445 * "name" points to the start of the name.
2446 * If "rettv" is not NULL it points to the value to be assigned.
2447 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2448 * wrong; must end in space or cmd separator.
2449 *
2450 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002451 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 */
2454 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002455get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 typval_T *rettv;
2458 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 int unlet;
2460 int skip;
2461 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 char_u *expr_start, *expr_end;
2466 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002467 dictitem_T *v;
2468 typval_T var1;
2469 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002474 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478
2479 if (skip)
2480 {
2481 /* When skipping just find the end of the name. */
2482 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002483 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 }
2485
2486 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002487 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (expr_start != NULL)
2489 {
2490 /* Don't expand the name when we already know there is an error. */
2491 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2492 && *p != '[' && *p != '.')
2493 {
2494 EMSG(_(e_trailing));
2495 return NULL;
2496 }
2497
2498 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2499 if (lp->ll_exp_name == NULL)
2500 {
2501 /* Report an invalid expression in braces, unless the
2502 * expression evaluation has been cancelled due to an
2503 * aborting error, an interrupt, or an exception. */
2504 if (!aborting() && !quiet)
2505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002506 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 EMSG2(_(e_invarg2), name);
2508 return NULL;
2509 }
2510 }
2511 lp->ll_name = lp->ll_exp_name;
2512 }
2513 else
2514 lp->ll_name = name;
2515
2516 /* Without [idx] or .key we are done. */
2517 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2518 return p;
2519
2520 cc = *p;
2521 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002522 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (v == NULL && !quiet)
2524 EMSG2(_(e_undefvar), lp->ll_name);
2525 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 if (v == NULL)
2527 return NULL;
2528
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 /*
2530 * Loop until no more [idx] or .key is following.
2531 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2536 && !(lp->ll_tv->v_type == VAR_DICT
2537 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (!quiet)
2540 EMSG(_("E689: Can only index a List or Dictionary"));
2541 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (!quiet)
2546 EMSG(_("E708: [:] must come last"));
2547 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002549
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 len = -1;
2551 if (*p == '.')
2552 {
2553 key = p + 1;
2554 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2555 ;
2556 if (len == 0)
2557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (!quiet)
2559 EMSG(_(e_emptykey));
2560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562 p = key + len;
2563 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002564 else
2565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002567 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 if (*p == ':')
2569 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002570 else
2571 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 empty1 = FALSE;
2573 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002575 if (get_tv_string_chk(&var1) == NULL)
2576 {
2577 /* not a number or string */
2578 clear_tv(&var1);
2579 return NULL;
2580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 }
2582
2583 /* Optionally get the second index [ :expr]. */
2584 if (*p == ':')
2585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002590 if (!empty1)
2591 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (rettv != NULL && (rettv->v_type != VAR_LIST
2595 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!quiet)
2598 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (!empty1)
2600 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 }
2603 p = skipwhite(p + 1);
2604 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 else
2607 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2610 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 if (!empty1)
2612 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002615 if (get_tv_string_chk(&var2) == NULL)
2616 {
2617 /* not a number or string */
2618 if (!empty1)
2619 clear_tv(&var1);
2620 clear_tv(&var2);
2621 return NULL;
2622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (!quiet)
2632 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 if (!empty1)
2634 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 }
2639
2640 /* Skip to past ']'. */
2641 ++p;
2642 }
2643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 {
2646 if (len == -1)
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 if (*key == NUL)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 lp->ll_list = NULL;
2659 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002660 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002663 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 if (len == -1)
2669 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 }
2672 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (len == -1)
2677 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 p = NULL;
2680 break;
2681 }
2682 if (len == -1)
2683 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686 else
2687 {
2688 /*
2689 * Get the number and item for the only or first index of the List.
2690 */
2691 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
2694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 clear_tv(&var1);
2697 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_list = lp->ll_tv->vval.v_list;
2700 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2701 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002703 if (lp->ll_n1 < 0)
2704 {
2705 lp->ll_n1 = 0;
2706 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2707 }
2708 }
2709 if (lp->ll_li == NULL)
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 }
2715
2716 /*
2717 * May need to find the item or absolute index for the second
2718 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 * When no index given: "lp->ll_empty2" is TRUE.
2720 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002724 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2735 if (lp->ll_n1 < 0)
2736 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2737 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 }
2740
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002743 }
2744
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return p;
2746}
2747
2748/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002749 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 */
2751 static void
2752clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002753 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754{
2755 vim_free(lp->ll_exp_name);
2756 vim_free(lp->ll_newkey);
2757}
2758
2759/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002760 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 */
2764 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002766 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771{
2772 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002773 listitem_T *ri;
2774 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775
2776 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 cc = *endp;
2781 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 if (op != NULL && *op != '=')
2783 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002784 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002786 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002787 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002788 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002789 {
2790 if (tv_op(&tv, rettv, op) == OK)
2791 set_var(lp->ll_name, &tv, FALSE);
2792 clear_tv(&tv);
2793 }
2794 }
2795 else
2796 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002800 else if (tv_check_lock(lp->ll_newkey == NULL
2801 ? lp->ll_tv->v_lock
2802 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2803 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 else if (lp->ll_range)
2805 {
2806 /*
2807 * Assign the List values to the list items.
2808 */
2809 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 if (op != NULL && *op != '=')
2812 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2813 else
2814 {
2815 clear_tv(&lp->ll_li->li_tv);
2816 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2817 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 ri = ri->li_next;
2819 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2820 break;
2821 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002824 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 ri = NULL;
2827 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002828 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_li = lp->ll_li->li_next;
2831 ++lp->ll_n1;
2832 }
2833 if (ri != NULL)
2834 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002835 else if (lp->ll_empty2
2836 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 : lp->ll_n1 != lp->ll_n2)
2838 EMSG(_("E711: List value has not enough items"));
2839 }
2840 else
2841 {
2842 /*
2843 * Assign to a List or Dictionary item.
2844 */
2845 if (lp->ll_newkey != NULL)
2846 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002847 if (op != NULL && *op != '=')
2848 {
2849 EMSG2(_(e_letwrong), op);
2850 return;
2851 }
2852
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002854 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (di == NULL)
2856 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002857 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2858 {
2859 vim_free(di);
2860 return;
2861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002863 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864 else if (op != NULL && *op != '=')
2865 {
2866 tv_op(lp->ll_tv, rettv, op);
2867 return;
2868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002869 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /*
2873 * Assign the value to the variable or list item.
2874 */
2875 if (copy)
2876 copy_tv(rettv, lp->ll_tv);
2877 else
2878 {
2879 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002880 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002882 }
2883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884}
2885
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002887 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2888 * Returns OK or FAIL.
2889 */
2890 static int
2891tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 typval_T *tv1;
2893 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 char_u *op;
2895{
2896 long n;
2897 char_u numbuf[NUMBUFLEN];
2898 char_u *s;
2899
2900 /* Can't do anything with a Funcref or a Dict on the right. */
2901 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2902 {
2903 switch (tv1->v_type)
2904 {
2905 case VAR_DICT:
2906 case VAR_FUNC:
2907 break;
2908
2909 case VAR_LIST:
2910 if (*op != '+' || tv2->v_type != VAR_LIST)
2911 break;
2912 /* List += List */
2913 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2914 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2915 return OK;
2916
2917 case VAR_NUMBER:
2918 case VAR_STRING:
2919 if (tv2->v_type == VAR_LIST)
2920 break;
2921 if (*op == '+' || *op == '-')
2922 {
2923 /* nr += nr or nr -= nr*/
2924 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002925#ifdef FEAT_FLOAT
2926 if (tv2->v_type == VAR_FLOAT)
2927 {
2928 float_T f = n;
2929
2930 if (*op == '+')
2931 f += tv2->vval.v_float;
2932 else
2933 f -= tv2->vval.v_float;
2934 clear_tv(tv1);
2935 tv1->v_type = VAR_FLOAT;
2936 tv1->vval.v_float = f;
2937 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002939#endif
2940 {
2941 if (*op == '+')
2942 n += get_tv_number(tv2);
2943 else
2944 n -= get_tv_number(tv2);
2945 clear_tv(tv1);
2946 tv1->v_type = VAR_NUMBER;
2947 tv1->vval.v_number = n;
2948 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 }
2950 else
2951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002952 if (tv2->v_type == VAR_FLOAT)
2953 break;
2954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 /* str .= str */
2956 s = get_tv_string(tv1);
2957 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2958 clear_tv(tv1);
2959 tv1->v_type = VAR_STRING;
2960 tv1->vval.v_string = s;
2961 }
2962 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002963
2964#ifdef FEAT_FLOAT
2965 case VAR_FLOAT:
2966 {
2967 float_T f;
2968
2969 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2970 && tv2->v_type != VAR_NUMBER
2971 && tv2->v_type != VAR_STRING))
2972 break;
2973 if (tv2->v_type == VAR_FLOAT)
2974 f = tv2->vval.v_float;
2975 else
2976 f = get_tv_number(tv2);
2977 if (*op == '+')
2978 tv1->vval.v_float += f;
2979 else
2980 tv1->vval.v_float -= f;
2981 }
2982 return OK;
2983#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002984 }
2985 }
2986
2987 EMSG2(_(e_letwrong), op);
2988 return FAIL;
2989}
2990
2991/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002992 * Add a watcher to a list.
2993 */
2994 static void
2995list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002996 list_T *l;
2997 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002998{
2999 lw->lw_next = l->lv_watch;
3000 l->lv_watch = lw;
3001}
3002
3003/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003004 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005 * No warning when it isn't found...
3006 */
3007 static void
3008list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 list_T *l;
3010 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011{
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013
3014 lwp = &l->lv_watch;
3015 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3016 {
3017 if (lw == lwrem)
3018 {
3019 *lwp = lw->lw_next;
3020 break;
3021 }
3022 lwp = &lw->lw_next;
3023 }
3024}
3025
3026/*
3027 * Just before removing an item from a list: advance watchers to the next
3028 * item.
3029 */
3030 static void
3031list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 list_T *l;
3033 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003034{
Bram Moolenaar33570922005-01-25 22:26:29 +00003035 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003036
3037 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3038 if (lw->lw_item == item)
3039 lw->lw_item = item->li_next;
3040}
3041
3042/*
3043 * Evaluate the expression used in a ":for var in expr" command.
3044 * "arg" points to "var".
3045 * Set "*errp" to TRUE for an error, FALSE otherwise;
3046 * Return a pointer that holds the info. Null when there is an error.
3047 */
3048 void *
3049eval_for_line(arg, errp, nextcmdp, skip)
3050 char_u *arg;
3051 int *errp;
3052 char_u **nextcmdp;
3053 int skip;
3054{
Bram Moolenaar33570922005-01-25 22:26:29 +00003055 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 typval_T tv;
3058 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059
3060 *errp = TRUE; /* default: there is an error */
3061
Bram Moolenaar33570922005-01-25 22:26:29 +00003062 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003063 if (fi == NULL)
3064 return NULL;
3065
3066 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3067 if (expr == NULL)
3068 return fi;
3069
3070 expr = skipwhite(expr);
3071 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3072 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003073 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074 return fi;
3075 }
3076
3077 if (skip)
3078 ++emsg_skip;
3079 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3080 {
3081 *errp = FALSE;
3082 if (!skip)
3083 {
3084 l = tv.vval.v_list;
3085 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003086 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003088 clear_tv(&tv);
3089 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090 else
3091 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003092 /* No need to increment the refcount, it's already set for the
3093 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094 fi->fi_list = l;
3095 list_add_watch(l, &fi->fi_lw);
3096 fi->fi_lw.lw_item = l->lv_first;
3097 }
3098 }
3099 }
3100 if (skip)
3101 --emsg_skip;
3102
3103 return fi;
3104}
3105
3106/*
3107 * Use the first item in a ":for" list. Advance to the next.
3108 * Assign the values to the variable (list). "arg" points to the first one.
3109 * Return TRUE when a valid item was found, FALSE when at end of list or
3110 * something wrong.
3111 */
3112 int
3113next_for_item(fi_void, arg)
3114 void *fi_void;
3115 char_u *arg;
3116{
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120
3121 item = fi->fi_lw.lw_item;
3122 if (item == NULL)
3123 result = FALSE;
3124 else
3125 {
3126 fi->fi_lw.lw_item = item->li_next;
3127 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3128 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3129 }
3130 return result;
3131}
3132
3133/*
3134 * Free the structure used to store info used by ":for".
3135 */
3136 void
3137free_for_info(fi_void)
3138 void *fi_void;
3139{
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003142 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003143 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003144 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003145 list_unref(fi->fi_list);
3146 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147 vim_free(fi);
3148}
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3151
3152 void
3153set_context_for_expression(xp, arg, cmdidx)
3154 expand_T *xp;
3155 char_u *arg;
3156 cmdidx_T cmdidx;
3157{
3158 int got_eq = FALSE;
3159 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 if (cmdidx == CMD_let)
3163 {
3164 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003165 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166 {
3167 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003168 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 {
3170 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 if (vim_iswhite(*p))
3173 break;
3174 }
3175 return;
3176 }
3177 }
3178 else
3179 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3180 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 while ((xp->xp_pattern = vim_strpbrk(arg,
3182 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3183 {
3184 c = *xp->xp_pattern;
3185 if (c == '&')
3186 {
3187 c = xp->xp_pattern[1];
3188 if (c == '&')
3189 {
3190 ++xp->xp_pattern;
3191 xp->xp_context = cmdidx != CMD_let || got_eq
3192 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3193 }
3194 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003197 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3198 xp->xp_pattern += 2;
3199
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 else if (c == '$')
3203 {
3204 /* environment variable */
3205 xp->xp_context = EXPAND_ENV_VARS;
3206 }
3207 else if (c == '=')
3208 {
3209 got_eq = TRUE;
3210 xp->xp_context = EXPAND_EXPRESSION;
3211 }
3212 else if (c == '<'
3213 && xp->xp_context == EXPAND_FUNCTIONS
3214 && vim_strchr(xp->xp_pattern, '(') == NULL)
3215 {
3216 /* Function name can start with "<SNR>" */
3217 break;
3218 }
3219 else if (cmdidx != CMD_let || got_eq)
3220 {
3221 if (c == '"') /* string */
3222 {
3223 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3224 if (c == '\\' && xp->xp_pattern[1] != NUL)
3225 ++xp->xp_pattern;
3226 xp->xp_context = EXPAND_NOTHING;
3227 }
3228 else if (c == '\'') /* literal string */
3229 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003230 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3232 /* skip */ ;
3233 xp->xp_context = EXPAND_NOTHING;
3234 }
3235 else if (c == '|')
3236 {
3237 if (xp->xp_pattern[1] == '|')
3238 {
3239 ++xp->xp_pattern;
3240 xp->xp_context = EXPAND_EXPRESSION;
3241 }
3242 else
3243 xp->xp_context = EXPAND_COMMANDS;
3244 }
3245 else
3246 xp->xp_context = EXPAND_EXPRESSION;
3247 }
3248 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 /* Doesn't look like something valid, expand as an expression
3250 * anyway. */
3251 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 arg = xp->xp_pattern;
3253 if (*arg != NUL)
3254 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3255 /* skip */ ;
3256 }
3257 xp->xp_pattern = arg;
3258}
3259
3260#endif /* FEAT_CMDL_COMPL */
3261
3262/*
3263 * ":1,25call func(arg1, arg2)" function call.
3264 */
3265 void
3266ex_call(eap)
3267 exarg_T *eap;
3268{
3269 char_u *arg = eap->arg;
3270 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003272 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 linenr_T lnum;
3276 int doesrange;
3277 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003280 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003281 if (fudi.fd_newkey != NULL)
3282 {
3283 /* Still need to give an error message for missing key. */
3284 EMSG2(_(e_dictkey), fudi.fd_newkey);
3285 vim_free(fudi.fd_newkey);
3286 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003287 if (tofree == NULL)
3288 return;
3289
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003290 /* Increase refcount on dictionary, it could get deleted when evaluating
3291 * the arguments. */
3292 if (fudi.fd_dict != NULL)
3293 ++fudi.fd_dict->dv_refcount;
3294
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003295 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003296 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003297 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
Bram Moolenaar532c7802005-01-27 14:44:31 +00003299 /* Skip white space to allow ":call func ()". Not good, but required for
3300 * backward compatibility. */
3301 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003302 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 if (*startarg != '(')
3305 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003306 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 goto end;
3308 }
3309
3310 /*
3311 * When skipping, evaluate the function once, to find the end of the
3312 * arguments.
3313 * When the function takes a range, this is discovered after the first
3314 * call, and the loop is broken.
3315 */
3316 if (eap->skip)
3317 {
3318 ++emsg_skip;
3319 lnum = eap->line2; /* do it once, also with an invalid range */
3320 }
3321 else
3322 lnum = eap->line1;
3323 for ( ; lnum <= eap->line2; ++lnum)
3324 {
3325 if (!eap->skip && eap->addr_count > 0)
3326 {
3327 curwin->w_cursor.lnum = lnum;
3328 curwin->w_cursor.col = 0;
3329 }
3330 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003331 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003332 eap->line1, eap->line2, &doesrange,
3333 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 failed = TRUE;
3336 break;
3337 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003338
3339 /* Handle a function returning a Funcref, Dictionary or List. */
3340 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3341 {
3342 failed = TRUE;
3343 break;
3344 }
3345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003346 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (doesrange || eap->skip)
3348 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003351 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003353 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (aborting())
3355 break;
3356 }
3357 if (eap->skip)
3358 --emsg_skip;
3359
3360 if (!failed)
3361 {
3362 /* Check for trailing illegal characters and a following command. */
3363 if (!ends_excmd(*arg))
3364 {
3365 emsg_severe = TRUE;
3366 EMSG(_(e_trailing));
3367 }
3368 else
3369 eap->nextcmd = check_nextcmd(arg);
3370 }
3371
3372end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003373 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003374 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
3377/*
3378 * ":unlet[!] var1 ... " command.
3379 */
3380 void
3381ex_unlet(eap)
3382 exarg_T *eap;
3383{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003384 ex_unletlock(eap, eap->arg, 0);
3385}
3386
3387/*
3388 * ":lockvar" and ":unlockvar" commands
3389 */
3390 void
3391ex_lockvar(eap)
3392 exarg_T *eap;
3393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003395 int deep = 2;
3396
3397 if (eap->forceit)
3398 deep = -1;
3399 else if (vim_isdigit(*arg))
3400 {
3401 deep = getdigits(&arg);
3402 arg = skipwhite(arg);
3403 }
3404
3405 ex_unletlock(eap, arg, deep);
3406}
3407
3408/*
3409 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3410 */
3411 static void
3412ex_unletlock(eap, argstart, deep)
3413 exarg_T *eap;
3414 char_u *argstart;
3415 int deep;
3416{
3417 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003420 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 do
3423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003424 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003425 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3426 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003427 if (lv.ll_name == NULL)
3428 error = TRUE; /* error but continue parsing */
3429 if (name_end == NULL || (!vim_iswhite(*name_end)
3430 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003432 if (name_end != NULL)
3433 {
3434 emsg_severe = TRUE;
3435 EMSG(_(e_trailing));
3436 }
3437 if (!(eap->skip || error))
3438 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 break;
3440 }
3441
3442 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003443 {
3444 if (eap->cmdidx == CMD_unlet)
3445 {
3446 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3447 error = TRUE;
3448 }
3449 else
3450 {
3451 if (do_lock_var(&lv, name_end, deep,
3452 eap->cmdidx == CMD_lockvar) == FAIL)
3453 error = TRUE;
3454 }
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003457 if (!eap->skip)
3458 clear_lval(&lv);
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 arg = skipwhite(name_end);
3461 } while (!ends_excmd(*arg));
3462
3463 eap->nextcmd = check_nextcmd(arg);
3464}
3465
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003467do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003468 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003469 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003470 int forceit;
3471{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003472 int ret = OK;
3473 int cc;
3474
3475 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003477 cc = *name_end;
3478 *name_end = NUL;
3479
3480 /* Normal name or expanded name. */
3481 if (check_changedtick(lp->ll_name))
3482 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003483 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003484 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003485 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003486 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3488 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003489 else if (lp->ll_range)
3490 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003491 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003492
3493 /* Delete a range of List items. */
3494 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3495 {
3496 li = lp->ll_li->li_next;
3497 listitem_remove(lp->ll_list, lp->ll_li);
3498 lp->ll_li = li;
3499 ++lp->ll_n1;
3500 }
3501 }
3502 else
3503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003504 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003505 /* unlet a List item. */
3506 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003507 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003508 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003509 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003510 }
3511
3512 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003513}
3514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515/*
3516 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003517 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 */
3519 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003520do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003522 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
Bram Moolenaar33570922005-01-25 22:26:29 +00003524 hashtab_T *ht;
3525 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003526 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003527 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 ht = find_var_ht(name, &varname);
3530 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 hi = hash_find(ht, varname);
3533 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003535 di = HI2DI(hi);
3536 if (var_check_fixed(di->di_flags, name)
3537 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003538 return FAIL;
3539 delete_var(ht, hi);
3540 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003543 if (forceit)
3544 return OK;
3545 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return FAIL;
3547}
3548
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549/*
3550 * Lock or unlock variable indicated by "lp".
3551 * "deep" is the levels to go (-1 for unlimited);
3552 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3553 */
3554 static int
3555do_lock_var(lp, name_end, deep, lock)
3556 lval_T *lp;
3557 char_u *name_end;
3558 int deep;
3559 int lock;
3560{
3561 int ret = OK;
3562 int cc;
3563 dictitem_T *di;
3564
3565 if (deep == 0) /* nothing to do */
3566 return OK;
3567
3568 if (lp->ll_tv == NULL)
3569 {
3570 cc = *name_end;
3571 *name_end = NUL;
3572
3573 /* Normal name or expanded name. */
3574 if (check_changedtick(lp->ll_name))
3575 ret = FAIL;
3576 else
3577 {
3578 di = find_var(lp->ll_name, NULL);
3579 if (di == NULL)
3580 ret = FAIL;
3581 else
3582 {
3583 if (lock)
3584 di->di_flags |= DI_FLAGS_LOCK;
3585 else
3586 di->di_flags &= ~DI_FLAGS_LOCK;
3587 item_lock(&di->di_tv, deep, lock);
3588 }
3589 }
3590 *name_end = cc;
3591 }
3592 else if (lp->ll_range)
3593 {
3594 listitem_T *li = lp->ll_li;
3595
3596 /* (un)lock a range of List items. */
3597 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3598 {
3599 item_lock(&li->li_tv, deep, lock);
3600 li = li->li_next;
3601 ++lp->ll_n1;
3602 }
3603 }
3604 else if (lp->ll_list != NULL)
3605 /* (un)lock a List item. */
3606 item_lock(&lp->ll_li->li_tv, deep, lock);
3607 else
3608 /* un(lock) a Dictionary item. */
3609 item_lock(&lp->ll_di->di_tv, deep, lock);
3610
3611 return ret;
3612}
3613
3614/*
3615 * Lock or unlock an item. "deep" is nr of levels to go.
3616 */
3617 static void
3618item_lock(tv, deep, lock)
3619 typval_T *tv;
3620 int deep;
3621 int lock;
3622{
3623 static int recurse = 0;
3624 list_T *l;
3625 listitem_T *li;
3626 dict_T *d;
3627 hashitem_T *hi;
3628 int todo;
3629
3630 if (recurse >= DICT_MAXNEST)
3631 {
3632 EMSG(_("E743: variable nested too deep for (un)lock"));
3633 return;
3634 }
3635 if (deep == 0)
3636 return;
3637 ++recurse;
3638
3639 /* lock/unlock the item itself */
3640 if (lock)
3641 tv->v_lock |= VAR_LOCKED;
3642 else
3643 tv->v_lock &= ~VAR_LOCKED;
3644
3645 switch (tv->v_type)
3646 {
3647 case VAR_LIST:
3648 if ((l = tv->vval.v_list) != NULL)
3649 {
3650 if (lock)
3651 l->lv_lock |= VAR_LOCKED;
3652 else
3653 l->lv_lock &= ~VAR_LOCKED;
3654 if (deep < 0 || deep > 1)
3655 /* recursive: lock/unlock the items the List contains */
3656 for (li = l->lv_first; li != NULL; li = li->li_next)
3657 item_lock(&li->li_tv, deep - 1, lock);
3658 }
3659 break;
3660 case VAR_DICT:
3661 if ((d = tv->vval.v_dict) != NULL)
3662 {
3663 if (lock)
3664 d->dv_lock |= VAR_LOCKED;
3665 else
3666 d->dv_lock &= ~VAR_LOCKED;
3667 if (deep < 0 || deep > 1)
3668 {
3669 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003670 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3672 {
3673 if (!HASHITEM_EMPTY(hi))
3674 {
3675 --todo;
3676 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3677 }
3678 }
3679 }
3680 }
3681 }
3682 --recurse;
3683}
3684
Bram Moolenaara40058a2005-07-11 22:42:07 +00003685/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003686 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3687 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003688 */
3689 static int
3690tv_islocked(tv)
3691 typval_T *tv;
3692{
3693 return (tv->v_lock & VAR_LOCKED)
3694 || (tv->v_type == VAR_LIST
3695 && tv->vval.v_list != NULL
3696 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3697 || (tv->v_type == VAR_DICT
3698 && tv->vval.v_dict != NULL
3699 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3700}
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3703/*
3704 * Delete all "menutrans_" variables.
3705 */
3706 void
3707del_menutrans_vars()
3708{
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003710 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003713 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003715 {
3716 if (!HASHITEM_EMPTY(hi))
3717 {
3718 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3720 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003721 }
3722 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724}
3725#endif
3726
3727#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3728
3729/*
3730 * Local string buffer for the next two functions to store a variable name
3731 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3732 * get_user_var_name().
3733 */
3734
3735static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3736
3737static char_u *varnamebuf = NULL;
3738static int varnamebuflen = 0;
3739
3740/*
3741 * Function to concatenate a prefix and a variable name.
3742 */
3743 static char_u *
3744cat_prefix_varname(prefix, name)
3745 int prefix;
3746 char_u *name;
3747{
3748 int len;
3749
3750 len = (int)STRLEN(name) + 3;
3751 if (len > varnamebuflen)
3752 {
3753 vim_free(varnamebuf);
3754 len += 10; /* some additional space */
3755 varnamebuf = alloc(len);
3756 if (varnamebuf == NULL)
3757 {
3758 varnamebuflen = 0;
3759 return NULL;
3760 }
3761 varnamebuflen = len;
3762 }
3763 *varnamebuf = prefix;
3764 varnamebuf[1] = ':';
3765 STRCPY(varnamebuf + 2, name);
3766 return varnamebuf;
3767}
3768
3769/*
3770 * Function given to ExpandGeneric() to obtain the list of user defined
3771 * (global/buffer/window/built-in) variable names.
3772 */
3773/*ARGSUSED*/
3774 char_u *
3775get_user_var_name(xp, idx)
3776 expand_T *xp;
3777 int idx;
3778{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003779 static long_u gdone;
3780 static long_u bdone;
3781 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003782#ifdef FEAT_WINDOWS
3783 static long_u tdone;
3784#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003785 static int vidx;
3786 static hashitem_T *hi;
3787 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003790 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003791 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003792#ifdef FEAT_WINDOWS
3793 tdone = 0;
3794#endif
3795 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003796
3797 /* Global variables */
3798 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003800 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003802 else
3803 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003804 while (HASHITEM_EMPTY(hi))
3805 ++hi;
3806 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3807 return cat_prefix_varname('g', hi->hi_key);
3808 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003810
3811 /* b: variables */
3812 ht = &curbuf->b_vars.dv_hashtab;
3813 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003815 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003816 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003817 else
3818 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003819 while (HASHITEM_EMPTY(hi))
3820 ++hi;
3821 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003825 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return (char_u *)"b:changedtick";
3827 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003828
3829 /* w: variables */
3830 ht = &curwin->w_vars.dv_hashtab;
3831 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003833 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003835 else
3836 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003837 while (HASHITEM_EMPTY(hi))
3838 ++hi;
3839 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003841
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003842#ifdef FEAT_WINDOWS
3843 /* t: variables */
3844 ht = &curtab->tp_vars.dv_hashtab;
3845 if (tdone < ht->ht_used)
3846 {
3847 if (tdone++ == 0)
3848 hi = ht->ht_array;
3849 else
3850 ++hi;
3851 while (HASHITEM_EMPTY(hi))
3852 ++hi;
3853 return cat_prefix_varname('t', hi->hi_key);
3854 }
3855#endif
3856
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 /* v: variables */
3858 if (vidx < VV_LEN)
3859 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 vim_free(varnamebuf);
3862 varnamebuf = NULL;
3863 varnamebuflen = 0;
3864 return NULL;
3865}
3866
3867#endif /* FEAT_CMDL_COMPL */
3868
3869/*
3870 * types for expressions.
3871 */
3872typedef enum
3873{
3874 TYPE_UNKNOWN = 0
3875 , TYPE_EQUAL /* == */
3876 , TYPE_NEQUAL /* != */
3877 , TYPE_GREATER /* > */
3878 , TYPE_GEQUAL /* >= */
3879 , TYPE_SMALLER /* < */
3880 , TYPE_SEQUAL /* <= */
3881 , TYPE_MATCH /* =~ */
3882 , TYPE_NOMATCH /* !~ */
3883} exptype_T;
3884
3885/*
3886 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3889 */
3890
3891/*
3892 * Handle zero level expression.
3893 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003894 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003895 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 * Return OK or FAIL.
3897 */
3898 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 char_u **nextcmd;
3903 int evaluate;
3904{
3905 int ret;
3906 char_u *p;
3907
3908 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (ret == FAIL || !ends_excmd(*p))
3911 {
3912 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 /*
3915 * Report the invalid expression unless the expression evaluation has
3916 * been cancelled due to an aborting error, an interrupt, or an
3917 * exception.
3918 */
3919 if (!aborting())
3920 EMSG2(_(e_invexpr2), arg);
3921 ret = FAIL;
3922 }
3923 if (nextcmd != NULL)
3924 *nextcmd = check_nextcmd(p);
3925
3926 return ret;
3927}
3928
3929/*
3930 * Handle top level expression:
3931 * expr1 ? expr0 : expr0
3932 *
3933 * "arg" must point to the first non-white of the expression.
3934 * "arg" is advanced to the next non-white after the recognized expression.
3935 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003936 * Note: "rettv.v_lock" is not set.
3937 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * Return OK or FAIL.
3939 */
3940 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 int evaluate;
3945{
3946 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
3949 /*
3950 * Get the first variable.
3951 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 return FAIL;
3954
3955 if ((*arg)[0] == '?')
3956 {
3957 result = FALSE;
3958 if (evaluate)
3959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003960 int error = FALSE;
3961
3962 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003965 if (error)
3966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968
3969 /*
3970 * Get the second variable.
3971 */
3972 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return FAIL;
3975
3976 /*
3977 * Check for the ":".
3978 */
3979 if ((*arg)[0] != ':')
3980 {
3981 EMSG(_("E109: Missing ':' after '?'"));
3982 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985 }
3986
3987 /*
3988 * Get the third variable.
3989 */
3990 *arg = skipwhite(*arg + 1);
3991 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3992 {
3993 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003994 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 return FAIL;
3996 }
3997 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
4001 return OK;
4002}
4003
4004/*
4005 * Handle first level expression:
4006 * expr2 || expr2 || expr2 logical OR
4007 *
4008 * "arg" must point to the first non-white of the expression.
4009 * "arg" is advanced to the next non-white after the recognized expression.
4010 *
4011 * Return OK or FAIL.
4012 */
4013 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 int evaluate;
4018{
Bram Moolenaar33570922005-01-25 22:26:29 +00004019 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 long result;
4021 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004022 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 /*
4025 * Get the first variable.
4026 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return FAIL;
4029
4030 /*
4031 * Repeat until there is no following "||".
4032 */
4033 first = TRUE;
4034 result = FALSE;
4035 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4036 {
4037 if (evaluate && first)
4038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004039 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004042 if (error)
4043 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 first = FALSE;
4045 }
4046
4047 /*
4048 * Get the second variable.
4049 */
4050 *arg = skipwhite(*arg + 2);
4051 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4052 return FAIL;
4053
4054 /*
4055 * Compute the result.
4056 */
4057 if (evaluate && !result)
4058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004059 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004062 if (error)
4063 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 if (evaluate)
4066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 rettv->v_type = VAR_NUMBER;
4068 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 }
4071
4072 return OK;
4073}
4074
4075/*
4076 * Handle second level expression:
4077 * expr3 && expr3 && expr3 logical AND
4078 *
4079 * "arg" must point to the first non-white of the expression.
4080 * "arg" is advanced to the next non-white after the recognized expression.
4081 *
4082 * Return OK or FAIL.
4083 */
4084 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 int evaluate;
4089{
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 long result;
4092 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 /*
4096 * Get the first variable.
4097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 /*
4102 * Repeat until there is no following "&&".
4103 */
4104 first = TRUE;
4105 result = TRUE;
4106 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4107 {
4108 if (evaluate && first)
4109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004110 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113 if (error)
4114 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 first = FALSE;
4116 }
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(*arg + 2);
4122 if (eval4(arg, &var2, evaluate && result) == FAIL)
4123 return FAIL;
4124
4125 /*
4126 * Compute the result.
4127 */
4128 if (evaluate && result)
4129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004130 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 if (error)
4134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 if (evaluate)
4137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 rettv->v_type = VAR_NUMBER;
4139 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 }
4142
4143 return OK;
4144}
4145
4146/*
4147 * Handle third level expression:
4148 * var1 == var2
4149 * var1 =~ var2
4150 * var1 != var2
4151 * var1 !~ var2
4152 * var1 > var2
4153 * var1 >= var2
4154 * var1 < var2
4155 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004156 * var1 is var2
4157 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 *
4159 * "arg" must point to the first non-white of the expression.
4160 * "arg" is advanced to the next non-white after the recognized expression.
4161 *
4162 * Return OK or FAIL.
4163 */
4164 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int evaluate;
4169{
Bram Moolenaar33570922005-01-25 22:26:29 +00004170 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 char_u *p;
4172 int i;
4173 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004174 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 int len = 2;
4176 long n1, n2;
4177 char_u *s1, *s2;
4178 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4179 regmatch_T regmatch;
4180 int ic;
4181 char_u *save_cpo;
4182
4183 /*
4184 * Get the first variable.
4185 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return FAIL;
4188
4189 p = *arg;
4190 switch (p[0])
4191 {
4192 case '=': if (p[1] == '=')
4193 type = TYPE_EQUAL;
4194 else if (p[1] == '~')
4195 type = TYPE_MATCH;
4196 break;
4197 case '!': if (p[1] == '=')
4198 type = TYPE_NEQUAL;
4199 else if (p[1] == '~')
4200 type = TYPE_NOMATCH;
4201 break;
4202 case '>': if (p[1] != '=')
4203 {
4204 type = TYPE_GREATER;
4205 len = 1;
4206 }
4207 else
4208 type = TYPE_GEQUAL;
4209 break;
4210 case '<': if (p[1] != '=')
4211 {
4212 type = TYPE_SMALLER;
4213 len = 1;
4214 }
4215 else
4216 type = TYPE_SEQUAL;
4217 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 case 'i': if (p[1] == 's')
4219 {
4220 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4221 len = 5;
4222 if (!vim_isIDc(p[len]))
4223 {
4224 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4225 type_is = TRUE;
4226 }
4227 }
4228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230
4231 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004232 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 */
4234 if (type != TYPE_UNKNOWN)
4235 {
4236 /* extra question mark appended: ignore case */
4237 if (p[len] == '?')
4238 {
4239 ic = TRUE;
4240 ++len;
4241 }
4242 /* extra '#' appended: match case */
4243 else if (p[len] == '#')
4244 {
4245 ic = FALSE;
4246 ++len;
4247 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004248 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 else
4250 ic = p_ic;
4251
4252 /*
4253 * Get the second variable.
4254 */
4255 *arg = skipwhite(p + len);
4256 if (eval5(arg, &var2, evaluate) == FAIL)
4257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260 }
4261
4262 if (evaluate)
4263 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004264 if (type_is && rettv->v_type != var2.v_type)
4265 {
4266 /* For "is" a different type always means FALSE, for "notis"
4267 * it means TRUE. */
4268 n1 = (type == TYPE_NEQUAL);
4269 }
4270 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4271 {
4272 if (type_is)
4273 {
4274 n1 = (rettv->v_type == var2.v_type
4275 && rettv->vval.v_list == var2.vval.v_list);
4276 if (type == TYPE_NEQUAL)
4277 n1 = !n1;
4278 }
4279 else if (rettv->v_type != var2.v_type
4280 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4281 {
4282 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004283 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004285 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004286 clear_tv(rettv);
4287 clear_tv(&var2);
4288 return FAIL;
4289 }
4290 else
4291 {
4292 /* Compare two Lists for being equal or unequal. */
4293 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4294 if (type == TYPE_NEQUAL)
4295 n1 = !n1;
4296 }
4297 }
4298
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004299 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4300 {
4301 if (type_is)
4302 {
4303 n1 = (rettv->v_type == var2.v_type
4304 && rettv->vval.v_dict == var2.vval.v_dict);
4305 if (type == TYPE_NEQUAL)
4306 n1 = !n1;
4307 }
4308 else if (rettv->v_type != var2.v_type
4309 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4310 {
4311 if (rettv->v_type != var2.v_type)
4312 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4313 else
4314 EMSG(_("E736: Invalid operation for Dictionary"));
4315 clear_tv(rettv);
4316 clear_tv(&var2);
4317 return FAIL;
4318 }
4319 else
4320 {
4321 /* Compare two Dictionaries for being equal or unequal. */
4322 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4323 if (type == TYPE_NEQUAL)
4324 n1 = !n1;
4325 }
4326 }
4327
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004328 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4329 {
4330 if (rettv->v_type != var2.v_type
4331 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4332 {
4333 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004334 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004335 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004336 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004337 clear_tv(rettv);
4338 clear_tv(&var2);
4339 return FAIL;
4340 }
4341 else
4342 {
4343 /* Compare two Funcrefs for being equal or unequal. */
4344 if (rettv->vval.v_string == NULL
4345 || var2.vval.v_string == NULL)
4346 n1 = FALSE;
4347 else
4348 n1 = STRCMP(rettv->vval.v_string,
4349 var2.vval.v_string) == 0;
4350 if (type == TYPE_NEQUAL)
4351 n1 = !n1;
4352 }
4353 }
4354
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004355#ifdef FEAT_FLOAT
4356 /*
4357 * If one of the two variables is a float, compare as a float.
4358 * When using "=~" or "!~", always compare as string.
4359 */
4360 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4361 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4362 {
4363 float_T f1, f2;
4364
4365 if (rettv->v_type == VAR_FLOAT)
4366 f1 = rettv->vval.v_float;
4367 else
4368 f1 = get_tv_number(rettv);
4369 if (var2.v_type == VAR_FLOAT)
4370 f2 = var2.vval.v_float;
4371 else
4372 f2 = get_tv_number(&var2);
4373 n1 = FALSE;
4374 switch (type)
4375 {
4376 case TYPE_EQUAL: n1 = (f1 == f2); break;
4377 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4378 case TYPE_GREATER: n1 = (f1 > f2); break;
4379 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4380 case TYPE_SMALLER: n1 = (f1 < f2); break;
4381 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4382 case TYPE_UNKNOWN:
4383 case TYPE_MATCH:
4384 case TYPE_NOMATCH: break; /* avoid gcc warning */
4385 }
4386 }
4387#endif
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 /*
4390 * If one of the two variables is a number, compare as a number.
4391 * When using "=~" or "!~", always compare as string.
4392 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 n1 = get_tv_number(rettv);
4397 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 switch (type)
4399 {
4400 case TYPE_EQUAL: n1 = (n1 == n2); break;
4401 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4402 case TYPE_GREATER: n1 = (n1 > n2); break;
4403 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4404 case TYPE_SMALLER: n1 = (n1 < n2); break;
4405 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4406 case TYPE_UNKNOWN:
4407 case TYPE_MATCH:
4408 case TYPE_NOMATCH: break; /* avoid gcc warning */
4409 }
4410 }
4411 else
4412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 s1 = get_tv_string_buf(rettv, buf1);
4414 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4416 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4417 else
4418 i = 0;
4419 n1 = FALSE;
4420 switch (type)
4421 {
4422 case TYPE_EQUAL: n1 = (i == 0); break;
4423 case TYPE_NEQUAL: n1 = (i != 0); break;
4424 case TYPE_GREATER: n1 = (i > 0); break;
4425 case TYPE_GEQUAL: n1 = (i >= 0); break;
4426 case TYPE_SMALLER: n1 = (i < 0); break;
4427 case TYPE_SEQUAL: n1 = (i <= 0); break;
4428
4429 case TYPE_MATCH:
4430 case TYPE_NOMATCH:
4431 /* avoid 'l' flag in 'cpoptions' */
4432 save_cpo = p_cpo;
4433 p_cpo = (char_u *)"";
4434 regmatch.regprog = vim_regcomp(s2,
4435 RE_MAGIC + RE_STRING);
4436 regmatch.rm_ic = ic;
4437 if (regmatch.regprog != NULL)
4438 {
4439 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4440 vim_free(regmatch.regprog);
4441 if (type == TYPE_NOMATCH)
4442 n1 = !n1;
4443 }
4444 p_cpo = save_cpo;
4445 break;
4446
4447 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4448 }
4449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 clear_tv(rettv);
4451 clear_tv(&var2);
4452 rettv->v_type = VAR_NUMBER;
4453 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455 }
4456
4457 return OK;
4458}
4459
4460/*
4461 * Handle fourth level expression:
4462 * + number addition
4463 * - number subtraction
4464 * . string concatenation
4465 *
4466 * "arg" must point to the first non-white of the expression.
4467 * "arg" is advanced to the next non-white after the recognized expression.
4468 *
4469 * Return OK or FAIL.
4470 */
4471 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 int evaluate;
4476{
Bram Moolenaar33570922005-01-25 22:26:29 +00004477 typval_T var2;
4478 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 int op;
4480 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004481#ifdef FEAT_FLOAT
4482 float_T f1 = 0, f2 = 0;
4483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 char_u *s1, *s2;
4485 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4486 char_u *p;
4487
4488 /*
4489 * Get the first variable.
4490 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004491 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 return FAIL;
4493
4494 /*
4495 * Repeat computing, until no '+', '-' or '.' is following.
4496 */
4497 for (;;)
4498 {
4499 op = **arg;
4500 if (op != '+' && op != '-' && op != '.')
4501 break;
4502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004503 if ((op != '+' || rettv->v_type != VAR_LIST)
4504#ifdef FEAT_FLOAT
4505 && (op == '.' || rettv->v_type != VAR_FLOAT)
4506#endif
4507 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 {
4509 /* For "list + ...", an illegal use of the first operand as
4510 * a number cannot be determined before evaluating the 2nd
4511 * operand: if this is also a list, all is ok.
4512 * For "something . ...", "something - ..." or "non-list + ...",
4513 * we know that the first operand needs to be a string or number
4514 * without evaluating the 2nd operand. So check before to avoid
4515 * side effects after an error. */
4516 if (evaluate && get_tv_string_chk(rettv) == NULL)
4517 {
4518 clear_tv(rettv);
4519 return FAIL;
4520 }
4521 }
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /*
4524 * Get the second variable.
4525 */
4526 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004527 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 return FAIL;
4531 }
4532
4533 if (evaluate)
4534 {
4535 /*
4536 * Compute the result.
4537 */
4538 if (op == '.')
4539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004540 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4541 s2 = get_tv_string_buf_chk(&var2, buf2);
4542 if (s2 == NULL) /* type error ? */
4543 {
4544 clear_tv(rettv);
4545 clear_tv(&var2);
4546 return FAIL;
4547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004548 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 clear_tv(rettv);
4550 rettv->v_type = VAR_STRING;
4551 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004553 else if (op == '+' && rettv->v_type == VAR_LIST
4554 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 {
4556 /* concatenate Lists */
4557 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4558 &var3) == FAIL)
4559 {
4560 clear_tv(rettv);
4561 clear_tv(&var2);
4562 return FAIL;
4563 }
4564 clear_tv(rettv);
4565 *rettv = var3;
4566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else
4568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004569 int error = FALSE;
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571#ifdef FEAT_FLOAT
4572 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004574 f1 = rettv->vval.v_float;
4575 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004577 else
4578#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004580 n1 = get_tv_number_chk(rettv, &error);
4581 if (error)
4582 {
4583 /* This can only happen for "list + non-list". For
4584 * "non-list + ..." or "something - ...", we returned
4585 * before evaluating the 2nd operand. */
4586 clear_tv(rettv);
4587 return FAIL;
4588 }
4589#ifdef FEAT_FLOAT
4590 if (var2.v_type == VAR_FLOAT)
4591 f1 = n1;
4592#endif
4593 }
4594#ifdef FEAT_FLOAT
4595 if (var2.v_type == VAR_FLOAT)
4596 {
4597 f2 = var2.vval.v_float;
4598 n2 = 0;
4599 }
4600 else
4601#endif
4602 {
4603 n2 = get_tv_number_chk(&var2, &error);
4604 if (error)
4605 {
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610#ifdef FEAT_FLOAT
4611 if (rettv->v_type == VAR_FLOAT)
4612 f2 = n2;
4613#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004616
4617#ifdef FEAT_FLOAT
4618 /* If there is a float on either side the result is a float. */
4619 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4620 {
4621 if (op == '+')
4622 f1 = f1 + f2;
4623 else
4624 f1 = f1 - f2;
4625 rettv->v_type = VAR_FLOAT;
4626 rettv->vval.v_float = f1;
4627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629#endif
4630 {
4631 if (op == '+')
4632 n1 = n1 + n2;
4633 else
4634 n1 = n1 - n2;
4635 rettv->v_type = VAR_NUMBER;
4636 rettv->vval.v_number = n1;
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004639 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 }
4642 return OK;
4643}
4644
4645/*
4646 * Handle fifth level expression:
4647 * * number multiplication
4648 * / number division
4649 * % number modulo
4650 *
4651 * "arg" must point to the first non-white of the expression.
4652 * "arg" is advanced to the next non-white after the recognized expression.
4653 *
4654 * Return OK or FAIL.
4655 */
4656 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004657eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004661 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
Bram Moolenaar33570922005-01-25 22:26:29 +00004663 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 int op;
4665 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004666#ifdef FEAT_FLOAT
4667 int use_float = FALSE;
4668 float_T f1 = 0, f2;
4669#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 /*
4673 * Get the first variable.
4674 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004675 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 return FAIL;
4677
4678 /*
4679 * Repeat computing, until no '*', '/' or '%' is following.
4680 */
4681 for (;;)
4682 {
4683 op = **arg;
4684 if (op != '*' && op != '/' && op != '%')
4685 break;
4686
4687 if (evaluate)
4688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689#ifdef FEAT_FLOAT
4690 if (rettv->v_type == VAR_FLOAT)
4691 {
4692 f1 = rettv->vval.v_float;
4693 use_float = TRUE;
4694 n1 = 0;
4695 }
4696 else
4697#endif
4698 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 if (error)
4701 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 else
4704 n1 = 0;
4705
4706 /*
4707 * Get the second variable.
4708 */
4709 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004710 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 return FAIL;
4712
4713 if (evaluate)
4714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715#ifdef FEAT_FLOAT
4716 if (var2.v_type == VAR_FLOAT)
4717 {
4718 if (!use_float)
4719 {
4720 f1 = n1;
4721 use_float = TRUE;
4722 }
4723 f2 = var2.vval.v_float;
4724 n2 = 0;
4725 }
4726 else
4727#endif
4728 {
4729 n2 = get_tv_number_chk(&var2, &error);
4730 clear_tv(&var2);
4731 if (error)
4732 return FAIL;
4733#ifdef FEAT_FLOAT
4734 if (use_float)
4735 f2 = n2;
4736#endif
4737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
4739 /*
4740 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004741 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743#ifdef FEAT_FLOAT
4744 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746 if (op == '*')
4747 f1 = f1 * f2;
4748 else if (op == '/')
4749 {
4750 /* We rely on the floating point library to handle divide
4751 * by zero to result in "inf" and not a crash. */
4752 f1 = f1 / f2;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004756 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757 return FAIL;
4758 }
4759 rettv->v_type = VAR_FLOAT;
4760 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765 if (op == '*')
4766 n1 = n1 * n2;
4767 else if (op == '/')
4768 {
4769 if (n2 == 0) /* give an error message? */
4770 {
4771 if (n1 == 0)
4772 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4773 else if (n1 < 0)
4774 n1 = -0x7fffffffL;
4775 else
4776 n1 = 0x7fffffffL;
4777 }
4778 else
4779 n1 = n1 / n2;
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782 {
4783 if (n2 == 0) /* give an error message? */
4784 n1 = 0;
4785 else
4786 n1 = n1 % n2;
4787 }
4788 rettv->v_type = VAR_NUMBER;
4789 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
4793
4794 return OK;
4795}
4796
4797/*
4798 * Handle sixth level expression:
4799 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004800 * "string" string constant
4801 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 * &option-name option value
4803 * @r register contents
4804 * identifier variable value
4805 * function() function call
4806 * $VAR environment variable
4807 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004808 * [expr, expr] List
4809 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 *
4811 * Also handle:
4812 * ! in front logical NOT
4813 * - in front unary minus
4814 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004815 * trailing [] subscript in String or List
4816 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 *
4818 * "arg" must point to the first non-white of the expression.
4819 * "arg" is advanced to the next non-white after the recognized expression.
4820 *
4821 * Return OK or FAIL.
4822 */
4823 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004824eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004828 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 long n;
4831 int len;
4832 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 char_u *start_leader, *end_leader;
4834 int ret = OK;
4835 char_u *alias;
4836
4837 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 /*
4844 * Skip '!' and '-' characters. They are handled later.
4845 */
4846 start_leader = *arg;
4847 while (**arg == '!' || **arg == '-' || **arg == '+')
4848 *arg = skipwhite(*arg + 1);
4849 end_leader = *arg;
4850
4851 switch (**arg)
4852 {
4853 /*
4854 * Number constant.
4855 */
4856 case '0':
4857 case '1':
4858 case '2':
4859 case '3':
4860 case '4':
4861 case '5':
4862 case '6':
4863 case '7':
4864 case '8':
4865 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 {
4867#ifdef FEAT_FLOAT
4868 char_u *p = skipdigits(*arg + 1);
4869 int get_float = FALSE;
4870
4871 /* We accept a float when the format matches
4872 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004873 * strict to avoid backwards compatibility problems.
4874 * Don't look for a float after the "." operator, so that
4875 * ":let vers = 1.2.3" doesn't fail. */
4876 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878 get_float = TRUE;
4879 p = skipdigits(p + 2);
4880 if (*p == 'e' || *p == 'E')
4881 {
4882 ++p;
4883 if (*p == '-' || *p == '+')
4884 ++p;
4885 if (!vim_isdigit(*p))
4886 get_float = FALSE;
4887 else
4888 p = skipdigits(p + 1);
4889 }
4890 if (ASCII_ISALPHA(*p) || *p == '.')
4891 get_float = FALSE;
4892 }
4893 if (get_float)
4894 {
4895 float_T f;
4896
4897 *arg += string2float(*arg, &f);
4898 if (evaluate)
4899 {
4900 rettv->v_type = VAR_FLOAT;
4901 rettv->vval.v_float = f;
4902 }
4903 }
4904 else
4905#endif
4906 {
4907 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4908 *arg += len;
4909 if (evaluate)
4910 {
4911 rettv->v_type = VAR_NUMBER;
4912 rettv->vval.v_number = n;
4913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 /*
4919 * String constant: "string".
4920 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 break;
4923
4924 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004927 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004928 break;
4929
4930 /*
4931 * List: [expr, expr]
4932 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 break;
4935
4936 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937 * Dictionary: {key: val, key: val}
4938 */
4939 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4940 break;
4941
4942 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004943 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004945 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 break;
4947
4948 /*
4949 * Environment variable: $VAR.
4950 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004951 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 break;
4953
4954 /*
4955 * Register contents: @r.
4956 */
4957 case '@': ++*arg;
4958 if (evaluate)
4959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004961 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 if (**arg != NUL)
4964 ++*arg;
4965 break;
4966
4967 /*
4968 * nested expression: (expression).
4969 */
4970 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (**arg == ')')
4973 ++*arg;
4974 else if (ret == OK)
4975 {
4976 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 ret = FAIL;
4979 }
4980 break;
4981
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 break;
4984 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985
4986 if (ret == NOTDONE)
4987 {
4988 /*
4989 * Must be a variable or function name.
4990 * Can also be a curly-braces kind of name: {expr}.
4991 */
4992 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 if (alias != NULL)
4995 s = alias;
4996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004997 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 ret = FAIL;
4999 else
5000 {
5001 if (**arg == '(') /* recursive! */
5002 {
5003 /* If "s" is the name of a variable of type VAR_FUNC
5004 * use its contents. */
5005 s = deref_func_name(s, &len);
5006
5007 /* Invoke the function. */
5008 ret = get_func_tv(s, len, rettv, arg,
5009 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005010 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 /* Stop the expression evaluation when immediately
5012 * aborting on error, or when an interrupt occurred or
5013 * an exception was thrown but not caught. */
5014 if (aborting())
5015 {
5016 if (ret == OK)
5017 clear_tv(rettv);
5018 ret = FAIL;
5019 }
5020 }
5021 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005022 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005023 else
5024 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 }
5026
5027 if (alias != NULL)
5028 vim_free(alias);
5029 }
5030
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *arg = skipwhite(*arg);
5032
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005033 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5034 * expr(expr). */
5035 if (ret == OK)
5036 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 /*
5039 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5040 */
5041 if (ret == OK && evaluate && end_leader > start_leader)
5042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005043 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 int val = 0;
5045#ifdef FEAT_FLOAT
5046 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005047
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005048 if (rettv->v_type == VAR_FLOAT)
5049 f = rettv->vval.v_float;
5050 else
5051#endif
5052 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005053 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005055 clear_tv(rettv);
5056 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005058 else
5059 {
5060 while (end_leader > start_leader)
5061 {
5062 --end_leader;
5063 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005064 {
5065#ifdef FEAT_FLOAT
5066 if (rettv->v_type == VAR_FLOAT)
5067 f = !f;
5068 else
5069#endif
5070 val = !val;
5071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 {
5074#ifdef FEAT_FLOAT
5075 if (rettv->v_type == VAR_FLOAT)
5076 f = -f;
5077 else
5078#endif
5079 val = -val;
5080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005081 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082#ifdef FEAT_FLOAT
5083 if (rettv->v_type == VAR_FLOAT)
5084 {
5085 clear_tv(rettv);
5086 rettv->vval.v_float = f;
5087 }
5088 else
5089#endif
5090 {
5091 clear_tv(rettv);
5092 rettv->v_type = VAR_NUMBER;
5093 rettv->vval.v_number = val;
5094 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
5098 return ret;
5099}
5100
5101/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005102 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5103 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005104 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5105 */
5106 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005107eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005109 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005111 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112{
5113 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005114 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 long len = -1;
5117 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005121 if (rettv->v_type == VAR_FUNC
5122#ifdef FEAT_FLOAT
5123 || rettv->v_type == VAR_FLOAT
5124#endif
5125 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005127 if (verbose)
5128 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 return FAIL;
5130 }
5131
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 /*
5135 * dict.name
5136 */
5137 key = *arg + 1;
5138 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5139 ;
5140 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 }
5144 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 /*
5147 * something[idx]
5148 *
5149 * Get the (first) variable from inside the [].
5150 */
5151 *arg = skipwhite(*arg + 1);
5152 if (**arg == ':')
5153 empty1 = TRUE;
5154 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5155 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005156 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5157 {
5158 /* not a number or string */
5159 clear_tv(&var1);
5160 return FAIL;
5161 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162
5163 /*
5164 * Get the second variable from inside the [:].
5165 */
5166 if (**arg == ':')
5167 {
5168 range = TRUE;
5169 *arg = skipwhite(*arg + 1);
5170 if (**arg == ']')
5171 empty2 = TRUE;
5172 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005174 if (!empty1)
5175 clear_tv(&var1);
5176 return FAIL;
5177 }
5178 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5179 {
5180 /* not a number or string */
5181 if (!empty1)
5182 clear_tv(&var1);
5183 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 return FAIL;
5185 }
5186 }
5187
5188 /* Check for the ']'. */
5189 if (**arg != ']')
5190 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191 if (verbose)
5192 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 clear_tv(&var1);
5194 if (range)
5195 clear_tv(&var2);
5196 return FAIL;
5197 }
5198 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 }
5200
5201 if (evaluate)
5202 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 n1 = 0;
5204 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 n1 = get_tv_number(&var1);
5207 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 }
5209 if (range)
5210 {
5211 if (empty2)
5212 n2 = -1;
5213 else
5214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 n2 = get_tv_number(&var2);
5216 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217 }
5218 }
5219
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 {
5222 case VAR_NUMBER:
5223 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 len = (long)STRLEN(s);
5226 if (range)
5227 {
5228 /* The resulting variable is a substring. If the indexes
5229 * are out of range the result is empty. */
5230 if (n1 < 0)
5231 {
5232 n1 = len + n1;
5233 if (n1 < 0)
5234 n1 = 0;
5235 }
5236 if (n2 < 0)
5237 n2 = len + n2;
5238 else if (n2 >= len)
5239 n2 = len;
5240 if (n1 >= len || n2 < 0 || n1 > n2)
5241 s = NULL;
5242 else
5243 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5244 }
5245 else
5246 {
5247 /* The resulting variable is a string of a single
5248 * character. If the index is too big or negative the
5249 * result is empty. */
5250 if (n1 >= len || n1 < 0)
5251 s = NULL;
5252 else
5253 s = vim_strnsave(s + n1, 1);
5254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 clear_tv(rettv);
5256 rettv->v_type = VAR_STRING;
5257 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 break;
5259
5260 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 if (n1 < 0)
5263 n1 = len + n1;
5264 if (!empty1 && (n1 < 0 || n1 >= len))
5265 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005266 /* For a range we allow invalid values and return an empty
5267 * list. A list index out of range is an error. */
5268 if (!range)
5269 {
5270 if (verbose)
5271 EMSGN(_(e_listidx), n1);
5272 return FAIL;
5273 }
5274 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 }
5276 if (range)
5277 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 list_T *l;
5279 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280
5281 if (n2 < 0)
5282 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005283 else if (n2 >= len)
5284 n2 = len - 1;
5285 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005286 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 l = list_alloc();
5288 if (l == NULL)
5289 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 n1 <= n2; ++n1)
5292 {
5293 if (list_append_tv(l, &item->li_tv) == FAIL)
5294 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005295 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 return FAIL;
5297 }
5298 item = item->li_next;
5299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005300 clear_tv(rettv);
5301 rettv->v_type = VAR_LIST;
5302 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005303 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 }
5305 else
5306 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005307 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 clear_tv(rettv);
5309 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 }
5311 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 case VAR_DICT:
5314 if (range)
5315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005316 if (verbose)
5317 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 if (len == -1)
5319 clear_tv(&var1);
5320 return FAIL;
5321 }
5322 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005323 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324
5325 if (len == -1)
5326 {
5327 key = get_tv_string(&var1);
5328 if (*key == NUL)
5329 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005330 if (verbose)
5331 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 clear_tv(&var1);
5333 return FAIL;
5334 }
5335 }
5336
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005337 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005340 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 if (len == -1)
5342 clear_tv(&var1);
5343 if (item == NULL)
5344 return FAIL;
5345
5346 copy_tv(&item->di_tv, &var1);
5347 clear_tv(rettv);
5348 *rettv = var1;
5349 }
5350 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 }
5352 }
5353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 return OK;
5355}
5356
5357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 * Get an option value.
5359 * "arg" points to the '&' or '+' before the option name.
5360 * "arg" is advanced to character after the option name.
5361 * Return OK or FAIL.
5362 */
5363 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005366 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 int evaluate;
5368{
5369 char_u *option_end;
5370 long numval;
5371 char_u *stringval;
5372 int opt_type;
5373 int c;
5374 int working = (**arg == '+'); /* has("+option") */
5375 int ret = OK;
5376 int opt_flags;
5377
5378 /*
5379 * Isolate the option name and find its value.
5380 */
5381 option_end = find_option_end(arg, &opt_flags);
5382 if (option_end == NULL)
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 EMSG2(_("E112: Option name missing: %s"), *arg);
5386 return FAIL;
5387 }
5388
5389 if (!evaluate)
5390 {
5391 *arg = option_end;
5392 return OK;
5393 }
5394
5395 c = *option_end;
5396 *option_end = NUL;
5397 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005398 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
5400 if (opt_type == -3) /* invalid name */
5401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005402 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 EMSG2(_("E113: Unknown option: %s"), *arg);
5404 ret = FAIL;
5405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 if (opt_type == -2) /* hidden string option */
5409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 rettv->v_type = VAR_STRING;
5411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413 else if (opt_type == -1) /* hidden number option */
5414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 rettv->v_type = VAR_NUMBER;
5416 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 else if (opt_type == 1) /* number option */
5419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 rettv->v_type = VAR_NUMBER;
5421 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423 else /* string option */
5424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 rettv->v_type = VAR_STRING;
5426 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428 }
5429 else if (working && (opt_type == -2 || opt_type == -1))
5430 ret = FAIL;
5431
5432 *option_end = c; /* put back for error messages */
5433 *arg = option_end;
5434
5435 return ret;
5436}
5437
5438/*
5439 * Allocate a variable for a string constant.
5440 * Return OK or FAIL.
5441 */
5442 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005443get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 int evaluate;
5447{
5448 char_u *p;
5449 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 int extra = 0;
5451
5452 /*
5453 * Find the end of the string, skipping backslashed characters.
5454 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005455 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 if (*p == '\\' && p[1] != NUL)
5458 {
5459 ++p;
5460 /* A "\<x>" form occupies at least 4 characters, and produces up
5461 * to 6 characters: reserve space for 2 extra */
5462 if (*p == '<')
5463 extra += 2;
5464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466
5467 if (*p != '"')
5468 {
5469 EMSG2(_("E114: Missing quote: %s"), *arg);
5470 return FAIL;
5471 }
5472
5473 /* If only parsing, set *arg and return here */
5474 if (!evaluate)
5475 {
5476 *arg = p + 1;
5477 return OK;
5478 }
5479
5480 /*
5481 * Copy the string into allocated memory, handling backslashed
5482 * characters.
5483 */
5484 name = alloc((unsigned)(p - *arg + extra));
5485 if (name == NULL)
5486 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 rettv->v_type = VAR_STRING;
5488 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 if (*p == '\\')
5493 {
5494 switch (*++p)
5495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 case 'b': *name++ = BS; ++p; break;
5497 case 'e': *name++ = ESC; ++p; break;
5498 case 'f': *name++ = FF; ++p; break;
5499 case 'n': *name++ = NL; ++p; break;
5500 case 'r': *name++ = CAR; ++p; break;
5501 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503 case 'X': /* hex: "\x1", "\x12" */
5504 case 'x':
5505 case 'u': /* Unicode: "\u0023" */
5506 case 'U':
5507 if (vim_isxdigit(p[1]))
5508 {
5509 int n, nr;
5510 int c = toupper(*p);
5511
5512 if (c == 'X')
5513 n = 2;
5514 else
5515 n = 4;
5516 nr = 0;
5517 while (--n >= 0 && vim_isxdigit(p[1]))
5518 {
5519 ++p;
5520 nr = (nr << 4) + hex2nr(*p);
5521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523#ifdef FEAT_MBYTE
5524 /* For "\u" store the number according to
5525 * 'encoding'. */
5526 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 else
5529#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 break;
5533
5534 /* octal: "\1", "\12", "\123" */
5535 case '0':
5536 case '1':
5537 case '2':
5538 case '3':
5539 case '4':
5540 case '5':
5541 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 case '7': *name = *p++ - '0';
5543 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005545 *name = (*name << 3) + *p++ - '0';
5546 if (*p >= '0' && *p <= '7')
5547 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005549 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 break;
5551
5552 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (extra != 0)
5555 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005556 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 break;
5558 }
5559 /* FALLTHROUGH */
5560
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 break;
5563 }
5564 }
5565 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 *arg = p + 1;
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 return OK;
5573}
5574
5575/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 * Return OK or FAIL.
5578 */
5579 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 int evaluate;
5584{
5585 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005586 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005587 int reduce = 0;
5588
5589 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005591 */
5592 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5593 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005597 break;
5598 ++reduce;
5599 ++p;
5600 }
5601 }
5602
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005606 return FAIL;
5607 }
5608
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005610 if (!evaluate)
5611 {
5612 *arg = p + 1;
5613 return OK;
5614 }
5615
5616 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 */
5619 str = alloc((unsigned)((p - *arg) - reduce));
5620 if (str == NULL)
5621 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 rettv->v_type = VAR_STRING;
5623 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005630 break;
5631 ++p;
5632 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 *arg = p + 1;
5637
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005638 return OK;
5639}
5640
5641/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 * Allocate a variable for a List and fill it from "*arg".
5643 * Return OK or FAIL.
5644 */
5645 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 int evaluate;
5650{
Bram Moolenaar33570922005-01-25 22:26:29 +00005651 list_T *l = NULL;
5652 typval_T tv;
5653 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654
5655 if (evaluate)
5656 {
5657 l = list_alloc();
5658 if (l == NULL)
5659 return FAIL;
5660 }
5661
5662 *arg = skipwhite(*arg + 1);
5663 while (**arg != ']' && **arg != NUL)
5664 {
5665 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5666 goto failret;
5667 if (evaluate)
5668 {
5669 item = listitem_alloc();
5670 if (item != NULL)
5671 {
5672 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005673 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 list_append(l, item);
5675 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005676 else
5677 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 }
5679
5680 if (**arg == ']')
5681 break;
5682 if (**arg != ',')
5683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685 goto failret;
5686 }
5687 *arg = skipwhite(*arg + 1);
5688 }
5689
5690 if (**arg != ']')
5691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693failret:
5694 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005695 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 return FAIL;
5697 }
5698
5699 *arg = skipwhite(*arg + 1);
5700 if (evaluate)
5701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 rettv->v_type = VAR_LIST;
5703 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 ++l->lv_refcount;
5705 }
5706
5707 return OK;
5708}
5709
5710/*
5711 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005712 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005714 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715list_alloc()
5716{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005717 list_T *l;
5718
5719 l = (list_T *)alloc_clear(sizeof(list_T));
5720 if (l != NULL)
5721 {
5722 /* Prepend the list to the list of lists for garbage collection. */
5723 if (first_list != NULL)
5724 first_list->lv_used_prev = l;
5725 l->lv_used_prev = NULL;
5726 l->lv_used_next = first_list;
5727 first_list = l;
5728 }
5729 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730}
5731
5732/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005733 * Allocate an empty list for a return value.
5734 * Returns OK or FAIL.
5735 */
5736 static int
5737rettv_list_alloc(rettv)
5738 typval_T *rettv;
5739{
5740 list_T *l = list_alloc();
5741
5742 if (l == NULL)
5743 return FAIL;
5744
5745 rettv->vval.v_list = l;
5746 rettv->v_type = VAR_LIST;
5747 ++l->lv_refcount;
5748 return OK;
5749}
5750
5751/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752 * Unreference a list: decrement the reference count and free it when it
5753 * becomes zero.
5754 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005755 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005759 if (l != NULL && --l->lv_refcount <= 0)
5760 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761}
5762
5763/*
5764 * Free a list, including all items it points to.
5765 * Ignores the reference count.
5766 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005767 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005768list_free(l, recurse)
5769 list_T *l;
5770 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771{
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005774 /* Remove the list from the list of lists for garbage collection. */
5775 if (l->lv_used_prev == NULL)
5776 first_list = l->lv_used_next;
5777 else
5778 l->lv_used_prev->lv_used_next = l->lv_used_next;
5779 if (l->lv_used_next != NULL)
5780 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5781
Bram Moolenaard9fba312005-06-26 22:34:35 +00005782 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005784 /* Remove the item before deleting it. */
5785 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005786 if (recurse || (item->li_tv.v_type != VAR_LIST
5787 && item->li_tv.v_type != VAR_DICT))
5788 clear_tv(&item->li_tv);
5789 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 }
5791 vim_free(l);
5792}
5793
5794/*
5795 * Allocate a list item.
5796 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798listitem_alloc()
5799{
Bram Moolenaar33570922005-01-25 22:26:29 +00005800 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801}
5802
5803/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005804 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 */
5806 static void
5807listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005808 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 vim_free(item);
5812}
5813
5814/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005815 * Remove a list item from a List and free it. Also clears the value.
5816 */
5817 static void
5818listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 list_T *l;
5820 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005821{
5822 list_remove(l, item, item);
5823 listitem_free(item);
5824}
5825
5826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 * Get the number of items in a list.
5828 */
5829 static long
5830list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 if (l == NULL)
5834 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005835 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836}
5837
5838/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005839 * Return TRUE when two lists have exactly the same values.
5840 */
5841 static int
5842list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005843 list_T *l1;
5844 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005845 int ic; /* ignore case for strings */
5846{
Bram Moolenaar33570922005-01-25 22:26:29 +00005847 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005848
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005849 if (l1 == NULL || l2 == NULL)
5850 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005851 if (l1 == l2)
5852 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005853 if (list_len(l1) != list_len(l2))
5854 return FALSE;
5855
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856 for (item1 = l1->lv_first, item2 = l2->lv_first;
5857 item1 != NULL && item2 != NULL;
5858 item1 = item1->li_next, item2 = item2->li_next)
5859 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5860 return FALSE;
5861 return item1 == NULL && item2 == NULL;
5862}
5863
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005864#if defined(FEAT_PYTHON) || defined(PROTO)
5865/*
5866 * Return the dictitem that an entry in a hashtable points to.
5867 */
5868 dictitem_T *
5869dict_lookup(hi)
5870 hashitem_T *hi;
5871{
5872 return HI2DI(hi);
5873}
5874#endif
5875
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005876/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005877 * Return TRUE when two dictionaries have exactly the same key/values.
5878 */
5879 static int
5880dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 dict_T *d1;
5882 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005883 int ic; /* ignore case for strings */
5884{
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 hashitem_T *hi;
5886 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005887 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005888
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005889 if (d1 == NULL || d2 == NULL)
5890 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005891 if (d1 == d2)
5892 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005893 if (dict_len(d1) != dict_len(d2))
5894 return FALSE;
5895
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005896 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005898 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005899 if (!HASHITEM_EMPTY(hi))
5900 {
5901 item2 = dict_find(d2, hi->hi_key, -1);
5902 if (item2 == NULL)
5903 return FALSE;
5904 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5905 return FALSE;
5906 --todo;
5907 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005908 }
5909 return TRUE;
5910}
5911
5912/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005913 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005914 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005915 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005916 */
5917 static int
5918tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005919 typval_T *tv1;
5920 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005921 int ic; /* ignore case */
5922{
5923 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005924 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005925 static int recursive = 0; /* cach recursive loops */
5926 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005927
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005928 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005929 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005930 /* Catch lists and dicts that have an endless loop by limiting
5931 * recursiveness to 1000. We guess they are equal then. */
5932 if (recursive >= 1000)
5933 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005934
5935 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005936 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005937 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005938 ++recursive;
5939 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5940 --recursive;
5941 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005942
5943 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005944 ++recursive;
5945 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5946 --recursive;
5947 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005948
5949 case VAR_FUNC:
5950 return (tv1->vval.v_string != NULL
5951 && tv2->vval.v_string != NULL
5952 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5953
5954 case VAR_NUMBER:
5955 return tv1->vval.v_number == tv2->vval.v_number;
5956
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005957#ifdef FEAT_FLOAT
5958 case VAR_FLOAT:
5959 return tv1->vval.v_float == tv2->vval.v_float;
5960#endif
5961
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005962 case VAR_STRING:
5963 s1 = get_tv_string_buf(tv1, buf1);
5964 s2 = get_tv_string_buf(tv2, buf2);
5965 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005966 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005967
5968 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969 return TRUE;
5970}
5971
5972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 * Locate item with index "n" in list "l" and return it.
5974 * A negative index is counted from the end; -1 is the last item.
5975 * Returns NULL when "n" is out of range.
5976 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 long n;
5981{
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 long idx;
5984
5985 if (l == NULL)
5986 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005987
5988 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005990 n = l->lv_len + n;
5991
5992 /* Check for index out of range. */
5993 if (n < 0 || n >= l->lv_len)
5994 return NULL;
5995
5996 /* When there is a cached index may start search from there. */
5997 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999 if (n < l->lv_idx / 2)
6000 {
6001 /* closest to the start of the list */
6002 item = l->lv_first;
6003 idx = 0;
6004 }
6005 else if (n > (l->lv_idx + l->lv_len) / 2)
6006 {
6007 /* closest to the end of the list */
6008 item = l->lv_last;
6009 idx = l->lv_len - 1;
6010 }
6011 else
6012 {
6013 /* closest to the cached index */
6014 item = l->lv_idx_item;
6015 idx = l->lv_idx;
6016 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 }
6018 else
6019 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006020 if (n < l->lv_len / 2)
6021 {
6022 /* closest to the start of the list */
6023 item = l->lv_first;
6024 idx = 0;
6025 }
6026 else
6027 {
6028 /* closest to the end of the list */
6029 item = l->lv_last;
6030 idx = l->lv_len - 1;
6031 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006033
6034 while (n > idx)
6035 {
6036 /* search forward */
6037 item = item->li_next;
6038 ++idx;
6039 }
6040 while (n < idx)
6041 {
6042 /* search backward */
6043 item = item->li_prev;
6044 --idx;
6045 }
6046
6047 /* cache the used index */
6048 l->lv_idx = idx;
6049 l->lv_idx_item = item;
6050
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 return item;
6052}
6053
6054/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006055 * Get list item "l[idx]" as a number.
6056 */
6057 static long
6058list_find_nr(l, idx, errorp)
6059 list_T *l;
6060 long idx;
6061 int *errorp; /* set to TRUE when something wrong */
6062{
6063 listitem_T *li;
6064
6065 li = list_find(l, idx);
6066 if (li == NULL)
6067 {
6068 if (errorp != NULL)
6069 *errorp = TRUE;
6070 return -1L;
6071 }
6072 return get_tv_number_chk(&li->li_tv, errorp);
6073}
6074
6075/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006076 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6077 */
6078 char_u *
6079list_find_str(l, idx)
6080 list_T *l;
6081 long idx;
6082{
6083 listitem_T *li;
6084
6085 li = list_find(l, idx - 1);
6086 if (li == NULL)
6087 {
6088 EMSGN(_(e_listidx), idx);
6089 return NULL;
6090 }
6091 return get_tv_string(&li->li_tv);
6092}
6093
6094/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006095 * Locate "item" list "l" and return its index.
6096 * Returns -1 when "item" is not in the list.
6097 */
6098 static long
6099list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 list_T *l;
6101 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006102{
6103 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006105
6106 if (l == NULL)
6107 return -1;
6108 idx = 0;
6109 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6110 ++idx;
6111 if (li == NULL)
6112 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006113 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006114}
6115
6116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 * Append item "item" to the end of list "l".
6118 */
6119 static void
6120list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 list_T *l;
6122 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123{
6124 if (l->lv_last == NULL)
6125 {
6126 /* empty list */
6127 l->lv_first = item;
6128 l->lv_last = item;
6129 item->li_prev = NULL;
6130 }
6131 else
6132 {
6133 l->lv_last->li_next = item;
6134 item->li_prev = l->lv_last;
6135 l->lv_last = item;
6136 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006137 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138 item->li_next = NULL;
6139}
6140
6141/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144 */
6145 static int
6146list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 list_T *l;
6148 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006150 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151
Bram Moolenaar05159a02005-02-26 23:04:13 +00006152 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006154 copy_tv(tv, &li->li_tv);
6155 list_append(l, li);
6156 return OK;
6157}
6158
6159/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006160 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006161 * Return FAIL when out of memory.
6162 */
6163 int
6164list_append_dict(list, dict)
6165 list_T *list;
6166 dict_T *dict;
6167{
6168 listitem_T *li = listitem_alloc();
6169
6170 if (li == NULL)
6171 return FAIL;
6172 li->li_tv.v_type = VAR_DICT;
6173 li->li_tv.v_lock = 0;
6174 li->li_tv.vval.v_dict = dict;
6175 list_append(list, li);
6176 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177 return OK;
6178}
6179
6180/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006181 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006182 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006183 * Returns FAIL when out of memory.
6184 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006185 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006186list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006187 list_T *l;
6188 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006189 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006190{
6191 listitem_T *li = listitem_alloc();
6192
6193 if (li == NULL)
6194 return FAIL;
6195 list_append(l, li);
6196 li->li_tv.v_type = VAR_STRING;
6197 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006198 if (str == NULL)
6199 li->li_tv.vval.v_string = NULL;
6200 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006201 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006202 return FAIL;
6203 return OK;
6204}
6205
6206/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006207 * Append "n" to list "l".
6208 * Returns FAIL when out of memory.
6209 */
6210 static int
6211list_append_number(l, n)
6212 list_T *l;
6213 varnumber_T n;
6214{
6215 listitem_T *li;
6216
6217 li = listitem_alloc();
6218 if (li == NULL)
6219 return FAIL;
6220 li->li_tv.v_type = VAR_NUMBER;
6221 li->li_tv.v_lock = 0;
6222 li->li_tv.vval.v_number = n;
6223 list_append(l, li);
6224 return OK;
6225}
6226
6227/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006229 * If "item" is NULL append at the end.
6230 * Return FAIL when out of memory.
6231 */
6232 static int
6233list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 list_T *l;
6235 typval_T *tv;
6236 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006237{
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239
6240 if (ni == NULL)
6241 return FAIL;
6242 copy_tv(tv, &ni->li_tv);
6243 if (item == NULL)
6244 /* Append new item at end of list. */
6245 list_append(l, ni);
6246 else
6247 {
6248 /* Insert new item before existing item. */
6249 ni->li_prev = item->li_prev;
6250 ni->li_next = item;
6251 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006252 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006254 ++l->lv_idx;
6255 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006256 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006257 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006259 l->lv_idx_item = NULL;
6260 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006262 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006263 }
6264 return OK;
6265}
6266
6267/*
6268 * Extend "l1" with "l2".
6269 * If "bef" is NULL append at the end, otherwise insert before this item.
6270 * Returns FAIL when out of memory.
6271 */
6272 static int
6273list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 list_T *l1;
6275 list_T *l2;
6276 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277{
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006279 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006280
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006281 /* We also quit the loop when we have inserted the original item count of
6282 * the list, avoid a hang when we extend a list with itself. */
6283 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6285 return FAIL;
6286 return OK;
6287}
6288
6289/*
6290 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6291 * Return FAIL when out of memory.
6292 */
6293 static int
6294list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l1;
6296 list_T *l2;
6297 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006298{
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006300
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006301 if (l1 == NULL || l2 == NULL)
6302 return FAIL;
6303
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006305 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 if (l == NULL)
6307 return FAIL;
6308 tv->v_type = VAR_LIST;
6309 tv->vval.v_list = l;
6310
6311 /* append all items from the second list */
6312 return list_extend(l, l2, NULL);
6313}
6314
6315/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006318 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 * Returns NULL when out of memory.
6320 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006322list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006325 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326{
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *copy;
6328 listitem_T *item;
6329 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330
6331 if (orig == NULL)
6332 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333
6334 copy = list_alloc();
6335 if (copy != NULL)
6336 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006337 if (copyID != 0)
6338 {
6339 /* Do this before adding the items, because one of the items may
6340 * refer back to this list. */
6341 orig->lv_copyID = copyID;
6342 orig->lv_copylist = copy;
6343 }
6344 for (item = orig->lv_first; item != NULL && !got_int;
6345 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 {
6347 ni = listitem_alloc();
6348 if (ni == NULL)
6349 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 {
6352 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6353 {
6354 vim_free(ni);
6355 break;
6356 }
6357 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006359 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360 list_append(copy, ni);
6361 }
6362 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006363 if (item != NULL)
6364 {
6365 list_unref(copy);
6366 copy = NULL;
6367 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 }
6369
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 return copy;
6371}
6372
6373/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006375 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006378list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 list_T *l;
6380 listitem_T *item;
6381 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006382{
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006385 /* notify watchers */
6386 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006388 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006389 list_fix_watch(l, ip);
6390 if (ip == item2)
6391 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393
6394 if (item2->li_next == NULL)
6395 l->lv_last = item->li_prev;
6396 else
6397 item2->li_next->li_prev = item->li_prev;
6398 if (item->li_prev == NULL)
6399 l->lv_first = item2->li_next;
6400 else
6401 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403}
6404
6405/*
6406 * Return an allocated string with the string representation of a list.
6407 * May return NULL.
6408 */
6409 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006410list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006412 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413{
6414 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415
6416 if (tv->vval.v_list == NULL)
6417 return NULL;
6418 ga_init2(&ga, (int)sizeof(char), 80);
6419 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006420 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006421 {
6422 vim_free(ga.ga_data);
6423 return NULL;
6424 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 ga_append(&ga, ']');
6426 ga_append(&ga, NUL);
6427 return (char_u *)ga.ga_data;
6428}
6429
6430/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006431 * Join list "l" into a string in "*gap", using separator "sep".
6432 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006433 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006434 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006436list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006437 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006439 char_u *sep;
6440 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006441 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006442{
6443 int first = TRUE;
6444 char_u *tofree;
6445 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006447 char_u *s;
6448
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006450 {
6451 if (first)
6452 first = FALSE;
6453 else
6454 ga_concat(gap, sep);
6455
6456 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006457 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006458 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006459 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006460 if (s != NULL)
6461 ga_concat(gap, s);
6462 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463 if (s == NULL)
6464 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006465 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006467}
6468
6469/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006470 * Garbage collection for lists and dictionaries.
6471 *
6472 * We use reference counts to be able to free most items right away when they
6473 * are no longer used. But for composite items it's possible that it becomes
6474 * unused while the reference count is > 0: When there is a recursive
6475 * reference. Example:
6476 * :let l = [1, 2, 3]
6477 * :let d = {9: l}
6478 * :let l[1] = d
6479 *
6480 * Since this is quite unusual we handle this with garbage collection: every
6481 * once in a while find out which lists and dicts are not referenced from any
6482 * variable.
6483 *
6484 * Here is a good reference text about garbage collection (refers to Python
6485 * but it applies to all reference-counting mechanisms):
6486 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006487 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006488
6489/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006490 * Do garbage collection for lists and dicts.
6491 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006492 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006493 int
6494garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006495{
6496 dict_T *dd;
6497 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006498 int copyID = ++current_copyID;
6499 buf_T *buf;
6500 win_T *wp;
6501 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006502 funccall_T *fc, **pfc;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006503 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006504#ifdef FEAT_WINDOWS
6505 tabpage_T *tp;
6506#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006507
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006508 /* Only do this once. */
6509 want_garbage_collect = FALSE;
6510 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006511 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006512
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006513 /*
6514 * 1. Go through all accessible variables and mark all lists and dicts
6515 * with copyID.
6516 */
6517 /* script-local variables */
6518 for (i = 1; i <= ga_scripts.ga_len; ++i)
6519 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6520
6521 /* buffer-local variables */
6522 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6523 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6524
6525 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006526 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006527 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6528
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006529#ifdef FEAT_WINDOWS
6530 /* tabpage-local variables */
6531 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6532 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6533#endif
6534
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006535 /* global variables */
6536 set_ref_in_ht(&globvarht, copyID);
6537
6538 /* function-local variables */
6539 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6540 {
6541 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6542 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6543 }
6544
Bram Moolenaard812df62008-11-09 12:46:09 +00006545 /* v: vars */
6546 set_ref_in_ht(&vimvarht, copyID);
6547
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006548 /*
6549 * 2. Go through the list of dicts and free items without the copyID.
6550 */
6551 for (dd = first_dict; dd != NULL; )
6552 if (dd->dv_copyID != copyID)
6553 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006554 /* Free the Dictionary and ordinary items it contains, but don't
6555 * recurse into Lists and Dictionaries, they will be in the list
6556 * of dicts or list of lists. */
6557 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006558 did_free = TRUE;
6559
6560 /* restart, next dict may also have been freed */
6561 dd = first_dict;
6562 }
6563 else
6564 dd = dd->dv_used_next;
6565
6566 /*
6567 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006568 * But don't free a list that has a watcher (used in a for loop), these
6569 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006570 */
6571 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006572 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006573 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006574 /* Free the List and ordinary items it contains, but don't recurse
6575 * into Lists and Dictionaries, they will be in the list of dicts
6576 * or list of lists. */
6577 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006578 did_free = TRUE;
6579
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006580 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006581 ll = first_list;
6582 }
6583 else
6584 ll = ll->lv_used_next;
6585
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006586 /* check if any funccal can be freed now */
6587 for (pfc = &previous_funccal; *pfc != NULL; )
6588 {
6589 if (can_free_funccal(*pfc, copyID))
6590 {
6591 fc = *pfc;
6592 *pfc = fc->caller;
6593 free_funccal(fc, TRUE);
6594 did_free = TRUE;
6595 }
6596 else
6597 pfc = &(*pfc)->caller;
6598 }
6599
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006600 return did_free;
6601}
6602
6603/*
6604 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6605 */
6606 static void
6607set_ref_in_ht(ht, copyID)
6608 hashtab_T *ht;
6609 int copyID;
6610{
6611 int todo;
6612 hashitem_T *hi;
6613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006614 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006615 for (hi = ht->ht_array; todo > 0; ++hi)
6616 if (!HASHITEM_EMPTY(hi))
6617 {
6618 --todo;
6619 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6620 }
6621}
6622
6623/*
6624 * Mark all lists and dicts referenced through list "l" with "copyID".
6625 */
6626 static void
6627set_ref_in_list(l, copyID)
6628 list_T *l;
6629 int copyID;
6630{
6631 listitem_T *li;
6632
6633 for (li = l->lv_first; li != NULL; li = li->li_next)
6634 set_ref_in_item(&li->li_tv, copyID);
6635}
6636
6637/*
6638 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6639 */
6640 static void
6641set_ref_in_item(tv, copyID)
6642 typval_T *tv;
6643 int copyID;
6644{
6645 dict_T *dd;
6646 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006647
6648 switch (tv->v_type)
6649 {
6650 case VAR_DICT:
6651 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006652 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006653 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006654 /* Didn't see this dict yet. */
6655 dd->dv_copyID = copyID;
6656 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006657 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006658 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006659
6660 case VAR_LIST:
6661 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006662 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006663 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006664 /* Didn't see this list yet. */
6665 ll->lv_copyID = copyID;
6666 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006667 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006668 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006669 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006670 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006671}
6672
6673/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 * Allocate an empty header for a dictionary.
6675 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006676 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006677dict_alloc()
6678{
Bram Moolenaar33570922005-01-25 22:26:29 +00006679 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006680
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006683 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006684 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006685 if (first_dict != NULL)
6686 first_dict->dv_used_prev = d;
6687 d->dv_used_next = first_dict;
6688 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006689 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006690
Bram Moolenaar33570922005-01-25 22:26:29 +00006691 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006692 d->dv_lock = 0;
6693 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006694 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006695 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006696 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006697}
6698
6699/*
6700 * Unreference a Dictionary: decrement the reference count and free it when it
6701 * becomes zero.
6702 */
6703 static void
6704dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006706{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006707 if (d != NULL && --d->dv_refcount <= 0)
6708 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006709}
6710
6711/*
6712 * Free a Dictionary, including all items it contains.
6713 * Ignores the reference count.
6714 */
6715 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006716dict_free(d, recurse)
6717 dict_T *d;
6718 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006720 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006721 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006722 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006723
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006724 /* Remove the dict from the list of dicts for garbage collection. */
6725 if (d->dv_used_prev == NULL)
6726 first_dict = d->dv_used_next;
6727 else
6728 d->dv_used_prev->dv_used_next = d->dv_used_next;
6729 if (d->dv_used_next != NULL)
6730 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6731
6732 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006734 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006735 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 if (!HASHITEM_EMPTY(hi))
6738 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006739 /* Remove the item before deleting it, just in case there is
6740 * something recursive causing trouble. */
6741 di = HI2DI(hi);
6742 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006743 if (recurse || (di->di_tv.v_type != VAR_LIST
6744 && di->di_tv.v_type != VAR_DICT))
6745 clear_tv(&di->di_tv);
6746 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006747 --todo;
6748 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006750 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 vim_free(d);
6752}
6753
6754/*
6755 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006756 * The "key" is copied to the new item.
6757 * Note that the value of the item "di_tv" still needs to be initialized!
6758 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006759 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006761dictitem_alloc(key)
6762 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006763{
Bram Moolenaar33570922005-01-25 22:26:29 +00006764 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006765
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006766 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006767 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006769 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006770 di->di_flags = 0;
6771 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006772 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773}
6774
6775/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 * Make a copy of a Dictionary item.
6777 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006778 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006779dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781{
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006784 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6785 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006786 if (di != NULL)
6787 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006788 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006789 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006790 copy_tv(&org->di_tv, &di->di_tv);
6791 }
6792 return di;
6793}
6794
6795/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006796 * Remove item "item" from Dictionary "dict" and free it.
6797 */
6798 static void
6799dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006800 dict_T *dict;
6801 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006802{
Bram Moolenaar33570922005-01-25 22:26:29 +00006803 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006804
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006806 if (HASHITEM_EMPTY(hi))
6807 EMSG2(_(e_intern2), "dictitem_remove()");
6808 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006810 dictitem_free(item);
6811}
6812
6813/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006814 * Free a dict item. Also clears the value.
6815 */
6816 static void
6817dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820 clear_tv(&item->di_tv);
6821 vim_free(item);
6822}
6823
6824/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6826 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006827 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006828 * Returns NULL when out of memory.
6829 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006830 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006831dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006833 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006834 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835{
Bram Moolenaar33570922005-01-25 22:26:29 +00006836 dict_T *copy;
6837 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006838 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840
6841 if (orig == NULL)
6842 return NULL;
6843
6844 copy = dict_alloc();
6845 if (copy != NULL)
6846 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006847 if (copyID != 0)
6848 {
6849 orig->dv_copyID = copyID;
6850 orig->dv_copydict = copy;
6851 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006852 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006853 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006855 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 --todo;
6858
6859 di = dictitem_alloc(hi->hi_key);
6860 if (di == NULL)
6861 break;
6862 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006863 {
6864 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6865 copyID) == FAIL)
6866 {
6867 vim_free(di);
6868 break;
6869 }
6870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006871 else
6872 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6873 if (dict_add(copy, di) == FAIL)
6874 {
6875 dictitem_free(di);
6876 break;
6877 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880
Bram Moolenaare9a41262005-01-15 22:18:47 +00006881 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006882 if (todo > 0)
6883 {
6884 dict_unref(copy);
6885 copy = NULL;
6886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006887 }
6888
6889 return copy;
6890}
6891
6892/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006894 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006896 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006897dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 dict_T *d;
6899 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006900{
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902}
6903
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006905 * Add a number or string entry to dictionary "d".
6906 * When "str" is NULL use number "nr", otherwise use "str".
6907 * Returns FAIL when out of memory and when key already exists.
6908 */
6909 int
6910dict_add_nr_str(d, key, nr, str)
6911 dict_T *d;
6912 char *key;
6913 long nr;
6914 char_u *str;
6915{
6916 dictitem_T *item;
6917
6918 item = dictitem_alloc((char_u *)key);
6919 if (item == NULL)
6920 return FAIL;
6921 item->di_tv.v_lock = 0;
6922 if (str == NULL)
6923 {
6924 item->di_tv.v_type = VAR_NUMBER;
6925 item->di_tv.vval.v_number = nr;
6926 }
6927 else
6928 {
6929 item->di_tv.v_type = VAR_STRING;
6930 item->di_tv.vval.v_string = vim_strsave(str);
6931 }
6932 if (dict_add(d, item) == FAIL)
6933 {
6934 dictitem_free(item);
6935 return FAIL;
6936 }
6937 return OK;
6938}
6939
6940/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006941 * Get the number of items in a Dictionary.
6942 */
6943 static long
6944dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006946{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006947 if (d == NULL)
6948 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006949 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950}
6951
6952/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006953 * Find item "key[len]" in Dictionary "d".
6954 * If "len" is negative use strlen(key).
6955 * Returns NULL when not found.
6956 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006958dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006960 char_u *key;
6961 int len;
6962{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006963#define AKEYLEN 200
6964 char_u buf[AKEYLEN];
6965 char_u *akey;
6966 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969 if (len < 0)
6970 akey = key;
6971 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006972 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006973 tofree = akey = vim_strnsave(key, len);
6974 if (akey == NULL)
6975 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006976 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006977 else
6978 {
6979 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006980 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006981 akey = buf;
6982 }
6983
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006985 vim_free(tofree);
6986 if (HASHITEM_EMPTY(hi))
6987 return NULL;
6988 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006989}
6990
6991/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006992 * Get a string item from a dictionary.
6993 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006994 * Returns NULL if the entry doesn't exist or out of memory.
6995 */
6996 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006997get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006998 dict_T *d;
6999 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007000 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007001{
7002 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007003 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007004
7005 di = dict_find(d, key, -1);
7006 if (di == NULL)
7007 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007008 s = get_tv_string(&di->di_tv);
7009 if (save && s != NULL)
7010 s = vim_strsave(s);
7011 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007012}
7013
7014/*
7015 * Get a number item from a dictionary.
7016 * Returns 0 if the entry doesn't exist or out of memory.
7017 */
7018 long
7019get_dict_number(d, key)
7020 dict_T *d;
7021 char_u *key;
7022{
7023 dictitem_T *di;
7024
7025 di = dict_find(d, key, -1);
7026 if (di == NULL)
7027 return 0;
7028 return get_tv_number(&di->di_tv);
7029}
7030
7031/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 * Return an allocated string with the string representation of a Dictionary.
7033 * May return NULL.
7034 */
7035 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007036dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007038 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039{
7040 garray_T ga;
7041 int first = TRUE;
7042 char_u *tofree;
7043 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007044 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007049 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050 return NULL;
7051 ga_init2(&ga, (int)sizeof(char), 80);
7052 ga_append(&ga, '{');
7053
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007054 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007055 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 --todo;
7060
7061 if (first)
7062 first = FALSE;
7063 else
7064 ga_concat(&ga, (char_u *)", ");
7065
7066 tofree = string_quote(hi->hi_key, FALSE);
7067 if (tofree != NULL)
7068 {
7069 ga_concat(&ga, tofree);
7070 vim_free(tofree);
7071 }
7072 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007073 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 if (s != NULL)
7075 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007077 if (s == NULL)
7078 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007081 if (todo > 0)
7082 {
7083 vim_free(ga.ga_data);
7084 return NULL;
7085 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086
7087 ga_append(&ga, '}');
7088 ga_append(&ga, NUL);
7089 return (char_u *)ga.ga_data;
7090}
7091
7092/*
7093 * Allocate a variable for a Dictionary and fill it from "*arg".
7094 * Return OK or FAIL. Returns NOTDONE for {expr}.
7095 */
7096 static int
7097get_dict_tv(arg, rettv, evaluate)
7098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 int evaluate;
7101{
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 dict_T *d = NULL;
7103 typval_T tvkey;
7104 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007105 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007108 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109
7110 /*
7111 * First check if it's not a curly-braces thing: {expr}.
7112 * Must do this without evaluating, otherwise a function may be called
7113 * twice. Unfortunately this means we need to call eval1() twice for the
7114 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 if (*start != '}')
7118 {
7119 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7120 return FAIL;
7121 if (*start == '}')
7122 return NOTDONE;
7123 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124
7125 if (evaluate)
7126 {
7127 d = dict_alloc();
7128 if (d == NULL)
7129 return FAIL;
7130 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 tvkey.v_type = VAR_UNKNOWN;
7132 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133
7134 *arg = skipwhite(*arg + 1);
7135 while (**arg != '}' && **arg != NUL)
7136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138 goto failret;
7139 if (**arg != ':')
7140 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007141 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007142 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143 goto failret;
7144 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007145 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007147 key = get_tv_string_buf_chk(&tvkey, buf);
7148 if (key == NULL || *key == NUL)
7149 {
7150 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7151 if (key != NULL)
7152 EMSG(_(e_emptykey));
7153 clear_tv(&tvkey);
7154 goto failret;
7155 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157
7158 *arg = skipwhite(*arg + 1);
7159 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7160 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007161 if (evaluate)
7162 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007163 goto failret;
7164 }
7165 if (evaluate)
7166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 if (item != NULL)
7169 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007170 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007171 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 clear_tv(&tv);
7173 goto failret;
7174 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175 item = dictitem_alloc(key);
7176 clear_tv(&tvkey);
7177 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007180 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 if (dict_add(d, item) == FAIL)
7182 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183 }
7184 }
7185
7186 if (**arg == '}')
7187 break;
7188 if (**arg != ',')
7189 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007190 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191 goto failret;
7192 }
7193 *arg = skipwhite(*arg + 1);
7194 }
7195
7196 if (**arg != '}')
7197 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007198 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199failret:
7200 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007201 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 return FAIL;
7203 }
7204
7205 *arg = skipwhite(*arg + 1);
7206 if (evaluate)
7207 {
7208 rettv->v_type = VAR_DICT;
7209 rettv->vval.v_dict = d;
7210 ++d->dv_refcount;
7211 }
7212
7213 return OK;
7214}
7215
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 * Return a string with the string representation of a variable.
7218 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007219 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007220 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007221 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007222 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007223 */
7224 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007225echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007227 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007228 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007229 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007230{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231 static int recurse = 0;
7232 char_u *r = NULL;
7233
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007236 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 *tofree = NULL;
7238 return NULL;
7239 }
7240 ++recurse;
7241
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007242 switch (tv->v_type)
7243 {
7244 case VAR_FUNC:
7245 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007246 r = tv->vval.v_string;
7247 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007248
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007249 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007250 if (tv->vval.v_list == NULL)
7251 {
7252 *tofree = NULL;
7253 r = NULL;
7254 }
7255 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7256 {
7257 *tofree = NULL;
7258 r = (char_u *)"[...]";
7259 }
7260 else
7261 {
7262 tv->vval.v_list->lv_copyID = copyID;
7263 *tofree = list2string(tv, copyID);
7264 r = *tofree;
7265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007266 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007267
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007269 if (tv->vval.v_dict == NULL)
7270 {
7271 *tofree = NULL;
7272 r = NULL;
7273 }
7274 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7275 {
7276 *tofree = NULL;
7277 r = (char_u *)"{...}";
7278 }
7279 else
7280 {
7281 tv->vval.v_dict->dv_copyID = copyID;
7282 *tofree = dict2string(tv, copyID);
7283 r = *tofree;
7284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007287 case VAR_STRING:
7288 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 *tofree = NULL;
7290 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007293#ifdef FEAT_FLOAT
7294 case VAR_FLOAT:
7295 *tofree = NULL;
7296 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7297 r = numbuf;
7298 break;
7299#endif
7300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007301 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007302 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305
7306 --recurse;
7307 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007308}
7309
7310/*
7311 * Return a string with the string representation of a variable.
7312 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7313 * "numbuf" is used for a number.
7314 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007315 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007316 */
7317 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007318tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007320 char_u **tofree;
7321 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007322 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007323{
7324 switch (tv->v_type)
7325 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007326 case VAR_FUNC:
7327 *tofree = string_quote(tv->vval.v_string, TRUE);
7328 return *tofree;
7329 case VAR_STRING:
7330 *tofree = string_quote(tv->vval.v_string, FALSE);
7331 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007332#ifdef FEAT_FLOAT
7333 case VAR_FLOAT:
7334 *tofree = NULL;
7335 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7336 return numbuf;
7337#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007339 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007342 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007343 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007344 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007345 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346}
7347
7348/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 * Return string "str" in ' quotes, doubling ' characters.
7350 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007352 */
7353 static char_u *
7354string_quote(str, function)
7355 char_u *str;
7356 int function;
7357{
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007359 char_u *p, *r, *s;
7360
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 len = (function ? 13 : 3);
7362 if (str != NULL)
7363 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007364 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 for (p = str; *p != NUL; mb_ptr_adv(p))
7366 if (*p == '\'')
7367 ++len;
7368 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007369 s = r = alloc(len);
7370 if (r != NULL)
7371 {
7372 if (function)
7373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007375 r += 10;
7376 }
7377 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 if (str != NULL)
7380 for (p = str; *p != NUL; )
7381 {
7382 if (*p == '\'')
7383 *r++ = '\'';
7384 MB_COPY_CHAR(p, r);
7385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007387 if (function)
7388 *r++ = ')';
7389 *r++ = NUL;
7390 }
7391 return s;
7392}
7393
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007394#ifdef FEAT_FLOAT
7395/*
7396 * Convert the string "text" to a floating point number.
7397 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7398 * this always uses a decimal point.
7399 * Returns the length of the text that was consumed.
7400 */
7401 static int
7402string2float(text, value)
7403 char_u *text;
7404 float_T *value; /* result stored here */
7405{
7406 char *s = (char *)text;
7407 float_T f;
7408
7409 f = strtod(s, &s);
7410 *value = f;
7411 return (int)((char_u *)s - text);
7412}
7413#endif
7414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 * Get the value of an environment variable.
7417 * "arg" is pointing to the '$'. It is advanced to after the name.
7418 * If the environment variable was not set, silently assume it is empty.
7419 * Always return OK.
7420 */
7421 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007422get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 int evaluate;
7426{
7427 char_u *string = NULL;
7428 int len;
7429 int cc;
7430 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007431 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433 ++*arg;
7434 name = *arg;
7435 len = get_env_len(arg);
7436 if (evaluate)
7437 {
7438 if (len != 0)
7439 {
7440 cc = name[len];
7441 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007442 /* first try vim_getenv(), fast for normal environment vars */
7443 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007445 {
7446 if (!mustfree)
7447 string = vim_strsave(string);
7448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else
7450 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007451 if (mustfree)
7452 vim_free(string);
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 /* next try expanding things like $VIM and ${HOME} */
7455 string = expand_env_save(name - 1);
7456 if (string != NULL && *string == '$')
7457 {
7458 vim_free(string);
7459 string = NULL;
7460 }
7461 }
7462 name[len] = cc;
7463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464 rettv->v_type = VAR_STRING;
7465 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467
7468 return OK;
7469}
7470
7471/*
7472 * Array with names and number of arguments of all internal functions
7473 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7474 */
7475static struct fst
7476{
7477 char *f_name; /* function name */
7478 char f_min_argc; /* minimal number of arguments */
7479 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007481 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482} functions[] =
7483{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007484#ifdef FEAT_FLOAT
7485 {"abs", 1, 1, f_abs},
7486#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007487 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {"append", 2, 2, f_append},
7489 {"argc", 0, 0, f_argc},
7490 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007491 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007492#ifdef FEAT_FLOAT
7493 {"atan", 1, 1, f_atan},
7494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007496 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {"bufexists", 1, 1, f_bufexists},
7498 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7499 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7500 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7501 {"buflisted", 1, 1, f_buflisted},
7502 {"bufloaded", 1, 1, f_bufloaded},
7503 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007504 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 {"bufwinnr", 1, 1, f_bufwinnr},
7506 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007507 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007508 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007509#ifdef FEAT_FLOAT
7510 {"ceil", 1, 1, f_ceil},
7511#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007512 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {"char2nr", 1, 1, f_char2nr},
7514 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007515 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007517#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007518 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007519 {"complete_add", 1, 1, f_complete_add},
7520 {"complete_check", 0, 0, f_complete_check},
7521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007523 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007524#ifdef FEAT_FLOAT
7525 {"cos", 1, 1, f_cos},
7526#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007529 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007530 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {"delete", 1, 1, f_delete},
7532 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007533 {"diff_filler", 1, 1, f_diff_filler},
7534 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007535 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007537 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 {"eventhandler", 0, 0, f_eventhandler},
7539 {"executable", 1, 1, f_executable},
7540 {"exists", 1, 1, f_exists},
7541 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007542 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007543 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7545 {"filereadable", 1, 1, f_filereadable},
7546 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007547 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007548 {"finddir", 1, 3, f_finddir},
7549 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007550#ifdef FEAT_FLOAT
7551 {"float2nr", 1, 1, f_float2nr},
7552 {"floor", 1, 1, f_floor},
7553#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007554 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {"fnamemodify", 2, 2, f_fnamemodify},
7556 {"foldclosed", 1, 1, f_foldclosed},
7557 {"foldclosedend", 1, 1, f_foldclosedend},
7558 {"foldlevel", 1, 1, f_foldlevel},
7559 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007560 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007562 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007563 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007564 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007565 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {"getbufvar", 2, 2, f_getbufvar},
7567 {"getchar", 0, 1, f_getchar},
7568 {"getcharmod", 0, 0, f_getcharmod},
7569 {"getcmdline", 0, 0, f_getcmdline},
7570 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007571 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007573 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007574 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {"getfsize", 1, 1, f_getfsize},
7576 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007577 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007578 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007579 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007580 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007581 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007582 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007583 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007584 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007586 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {"getwinposx", 0, 0, f_getwinposx},
7588 {"getwinposy", 0, 0, f_getwinposy},
7589 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007590 {"glob", 1, 2, f_glob},
7591 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007593 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007594 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007595 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7597 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7598 {"histadd", 2, 2, f_histadd},
7599 {"histdel", 1, 2, f_histdel},
7600 {"histget", 1, 2, f_histget},
7601 {"histnr", 1, 1, f_histnr},
7602 {"hlID", 1, 1, f_hlID},
7603 {"hlexists", 1, 1, f_hlexists},
7604 {"hostname", 0, 0, f_hostname},
7605 {"iconv", 3, 3, f_iconv},
7606 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007607 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007608 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007610 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {"inputrestore", 0, 0, f_inputrestore},
7612 {"inputsave", 0, 0, f_inputsave},
7613 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007616 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007618 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {"libcall", 3, 3, f_libcall},
7623 {"libcallnr", 3, 3, f_libcallnr},
7624 {"line", 1, 1, f_line},
7625 {"line2byte", 1, 1, f_line2byte},
7626 {"lispindent", 1, 1, f_lispindent},
7627 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007628#ifdef FEAT_FLOAT
7629 {"log10", 1, 1, f_log10},
7630#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007631 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007632 {"maparg", 1, 3, f_maparg},
7633 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007634 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007635 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007636 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007637 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007638 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007639 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007640 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007641 {"max", 1, 1, f_max},
7642 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007643#ifdef vim_mkdir
7644 {"mkdir", 1, 3, f_mkdir},
7645#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007646 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {"nextnonblank", 1, 1, f_nextnonblank},
7648 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007649 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007650#ifdef FEAT_FLOAT
7651 {"pow", 2, 2, f_pow},
7652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007654 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007655 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007657 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007658 {"reltime", 0, 2, f_reltime},
7659 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {"remote_expr", 2, 3, f_remote_expr},
7661 {"remote_foreground", 1, 1, f_remote_foreground},
7662 {"remote_peek", 1, 2, f_remote_peek},
7663 {"remote_read", 1, 1, f_remote_read},
7664 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007665 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007667 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007669 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 {"round", 1, 1, f_round},
7672#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007673 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007674 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007675 {"searchpair", 3, 7, f_searchpair},
7676 {"searchpairpos", 3, 7, f_searchpairpos},
7677 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {"server2client", 2, 2, f_server2client},
7679 {"serverlist", 0, 0, f_serverlist},
7680 {"setbufvar", 3, 3, f_setbufvar},
7681 {"setcmdpos", 1, 1, f_setcmdpos},
7682 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007683 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007684 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007685 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007686 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007688 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007690 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007692#ifdef FEAT_FLOAT
7693 {"sin", 1, 1, f_sin},
7694#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007695 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007696 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007697 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007698 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007699 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701 {"sqrt", 1, 1, f_sqrt},
7702 {"str2float", 1, 1, f_str2float},
7703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007704 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705#ifdef HAVE_STRFTIME
7706 {"strftime", 1, 2, f_strftime},
7707#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007709 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {"strlen", 1, 1, f_strlen},
7711 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007712 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"strtrans", 1, 1, f_strtrans},
7714 {"submatch", 1, 1, f_submatch},
7715 {"substitute", 4, 4, f_substitute},
7716 {"synID", 3, 3, f_synID},
7717 {"synIDattr", 2, 3, f_synIDattr},
7718 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007719 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007720 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007721 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007722 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007723 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007724 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007725 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007727 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"tolower", 1, 1, f_tolower},
7729 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007730 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007731#ifdef FEAT_FLOAT
7732 {"trunc", 1, 1, f_trunc},
7733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"virtcol", 1, 1, f_virtcol},
7737 {"visualmode", 0, 1, f_visualmode},
7738 {"winbufnr", 1, 1, f_winbufnr},
7739 {"wincol", 0, 0, f_wincol},
7740 {"winheight", 1, 1, f_winheight},
7741 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007742 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007744 {"winrestview", 1, 1, f_winrestview},
7745 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007747 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748};
7749
7750#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7751
7752/*
7753 * Function given to ExpandGeneric() to obtain the list of internal
7754 * or user defined function names.
7755 */
7756 char_u *
7757get_function_name(xp, idx)
7758 expand_T *xp;
7759 int idx;
7760{
7761 static int intidx = -1;
7762 char_u *name;
7763
7764 if (idx == 0)
7765 intidx = -1;
7766 if (intidx < 0)
7767 {
7768 name = get_user_func_name(xp, idx);
7769 if (name != NULL)
7770 return name;
7771 }
7772 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7773 {
7774 STRCPY(IObuff, functions[intidx].f_name);
7775 STRCAT(IObuff, "(");
7776 if (functions[intidx].f_max_argc == 0)
7777 STRCAT(IObuff, ")");
7778 return IObuff;
7779 }
7780
7781 return NULL;
7782}
7783
7784/*
7785 * Function given to ExpandGeneric() to obtain the list of internal or
7786 * user defined variable or function names.
7787 */
7788/*ARGSUSED*/
7789 char_u *
7790get_expr_name(xp, idx)
7791 expand_T *xp;
7792 int idx;
7793{
7794 static int intidx = -1;
7795 char_u *name;
7796
7797 if (idx == 0)
7798 intidx = -1;
7799 if (intidx < 0)
7800 {
7801 name = get_function_name(xp, idx);
7802 if (name != NULL)
7803 return name;
7804 }
7805 return get_user_var_name(xp, ++intidx);
7806}
7807
7808#endif /* FEAT_CMDL_COMPL */
7809
7810/*
7811 * Find internal function in table above.
7812 * Return index, or -1 if not found
7813 */
7814 static int
7815find_internal_func(name)
7816 char_u *name; /* name of the function */
7817{
7818 int first = 0;
7819 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7820 int cmp;
7821 int x;
7822
7823 /*
7824 * Find the function name in the table. Binary search.
7825 */
7826 while (first <= last)
7827 {
7828 x = first + ((unsigned)(last - first) >> 1);
7829 cmp = STRCMP(name, functions[x].f_name);
7830 if (cmp < 0)
7831 last = x - 1;
7832 else if (cmp > 0)
7833 first = x + 1;
7834 else
7835 return x;
7836 }
7837 return -1;
7838}
7839
7840/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7842 * name it contains, otherwise return "name".
7843 */
7844 static char_u *
7845deref_func_name(name, lenp)
7846 char_u *name;
7847 int *lenp;
7848{
Bram Moolenaar33570922005-01-25 22:26:29 +00007849 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007850 int cc;
7851
7852 cc = name[*lenp];
7853 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007854 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007855 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 {
7860 *lenp = 0;
7861 return (char_u *)""; /* just in case */
7862 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007863 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 }
7866
7867 return name;
7868}
7869
7870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 * Allocate a variable for the result of a function.
7872 * Return OK or FAIL.
7873 */
7874 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7876 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 char_u *name; /* name of the function */
7878 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 char_u **arg; /* argument, pointing to the '(' */
7881 linenr_T firstline; /* first line of range */
7882 linenr_T lastline; /* last line of range */
7883 int *doesrange; /* return: function handled range */
7884 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 char_u *argp;
7888 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007889 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 int argcount = 0; /* number of arguments found */
7891
7892 /*
7893 * Get the arguments.
7894 */
7895 argp = *arg;
7896 while (argcount < MAX_FUNC_ARGS)
7897 {
7898 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7899 if (*argp == ')' || *argp == ',' || *argp == NUL)
7900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7902 {
7903 ret = FAIL;
7904 break;
7905 }
7906 ++argcount;
7907 if (*argp != ',')
7908 break;
7909 }
7910 if (*argp == ')')
7911 ++argp;
7912 else
7913 ret = FAIL;
7914
7915 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 {
7920 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007921 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007923 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 *arg = skipwhite(argp);
7930 return ret;
7931}
7932
7933
7934/*
7935 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007936 * Return OK when the function can't be called, FAIL otherwise.
7937 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 */
7939 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 char_u *name; /* name of the function */
7943 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007946 typval_T *argvars; /* vars for arguments, must have "argcount"
7947 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 linenr_T firstline; /* first line of range */
7949 linenr_T lastline; /* last line of range */
7950 int *doesrange; /* return: function handled range */
7951 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955#define ERROR_UNKNOWN 0
7956#define ERROR_TOOMANY 1
7957#define ERROR_TOOFEW 2
7958#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007959#define ERROR_DICT 4
7960#define ERROR_NONE 5
7961#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 int error = ERROR_NONE;
7963 int i;
7964 int llen;
7965 ufunc_T *fp;
7966 int cc;
7967#define FLEN_FIXED 40
7968 char_u fname_buf[FLEN_FIXED + 1];
7969 char_u *fname;
7970
7971 /*
7972 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7973 * Change <SNR>123_name() to K_SNR 123_name().
7974 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7975 */
7976 cc = name[len];
7977 name[len] = NUL;
7978 llen = eval_fname_script(name);
7979 if (llen > 0)
7980 {
7981 fname_buf[0] = K_SPECIAL;
7982 fname_buf[1] = KS_EXTRA;
7983 fname_buf[2] = (int)KE_SNR;
7984 i = 3;
7985 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7986 {
7987 if (current_SID <= 0)
7988 error = ERROR_SCRIPT;
7989 else
7990 {
7991 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7992 i = (int)STRLEN(fname_buf);
7993 }
7994 }
7995 if (i + STRLEN(name + llen) < FLEN_FIXED)
7996 {
7997 STRCPY(fname_buf + i, name + llen);
7998 fname = fname_buf;
7999 }
8000 else
8001 {
8002 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8003 if (fname == NULL)
8004 error = ERROR_OTHER;
8005 else
8006 {
8007 mch_memmove(fname, fname_buf, (size_t)i);
8008 STRCPY(fname + i, name + llen);
8009 }
8010 }
8011 }
8012 else
8013 fname = name;
8014
8015 *doesrange = FALSE;
8016
8017
8018 /* execute the function if no errors detected and executing */
8019 if (evaluate && error == ERROR_NONE)
8020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 error = ERROR_UNKNOWN;
8023
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008024 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
8026 /*
8027 * User defined function.
8028 */
8029 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008032 /* Trigger FuncUndefined event, may load the function. */
8033 if (fp == NULL
8034 && apply_autocmds(EVENT_FUNCUNDEFINED,
8035 fname, fname, TRUE, NULL)
8036 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008038 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 fp = find_func(fname);
8040 }
8041#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008042 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008043 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008044 {
8045 /* loaded a package, search for the function again */
8046 fp = find_func(fname);
8047 }
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 if (fp != NULL)
8050 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008051 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008053 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008055 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008057 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008058 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 else
8060 {
8061 /*
8062 * Call the user function.
8063 * Save and restore search patterns, script variables and
8064 * redo buffer.
8065 */
8066 save_search_patterns();
8067 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008068 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008069 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008071 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8072 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8073 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008074 /* Function was unreferenced while being used, free it
8075 * now. */
8076 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 restoreRedobuff();
8078 restore_search_patterns();
8079 error = ERROR_NONE;
8080 }
8081 }
8082 }
8083 else
8084 {
8085 /*
8086 * Find the function name in the table, call its implementation.
8087 */
8088 i = find_internal_func(fname);
8089 if (i >= 0)
8090 {
8091 if (argcount < functions[i].f_min_argc)
8092 error = ERROR_TOOFEW;
8093 else if (argcount > functions[i].f_max_argc)
8094 error = ERROR_TOOMANY;
8095 else
8096 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008098 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 error = ERROR_NONE;
8100 }
8101 }
8102 }
8103 /*
8104 * The function call (or "FuncUndefined" autocommand sequence) might
8105 * have been aborted by an error, an interrupt, or an explicitly thrown
8106 * exception that has not been caught so far. This situation can be
8107 * tested for by calling aborting(). For an error in an internal
8108 * function or for the "E132" error in call_user_func(), however, the
8109 * throw point at which the "force_abort" flag (temporarily reset by
8110 * emsg()) is normally updated has not been reached yet. We need to
8111 * update that flag first to make aborting() reliable.
8112 */
8113 update_force_abort();
8114 }
8115 if (error == ERROR_NONE)
8116 ret = OK;
8117
8118 /*
8119 * Report an error unless the argument evaluation or function call has been
8120 * cancelled due to an aborting error, an interrupt, or an exception.
8121 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008122 if (!aborting())
8123 {
8124 switch (error)
8125 {
8126 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008127 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008128 break;
8129 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008130 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008131 break;
8132 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008133 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008134 name);
8135 break;
8136 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008137 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008138 name);
8139 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008140 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008141 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008142 name);
8143 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008144 }
8145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
8147 name[len] = cc;
8148 if (fname != name && fname != fname_buf)
8149 vim_free(fname);
8150
8151 return ret;
8152}
8153
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008154/*
8155 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008156 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157 */
8158 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008159emsg_funcname(ermsg, name)
8160 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008161 char_u *name;
8162{
8163 char_u *p;
8164
8165 if (*name == K_SPECIAL)
8166 p = concat_str((char_u *)"<SNR>", name + 3);
8167 else
8168 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008169 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008170 if (p != name)
8171 vim_free(p);
8172}
8173
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008174/*
8175 * Return TRUE for a non-zero Number and a non-empty String.
8176 */
8177 static int
8178non_zero_arg(argvars)
8179 typval_T *argvars;
8180{
8181 return ((argvars[0].v_type == VAR_NUMBER
8182 && argvars[0].vval.v_number != 0)
8183 || (argvars[0].v_type == VAR_STRING
8184 && argvars[0].vval.v_string != NULL
8185 && *argvars[0].vval.v_string != NUL));
8186}
8187
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188/*********************************************
8189 * Implementation of the built-in functions
8190 */
8191
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008192#ifdef FEAT_FLOAT
8193/*
8194 * "abs(expr)" function
8195 */
8196 static void
8197f_abs(argvars, rettv)
8198 typval_T *argvars;
8199 typval_T *rettv;
8200{
8201 if (argvars[0].v_type == VAR_FLOAT)
8202 {
8203 rettv->v_type = VAR_FLOAT;
8204 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8205 }
8206 else
8207 {
8208 varnumber_T n;
8209 int error = FALSE;
8210
8211 n = get_tv_number_chk(&argvars[0], &error);
8212 if (error)
8213 rettv->vval.v_number = -1;
8214 else if (n > 0)
8215 rettv->vval.v_number = n;
8216 else
8217 rettv->vval.v_number = -n;
8218 }
8219}
8220#endif
8221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008223 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 */
8225 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008226f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008227 typval_T *argvars;
8228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008233 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008235 if ((l = argvars[0].vval.v_list) != NULL
8236 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8237 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008239 }
8240 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008241 EMSG(_(e_listreq));
8242}
8243
8244/*
8245 * "append(lnum, string/list)" function
8246 */
8247 static void
8248f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008249 typval_T *argvars;
8250 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008251{
8252 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008253 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 list_T *l = NULL;
8255 listitem_T *li = NULL;
8256 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008257 long added = 0;
8258
Bram Moolenaar0d660222005-01-07 21:51:51 +00008259 lnum = get_tv_lnum(argvars);
8260 if (lnum >= 0
8261 && lnum <= curbuf->b_ml.ml_line_count
8262 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008263 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008264 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008266 l = argvars[1].vval.v_list;
8267 if (l == NULL)
8268 return;
8269 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008271 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 for (;;)
8273 {
8274 if (l == NULL)
8275 tv = &argvars[1]; /* append a string */
8276 else if (li == NULL)
8277 break; /* end of list */
8278 else
8279 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008280 line = get_tv_string_chk(tv);
8281 if (line == NULL) /* type error */
8282 {
8283 rettv->vval.v_number = 1; /* Failed */
8284 break;
8285 }
8286 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287 ++added;
8288 if (l == NULL)
8289 break;
8290 li = li->li_next;
8291 }
8292
8293 appended_lines_mark(lnum, added);
8294 if (curwin->w_cursor.lnum > lnum)
8295 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008297 else
8298 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299}
8300
8301/*
8302 * "argc()" function
8303 */
8304/* ARGSUSED */
8305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *argvars;
8308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
8313/*
8314 * "argidx()" function
8315 */
8316/* ARGSUSED */
8317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 typval_T *argvars;
8320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323}
8324
8325/*
8326 * "argv(nr)" function
8327 */
8328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 typval_T *argvars;
8331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 int idx;
8334
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008335 if (argvars[0].v_type != VAR_UNKNOWN)
8336 {
8337 idx = get_tv_number_chk(&argvars[0], NULL);
8338 if (idx >= 0 && idx < ARGCOUNT)
8339 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8340 else
8341 rettv->vval.v_string = NULL;
8342 rettv->v_type = VAR_STRING;
8343 }
8344 else if (rettv_list_alloc(rettv) == OK)
8345 for (idx = 0; idx < ARGCOUNT; ++idx)
8346 list_append_string(rettv->vval.v_list,
8347 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#ifdef FEAT_FLOAT
8351static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8352
8353/*
8354 * Get the float value of "argvars[0]" into "f".
8355 * Returns FAIL when the argument is not a Number or Float.
8356 */
8357 static int
8358get_float_arg(argvars, f)
8359 typval_T *argvars;
8360 float_T *f;
8361{
8362 if (argvars[0].v_type == VAR_FLOAT)
8363 {
8364 *f = argvars[0].vval.v_float;
8365 return OK;
8366 }
8367 if (argvars[0].v_type == VAR_NUMBER)
8368 {
8369 *f = (float_T)argvars[0].vval.v_number;
8370 return OK;
8371 }
8372 EMSG(_("E808: Number or Float required"));
8373 return FAIL;
8374}
8375
8376/*
8377 * "atan()" function
8378 */
8379 static void
8380f_atan(argvars, rettv)
8381 typval_T *argvars;
8382 typval_T *rettv;
8383{
8384 float_T f;
8385
8386 rettv->v_type = VAR_FLOAT;
8387 if (get_float_arg(argvars, &f) == OK)
8388 rettv->vval.v_float = atan(f);
8389 else
8390 rettv->vval.v_float = 0.0;
8391}
8392#endif
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394/*
8395 * "browse(save, title, initdir, default)" function
8396 */
8397/* ARGSUSED */
8398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 typval_T *argvars;
8401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403#ifdef FEAT_BROWSE
8404 int save;
8405 char_u *title;
8406 char_u *initdir;
8407 char_u *defname;
8408 char_u buf[NUMBUFLEN];
8409 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008410 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 save = get_tv_number_chk(&argvars[0], &error);
8413 title = get_tv_string_chk(&argvars[1]);
8414 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8415 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 if (error || title == NULL || initdir == NULL || defname == NULL)
8418 rettv->vval.v_string = NULL;
8419 else
8420 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008421 do_browse(save ? BROWSE_SAVE : 0,
8422 title, defname, NULL, initdir, NULL, curbuf);
8423#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008424 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008425#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008427}
8428
8429/*
8430 * "browsedir(title, initdir)" function
8431 */
8432/* ARGSUSED */
8433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008435 typval_T *argvars;
8436 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008437{
8438#ifdef FEAT_BROWSE
8439 char_u *title;
8440 char_u *initdir;
8441 char_u buf[NUMBUFLEN];
8442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 title = get_tv_string_chk(&argvars[0]);
8444 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 if (title == NULL || initdir == NULL)
8447 rettv->vval.v_string = NULL;
8448 else
8449 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008450 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455}
8456
Bram Moolenaar33570922005-01-25 22:26:29 +00008457static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459/*
8460 * Find a buffer by number or exact name.
8461 */
8462 static buf_T *
8463find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008468 if (avar->v_type == VAR_NUMBER)
8469 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008470 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008473 if (buf == NULL)
8474 {
8475 /* No full path name match, try a match with a URL or a "nofile"
8476 * buffer, these don't use the full path. */
8477 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8478 if (buf->b_fname != NULL
8479 && (path_with_url(buf->b_fname)
8480#ifdef FEAT_QUICKFIX
8481 || bt_nofile(buf)
8482#endif
8483 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008485 break;
8486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 return buf;
8489}
8490
8491/*
8492 * "bufexists(expr)" function
8493 */
8494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 typval_T *argvars;
8497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500}
8501
8502/*
8503 * "buflisted(expr)" function
8504 */
8505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 typval_T *argvars;
8508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
8510 buf_T *buf;
8511
8512 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514}
8515
8516/*
8517 * "bufloaded(expr)" function
8518 */
8519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 typval_T *argvars;
8522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 buf_T *buf;
8525
8526 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528}
8529
Bram Moolenaar33570922005-01-25 22:26:29 +00008530static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532/*
8533 * Get buffer by number or pattern.
8534 */
8535 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 int save_magic;
8541 char_u *save_cpo;
8542 buf_T *buf;
8543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 if (tv->v_type == VAR_NUMBER)
8545 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008546 if (tv->v_type != VAR_STRING)
8547 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (name == NULL || *name == NUL)
8549 return curbuf;
8550 if (name[0] == '$' && name[1] == NUL)
8551 return lastbuf;
8552
8553 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8554 save_magic = p_magic;
8555 p_magic = TRUE;
8556 save_cpo = p_cpo;
8557 p_cpo = (char_u *)"";
8558
8559 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8560 TRUE, FALSE));
8561
8562 p_magic = save_magic;
8563 p_cpo = save_cpo;
8564
8565 /* If not found, try expanding the name, like done for bufexists(). */
8566 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568
8569 return buf;
8570}
8571
8572/*
8573 * "bufname(expr)" function
8574 */
8575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 typval_T *argvars;
8578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 buf_T *buf;
8581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 buf = get_buf_tv(&argvars[0]);
8585 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 --emsg_off;
8591}
8592
8593/*
8594 * "bufnr(expr)" function
8595 */
8596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 typval_T *argvars;
8599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008602 int error = FALSE;
8603 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008605 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008608 --emsg_off;
8609
8610 /* If the buffer isn't found and the second argument is not zero create a
8611 * new buffer. */
8612 if (buf == NULL
8613 && argvars[1].v_type != VAR_UNKNOWN
8614 && get_tv_number_chk(&argvars[1], &error) != 0
8615 && !error
8616 && (name = get_tv_string_chk(&argvars[0])) != NULL
8617 && !error)
8618 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624}
8625
8626/*
8627 * "bufwinnr(nr)" function
8628 */
8629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 typval_T *argvars;
8632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633{
8634#ifdef FEAT_WINDOWS
8635 win_T *wp;
8636 int winnr = 0;
8637#endif
8638 buf_T *buf;
8639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#ifdef FEAT_WINDOWS
8644 for (wp = firstwin; wp; wp = wp->w_next)
8645 {
8646 ++winnr;
8647 if (wp->w_buffer == buf)
8648 break;
8649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#endif
8654 --emsg_off;
8655}
8656
8657/*
8658 * "byte2line(byte)" function
8659 */
8660/*ARGSUSED*/
8661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 typval_T *argvars;
8664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
8666#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#else
8669 long boff = 0;
8670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008671 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 (linenr_T)0, &boff);
8677#endif
8678}
8679
8680/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008681 * "byteidx()" function
8682 */
8683/*ARGSUSED*/
8684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008686 typval_T *argvars;
8687 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008688{
8689#ifdef FEAT_MBYTE
8690 char_u *t;
8691#endif
8692 char_u *str;
8693 long idx;
8694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 str = get_tv_string_chk(&argvars[0]);
8696 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008698 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008699 return;
8700
8701#ifdef FEAT_MBYTE
8702 t = str;
8703 for ( ; idx > 0; idx--)
8704 {
8705 if (*t == NUL) /* EOL reached */
8706 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008707 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008708 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008709 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008710#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008711 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008713#endif
8714}
8715
8716/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008717 * "call(func, arglist)" function
8718 */
8719 static void
8720f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008723{
8724 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008725 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008726 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008728 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008730
8731 rettv->vval.v_number = 0;
8732 if (argvars[1].v_type != VAR_LIST)
8733 {
8734 EMSG(_(e_listreq));
8735 return;
8736 }
8737 if (argvars[1].vval.v_list == NULL)
8738 return;
8739
8740 if (argvars[0].v_type == VAR_FUNC)
8741 func = argvars[0].vval.v_string;
8742 else
8743 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 if (*func == NUL)
8745 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008746
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 if (argvars[2].v_type != VAR_UNKNOWN)
8748 {
8749 if (argvars[2].v_type != VAR_DICT)
8750 {
8751 EMSG(_(e_dictreq));
8752 return;
8753 }
8754 selfdict = argvars[2].vval.v_dict;
8755 }
8756
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008757 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8758 item = item->li_next)
8759 {
8760 if (argc == MAX_FUNC_ARGS)
8761 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008762 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008763 break;
8764 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008765 /* Make a copy of each argument. This is needed to be able to set
8766 * v_lock to VAR_FIXED in the copy without changing the original list.
8767 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008768 copy_tv(&item->li_tv, &argv[argc++]);
8769 }
8770
8771 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008772 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008773 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8774 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008775
8776 /* Free the arguments. */
8777 while (argc > 0)
8778 clear_tv(&argv[--argc]);
8779}
8780
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782/*
8783 * "ceil({float})" function
8784 */
8785 static void
8786f_ceil(argvars, rettv)
8787 typval_T *argvars;
8788 typval_T *rettv;
8789{
8790 float_T f;
8791
8792 rettv->v_type = VAR_FLOAT;
8793 if (get_float_arg(argvars, &f) == OK)
8794 rettv->vval.v_float = ceil(f);
8795 else
8796 rettv->vval.v_float = 0.0;
8797}
8798#endif
8799
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008800/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008801 * "changenr()" function
8802 */
8803/*ARGSUSED*/
8804 static void
8805f_changenr(argvars, rettv)
8806 typval_T *argvars;
8807 typval_T *rettv;
8808{
8809 rettv->vval.v_number = curbuf->b_u_seq_cur;
8810}
8811
8812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 * "char2nr(string)" function
8814 */
8815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008817 typval_T *argvars;
8818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819{
8820#ifdef FEAT_MBYTE
8821 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008822 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 else
8824#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
8829 * "cindent(lnum)" function
8830 */
8831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *argvars;
8834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836#ifdef FEAT_CINDENT
8837 pos_T pos;
8838 linenr_T lnum;
8839
8840 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8843 {
8844 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 curwin->w_cursor = pos;
8847 }
8848 else
8849#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851}
8852
8853/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008854 * "clearmatches()" function
8855 */
8856/*ARGSUSED*/
8857 static void
8858f_clearmatches(argvars, rettv)
8859 typval_T *argvars;
8860 typval_T *rettv;
8861{
8862#ifdef FEAT_SEARCH_EXTRA
8863 clear_matches(curwin);
8864#endif
8865}
8866
8867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 * "col(string)" function
8869 */
8870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875 colnr_T col = 0;
8876 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008877 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008879 fp = var2fpos(&argvars[0], FALSE, &fnum);
8880 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
8882 if (fp->col == MAXCOL)
8883 {
8884 /* '> can be MAXCOL, get the length of the line then */
8885 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008886 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 else
8888 col = MAXCOL;
8889 }
8890 else
8891 {
8892 col = fp->col + 1;
8893#ifdef FEAT_VIRTUALEDIT
8894 /* col(".") when the cursor is on the NUL at the end of the line
8895 * because of "coladd" can be seen as an extra column. */
8896 if (virtual_active() && fp == &curwin->w_cursor)
8897 {
8898 char_u *p = ml_get_cursor();
8899
8900 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8901 curwin->w_virtcol - curwin->w_cursor.coladd))
8902 {
8903# ifdef FEAT_MBYTE
8904 int l;
8905
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008906 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 col += l;
8908# else
8909 if (*p != NUL && p[1] == NUL)
8910 ++col;
8911# endif
8912 }
8913 }
8914#endif
8915 }
8916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
8919
Bram Moolenaar572cb562005-08-05 21:35:02 +00008920#if defined(FEAT_INS_EXPAND)
8921/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008922 * "complete()" function
8923 */
8924/*ARGSUSED*/
8925 static void
8926f_complete(argvars, rettv)
8927 typval_T *argvars;
8928 typval_T *rettv;
8929{
8930 int startcol;
8931
8932 if ((State & INSERT) == 0)
8933 {
8934 EMSG(_("E785: complete() can only be used in Insert mode"));
8935 return;
8936 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008937
8938 /* Check for undo allowed here, because if something was already inserted
8939 * the line was already saved for undo and this check isn't done. */
8940 if (!undo_allowed())
8941 return;
8942
Bram Moolenaarade00832006-03-10 21:46:58 +00008943 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8944 {
8945 EMSG(_(e_invarg));
8946 return;
8947 }
8948
8949 startcol = get_tv_number_chk(&argvars[0], NULL);
8950 if (startcol <= 0)
8951 return;
8952
8953 set_completion(startcol - 1, argvars[1].vval.v_list);
8954}
8955
8956/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008957 * "complete_add()" function
8958 */
8959/*ARGSUSED*/
8960 static void
8961f_complete_add(argvars, rettv)
8962 typval_T *argvars;
8963 typval_T *rettv;
8964{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008965 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008966}
8967
8968/*
8969 * "complete_check()" function
8970 */
8971/*ARGSUSED*/
8972 static void
8973f_complete_check(argvars, rettv)
8974 typval_T *argvars;
8975 typval_T *rettv;
8976{
8977 int saved = RedrawingDisabled;
8978
8979 RedrawingDisabled = 0;
8980 ins_compl_check_keys(0);
8981 rettv->vval.v_number = compl_interrupted;
8982 RedrawingDisabled = saved;
8983}
8984#endif
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986/*
8987 * "confirm(message, buttons[, default [, type]])" function
8988 */
8989/*ARGSUSED*/
8990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 typval_T *argvars;
8993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8996 char_u *message;
8997 char_u *buttons = NULL;
8998 char_u buf[NUMBUFLEN];
8999 char_u buf2[NUMBUFLEN];
9000 int def = 1;
9001 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 char_u *typestr;
9003 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 message = get_tv_string_chk(&argvars[0]);
9006 if (message == NULL)
9007 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9011 if (buttons == NULL)
9012 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009013 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9019 if (typestr == NULL)
9020 error = TRUE;
9021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 switch (TOUPPER_ASC(*typestr))
9024 {
9025 case 'E': type = VIM_ERROR; break;
9026 case 'Q': type = VIM_QUESTION; break;
9027 case 'I': type = VIM_INFO; break;
9028 case 'W': type = VIM_WARNING; break;
9029 case 'G': type = VIM_GENERIC; break;
9030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 }
9033 }
9034 }
9035
9036 if (buttons == NULL || *buttons == NUL)
9037 buttons = (char_u *)_("&Ok");
9038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009039 if (error)
9040 rettv->vval.v_number = 0;
9041 else
9042 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 def, NULL);
9044#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046#endif
9047}
9048
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009049/*
9050 * "copy()" function
9051 */
9052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 typval_T *argvars;
9055 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009057 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009060#ifdef FEAT_FLOAT
9061/*
9062 * "cos()" function
9063 */
9064 static void
9065f_cos(argvars, rettv)
9066 typval_T *argvars;
9067 typval_T *rettv;
9068{
9069 float_T f;
9070
9071 rettv->v_type = VAR_FLOAT;
9072 if (get_float_arg(argvars, &f) == OK)
9073 rettv->vval.v_float = cos(f);
9074 else
9075 rettv->vval.v_float = 0.0;
9076}
9077#endif
9078
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009080 * "count()" function
9081 */
9082 static void
9083f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009086{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009087 long n = 0;
9088 int ic = FALSE;
9089
Bram Moolenaare9a41262005-01-15 22:18:47 +00009090 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009091 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 listitem_T *li;
9093 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009094 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009095
Bram Moolenaare9a41262005-01-15 22:18:47 +00009096 if ((l = argvars[0].vval.v_list) != NULL)
9097 {
9098 li = l->lv_first;
9099 if (argvars[2].v_type != VAR_UNKNOWN)
9100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009101 int error = FALSE;
9102
9103 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009104 if (argvars[3].v_type != VAR_UNKNOWN)
9105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 idx = get_tv_number_chk(&argvars[3], &error);
9107 if (!error)
9108 {
9109 li = list_find(l, idx);
9110 if (li == NULL)
9111 EMSGN(_(e_listidx), idx);
9112 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009113 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 if (error)
9115 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009116 }
9117
9118 for ( ; li != NULL; li = li->li_next)
9119 if (tv_equal(&li->li_tv, &argvars[1], ic))
9120 ++n;
9121 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009123 else if (argvars[0].v_type == VAR_DICT)
9124 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009125 int todo;
9126 dict_T *d;
9127 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009128
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009129 if ((d = argvars[0].vval.v_dict) != NULL)
9130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 int error = FALSE;
9132
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 if (argvars[2].v_type != VAR_UNKNOWN)
9134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009136 if (argvars[3].v_type != VAR_UNKNOWN)
9137 EMSG(_(e_invarg));
9138 }
9139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009140 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009141 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009142 {
9143 if (!HASHITEM_EMPTY(hi))
9144 {
9145 --todo;
9146 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9147 ++n;
9148 }
9149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009150 }
9151 }
9152 else
9153 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009154 rettv->vval.v_number = n;
9155}
9156
9157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9159 *
9160 * Checks the existence of a cscope connection.
9161 */
9162/*ARGSUSED*/
9163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 typval_T *argvars;
9166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168#ifdef FEAT_CSCOPE
9169 int num = 0;
9170 char_u *dbpath = NULL;
9171 char_u *prepend = NULL;
9172 char_u buf[NUMBUFLEN];
9173
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009174 if (argvars[0].v_type != VAR_UNKNOWN
9175 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 num = (int)get_tv_number(&argvars[0]);
9178 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009179 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
9182
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186#endif
9187}
9188
9189/*
9190 * "cursor(lnum, col)" function
9191 *
9192 * Moves the cursor to the specified line and column
9193 */
9194/*ARGSUSED*/
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009201#ifdef FEAT_VIRTUALEDIT
9202 long coladd = 0;
9203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
Bram Moolenaara5525202006-03-02 22:52:09 +00009205 if (argvars[1].v_type == VAR_UNKNOWN)
9206 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009207 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009208
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009209 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009210 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009211 line = pos.lnum;
9212 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009213#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009214 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009215#endif
9216 }
9217 else
9218 {
9219 line = get_tv_lnum(argvars);
9220 col = get_tv_number_chk(&argvars[1], NULL);
9221#ifdef FEAT_VIRTUALEDIT
9222 if (argvars[2].v_type != VAR_UNKNOWN)
9223 coladd = get_tv_number_chk(&argvars[2], NULL);
9224#endif
9225 }
9226 if (line < 0 || col < 0
9227#ifdef FEAT_VIRTUALEDIT
9228 || coladd < 0
9229#endif
9230 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 if (line > 0)
9233 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (col > 0)
9235 curwin->w_cursor.col = col - 1;
9236#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009237 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238#endif
9239
9240 /* Make sure the cursor is in a valid position. */
9241 check_cursor();
9242#ifdef FEAT_MBYTE
9243 /* Correct cursor for multi-byte character. */
9244 if (has_mbyte)
9245 mb_adjust_cursor();
9246#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009247
9248 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009252 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 */
9254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009259 int noref = 0;
9260
9261 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009263 if (noref < 0 || noref > 1)
9264 EMSG(_(e_invarg));
9265 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009266 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267}
9268
9269/*
9270 * "delete()" function
9271 */
9272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009273f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009274 typval_T *argvars;
9275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
9277 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281}
9282
9283/*
9284 * "did_filetype()" function
9285 */
9286/*ARGSUSED*/
9287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009288f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 typval_T *argvars;
9290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291{
9292#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296#endif
9297}
9298
9299/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009300 * "diff_filler()" function
9301 */
9302/*ARGSUSED*/
9303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009305 typval_T *argvars;
9306 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009307{
9308#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009310#endif
9311}
9312
9313/*
9314 * "diff_hlID()" function
9315 */
9316/*ARGSUSED*/
9317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *argvars;
9320 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009321{
9322#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009324 static linenr_T prev_lnum = 0;
9325 static int changedtick = 0;
9326 static int fnum = 0;
9327 static int change_start = 0;
9328 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009329 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009330 int filler_lines;
9331 int col;
9332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 if (lnum < 0) /* ignore type error in {lnum} arg */
9334 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009335 if (lnum != prev_lnum
9336 || changedtick != curbuf->b_changedtick
9337 || fnum != curbuf->b_fnum)
9338 {
9339 /* New line, buffer, change: need to get the values. */
9340 filler_lines = diff_check(curwin, lnum);
9341 if (filler_lines < 0)
9342 {
9343 if (filler_lines == -1)
9344 {
9345 change_start = MAXCOL;
9346 change_end = -1;
9347 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9348 hlID = HLF_ADD; /* added line */
9349 else
9350 hlID = HLF_CHD; /* changed line */
9351 }
9352 else
9353 hlID = HLF_ADD; /* added line */
9354 }
9355 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009356 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009357 prev_lnum = lnum;
9358 changedtick = curbuf->b_changedtick;
9359 fnum = curbuf->b_fnum;
9360 }
9361
9362 if (hlID == HLF_CHD || hlID == HLF_TXD)
9363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009364 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009365 if (col >= change_start && col <= change_end)
9366 hlID = HLF_TXD; /* changed text */
9367 else
9368 hlID = HLF_CHD; /* changed line */
9369 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009370 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009371#endif
9372}
9373
9374/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009375 * "empty({expr})" function
9376 */
9377 static void
9378f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009379 typval_T *argvars;
9380 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009381{
9382 int n;
9383
9384 switch (argvars[0].v_type)
9385 {
9386 case VAR_STRING:
9387 case VAR_FUNC:
9388 n = argvars[0].vval.v_string == NULL
9389 || *argvars[0].vval.v_string == NUL;
9390 break;
9391 case VAR_NUMBER:
9392 n = argvars[0].vval.v_number == 0;
9393 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009394#ifdef FEAT_FLOAT
9395 case VAR_FLOAT:
9396 n = argvars[0].vval.v_float == 0.0;
9397 break;
9398#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009399 case VAR_LIST:
9400 n = argvars[0].vval.v_list == NULL
9401 || argvars[0].vval.v_list->lv_first == NULL;
9402 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 case VAR_DICT:
9404 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009405 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009406 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009407 default:
9408 EMSG2(_(e_intern2), "f_empty()");
9409 n = 0;
9410 }
9411
9412 rettv->vval.v_number = n;
9413}
9414
9415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 * "escape({string}, {chars})" function
9417 */
9418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 char_u buf[NUMBUFLEN];
9424
Bram Moolenaar758711c2005-02-02 23:11:38 +00009425 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9426 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428}
9429
9430/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009431 * "eval()" function
9432 */
9433/*ARGSUSED*/
9434 static void
9435f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009438{
9439 char_u *s;
9440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 s = get_tv_string_chk(&argvars[0]);
9442 if (s != NULL)
9443 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9446 {
9447 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009448 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009450 else if (*s != NUL)
9451 EMSG(_(e_trailing));
9452}
9453
9454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 * "eventhandler()" function
9456 */
9457/*ARGSUSED*/
9458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465
9466/*
9467 * "executable()" function
9468 */
9469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *argvars;
9472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475}
9476
9477/*
9478 * "exists()" function
9479 */
9480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009482 typval_T *argvars;
9483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 char_u *p;
9486 char_u *name;
9487 int n = FALSE;
9488 int len = 0;
9489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 if (*p == '$') /* environment variable */
9492 {
9493 /* first try "normal" environment variables (fast) */
9494 if (mch_getenv(p + 1) != NULL)
9495 n = TRUE;
9496 else
9497 {
9498 /* try expanding things like $VIM and ${HOME} */
9499 p = expand_env_save(p);
9500 if (p != NULL && *p != '$')
9501 n = TRUE;
9502 vim_free(p);
9503 }
9504 }
9505 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009508 if (*skipwhite(p) != NUL)
9509 n = FALSE; /* trailing garbage */
9510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 else if (*p == '*') /* internal or user defined function */
9512 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 }
9515 else if (*p == ':')
9516 {
9517 n = cmd_exists(p + 1);
9518 }
9519 else if (*p == '#')
9520 {
9521#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009522 if (p[1] == '#')
9523 n = autocmd_supported(p + 2);
9524 else
9525 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526#endif
9527 }
9528 else /* internal variable */
9529 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009530 char_u *tofree;
9531 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009533 /* get_name_len() takes care of expanding curly braces */
9534 name = p;
9535 len = get_name_len(&p, &tofree, TRUE, FALSE);
9536 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009538 if (tofree != NULL)
9539 name = tofree;
9540 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9541 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009543 /* handle d.key, l[idx], f(expr) */
9544 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9545 if (n)
9546 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
9548 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009549 if (*p != NUL)
9550 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009552 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 }
9554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556}
9557
9558/*
9559 * "expand()" function
9560 */
9561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566 char_u *s;
9567 int len;
9568 char_u *errormsg;
9569 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9570 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->v_type = VAR_STRING;
9574 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 if (*s == '%' || *s == '#' || *s == '<')
9576 {
9577 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009578 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 --emsg_off;
9580 }
9581 else
9582 {
9583 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009584 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 if (argvars[1].v_type != VAR_UNKNOWN
9586 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 if (!error)
9589 {
9590 ExpandInit(&xpc);
9591 xpc.xp_context = EXPAND_FILES;
9592 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 }
9594 else
9595 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 }
9597}
9598
9599/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009601 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009602 */
9603 static void
9604f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 typval_T *argvars;
9606 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009608 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009610 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 list_T *l1, *l2;
9612 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009615
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 l1 = argvars[0].vval.v_list;
9617 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009618 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9619 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 {
9621 if (argvars[2].v_type != VAR_UNKNOWN)
9622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 before = get_tv_number_chk(&argvars[2], &error);
9624 if (error)
9625 return; /* type error; errmsg already given */
9626
Bram Moolenaar758711c2005-02-02 23:11:38 +00009627 if (before == l1->lv_len)
9628 item = NULL;
9629 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009630 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009631 item = list_find(l1, before);
9632 if (item == NULL)
9633 {
9634 EMSGN(_(e_listidx), before);
9635 return;
9636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637 }
9638 }
9639 else
9640 item = NULL;
9641 list_extend(l1, l2, item);
9642
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 copy_tv(&argvars[0], rettv);
9644 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9647 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 dict_T *d1, *d2;
9649 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009650 char_u *action;
9651 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009653 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654
9655 d1 = argvars[0].vval.v_dict;
9656 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009657 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9658 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 {
9660 /* Check the third argument. */
9661 if (argvars[2].v_type != VAR_UNKNOWN)
9662 {
9663 static char *(av[]) = {"keep", "force", "error"};
9664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 action = get_tv_string_chk(&argvars[2]);
9666 if (action == NULL)
9667 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 for (i = 0; i < 3; ++i)
9669 if (STRCMP(action, av[i]) == 0)
9670 break;
9671 if (i == 3)
9672 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009673 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 return;
9675 }
9676 }
9677 else
9678 action = (char_u *)"force";
9679
9680 /* Go over all entries in the second dict and add them to the
9681 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009682 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009683 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009684 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009685 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009687 --todo;
9688 di1 = dict_find(d1, hi2->hi_key, -1);
9689 if (di1 == NULL)
9690 {
9691 di1 = dictitem_copy(HI2DI(hi2));
9692 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9693 dictitem_free(di1);
9694 }
9695 else if (*action == 'e')
9696 {
9697 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9698 break;
9699 }
9700 else if (*action == 'f')
9701 {
9702 clear_tv(&di1->di_tv);
9703 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 }
9706 }
9707
Bram Moolenaare9a41262005-01-15 22:18:47 +00009708 copy_tv(&argvars[0], rettv);
9709 }
9710 }
9711 else
9712 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009713}
9714
9715/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009716 * "feedkeys()" function
9717 */
9718/*ARGSUSED*/
9719 static void
9720f_feedkeys(argvars, rettv)
9721 typval_T *argvars;
9722 typval_T *rettv;
9723{
9724 int remap = TRUE;
9725 char_u *keys, *flags;
9726 char_u nbuf[NUMBUFLEN];
9727 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009728 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009729
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009730 /* This is not allowed in the sandbox. If the commands would still be
9731 * executed in the sandbox it would be OK, but it probably happens later,
9732 * when "sandbox" is no longer set. */
9733 if (check_secure())
9734 return;
9735
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009736 rettv->vval.v_number = 0;
9737 keys = get_tv_string(&argvars[0]);
9738 if (*keys != NUL)
9739 {
9740 if (argvars[1].v_type != VAR_UNKNOWN)
9741 {
9742 flags = get_tv_string_buf(&argvars[1], nbuf);
9743 for ( ; *flags != NUL; ++flags)
9744 {
9745 switch (*flags)
9746 {
9747 case 'n': remap = FALSE; break;
9748 case 'm': remap = TRUE; break;
9749 case 't': typed = TRUE; break;
9750 }
9751 }
9752 }
9753
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009754 /* Need to escape K_SPECIAL and CSI before putting the string in the
9755 * typeahead buffer. */
9756 keys_esc = vim_strsave_escape_csi(keys);
9757 if (keys_esc != NULL)
9758 {
9759 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009760 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009761 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009762 if (vgetc_busy)
9763 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009764 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009765 }
9766}
9767
9768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 * "filereadable()" function
9770 */
9771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009773 typval_T *argvars;
9774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009776 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 char_u *p;
9778 int n;
9779
Bram Moolenaarc236c162008-07-13 17:41:49 +00009780#ifndef O_NONBLOCK
9781# define O_NONBLOCK 0
9782#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009784 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9785 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
9787 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009788 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 }
9790 else
9791 n = FALSE;
9792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794}
9795
9796/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009797 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 * rights to write into.
9799 */
9800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009802 typval_T *argvars;
9803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009805 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009808static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009809
9810 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009811findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009812 typval_T *argvars;
9813 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009814 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009815{
9816#ifdef FEAT_SEARCHPATH
9817 char_u *fname;
9818 char_u *fresult = NULL;
9819 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9820 char_u *p;
9821 char_u pathbuf[NUMBUFLEN];
9822 int count = 1;
9823 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009824 int error = FALSE;
9825#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009826
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009827 rettv->vval.v_string = NULL;
9828 rettv->v_type = VAR_STRING;
9829
9830#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009835 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9836 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009837 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009838 else
9839 {
9840 if (*p != NUL)
9841 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009844 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009845 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009846 }
9847
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009848 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9849 error = TRUE;
9850
9851 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 do
9854 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009855 if (rettv->v_type == VAR_STRING)
9856 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 fresult = find_file_in_path_option(first ? fname : NULL,
9858 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009859 0, first, path,
9860 find_what,
9861 curbuf->b_ffname,
9862 find_what == FINDFILE_DIR
9863 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009865
9866 if (fresult != NULL && rettv->v_type == VAR_LIST)
9867 list_append_string(rettv->vval.v_list, fresult, -1);
9868
9869 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009870 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009871
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009872 if (rettv->v_type == VAR_STRING)
9873 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009874#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009875}
9876
Bram Moolenaar33570922005-01-25 22:26:29 +00009877static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9878static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009879
9880/*
9881 * Implementation of map() and filter().
9882 */
9883 static void
9884filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009885 typval_T *argvars;
9886 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009887 int map;
9888{
9889 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 listitem_T *li, *nli;
9892 list_T *l = NULL;
9893 dictitem_T *di;
9894 hashtab_T *ht;
9895 hashitem_T *hi;
9896 dict_T *d = NULL;
9897 typval_T save_val;
9898 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009900 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009901 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009902 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009903
9904 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009906 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009907 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009908 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 return;
9910 }
9911 else if (argvars[0].v_type == VAR_DICT)
9912 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009913 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009914 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 return;
9916 }
9917 else
9918 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009919 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 return;
9921 }
9922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 expr = get_tv_string_buf_chk(&argvars[1], buf);
9924 /* On type errors, the preceding call has already displayed an error
9925 * message. Avoid a misleading error message for an empty string that
9926 * was not passed as argument. */
9927 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 prepare_vimvar(VV_VAL, &save_val);
9930 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009931
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009932 /* We reset "did_emsg" to be able to detect whether an error
9933 * occurred during evaluation of the expression. */
9934 save_did_emsg = did_emsg;
9935 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009937 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 prepare_vimvar(VV_KEY, &save_key);
9940 vimvars[VV_KEY].vv_type = VAR_STRING;
9941
9942 ht = &d->dv_hashtab;
9943 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009944 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 if (!HASHITEM_EMPTY(hi))
9948 {
9949 --todo;
9950 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009951 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 break;
9953 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009954 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009955 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 break;
9957 if (!map && rem)
9958 dictitem_remove(d, di);
9959 clear_tv(&vimvars[VV_KEY].vv_tv);
9960 }
9961 }
9962 hash_unlock(ht);
9963
9964 restore_vimvar(VV_KEY, &save_key);
9965 }
9966 else
9967 {
9968 for (li = l->lv_first; li != NULL; li = nli)
9969 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009970 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009971 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009973 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009974 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009975 break;
9976 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009977 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009978 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009979 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009982
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009983 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009984 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985
9986 copy_tv(&argvars[0], rettv);
9987}
9988
9989 static int
9990filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009992 char_u *expr;
9993 int map;
9994 int *remp;
9995{
Bram Moolenaar33570922005-01-25 22:26:29 +00009996 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009998 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999
Bram Moolenaar33570922005-01-25 22:26:29 +000010000 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 s = expr;
10002 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010003 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010004 if (*s != NUL) /* check for trailing chars after expr */
10005 {
10006 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010007 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010008 }
10009 if (map)
10010 {
10011 /* map(): replace the list item value */
10012 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010013 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010014 *tv = rettv;
10015 }
10016 else
10017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 int error = FALSE;
10019
Bram Moolenaare9a41262005-01-15 22:18:47 +000010020 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010022 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010023 /* On type error, nothing has been removed; return FAIL to stop the
10024 * loop. The error message was given by get_tv_number_chk(). */
10025 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010026 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010027 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010028 retval = OK;
10029theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010030 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010031 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010032}
10033
10034/*
10035 * "filter()" function
10036 */
10037 static void
10038f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *argvars;
10040 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010041{
10042 filter_map(argvars, rettv, FALSE);
10043}
10044
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010046 * "finddir({fname}[, {path}[, {count}]])" function
10047 */
10048 static void
10049f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010052{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010053 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010054}
10055
10056/*
10057 * "findfile({fname}[, {path}[, {count}]])" function
10058 */
10059 static void
10060f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010061 typval_T *argvars;
10062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010063{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010064 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010065}
10066
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010067#ifdef FEAT_FLOAT
10068/*
10069 * "float2nr({float})" function
10070 */
10071 static void
10072f_float2nr(argvars, rettv)
10073 typval_T *argvars;
10074 typval_T *rettv;
10075{
10076 float_T f;
10077
10078 if (get_float_arg(argvars, &f) == OK)
10079 {
10080 if (f < -0x7fffffff)
10081 rettv->vval.v_number = -0x7fffffff;
10082 else if (f > 0x7fffffff)
10083 rettv->vval.v_number = 0x7fffffff;
10084 else
10085 rettv->vval.v_number = (varnumber_T)f;
10086 }
10087 else
10088 rettv->vval.v_number = 0;
10089}
10090
10091/*
10092 * "floor({float})" function
10093 */
10094 static void
10095f_floor(argvars, rettv)
10096 typval_T *argvars;
10097 typval_T *rettv;
10098{
10099 float_T f;
10100
10101 rettv->v_type = VAR_FLOAT;
10102 if (get_float_arg(argvars, &f) == OK)
10103 rettv->vval.v_float = floor(f);
10104 else
10105 rettv->vval.v_float = 0.0;
10106}
10107#endif
10108
Bram Moolenaar0d660222005-01-07 21:51:51 +000010109/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010110 * "fnameescape({string})" function
10111 */
10112 static void
10113f_fnameescape(argvars, rettv)
10114 typval_T *argvars;
10115 typval_T *rettv;
10116{
10117 rettv->vval.v_string = vim_strsave_fnameescape(
10118 get_tv_string(&argvars[0]), FALSE);
10119 rettv->v_type = VAR_STRING;
10120}
10121
10122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 * "fnamemodify({fname}, {mods})" function
10124 */
10125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010127 typval_T *argvars;
10128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129{
10130 char_u *fname;
10131 char_u *mods;
10132 int usedlen = 0;
10133 int len;
10134 char_u *fbuf = NULL;
10135 char_u buf[NUMBUFLEN];
10136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 fname = get_tv_string_chk(&argvars[0]);
10138 mods = get_tv_string_buf_chk(&argvars[1], buf);
10139 if (fname == NULL || mods == NULL)
10140 fname = NULL;
10141 else
10142 {
10143 len = (int)STRLEN(fname);
10144 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 vim_free(fbuf);
10153}
10154
Bram Moolenaar33570922005-01-25 22:26:29 +000010155static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156
10157/*
10158 * "foldclosed()" function
10159 */
10160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010162 typval_T *argvars;
10163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 int end;
10165{
10166#ifdef FEAT_FOLDING
10167 linenr_T lnum;
10168 linenr_T first, last;
10169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10172 {
10173 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10174 {
10175 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 return;
10180 }
10181 }
10182#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184}
10185
10186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010187 * "foldclosed()" function
10188 */
10189 static void
10190f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010191 typval_T *argvars;
10192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010193{
10194 foldclosed_both(argvars, rettv, FALSE);
10195}
10196
10197/*
10198 * "foldclosedend()" function
10199 */
10200 static void
10201f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010202 typval_T *argvars;
10203 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010204{
10205 foldclosed_both(argvars, rettv, TRUE);
10206}
10207
10208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 * "foldlevel()" function
10210 */
10211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 typval_T *argvars;
10214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
10216#ifdef FEAT_FOLDING
10217 linenr_T lnum;
10218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010219 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 else
10223#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225}
10226
10227/*
10228 * "foldtext()" function
10229 */
10230/*ARGSUSED*/
10231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 typval_T *argvars;
10234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236#ifdef FEAT_FOLDING
10237 linenr_T lnum;
10238 char_u *s;
10239 char_u *r;
10240 int len;
10241 char *txt;
10242#endif
10243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244 rettv->v_type = VAR_STRING;
10245 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010247 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10248 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10249 <= curbuf->b_ml.ml_line_count
10250 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 {
10252 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10254 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 {
10256 if (!linewhite(lnum))
10257 break;
10258 ++lnum;
10259 }
10260
10261 /* Find interesting text in this line. */
10262 s = skipwhite(ml_get(lnum));
10263 /* skip C comment-start */
10264 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010267 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010269 {
10270 s = skipwhite(ml_get(lnum + 1));
10271 if (*s == '*')
10272 s = skipwhite(s + 1);
10273 }
10274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 txt = _("+-%s%3ld lines: ");
10276 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 + 20 /* for %3ld */
10279 + STRLEN(s))); /* concatenated */
10280 if (r != NULL)
10281 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10283 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10284 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 len = (int)STRLEN(r);
10286 STRCAT(r, s);
10287 /* remove 'foldmarker' and 'commentstring' */
10288 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 }
10291 }
10292#endif
10293}
10294
10295/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010296 * "foldtextresult(lnum)" function
10297 */
10298/*ARGSUSED*/
10299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 typval_T *argvars;
10302 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010303{
10304#ifdef FEAT_FOLDING
10305 linenr_T lnum;
10306 char_u *text;
10307 char_u buf[51];
10308 foldinfo_T foldinfo;
10309 int fold_count;
10310#endif
10311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->v_type = VAR_STRING;
10313 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010314#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 /* treat illegal types and illegal string values for {lnum} the same */
10317 if (lnum < 0)
10318 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010319 fold_count = foldedCount(curwin, lnum, &foldinfo);
10320 if (fold_count > 0)
10321 {
10322 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10323 &foldinfo, buf);
10324 if (text == buf)
10325 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010327 }
10328#endif
10329}
10330
10331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 * "foreground()" function
10333 */
10334/*ARGSUSED*/
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010340 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341#ifdef FEAT_GUI
10342 if (gui.in_use)
10343 gui_mch_set_foreground();
10344#else
10345# ifdef WIN32
10346 win32_set_foreground();
10347# endif
10348#endif
10349}
10350
10351/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010352 * "function()" function
10353 */
10354/*ARGSUSED*/
10355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 typval_T *argvars;
10358 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010359{
10360 char_u *s;
10361
Bram Moolenaara7043832005-01-21 11:56:39 +000010362 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010363 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010364 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010365 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010366 /* Don't check an autoload name for existence here. */
10367 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010368 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010369 else
10370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371 rettv->vval.v_string = vim_strsave(s);
10372 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010373 }
10374}
10375
10376/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010377 * "garbagecollect()" function
10378 */
10379/*ARGSUSED*/
10380 static void
10381f_garbagecollect(argvars, rettv)
10382 typval_T *argvars;
10383 typval_T *rettv;
10384{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010385 /* This is postponed until we are back at the toplevel, because we may be
10386 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10387 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010388
10389 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10390 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010391}
10392
10393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010394 * "get()" function
10395 */
10396 static void
10397f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *argvars;
10399 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010400{
Bram Moolenaar33570922005-01-25 22:26:29 +000010401 listitem_T *li;
10402 list_T *l;
10403 dictitem_T *di;
10404 dict_T *d;
10405 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010406
Bram Moolenaare9a41262005-01-15 22:18:47 +000010407 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010408 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010409 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 int error = FALSE;
10412
10413 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10414 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010415 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010416 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 else if (argvars[0].v_type == VAR_DICT)
10419 {
10420 if ((d = argvars[0].vval.v_dict) != NULL)
10421 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010422 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010423 if (di != NULL)
10424 tv = &di->di_tv;
10425 }
10426 }
10427 else
10428 EMSG2(_(e_listdictarg), "get()");
10429
10430 if (tv == NULL)
10431 {
10432 if (argvars[2].v_type == VAR_UNKNOWN)
10433 rettv->vval.v_number = 0;
10434 else
10435 copy_tv(&argvars[2], rettv);
10436 }
10437 else
10438 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010439}
10440
Bram Moolenaar342337a2005-07-21 21:11:17 +000010441static 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 +000010442
10443/*
10444 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010445 * Return a range (from start to end) of lines in rettv from the specified
10446 * buffer.
10447 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010448 */
10449 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010450get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010451 buf_T *buf;
10452 linenr_T start;
10453 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010454 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010455 typval_T *rettv;
10456{
10457 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010458
Bram Moolenaar342337a2005-07-21 21:11:17 +000010459 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010460 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010461 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010462 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010463 }
10464 else
10465 rettv->vval.v_number = 0;
10466
10467 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10468 return;
10469
10470 if (!retlist)
10471 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010472 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10473 p = ml_get_buf(buf, start, FALSE);
10474 else
10475 p = (char_u *)"";
10476
10477 rettv->v_type = VAR_STRING;
10478 rettv->vval.v_string = vim_strsave(p);
10479 }
10480 else
10481 {
10482 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010483 return;
10484
10485 if (start < 1)
10486 start = 1;
10487 if (end > buf->b_ml.ml_line_count)
10488 end = buf->b_ml.ml_line_count;
10489 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010490 if (list_append_string(rettv->vval.v_list,
10491 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010492 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010493 }
10494}
10495
10496/*
10497 * "getbufline()" function
10498 */
10499 static void
10500f_getbufline(argvars, rettv)
10501 typval_T *argvars;
10502 typval_T *rettv;
10503{
10504 linenr_T lnum;
10505 linenr_T end;
10506 buf_T *buf;
10507
10508 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10509 ++emsg_off;
10510 buf = get_buf_tv(&argvars[0]);
10511 --emsg_off;
10512
Bram Moolenaar661b1822005-07-28 22:36:45 +000010513 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010514 if (argvars[2].v_type == VAR_UNKNOWN)
10515 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010516 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010517 end = get_tv_lnum_buf(&argvars[2], buf);
10518
Bram Moolenaar342337a2005-07-21 21:11:17 +000010519 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010520}
10521
Bram Moolenaar0d660222005-01-07 21:51:51 +000010522/*
10523 * "getbufvar()" function
10524 */
10525 static void
10526f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 typval_T *argvars;
10528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010529{
10530 buf_T *buf;
10531 buf_T *save_curbuf;
10532 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010533 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10536 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010537 ++emsg_off;
10538 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010539
10540 rettv->v_type = VAR_STRING;
10541 rettv->vval.v_string = NULL;
10542
10543 if (buf != NULL && varname != NULL)
10544 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010545 /* set curbuf to be our buf, temporarily */
10546 save_curbuf = curbuf;
10547 curbuf = buf;
10548
Bram Moolenaar0d660222005-01-07 21:51:51 +000010549 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010551 else
10552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 if (*varname == NUL)
10554 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10555 * scope prefix before the NUL byte is required by
10556 * find_var_in_ht(). */
10557 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010558 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010559 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010560 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010562 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010563
10564 /* restore previous notion of curbuf */
10565 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010566 }
10567
10568 --emsg_off;
10569}
10570
10571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 * "getchar()" function
10573 */
10574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010576 typval_T *argvars;
10577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578{
10579 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010582 /* Position the cursor. Needed after a message that ends in a space. */
10583 windgoto(msg_row, msg_col);
10584
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 ++no_mapping;
10586 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010587 for (;;)
10588 {
10589 if (argvars[0].v_type == VAR_UNKNOWN)
10590 /* getchar(): blocking wait. */
10591 n = safe_vgetc();
10592 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10593 /* getchar(1): only check if char avail */
10594 n = vpeekc();
10595 else if (error || vpeekc() == NUL)
10596 /* illegal argument or getchar(0) and no char avail: return zero */
10597 n = 0;
10598 else
10599 /* getchar(0) and char avail: return char */
10600 n = safe_vgetc();
10601 if (n == K_IGNORE)
10602 continue;
10603 break;
10604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 --no_mapping;
10606 --allow_keys;
10607
Bram Moolenaar219b8702006-11-01 14:32:36 +000010608 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10609 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10610 vimvars[VV_MOUSE_COL].vv_nr = 0;
10611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 if (IS_SPECIAL(n) || mod_mask != 0)
10614 {
10615 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10616 int i = 0;
10617
10618 /* Turn a special key into three bytes, plus modifier. */
10619 if (mod_mask != 0)
10620 {
10621 temp[i++] = K_SPECIAL;
10622 temp[i++] = KS_MODIFIER;
10623 temp[i++] = mod_mask;
10624 }
10625 if (IS_SPECIAL(n))
10626 {
10627 temp[i++] = K_SPECIAL;
10628 temp[i++] = K_SECOND(n);
10629 temp[i++] = K_THIRD(n);
10630 }
10631#ifdef FEAT_MBYTE
10632 else if (has_mbyte)
10633 i += (*mb_char2bytes)(n, temp + i);
10634#endif
10635 else
10636 temp[i++] = n;
10637 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 rettv->v_type = VAR_STRING;
10639 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010640
10641#ifdef FEAT_MOUSE
10642 if (n == K_LEFTMOUSE
10643 || n == K_LEFTMOUSE_NM
10644 || n == K_LEFTDRAG
10645 || n == K_LEFTRELEASE
10646 || n == K_LEFTRELEASE_NM
10647 || n == K_MIDDLEMOUSE
10648 || n == K_MIDDLEDRAG
10649 || n == K_MIDDLERELEASE
10650 || n == K_RIGHTMOUSE
10651 || n == K_RIGHTDRAG
10652 || n == K_RIGHTRELEASE
10653 || n == K_X1MOUSE
10654 || n == K_X1DRAG
10655 || n == K_X1RELEASE
10656 || n == K_X2MOUSE
10657 || n == K_X2DRAG
10658 || n == K_X2RELEASE
10659 || n == K_MOUSEDOWN
10660 || n == K_MOUSEUP)
10661 {
10662 int row = mouse_row;
10663 int col = mouse_col;
10664 win_T *win;
10665 linenr_T lnum;
10666# ifdef FEAT_WINDOWS
10667 win_T *wp;
10668# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010669 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010670
10671 if (row >= 0 && col >= 0)
10672 {
10673 /* Find the window at the mouse coordinates and compute the
10674 * text position. */
10675 win = mouse_find_win(&row, &col);
10676 (void)mouse_comp_pos(win, &row, &col, &lnum);
10677# ifdef FEAT_WINDOWS
10678 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010679 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010680# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010681 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010682 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10683 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10684 }
10685 }
10686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 }
10688}
10689
10690/*
10691 * "getcharmod()" function
10692 */
10693/*ARGSUSED*/
10694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010696 typval_T *argvars;
10697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700}
10701
10702/*
10703 * "getcmdline()" function
10704 */
10705/*ARGSUSED*/
10706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010708 typval_T *argvars;
10709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 rettv->v_type = VAR_STRING;
10712 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713}
10714
10715/*
10716 * "getcmdpos()" function
10717 */
10718/*ARGSUSED*/
10719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010721 typval_T *argvars;
10722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725}
10726
10727/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010728 * "getcmdtype()" function
10729 */
10730/*ARGSUSED*/
10731 static void
10732f_getcmdtype(argvars, rettv)
10733 typval_T *argvars;
10734 typval_T *rettv;
10735{
10736 rettv->v_type = VAR_STRING;
10737 rettv->vval.v_string = alloc(2);
10738 if (rettv->vval.v_string != NULL)
10739 {
10740 rettv->vval.v_string[0] = get_cmdline_type();
10741 rettv->vval.v_string[1] = NUL;
10742 }
10743}
10744
10745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 * "getcwd()" function
10747 */
10748/*ARGSUSED*/
10749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010751 typval_T *argvars;
10752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753{
10754 char_u cwd[MAXPATHL];
10755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
10760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010763 if (rettv->vval.v_string != NULL)
10764 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765#endif
10766 }
10767}
10768
10769/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010770 * "getfontname()" function
10771 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010772/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010775 typval_T *argvars;
10776 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010777{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->v_type = VAR_STRING;
10779 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010780#ifdef FEAT_GUI
10781 if (gui.in_use)
10782 {
10783 GuiFont font;
10784 char_u *name = NULL;
10785
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010786 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010787 {
10788 /* Get the "Normal" font. Either the name saved by
10789 * hl_set_font_name() or from the font ID. */
10790 font = gui.norm_font;
10791 name = hl_get_font_name();
10792 }
10793 else
10794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010795 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010796 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10797 return;
10798 font = gui_mch_get_font(name, FALSE);
10799 if (font == NOFONT)
10800 return; /* Invalid font name, return empty string. */
10801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010803 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010804 gui_mch_free_font(font);
10805 }
10806#endif
10807}
10808
10809/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010810 * "getfperm({fname})" function
10811 */
10812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 typval_T *argvars;
10815 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010816{
10817 char_u *fname;
10818 struct stat st;
10819 char_u *perm = NULL;
10820 char_u flags[] = "rwx";
10821 int i;
10822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010826 if (mch_stat((char *)fname, &st) >= 0)
10827 {
10828 perm = vim_strsave((char_u *)"---------");
10829 if (perm != NULL)
10830 {
10831 for (i = 0; i < 9; i++)
10832 {
10833 if (st.st_mode & (1 << (8 - i)))
10834 perm[i] = flags[i % 3];
10835 }
10836 }
10837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010839}
10840
10841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 * "getfsize({fname})" function
10843 */
10844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 typval_T *argvars;
10847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848{
10849 char_u *fname;
10850 struct stat st;
10851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855
10856 if (mch_stat((char *)fname, &st) >= 0)
10857 {
10858 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010863
10864 /* non-perfect check for overflow */
10865 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10866 rettv->vval.v_number = -2;
10867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 }
10869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871}
10872
10873/*
10874 * "getftime({fname})" function
10875 */
10876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010878 typval_T *argvars;
10879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880{
10881 char_u *fname;
10882 struct stat st;
10883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885
10886 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890}
10891
10892/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010893 * "getftype({fname})" function
10894 */
10895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 typval_T *argvars;
10898 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010899{
10900 char_u *fname;
10901 struct stat st;
10902 char_u *type = NULL;
10903 char *t;
10904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010908 if (mch_lstat((char *)fname, &st) >= 0)
10909 {
10910#ifdef S_ISREG
10911 if (S_ISREG(st.st_mode))
10912 t = "file";
10913 else if (S_ISDIR(st.st_mode))
10914 t = "dir";
10915# ifdef S_ISLNK
10916 else if (S_ISLNK(st.st_mode))
10917 t = "link";
10918# endif
10919# ifdef S_ISBLK
10920 else if (S_ISBLK(st.st_mode))
10921 t = "bdev";
10922# endif
10923# ifdef S_ISCHR
10924 else if (S_ISCHR(st.st_mode))
10925 t = "cdev";
10926# endif
10927# ifdef S_ISFIFO
10928 else if (S_ISFIFO(st.st_mode))
10929 t = "fifo";
10930# endif
10931# ifdef S_ISSOCK
10932 else if (S_ISSOCK(st.st_mode))
10933 t = "fifo";
10934# endif
10935 else
10936 t = "other";
10937#else
10938# ifdef S_IFMT
10939 switch (st.st_mode & S_IFMT)
10940 {
10941 case S_IFREG: t = "file"; break;
10942 case S_IFDIR: t = "dir"; break;
10943# ifdef S_IFLNK
10944 case S_IFLNK: t = "link"; break;
10945# endif
10946# ifdef S_IFBLK
10947 case S_IFBLK: t = "bdev"; break;
10948# endif
10949# ifdef S_IFCHR
10950 case S_IFCHR: t = "cdev"; break;
10951# endif
10952# ifdef S_IFIFO
10953 case S_IFIFO: t = "fifo"; break;
10954# endif
10955# ifdef S_IFSOCK
10956 case S_IFSOCK: t = "socket"; break;
10957# endif
10958 default: t = "other";
10959 }
10960# else
10961 if (mch_isdir(fname))
10962 t = "dir";
10963 else
10964 t = "file";
10965# endif
10966#endif
10967 type = vim_strsave((char_u *)t);
10968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010970}
10971
10972/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010973 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010974 */
10975 static void
10976f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010977 typval_T *argvars;
10978 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010979{
10980 linenr_T lnum;
10981 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010982 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010983
10984 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010985 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010986 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010987 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010988 retlist = FALSE;
10989 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010991 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010993 retlist = TRUE;
10994 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010995
Bram Moolenaar342337a2005-07-21 21:11:17 +000010996 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010997}
10998
10999/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011000 * "getmatches()" function
11001 */
11002/*ARGSUSED*/
11003 static void
11004f_getmatches(argvars, rettv)
11005 typval_T *argvars;
11006 typval_T *rettv;
11007{
11008#ifdef FEAT_SEARCH_EXTRA
11009 dict_T *dict;
11010 matchitem_T *cur = curwin->w_match_head;
11011
11012 rettv->vval.v_number = 0;
11013
11014 if (rettv_list_alloc(rettv) == OK)
11015 {
11016 while (cur != NULL)
11017 {
11018 dict = dict_alloc();
11019 if (dict == NULL)
11020 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011021 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11022 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11023 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11024 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11025 list_append_dict(rettv->vval.v_list, dict);
11026 cur = cur->next;
11027 }
11028 }
11029#endif
11030}
11031
11032/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011033 * "getpid()" function
11034 */
11035/*ARGSUSED*/
11036 static void
11037f_getpid(argvars, rettv)
11038 typval_T *argvars;
11039 typval_T *rettv;
11040{
11041 rettv->vval.v_number = mch_get_pid();
11042}
11043
11044/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011045 * "getpos(string)" function
11046 */
11047 static void
11048f_getpos(argvars, rettv)
11049 typval_T *argvars;
11050 typval_T *rettv;
11051{
11052 pos_T *fp;
11053 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011054 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011055
11056 if (rettv_list_alloc(rettv) == OK)
11057 {
11058 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011059 fp = var2fpos(&argvars[0], TRUE, &fnum);
11060 if (fnum != -1)
11061 list_append_number(l, (varnumber_T)fnum);
11062 else
11063 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011064 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11065 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011066 list_append_number(l, (fp != NULL)
11067 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011068 : (varnumber_T)0);
11069 list_append_number(l,
11070#ifdef FEAT_VIRTUALEDIT
11071 (fp != NULL) ? (varnumber_T)fp->coladd :
11072#endif
11073 (varnumber_T)0);
11074 }
11075 else
11076 rettv->vval.v_number = FALSE;
11077}
11078
11079/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011080 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011081 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000011082/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000011083 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011084f_getqflist(argvars, rettv)
11085 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011086 typval_T *rettv;
11087{
11088#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011089 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011090#endif
11091
Bram Moolenaar77f66d62007-02-20 02:16:18 +000011092 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011093#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011094 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011095 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011096 wp = NULL;
11097 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11098 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011099 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011100 if (wp == NULL)
11101 return;
11102 }
11103
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011104 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011105 }
11106#endif
11107}
11108
11109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 * "getreg()" function
11111 */
11112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 typval_T *argvars;
11115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116{
11117 char_u *strregname;
11118 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011119 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011122 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 strregname = get_tv_string_chk(&argvars[0]);
11125 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011126 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 regname = (strregname == NULL ? '"' : *strregname);
11132 if (regname == 0)
11133 regname = '"';
11134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 rettv->vval.v_string = error ? NULL :
11137 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138}
11139
11140/*
11141 * "getregtype()" function
11142 */
11143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 typval_T *argvars;
11146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
11148 char_u *strregname;
11149 int regname;
11150 char_u buf[NUMBUFLEN + 2];
11151 long reglen = 0;
11152
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011153 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 {
11155 strregname = get_tv_string_chk(&argvars[0]);
11156 if (strregname == NULL) /* type error; errmsg already given */
11157 {
11158 rettv->v_type = VAR_STRING;
11159 rettv->vval.v_string = NULL;
11160 return;
11161 }
11162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 else
11164 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011165 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166
11167 regname = (strregname == NULL ? '"' : *strregname);
11168 if (regname == 0)
11169 regname = '"';
11170
11171 buf[0] = NUL;
11172 buf[1] = NUL;
11173 switch (get_reg_type(regname, &reglen))
11174 {
11175 case MLINE: buf[0] = 'V'; break;
11176 case MCHAR: buf[0] = 'v'; break;
11177#ifdef FEAT_VISUAL
11178 case MBLOCK:
11179 buf[0] = Ctrl_V;
11180 sprintf((char *)buf + 1, "%ld", reglen + 1);
11181 break;
11182#endif
11183 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 rettv->v_type = VAR_STRING;
11185 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186}
11187
11188/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011189 * "gettabwinvar()" function
11190 */
11191 static void
11192f_gettabwinvar(argvars, rettv)
11193 typval_T *argvars;
11194 typval_T *rettv;
11195{
11196 getwinvar(argvars, rettv, 1);
11197}
11198
11199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 * "getwinposx()" function
11201 */
11202/*ARGSUSED*/
11203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011205 typval_T *argvars;
11206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209#ifdef FEAT_GUI
11210 if (gui.in_use)
11211 {
11212 int x, y;
11213
11214 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 }
11217#endif
11218}
11219
11220/*
11221 * "getwinposy()" function
11222 */
11223/*ARGSUSED*/
11224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T *argvars;
11227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230#ifdef FEAT_GUI
11231 if (gui.in_use)
11232 {
11233 int x, y;
11234
11235 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238#endif
11239}
11240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011241/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011242 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011243 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011244 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011245find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011246 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011247 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011248{
11249#ifdef FEAT_WINDOWS
11250 win_T *wp;
11251#endif
11252 int nr;
11253
11254 nr = get_tv_number_chk(vp, NULL);
11255
11256#ifdef FEAT_WINDOWS
11257 if (nr < 0)
11258 return NULL;
11259 if (nr == 0)
11260 return curwin;
11261
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011262 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11263 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011264 if (--nr <= 0)
11265 break;
11266 return wp;
11267#else
11268 if (nr == 0 || nr == 1)
11269 return curwin;
11270 return NULL;
11271#endif
11272}
11273
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274/*
11275 * "getwinvar()" function
11276 */
11277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011279 typval_T *argvars;
11280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011282 getwinvar(argvars, rettv, 0);
11283}
11284
11285/*
11286 * getwinvar() and gettabwinvar()
11287 */
11288 static void
11289getwinvar(argvars, rettv, off)
11290 typval_T *argvars;
11291 typval_T *rettv;
11292 int off; /* 1 for gettabwinvar() */
11293{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 win_T *win, *oldcurwin;
11295 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011297 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011299#ifdef FEAT_WINDOWS
11300 if (off == 1)
11301 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11302 else
11303 tp = curtab;
11304#endif
11305 win = find_win_by_nr(&argvars[off], tp);
11306 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011307 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->v_type = VAR_STRING;
11310 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
11312 if (win != NULL && varname != NULL)
11313 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011314 /* Set curwin to be our win, temporarily. Also set curbuf, so
11315 * that we can get buffer-local options. */
11316 oldcurwin = curwin;
11317 curwin = win;
11318 curbuf = win->w_buffer;
11319
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 else
11323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 if (*varname == NUL)
11325 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11326 * scope prefix before the NUL byte is required by
11327 * find_var_in_ht(). */
11328 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011330 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011334
11335 /* restore previous notion of curwin */
11336 curwin = oldcurwin;
11337 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 }
11339
11340 --emsg_off;
11341}
11342
11343/*
11344 * "glob()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011351 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011353 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011355 /* When the optional second argument is non-zero, don't remove matches
11356 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11357 if (argvars[1].v_type != VAR_UNKNOWN
11358 && get_tv_number_chk(&argvars[1], &error))
11359 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011361 if (!error)
11362 {
11363 ExpandInit(&xpc);
11364 xpc.xp_context = EXPAND_FILES;
11365 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11366 NULL, flags, WILD_ALL);
11367 }
11368 else
11369 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370}
11371
11372/*
11373 * "globpath()" function
11374 */
11375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011380 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011383 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011385 /* When the optional second argument is non-zero, don't remove matches
11386 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11387 if (argvars[2].v_type != VAR_UNKNOWN
11388 && get_tv_number_chk(&argvars[2], &error))
11389 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011391 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 rettv->vval.v_string = NULL;
11393 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011394 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11395 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396}
11397
11398/*
11399 * "has()" function
11400 */
11401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011403 typval_T *argvars;
11404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405{
11406 int i;
11407 char_u *name;
11408 int n = FALSE;
11409 static char *(has_list[]) =
11410 {
11411#ifdef AMIGA
11412 "amiga",
11413# ifdef FEAT_ARP
11414 "arp",
11415# endif
11416#endif
11417#ifdef __BEOS__
11418 "beos",
11419#endif
11420#ifdef MSDOS
11421# ifdef DJGPP
11422 "dos32",
11423# else
11424 "dos16",
11425# endif
11426#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011427#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 "mac",
11429#endif
11430#if defined(MACOS_X_UNIX)
11431 "macunix",
11432#endif
11433#ifdef OS2
11434 "os2",
11435#endif
11436#ifdef __QNX__
11437 "qnx",
11438#endif
11439#ifdef RISCOS
11440 "riscos",
11441#endif
11442#ifdef UNIX
11443 "unix",
11444#endif
11445#ifdef VMS
11446 "vms",
11447#endif
11448#ifdef WIN16
11449 "win16",
11450#endif
11451#ifdef WIN32
11452 "win32",
11453#endif
11454#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11455 "win32unix",
11456#endif
11457#ifdef WIN64
11458 "win64",
11459#endif
11460#ifdef EBCDIC
11461 "ebcdic",
11462#endif
11463#ifndef CASE_INSENSITIVE_FILENAME
11464 "fname_case",
11465#endif
11466#ifdef FEAT_ARABIC
11467 "arabic",
11468#endif
11469#ifdef FEAT_AUTOCMD
11470 "autocmd",
11471#endif
11472#ifdef FEAT_BEVAL
11473 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011474# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11475 "balloon_multiline",
11476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477#endif
11478#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11479 "builtin_terms",
11480# ifdef ALL_BUILTIN_TCAPS
11481 "all_builtin_terms",
11482# endif
11483#endif
11484#ifdef FEAT_BYTEOFF
11485 "byte_offset",
11486#endif
11487#ifdef FEAT_CINDENT
11488 "cindent",
11489#endif
11490#ifdef FEAT_CLIENTSERVER
11491 "clientserver",
11492#endif
11493#ifdef FEAT_CLIPBOARD
11494 "clipboard",
11495#endif
11496#ifdef FEAT_CMDL_COMPL
11497 "cmdline_compl",
11498#endif
11499#ifdef FEAT_CMDHIST
11500 "cmdline_hist",
11501#endif
11502#ifdef FEAT_COMMENTS
11503 "comments",
11504#endif
11505#ifdef FEAT_CRYPT
11506 "cryptv",
11507#endif
11508#ifdef FEAT_CSCOPE
11509 "cscope",
11510#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011511#ifdef CURSOR_SHAPE
11512 "cursorshape",
11513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514#ifdef DEBUG
11515 "debug",
11516#endif
11517#ifdef FEAT_CON_DIALOG
11518 "dialog_con",
11519#endif
11520#ifdef FEAT_GUI_DIALOG
11521 "dialog_gui",
11522#endif
11523#ifdef FEAT_DIFF
11524 "diff",
11525#endif
11526#ifdef FEAT_DIGRAPHS
11527 "digraphs",
11528#endif
11529#ifdef FEAT_DND
11530 "dnd",
11531#endif
11532#ifdef FEAT_EMACS_TAGS
11533 "emacs_tags",
11534#endif
11535 "eval", /* always present, of course! */
11536#ifdef FEAT_EX_EXTRA
11537 "ex_extra",
11538#endif
11539#ifdef FEAT_SEARCH_EXTRA
11540 "extra_search",
11541#endif
11542#ifdef FEAT_FKMAP
11543 "farsi",
11544#endif
11545#ifdef FEAT_SEARCHPATH
11546 "file_in_path",
11547#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011548#if defined(UNIX) && !defined(USE_SYSTEM)
11549 "filterpipe",
11550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551#ifdef FEAT_FIND_ID
11552 "find_in_path",
11553#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011554#ifdef FEAT_FLOAT
11555 "float",
11556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557#ifdef FEAT_FOLDING
11558 "folding",
11559#endif
11560#ifdef FEAT_FOOTER
11561 "footer",
11562#endif
11563#if !defined(USE_SYSTEM) && defined(UNIX)
11564 "fork",
11565#endif
11566#ifdef FEAT_GETTEXT
11567 "gettext",
11568#endif
11569#ifdef FEAT_GUI
11570 "gui",
11571#endif
11572#ifdef FEAT_GUI_ATHENA
11573# ifdef FEAT_GUI_NEXTAW
11574 "gui_neXtaw",
11575# else
11576 "gui_athena",
11577# endif
11578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579#ifdef FEAT_GUI_GTK
11580 "gui_gtk",
11581# ifdef HAVE_GTK2
11582 "gui_gtk2",
11583# endif
11584#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011585#ifdef FEAT_GUI_GNOME
11586 "gui_gnome",
11587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588#ifdef FEAT_GUI_MAC
11589 "gui_mac",
11590#endif
11591#ifdef FEAT_GUI_MOTIF
11592 "gui_motif",
11593#endif
11594#ifdef FEAT_GUI_PHOTON
11595 "gui_photon",
11596#endif
11597#ifdef FEAT_GUI_W16
11598 "gui_win16",
11599#endif
11600#ifdef FEAT_GUI_W32
11601 "gui_win32",
11602#endif
11603#ifdef FEAT_HANGULIN
11604 "hangul_input",
11605#endif
11606#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11607 "iconv",
11608#endif
11609#ifdef FEAT_INS_EXPAND
11610 "insert_expand",
11611#endif
11612#ifdef FEAT_JUMPLIST
11613 "jumplist",
11614#endif
11615#ifdef FEAT_KEYMAP
11616 "keymap",
11617#endif
11618#ifdef FEAT_LANGMAP
11619 "langmap",
11620#endif
11621#ifdef FEAT_LIBCALL
11622 "libcall",
11623#endif
11624#ifdef FEAT_LINEBREAK
11625 "linebreak",
11626#endif
11627#ifdef FEAT_LISP
11628 "lispindent",
11629#endif
11630#ifdef FEAT_LISTCMDS
11631 "listcmds",
11632#endif
11633#ifdef FEAT_LOCALMAP
11634 "localmap",
11635#endif
11636#ifdef FEAT_MENU
11637 "menu",
11638#endif
11639#ifdef FEAT_SESSION
11640 "mksession",
11641#endif
11642#ifdef FEAT_MODIFY_FNAME
11643 "modify_fname",
11644#endif
11645#ifdef FEAT_MOUSE
11646 "mouse",
11647#endif
11648#ifdef FEAT_MOUSESHAPE
11649 "mouseshape",
11650#endif
11651#if defined(UNIX) || defined(VMS)
11652# ifdef FEAT_MOUSE_DEC
11653 "mouse_dec",
11654# endif
11655# ifdef FEAT_MOUSE_GPM
11656 "mouse_gpm",
11657# endif
11658# ifdef FEAT_MOUSE_JSB
11659 "mouse_jsbterm",
11660# endif
11661# ifdef FEAT_MOUSE_NET
11662 "mouse_netterm",
11663# endif
11664# ifdef FEAT_MOUSE_PTERM
11665 "mouse_pterm",
11666# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011667# ifdef FEAT_SYSMOUSE
11668 "mouse_sysmouse",
11669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670# ifdef FEAT_MOUSE_XTERM
11671 "mouse_xterm",
11672# endif
11673#endif
11674#ifdef FEAT_MBYTE
11675 "multi_byte",
11676#endif
11677#ifdef FEAT_MBYTE_IME
11678 "multi_byte_ime",
11679#endif
11680#ifdef FEAT_MULTI_LANG
11681 "multi_lang",
11682#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011683#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011684#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011685 "mzscheme",
11686#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688#ifdef FEAT_OLE
11689 "ole",
11690#endif
11691#ifdef FEAT_OSFILETYPE
11692 "osfiletype",
11693#endif
11694#ifdef FEAT_PATH_EXTRA
11695 "path_extra",
11696#endif
11697#ifdef FEAT_PERL
11698#ifndef DYNAMIC_PERL
11699 "perl",
11700#endif
11701#endif
11702#ifdef FEAT_PYTHON
11703#ifndef DYNAMIC_PYTHON
11704 "python",
11705#endif
11706#endif
11707#ifdef FEAT_POSTSCRIPT
11708 "postscript",
11709#endif
11710#ifdef FEAT_PRINTER
11711 "printer",
11712#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011713#ifdef FEAT_PROFILE
11714 "profile",
11715#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011716#ifdef FEAT_RELTIME
11717 "reltime",
11718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719#ifdef FEAT_QUICKFIX
11720 "quickfix",
11721#endif
11722#ifdef FEAT_RIGHTLEFT
11723 "rightleft",
11724#endif
11725#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11726 "ruby",
11727#endif
11728#ifdef FEAT_SCROLLBIND
11729 "scrollbind",
11730#endif
11731#ifdef FEAT_CMDL_INFO
11732 "showcmd",
11733 "cmdline_info",
11734#endif
11735#ifdef FEAT_SIGNS
11736 "signs",
11737#endif
11738#ifdef FEAT_SMARTINDENT
11739 "smartindent",
11740#endif
11741#ifdef FEAT_SNIFF
11742 "sniff",
11743#endif
11744#ifdef FEAT_STL_OPT
11745 "statusline",
11746#endif
11747#ifdef FEAT_SUN_WORKSHOP
11748 "sun_workshop",
11749#endif
11750#ifdef FEAT_NETBEANS_INTG
11751 "netbeans_intg",
11752#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011753#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011754 "spell",
11755#endif
11756#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 "syntax",
11758#endif
11759#if defined(USE_SYSTEM) || !defined(UNIX)
11760 "system",
11761#endif
11762#ifdef FEAT_TAG_BINS
11763 "tag_binary",
11764#endif
11765#ifdef FEAT_TAG_OLDSTATIC
11766 "tag_old_static",
11767#endif
11768#ifdef FEAT_TAG_ANYWHITE
11769 "tag_any_white",
11770#endif
11771#ifdef FEAT_TCL
11772# ifndef DYNAMIC_TCL
11773 "tcl",
11774# endif
11775#endif
11776#ifdef TERMINFO
11777 "terminfo",
11778#endif
11779#ifdef FEAT_TERMRESPONSE
11780 "termresponse",
11781#endif
11782#ifdef FEAT_TEXTOBJ
11783 "textobjects",
11784#endif
11785#ifdef HAVE_TGETENT
11786 "tgetent",
11787#endif
11788#ifdef FEAT_TITLE
11789 "title",
11790#endif
11791#ifdef FEAT_TOOLBAR
11792 "toolbar",
11793#endif
11794#ifdef FEAT_USR_CMDS
11795 "user-commands", /* was accidentally included in 5.4 */
11796 "user_commands",
11797#endif
11798#ifdef FEAT_VIMINFO
11799 "viminfo",
11800#endif
11801#ifdef FEAT_VERTSPLIT
11802 "vertsplit",
11803#endif
11804#ifdef FEAT_VIRTUALEDIT
11805 "virtualedit",
11806#endif
11807#ifdef FEAT_VISUAL
11808 "visual",
11809#endif
11810#ifdef FEAT_VISUALEXTRA
11811 "visualextra",
11812#endif
11813#ifdef FEAT_VREPLACE
11814 "vreplace",
11815#endif
11816#ifdef FEAT_WILDIGN
11817 "wildignore",
11818#endif
11819#ifdef FEAT_WILDMENU
11820 "wildmenu",
11821#endif
11822#ifdef FEAT_WINDOWS
11823 "windows",
11824#endif
11825#ifdef FEAT_WAK
11826 "winaltkeys",
11827#endif
11828#ifdef FEAT_WRITEBACKUP
11829 "writebackup",
11830#endif
11831#ifdef FEAT_XIM
11832 "xim",
11833#endif
11834#ifdef FEAT_XFONTSET
11835 "xfontset",
11836#endif
11837#ifdef USE_XSMP
11838 "xsmp",
11839#endif
11840#ifdef USE_XSMP_INTERACT
11841 "xsmp_interact",
11842#endif
11843#ifdef FEAT_XCLIPBOARD
11844 "xterm_clipboard",
11845#endif
11846#ifdef FEAT_XTERM_SAVE
11847 "xterm_save",
11848#endif
11849#if defined(UNIX) && defined(FEAT_X11)
11850 "X11",
11851#endif
11852 NULL
11853 };
11854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 for (i = 0; has_list[i] != NULL; ++i)
11857 if (STRICMP(name, has_list[i]) == 0)
11858 {
11859 n = TRUE;
11860 break;
11861 }
11862
11863 if (n == FALSE)
11864 {
11865 if (STRNICMP(name, "patch", 5) == 0)
11866 n = has_patch(atoi((char *)name + 5));
11867 else if (STRICMP(name, "vim_starting") == 0)
11868 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011869#ifdef FEAT_MBYTE
11870 else if (STRICMP(name, "multi_byte_encoding") == 0)
11871 n = has_mbyte;
11872#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011873#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11874 else if (STRICMP(name, "balloon_multiline") == 0)
11875 n = multiline_balloon_available();
11876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877#ifdef DYNAMIC_TCL
11878 else if (STRICMP(name, "tcl") == 0)
11879 n = tcl_enabled(FALSE);
11880#endif
11881#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11882 else if (STRICMP(name, "iconv") == 0)
11883 n = iconv_enabled(FALSE);
11884#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011885#ifdef DYNAMIC_MZSCHEME
11886 else if (STRICMP(name, "mzscheme") == 0)
11887 n = mzscheme_enabled(FALSE);
11888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889#ifdef DYNAMIC_RUBY
11890 else if (STRICMP(name, "ruby") == 0)
11891 n = ruby_enabled(FALSE);
11892#endif
11893#ifdef DYNAMIC_PYTHON
11894 else if (STRICMP(name, "python") == 0)
11895 n = python_enabled(FALSE);
11896#endif
11897#ifdef DYNAMIC_PERL
11898 else if (STRICMP(name, "perl") == 0)
11899 n = perl_enabled(FALSE);
11900#endif
11901#ifdef FEAT_GUI
11902 else if (STRICMP(name, "gui_running") == 0)
11903 n = (gui.in_use || gui.starting);
11904# ifdef FEAT_GUI_W32
11905 else if (STRICMP(name, "gui_win32s") == 0)
11906 n = gui_is_win32s();
11907# endif
11908# ifdef FEAT_BROWSE
11909 else if (STRICMP(name, "browse") == 0)
11910 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11911# endif
11912#endif
11913#ifdef FEAT_SYN_HL
11914 else if (STRICMP(name, "syntax_items") == 0)
11915 n = syntax_present(curbuf);
11916#endif
11917#if defined(WIN3264)
11918 else if (STRICMP(name, "win95") == 0)
11919 n = mch_windows95();
11920#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011921#ifdef FEAT_NETBEANS_INTG
11922 else if (STRICMP(name, "netbeans_enabled") == 0)
11923 n = usingNetbeans;
11924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 }
11926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011931 * "has_key()" function
11932 */
11933 static void
11934f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *argvars;
11936 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011937{
11938 rettv->vval.v_number = 0;
11939 if (argvars[0].v_type != VAR_DICT)
11940 {
11941 EMSG(_(e_dictreq));
11942 return;
11943 }
11944 if (argvars[0].vval.v_dict == NULL)
11945 return;
11946
11947 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011948 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011949}
11950
11951/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011952 * "haslocaldir()" function
11953 */
11954/*ARGSUSED*/
11955 static void
11956f_haslocaldir(argvars, rettv)
11957 typval_T *argvars;
11958 typval_T *rettv;
11959{
11960 rettv->vval.v_number = (curwin->w_localdir != NULL);
11961}
11962
11963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 * "hasmapto()" function
11965 */
11966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 typval_T *argvars;
11969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970{
11971 char_u *name;
11972 char_u *mode;
11973 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011974 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011977 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 mode = (char_u *)"nvo";
11979 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011982 if (argvars[2].v_type != VAR_UNKNOWN)
11983 abbr = get_tv_number(&argvars[2]);
11984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985
Bram Moolenaar2c932302006-03-18 21:42:09 +000011986 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
11993 * "histadd()" function
11994 */
11995/*ARGSUSED*/
11996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011998 typval_T *argvars;
11999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001#ifdef FEAT_CMDHIST
12002 int histype;
12003 char_u *str;
12004 char_u buf[NUMBUFLEN];
12005#endif
12006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 if (check_restricted() || check_secure())
12009 return;
12010#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12012 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 if (histype >= 0)
12014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 if (*str != NUL)
12017 {
12018 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 return;
12021 }
12022 }
12023#endif
12024}
12025
12026/*
12027 * "histdel()" function
12028 */
12029/*ARGSUSED*/
12030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 typval_T *argvars;
12033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034{
12035#ifdef FEAT_CMDHIST
12036 int n;
12037 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012038 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12041 if (str == NULL)
12042 n = 0;
12043 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012046 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 else
12051 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 get_tv_string_buf(&argvars[1], buf));
12054 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057#endif
12058}
12059
12060/*
12061 * "histget()" function
12062 */
12063/*ARGSUSED*/
12064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 typval_T *argvars;
12067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068{
12069#ifdef FEAT_CMDHIST
12070 int type;
12071 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12075 if (str == NULL)
12076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012078 {
12079 type = get_histtype(str);
12080 if (argvars[1].v_type == VAR_UNKNOWN)
12081 idx = get_history_idx(type);
12082 else
12083 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12084 /* -1 on type error */
12085 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091}
12092
12093/*
12094 * "histnr()" function
12095 */
12096/*ARGSUSED*/
12097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012098f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012099 typval_T *argvars;
12100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101{
12102 int i;
12103
12104#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012105 char_u *history = get_tv_string_chk(&argvars[0]);
12106
12107 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 if (i >= HIST_CMD && i < HIST_COUNT)
12109 i = get_history_idx(i);
12110 else
12111#endif
12112 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114}
12115
12116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 * "highlightID(name)" function
12118 */
12119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012121 typval_T *argvars;
12122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012124 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125}
12126
12127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128 * "highlight_exists()" function
12129 */
12130 static void
12131f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *argvars;
12133 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134{
12135 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12136}
12137
12138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 * "hostname()" function
12140 */
12141/*ARGSUSED*/
12142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012144 typval_T *argvars;
12145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146{
12147 char_u hostname[256];
12148
12149 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->v_type = VAR_STRING;
12151 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152}
12153
12154/*
12155 * iconv() function
12156 */
12157/*ARGSUSED*/
12158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *argvars;
12161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
12163#ifdef FEAT_MBYTE
12164 char_u buf1[NUMBUFLEN];
12165 char_u buf2[NUMBUFLEN];
12166 char_u *from, *to, *str;
12167 vimconv_T vimconv;
12168#endif
12169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->v_type = VAR_STRING;
12171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172
12173#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174 str = get_tv_string(&argvars[0]);
12175 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12176 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 vimconv.vc_type = CONV_NONE;
12178 convert_setup(&vimconv, from, to);
12179
12180 /* If the encodings are equal, no conversion needed. */
12181 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012182 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185
12186 convert_setup(&vimconv, NULL, NULL);
12187 vim_free(from);
12188 vim_free(to);
12189#endif
12190}
12191
12192/*
12193 * "indent()" function
12194 */
12195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012197 typval_T *argvars;
12198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199{
12200 linenr_T lnum;
12201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207}
12208
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012209/*
12210 * "index()" function
12211 */
12212 static void
12213f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012214 typval_T *argvars;
12215 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012216{
Bram Moolenaar33570922005-01-25 22:26:29 +000012217 list_T *l;
12218 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012219 long idx = 0;
12220 int ic = FALSE;
12221
12222 rettv->vval.v_number = -1;
12223 if (argvars[0].v_type != VAR_LIST)
12224 {
12225 EMSG(_(e_listreq));
12226 return;
12227 }
12228 l = argvars[0].vval.v_list;
12229 if (l != NULL)
12230 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012231 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012232 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012234 int error = FALSE;
12235
Bram Moolenaar758711c2005-02-02 23:11:38 +000012236 /* Start at specified item. Use the cached index that list_find()
12237 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012238 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012239 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012240 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 ic = get_tv_number_chk(&argvars[3], &error);
12242 if (error)
12243 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012244 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012245
Bram Moolenaar758711c2005-02-02 23:11:38 +000012246 for ( ; item != NULL; item = item->li_next, ++idx)
12247 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012248 {
12249 rettv->vval.v_number = idx;
12250 break;
12251 }
12252 }
12253}
12254
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255static int inputsecret_flag = 0;
12256
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012257static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12258
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012260 * This function is used by f_input() and f_inputdialog() functions. The third
12261 * argument to f_input() specifies the type of completion to use at the
12262 * prompt. The third argument to f_inputdialog() specifies the value to return
12263 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 */
12265 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012266get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012267 typval_T *argvars;
12268 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012269 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 char_u *p = NULL;
12273 int c;
12274 char_u buf[NUMBUFLEN];
12275 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012277 int xp_type = EXPAND_NOTHING;
12278 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012280 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012281 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282
12283#ifdef NO_CONSOLE_INPUT
12284 /* While starting up, there is no place to enter text. */
12285 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287#endif
12288
12289 cmd_silent = FALSE; /* Want to see the prompt. */
12290 if (prompt != NULL)
12291 {
12292 /* Only the part of the message after the last NL is considered as
12293 * prompt for the command line */
12294 p = vim_strrchr(prompt, '\n');
12295 if (p == NULL)
12296 p = prompt;
12297 else
12298 {
12299 ++p;
12300 c = *p;
12301 *p = NUL;
12302 msg_start();
12303 msg_clr_eos();
12304 msg_puts_attr(prompt, echo_attr);
12305 msg_didout = FALSE;
12306 msg_starthere();
12307 *p = c;
12308 }
12309 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012311 if (argvars[1].v_type != VAR_UNKNOWN)
12312 {
12313 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12314 if (defstr != NULL)
12315 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012317 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012318 {
12319 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012320 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012321 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012322
Bram Moolenaar4463f292005-09-25 22:20:24 +000012323 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012324
Bram Moolenaar4463f292005-09-25 22:20:24 +000012325 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12326 if (xp_name == NULL)
12327 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012328
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012329 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012330
Bram Moolenaar4463f292005-09-25 22:20:24 +000012331 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12332 &xp_arg) == FAIL)
12333 return;
12334 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012335 }
12336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012337 if (defstr != NULL)
12338 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012339 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12340 xp_type, xp_arg);
12341
12342 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012344 /* since the user typed this, no need to wait for return */
12345 need_wait_return = FALSE;
12346 msg_didout = FALSE;
12347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 cmd_silent = cmd_silent_save;
12349}
12350
12351/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012352 * "input()" function
12353 * Also handles inputsecret() when inputsecret is set.
12354 */
12355 static void
12356f_input(argvars, rettv)
12357 typval_T *argvars;
12358 typval_T *rettv;
12359{
12360 get_user_input(argvars, rettv, FALSE);
12361}
12362
12363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 * "inputdialog()" function
12365 */
12366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012368 typval_T *argvars;
12369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370{
12371#if defined(FEAT_GUI_TEXTDIALOG)
12372 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12373 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12374 {
12375 char_u *message;
12376 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012377 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 message = get_tv_string_chk(&argvars[0]);
12380 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012381 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012382 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 else
12384 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 if (message != NULL && defstr != NULL
12386 && do_dialog(VIM_QUESTION, NULL, message,
12387 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 else
12390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 if (message != NULL && defstr != NULL
12392 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012393 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_string = vim_strsave(
12395 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 }
12401 else
12402#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012403 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404}
12405
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012406/*
12407 * "inputlist()" function
12408 */
12409 static void
12410f_inputlist(argvars, rettv)
12411 typval_T *argvars;
12412 typval_T *rettv;
12413{
12414 listitem_T *li;
12415 int selected;
12416 int mouse_used;
12417
12418 rettv->vval.v_number = 0;
12419#ifdef NO_CONSOLE_INPUT
12420 /* While starting up, there is no place to enter text. */
12421 if (no_console_input())
12422 return;
12423#endif
12424 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12425 {
12426 EMSG2(_(e_listarg), "inputlist()");
12427 return;
12428 }
12429
12430 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012431 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012432 lines_left = Rows; /* avoid more prompt */
12433 msg_scroll = TRUE;
12434 msg_clr_eos();
12435
12436 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12437 {
12438 msg_puts(get_tv_string(&li->li_tv));
12439 msg_putchar('\n');
12440 }
12441
12442 /* Ask for choice. */
12443 selected = prompt_for_number(&mouse_used);
12444 if (mouse_used)
12445 selected -= lines_left;
12446
12447 rettv->vval.v_number = selected;
12448}
12449
12450
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12452
12453/*
12454 * "inputrestore()" function
12455 */
12456/*ARGSUSED*/
12457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012459 typval_T *argvars;
12460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461{
12462 if (ga_userinput.ga_len > 0)
12463 {
12464 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12466 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 }
12469 else if (p_verbose > 1)
12470 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012471 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 }
12474}
12475
12476/*
12477 * "inputsave()" function
12478 */
12479/*ARGSUSED*/
12480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012482 typval_T *argvars;
12483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012485 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 if (ga_grow(&ga_userinput, 1) == OK)
12487 {
12488 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12489 + ga_userinput.ga_len);
12490 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 }
12493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495}
12496
12497/*
12498 * "inputsecret()" function
12499 */
12500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *argvars;
12503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
12505 ++cmdline_star;
12506 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 --cmdline_star;
12509 --inputsecret_flag;
12510}
12511
12512/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012513 * "insert()" function
12514 */
12515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012517 typval_T *argvars;
12518 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012519{
12520 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 listitem_T *item;
12522 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012523 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012524
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012525 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012526 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012528 else if ((l = argvars[0].vval.v_list) != NULL
12529 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012530 {
12531 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012532 before = get_tv_number_chk(&argvars[2], &error);
12533 if (error)
12534 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012535
Bram Moolenaar758711c2005-02-02 23:11:38 +000012536 if (before == l->lv_len)
12537 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012538 else
12539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012540 item = list_find(l, before);
12541 if (item == NULL)
12542 {
12543 EMSGN(_(e_listidx), before);
12544 l = NULL;
12545 }
12546 }
12547 if (l != NULL)
12548 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012549 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012550 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012551 }
12552 }
12553}
12554
12555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 * "isdirectory()" function
12557 */
12558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 typval_T *argvars;
12561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012563 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564}
12565
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012566/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012567 * "islocked()" function
12568 */
12569 static void
12570f_islocked(argvars, rettv)
12571 typval_T *argvars;
12572 typval_T *rettv;
12573{
12574 lval_T lv;
12575 char_u *end;
12576 dictitem_T *di;
12577
12578 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012579 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12580 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012581 if (end != NULL && lv.ll_name != NULL)
12582 {
12583 if (*end != NUL)
12584 EMSG(_(e_trailing));
12585 else
12586 {
12587 if (lv.ll_tv == NULL)
12588 {
12589 if (check_changedtick(lv.ll_name))
12590 rettv->vval.v_number = 1; /* always locked */
12591 else
12592 {
12593 di = find_var(lv.ll_name, NULL);
12594 if (di != NULL)
12595 {
12596 /* Consider a variable locked when:
12597 * 1. the variable itself is locked
12598 * 2. the value of the variable is locked.
12599 * 3. the List or Dict value is locked.
12600 */
12601 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12602 || tv_islocked(&di->di_tv));
12603 }
12604 }
12605 }
12606 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012607 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012608 else if (lv.ll_newkey != NULL)
12609 EMSG2(_(e_dictkey), lv.ll_newkey);
12610 else if (lv.ll_list != NULL)
12611 /* List item. */
12612 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12613 else
12614 /* Dictionary item. */
12615 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12616 }
12617 }
12618
12619 clear_lval(&lv);
12620}
12621
Bram Moolenaar33570922005-01-25 22:26:29 +000012622static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012623
12624/*
12625 * Turn a dict into a list:
12626 * "what" == 0: list of keys
12627 * "what" == 1: list of values
12628 * "what" == 2: list of items
12629 */
12630 static void
12631dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012632 typval_T *argvars;
12633 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012634 int what;
12635{
Bram Moolenaar33570922005-01-25 22:26:29 +000012636 list_T *l2;
12637 dictitem_T *di;
12638 hashitem_T *hi;
12639 listitem_T *li;
12640 listitem_T *li2;
12641 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012642 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012643
12644 rettv->vval.v_number = 0;
12645 if (argvars[0].v_type != VAR_DICT)
12646 {
12647 EMSG(_(e_dictreq));
12648 return;
12649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012650 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012651 return;
12652
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012653 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012654 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012656 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012661 --todo;
12662 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012663
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012664 li = listitem_alloc();
12665 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012666 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012667 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012668
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012669 if (what == 0)
12670 {
12671 /* keys() */
12672 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012673 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012674 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12675 }
12676 else if (what == 1)
12677 {
12678 /* values() */
12679 copy_tv(&di->di_tv, &li->li_tv);
12680 }
12681 else
12682 {
12683 /* items() */
12684 l2 = list_alloc();
12685 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012686 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012687 li->li_tv.vval.v_list = l2;
12688 if (l2 == NULL)
12689 break;
12690 ++l2->lv_refcount;
12691
12692 li2 = listitem_alloc();
12693 if (li2 == NULL)
12694 break;
12695 list_append(l2, li2);
12696 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012697 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012698 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12699
12700 li2 = listitem_alloc();
12701 if (li2 == NULL)
12702 break;
12703 list_append(l2, li2);
12704 copy_tv(&di->di_tv, &li2->li_tv);
12705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012706 }
12707 }
12708}
12709
12710/*
12711 * "items(dict)" function
12712 */
12713 static void
12714f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 typval_T *argvars;
12716 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012717{
12718 dict_list(argvars, rettv, 2);
12719}
12720
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012722 * "join()" function
12723 */
12724 static void
12725f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012726 typval_T *argvars;
12727 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012728{
12729 garray_T ga;
12730 char_u *sep;
12731
12732 rettv->vval.v_number = 0;
12733 if (argvars[0].v_type != VAR_LIST)
12734 {
12735 EMSG(_(e_listreq));
12736 return;
12737 }
12738 if (argvars[0].vval.v_list == NULL)
12739 return;
12740 if (argvars[1].v_type == VAR_UNKNOWN)
12741 sep = (char_u *)" ";
12742 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012744
12745 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746
12747 if (sep != NULL)
12748 {
12749 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012750 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012751 ga_append(&ga, NUL);
12752 rettv->vval.v_string = (char_u *)ga.ga_data;
12753 }
12754 else
12755 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012756}
12757
12758/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012759 * "keys()" function
12760 */
12761 static void
12762f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012765{
12766 dict_list(argvars, rettv, 0);
12767}
12768
12769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 * "last_buffer_nr()" function.
12771 */
12772/*ARGSUSED*/
12773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012775 typval_T *argvars;
12776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777{
12778 int n = 0;
12779 buf_T *buf;
12780
12781 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12782 if (n < buf->b_fnum)
12783 n = buf->b_fnum;
12784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012786}
12787
12788/*
12789 * "len()" function
12790 */
12791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012793 typval_T *argvars;
12794 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795{
12796 switch (argvars[0].v_type)
12797 {
12798 case VAR_STRING:
12799 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_number = (varnumber_T)STRLEN(
12801 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802 break;
12803 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012805 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012806 case VAR_DICT:
12807 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12808 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012809 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012810 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012811 break;
12812 }
12813}
12814
Bram Moolenaar33570922005-01-25 22:26:29 +000012815static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012816
12817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *argvars;
12820 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012821 int type;
12822{
12823#ifdef FEAT_LIBCALL
12824 char_u *string_in;
12825 char_u **string_result;
12826 int nr_result;
12827#endif
12828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012830 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012834
12835 if (check_restricted() || check_secure())
12836 return;
12837
12838#ifdef FEAT_LIBCALL
12839 /* The first two args must be strings, otherwise its meaningless */
12840 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012842 string_in = NULL;
12843 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012844 string_in = argvars[2].vval.v_string;
12845 if (type == VAR_NUMBER)
12846 string_result = NULL;
12847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012849 if (mch_libcall(argvars[0].vval.v_string,
12850 argvars[1].vval.v_string,
12851 string_in,
12852 argvars[2].vval.v_number,
12853 string_result,
12854 &nr_result) == OK
12855 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012857 }
12858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859}
12860
12861/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012862 * "libcall()" function
12863 */
12864 static void
12865f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 typval_T *argvars;
12867 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868{
12869 libcall_common(argvars, rettv, VAR_STRING);
12870}
12871
12872/*
12873 * "libcallnr()" function
12874 */
12875 static void
12876f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012879{
12880 libcall_common(argvars, rettv, VAR_NUMBER);
12881}
12882
12883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 * "line(string)" function
12885 */
12886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012888 typval_T *argvars;
12889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890{
12891 linenr_T lnum = 0;
12892 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012893 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012895 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 if (fp != NULL)
12897 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899}
12900
12901/*
12902 * "line2byte(lnum)" function
12903 */
12904/*ARGSUSED*/
12905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012907 typval_T *argvars;
12908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909{
12910#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#else
12913 linenr_T lnum;
12914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12920 if (rettv->vval.v_number >= 0)
12921 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922#endif
12923}
12924
12925/*
12926 * "lispindent(lnum)" function
12927 */
12928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932{
12933#ifdef FEAT_LISP
12934 pos_T pos;
12935 linenr_T lnum;
12936
12937 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12940 {
12941 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 curwin->w_cursor = pos;
12944 }
12945 else
12946#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948}
12949
12950/*
12951 * "localtime()" function
12952 */
12953/*ARGSUSED*/
12954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 typval_T *argvars;
12957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960}
12961
Bram Moolenaar33570922005-01-25 22:26:29 +000012962static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963
12964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 typval_T *argvars;
12967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 int exact;
12969{
12970 char_u *keys;
12971 char_u *which;
12972 char_u buf[NUMBUFLEN];
12973 char_u *keys_buf = NULL;
12974 char_u *rhs;
12975 int mode;
12976 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012977 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978
12979 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->v_type = VAR_STRING;
12981 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 if (*keys == NUL)
12985 return;
12986
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012987 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012990 if (argvars[2].v_type != VAR_UNKNOWN)
12991 abbr = get_tv_number(&argvars[2]);
12992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 else
12994 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012995 if (which == NULL)
12996 return;
12997
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 mode = get_map_mode(&which, 0);
12999
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013000 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013001 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 vim_free(keys_buf);
13003 if (rhs != NULL)
13004 {
13005 ga_init(&ga);
13006 ga.ga_itemsize = 1;
13007 ga.ga_growsize = 40;
13008
13009 while (*rhs != NUL)
13010 ga_concat(&ga, str2special(&rhs, FALSE));
13011
13012 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 }
13015}
13016
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013017#ifdef FEAT_FLOAT
13018/*
13019 * "log10()" function
13020 */
13021 static void
13022f_log10(argvars, rettv)
13023 typval_T *argvars;
13024 typval_T *rettv;
13025{
13026 float_T f;
13027
13028 rettv->v_type = VAR_FLOAT;
13029 if (get_float_arg(argvars, &f) == OK)
13030 rettv->vval.v_float = log10(f);
13031 else
13032 rettv->vval.v_float = 0.0;
13033}
13034#endif
13035
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013037 * "map()" function
13038 */
13039 static void
13040f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 typval_T *argvars;
13042 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013043{
13044 filter_map(argvars, rettv, TRUE);
13045}
13046
13047/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013048 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 */
13050 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013051f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013052 typval_T *argvars;
13053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013055 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056}
13057
13058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013059 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 */
13061 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013063 typval_T *argvars;
13064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013066 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067}
13068
Bram Moolenaar33570922005-01-25 22:26:29 +000013069static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070
13071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013073 typval_T *argvars;
13074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 int type;
13076{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013077 char_u *str = NULL;
13078 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 char_u *pat;
13080 regmatch_T regmatch;
13081 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013082 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 char_u *save_cpo;
13084 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013085 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013086 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013087 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013088 list_T *l = NULL;
13089 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013090 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013091 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092
13093 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13094 save_cpo = p_cpo;
13095 p_cpo = (char_u *)"";
13096
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013097 rettv->vval.v_number = -1;
13098 if (type == 3)
13099 {
13100 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013101 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013102 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013103 }
13104 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->v_type = VAR_STRING;
13107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013110 if (argvars[0].v_type == VAR_LIST)
13111 {
13112 if ((l = argvars[0].vval.v_list) == NULL)
13113 goto theend;
13114 li = l->lv_first;
13115 }
13116 else
13117 expr = str = get_tv_string(&argvars[0]);
13118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013119 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13120 if (pat == NULL)
13121 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013123 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 int error = FALSE;
13126
13127 start = get_tv_number_chk(&argvars[2], &error);
13128 if (error)
13129 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013130 if (l != NULL)
13131 {
13132 li = list_find(l, start);
13133 if (li == NULL)
13134 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013135 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013136 }
13137 else
13138 {
13139 if (start < 0)
13140 start = 0;
13141 if (start > (long)STRLEN(str))
13142 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013143 /* When "count" argument is there ignore matches before "start",
13144 * otherwise skip part of the string. Differs when pattern is "^"
13145 * or "\<". */
13146 if (argvars[3].v_type != VAR_UNKNOWN)
13147 startcol = start;
13148 else
13149 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013150 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013152 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 nth = get_tv_number_chk(&argvars[3], &error);
13154 if (error)
13155 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 }
13157
13158 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13159 if (regmatch.regprog != NULL)
13160 {
13161 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013162
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013163 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013164 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013165 if (l != NULL)
13166 {
13167 if (li == NULL)
13168 {
13169 match = FALSE;
13170 break;
13171 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013172 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013173 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013174 if (str == NULL)
13175 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013176 }
13177
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013178 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013179
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013180 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013181 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013182 if (l == NULL && !match)
13183 break;
13184
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013185 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013186 if (l != NULL)
13187 {
13188 li = li->li_next;
13189 ++idx;
13190 }
13191 else
13192 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013193#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013194 startcol = (colnr_T)(regmatch.startp[0]
13195 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013196#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013197 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013198#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013199 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013200 }
13201
13202 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013204 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013206 int i;
13207
13208 /* return list with matched string and submatches */
13209 for (i = 0; i < NSUBEXP; ++i)
13210 {
13211 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013212 {
13213 if (list_append_string(rettv->vval.v_list,
13214 (char_u *)"", 0) == FAIL)
13215 break;
13216 }
13217 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013218 regmatch.startp[i],
13219 (int)(regmatch.endp[i] - regmatch.startp[i]))
13220 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013221 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013222 }
13223 }
13224 else if (type == 2)
13225 {
13226 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013227 if (l != NULL)
13228 copy_tv(&li->li_tv, rettv);
13229 else
13230 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013232 }
13233 else if (l != NULL)
13234 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 else
13236 {
13237 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 (varnumber_T)(regmatch.startp[0] - str);
13240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013243 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 }
13245 }
13246 vim_free(regmatch.regprog);
13247 }
13248
13249theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013250 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 p_cpo = save_cpo;
13252}
13253
13254/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255 * "match()" function
13256 */
13257 static void
13258f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013259 typval_T *argvars;
13260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261{
13262 find_some_match(argvars, rettv, 1);
13263}
13264
13265/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013266 * "matchadd()" function
13267 */
13268 static void
13269f_matchadd(argvars, rettv)
13270 typval_T *argvars;
13271 typval_T *rettv;
13272{
13273#ifdef FEAT_SEARCH_EXTRA
13274 char_u buf[NUMBUFLEN];
13275 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13276 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13277 int prio = 10; /* default priority */
13278 int id = -1;
13279 int error = FALSE;
13280
13281 rettv->vval.v_number = -1;
13282
13283 if (grp == NULL || pat == NULL)
13284 return;
13285 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013286 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013287 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013288 if (argvars[3].v_type != VAR_UNKNOWN)
13289 id = get_tv_number_chk(&argvars[3], &error);
13290 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013291 if (error == TRUE)
13292 return;
13293 if (id >= 1 && id <= 3)
13294 {
13295 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13296 return;
13297 }
13298
13299 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13300#endif
13301}
13302
13303/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013304 * "matcharg()" function
13305 */
13306 static void
13307f_matcharg(argvars, rettv)
13308 typval_T *argvars;
13309 typval_T *rettv;
13310{
13311 if (rettv_list_alloc(rettv) == OK)
13312 {
13313#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013314 int id = get_tv_number(&argvars[0]);
13315 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013316
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013317 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013318 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013319 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13320 {
13321 list_append_string(rettv->vval.v_list,
13322 syn_id2name(m->hlg_id), -1);
13323 list_append_string(rettv->vval.v_list, m->pattern, -1);
13324 }
13325 else
13326 {
13327 list_append_string(rettv->vval.v_list, NUL, -1);
13328 list_append_string(rettv->vval.v_list, NUL, -1);
13329 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013330 }
13331#endif
13332 }
13333}
13334
13335/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013336 * "matchdelete()" function
13337 */
13338 static void
13339f_matchdelete(argvars, rettv)
13340 typval_T *argvars;
13341 typval_T *rettv;
13342{
13343#ifdef FEAT_SEARCH_EXTRA
13344 rettv->vval.v_number = match_delete(curwin,
13345 (int)get_tv_number(&argvars[0]), TRUE);
13346#endif
13347}
13348
13349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013350 * "matchend()" function
13351 */
13352 static void
13353f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *argvars;
13355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013356{
13357 find_some_match(argvars, rettv, 0);
13358}
13359
13360/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013361 * "matchlist()" function
13362 */
13363 static void
13364f_matchlist(argvars, rettv)
13365 typval_T *argvars;
13366 typval_T *rettv;
13367{
13368 find_some_match(argvars, rettv, 3);
13369}
13370
13371/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013372 * "matchstr()" function
13373 */
13374 static void
13375f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *argvars;
13377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378{
13379 find_some_match(argvars, rettv, 2);
13380}
13381
Bram Moolenaar33570922005-01-25 22:26:29 +000013382static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013383
13384 static void
13385max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *argvars;
13387 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013388 int domax;
13389{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013390 long n = 0;
13391 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013393
13394 if (argvars[0].v_type == VAR_LIST)
13395 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 list_T *l;
13397 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013398
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013399 l = argvars[0].vval.v_list;
13400 if (l != NULL)
13401 {
13402 li = l->lv_first;
13403 if (li != NULL)
13404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013406 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013407 {
13408 li = li->li_next;
13409 if (li == NULL)
13410 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013412 if (domax ? i > n : i < n)
13413 n = i;
13414 }
13415 }
13416 }
13417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013418 else if (argvars[0].v_type == VAR_DICT)
13419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013420 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013421 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013422 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013423 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013424
13425 d = argvars[0].vval.v_dict;
13426 if (d != NULL)
13427 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013428 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013431 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013433 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013435 if (first)
13436 {
13437 n = i;
13438 first = FALSE;
13439 }
13440 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013441 n = i;
13442 }
13443 }
13444 }
13445 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013446 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013447 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013449}
13450
13451/*
13452 * "max()" function
13453 */
13454 static void
13455f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013458{
13459 max_min(argvars, rettv, TRUE);
13460}
13461
13462/*
13463 * "min()" function
13464 */
13465 static void
13466f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013467 typval_T *argvars;
13468 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013469{
13470 max_min(argvars, rettv, FALSE);
13471}
13472
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013473static int mkdir_recurse __ARGS((char_u *dir, int prot));
13474
13475/*
13476 * Create the directory in which "dir" is located, and higher levels when
13477 * needed.
13478 */
13479 static int
13480mkdir_recurse(dir, prot)
13481 char_u *dir;
13482 int prot;
13483{
13484 char_u *p;
13485 char_u *updir;
13486 int r = FAIL;
13487
13488 /* Get end of directory name in "dir".
13489 * We're done when it's "/" or "c:/". */
13490 p = gettail_sep(dir);
13491 if (p <= get_past_head(dir))
13492 return OK;
13493
13494 /* If the directory exists we're done. Otherwise: create it.*/
13495 updir = vim_strnsave(dir, (int)(p - dir));
13496 if (updir == NULL)
13497 return FAIL;
13498 if (mch_isdir(updir))
13499 r = OK;
13500 else if (mkdir_recurse(updir, prot) == OK)
13501 r = vim_mkdir_emsg(updir, prot);
13502 vim_free(updir);
13503 return r;
13504}
13505
13506#ifdef vim_mkdir
13507/*
13508 * "mkdir()" function
13509 */
13510 static void
13511f_mkdir(argvars, rettv)
13512 typval_T *argvars;
13513 typval_T *rettv;
13514{
13515 char_u *dir;
13516 char_u buf[NUMBUFLEN];
13517 int prot = 0755;
13518
13519 rettv->vval.v_number = FAIL;
13520 if (check_restricted() || check_secure())
13521 return;
13522
13523 dir = get_tv_string_buf(&argvars[0], buf);
13524 if (argvars[1].v_type != VAR_UNKNOWN)
13525 {
13526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013527 prot = get_tv_number_chk(&argvars[2], NULL);
13528 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013529 mkdir_recurse(dir, prot);
13530 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013532}
13533#endif
13534
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 * "mode()" function
13537 */
13538/*ARGSUSED*/
13539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *argvars;
13542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013544 char_u buf[3];
13545
13546 buf[1] = NUL;
13547 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548
13549#ifdef FEAT_VISUAL
13550 if (VIsual_active)
13551 {
13552 if (VIsual_select)
13553 buf[0] = VIsual_mode + 's' - 'v';
13554 else
13555 buf[0] = VIsual_mode;
13556 }
13557 else
13558#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013559 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13560 || State == CONFIRM)
13561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013563 if (State == ASKMORE)
13564 buf[1] = 'm';
13565 else if (State == CONFIRM)
13566 buf[1] = '?';
13567 }
13568 else if (State == EXTERNCMD)
13569 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 else if (State & INSERT)
13571 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013572#ifdef FEAT_VREPLACE
13573 if (State & VREPLACE_FLAG)
13574 {
13575 buf[0] = 'R';
13576 buf[1] = 'v';
13577 }
13578 else
13579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 if (State & REPLACE_FLAG)
13581 buf[0] = 'R';
13582 else
13583 buf[0] = 'i';
13584 }
13585 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013588 if (exmode_active)
13589 buf[1] = 'v';
13590 }
13591 else if (exmode_active)
13592 {
13593 buf[0] = 'c';
13594 buf[1] = 'e';
13595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013599 if (finish_op)
13600 buf[1] = 'o';
13601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013603 /* Clear out the minor mode when the argument is not a non-zero number or
13604 * non-empty string. */
13605 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013606 buf[1] = NUL;
13607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->vval.v_string = vim_strsave(buf);
13609 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610}
13611
13612/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013613 * "nextnonblank()" function
13614 */
13615 static void
13616f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013617 typval_T *argvars;
13618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013619{
13620 linenr_T lnum;
13621
13622 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625 {
13626 lnum = 0;
13627 break;
13628 }
13629 if (*skipwhite(ml_get(lnum)) != NUL)
13630 break;
13631 }
13632 rettv->vval.v_number = lnum;
13633}
13634
13635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 * "nr2char()" function
13637 */
13638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642{
13643 char_u buf[NUMBUFLEN];
13644
13645#ifdef FEAT_MBYTE
13646 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 else
13649#endif
13650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 buf[1] = NUL;
13653 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654 rettv->v_type = VAR_STRING;
13655 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013656}
13657
13658/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013659 * "pathshorten()" function
13660 */
13661 static void
13662f_pathshorten(argvars, rettv)
13663 typval_T *argvars;
13664 typval_T *rettv;
13665{
13666 char_u *p;
13667
13668 rettv->v_type = VAR_STRING;
13669 p = get_tv_string_chk(&argvars[0]);
13670 if (p == NULL)
13671 rettv->vval.v_string = NULL;
13672 else
13673 {
13674 p = vim_strsave(p);
13675 rettv->vval.v_string = p;
13676 if (p != NULL)
13677 shorten_dir(p);
13678 }
13679}
13680
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013681#ifdef FEAT_FLOAT
13682/*
13683 * "pow()" function
13684 */
13685 static void
13686f_pow(argvars, rettv)
13687 typval_T *argvars;
13688 typval_T *rettv;
13689{
13690 float_T fx, fy;
13691
13692 rettv->v_type = VAR_FLOAT;
13693 if (get_float_arg(argvars, &fx) == OK
13694 && get_float_arg(&argvars[1], &fy) == OK)
13695 rettv->vval.v_float = pow(fx, fy);
13696 else
13697 rettv->vval.v_float = 0.0;
13698}
13699#endif
13700
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 * "prevnonblank()" function
13703 */
13704 static void
13705f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 typval_T *argvars;
13707 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708{
13709 linenr_T lnum;
13710
13711 lnum = get_tv_lnum(argvars);
13712 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13713 lnum = 0;
13714 else
13715 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13716 --lnum;
13717 rettv->vval.v_number = lnum;
13718}
13719
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013720#ifdef HAVE_STDARG_H
13721/* This dummy va_list is here because:
13722 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13723 * - locally in the function results in a "used before set" warning
13724 * - using va_start() to initialize it gives "function with fixed args" error */
13725static va_list ap;
13726#endif
13727
Bram Moolenaar8c711452005-01-14 21:53:12 +000013728/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013729 * "printf()" function
13730 */
13731 static void
13732f_printf(argvars, rettv)
13733 typval_T *argvars;
13734 typval_T *rettv;
13735{
13736 rettv->v_type = VAR_STRING;
13737 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013738#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013739 {
13740 char_u buf[NUMBUFLEN];
13741 int len;
13742 char_u *s;
13743 int saved_did_emsg = did_emsg;
13744 char *fmt;
13745
13746 /* Get the required length, allocate the buffer and do it for real. */
13747 did_emsg = FALSE;
13748 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013749 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013750 if (!did_emsg)
13751 {
13752 s = alloc(len + 1);
13753 if (s != NULL)
13754 {
13755 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013756 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013757 }
13758 }
13759 did_emsg |= saved_did_emsg;
13760 }
13761#endif
13762}
13763
13764/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013765 * "pumvisible()" function
13766 */
13767/*ARGSUSED*/
13768 static void
13769f_pumvisible(argvars, rettv)
13770 typval_T *argvars;
13771 typval_T *rettv;
13772{
13773 rettv->vval.v_number = 0;
13774#ifdef FEAT_INS_EXPAND
13775 if (pum_visible())
13776 rettv->vval.v_number = 1;
13777#endif
13778}
13779
13780/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013781 * "range()" function
13782 */
13783 static void
13784f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013785 typval_T *argvars;
13786 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013787{
13788 long start;
13789 long end;
13790 long stride = 1;
13791 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013795 if (argvars[1].v_type == VAR_UNKNOWN)
13796 {
13797 end = start - 1;
13798 start = 0;
13799 }
13800 else
13801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013805 }
13806
13807 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013808 if (error)
13809 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013810 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013811 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013812 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013813 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013814 else
13815 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013816 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013817 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013818 if (list_append_number(rettv->vval.v_list,
13819 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013820 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013821 }
13822}
13823
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013824/*
13825 * "readfile()" function
13826 */
13827 static void
13828f_readfile(argvars, rettv)
13829 typval_T *argvars;
13830 typval_T *rettv;
13831{
13832 int binary = FALSE;
13833 char_u *fname;
13834 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013835 listitem_T *li;
13836#define FREAD_SIZE 200 /* optimized for text lines */
13837 char_u buf[FREAD_SIZE];
13838 int readlen; /* size of last fread() */
13839 int buflen; /* nr of valid chars in buf[] */
13840 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13841 int tolist; /* first byte in buf[] still to be put in list */
13842 int chop; /* how many CR to chop off */
13843 char_u *prev = NULL; /* previously read bytes, if any */
13844 int prevlen = 0; /* length of "prev" if not NULL */
13845 char_u *s;
13846 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013847 long maxline = MAXLNUM;
13848 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013849
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013850 if (argvars[1].v_type != VAR_UNKNOWN)
13851 {
13852 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13853 binary = TRUE;
13854 if (argvars[2].v_type != VAR_UNKNOWN)
13855 maxline = get_tv_number(&argvars[2]);
13856 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013857
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013858 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013859 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013860
13861 /* Always open the file in binary mode, library functions have a mind of
13862 * their own about CR-LF conversion. */
13863 fname = get_tv_string(&argvars[0]);
13864 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13865 {
13866 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13867 return;
13868 }
13869
13870 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013871 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013872 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013873 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013874 buflen = filtd + readlen;
13875 tolist = 0;
13876 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13877 {
13878 if (buf[filtd] == '\n' || readlen <= 0)
13879 {
13880 /* Only when in binary mode add an empty list item when the
13881 * last line ends in a '\n'. */
13882 if (!binary && readlen == 0 && filtd == 0)
13883 break;
13884
13885 /* Found end-of-line or end-of-file: add a text line to the
13886 * list. */
13887 chop = 0;
13888 if (!binary)
13889 while (filtd - chop - 1 >= tolist
13890 && buf[filtd - chop - 1] == '\r')
13891 ++chop;
13892 len = filtd - tolist - chop;
13893 if (prev == NULL)
13894 s = vim_strnsave(buf + tolist, len);
13895 else
13896 {
13897 s = alloc((unsigned)(prevlen + len + 1));
13898 if (s != NULL)
13899 {
13900 mch_memmove(s, prev, prevlen);
13901 vim_free(prev);
13902 prev = NULL;
13903 mch_memmove(s + prevlen, buf + tolist, len);
13904 s[prevlen + len] = NUL;
13905 }
13906 }
13907 tolist = filtd + 1;
13908
13909 li = listitem_alloc();
13910 if (li == NULL)
13911 {
13912 vim_free(s);
13913 break;
13914 }
13915 li->li_tv.v_type = VAR_STRING;
13916 li->li_tv.v_lock = 0;
13917 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013918 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013919
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013920 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013921 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013922 if (readlen <= 0)
13923 break;
13924 }
13925 else if (buf[filtd] == NUL)
13926 buf[filtd] = '\n';
13927 }
13928 if (readlen <= 0)
13929 break;
13930
13931 if (tolist == 0)
13932 {
13933 /* "buf" is full, need to move text to an allocated buffer */
13934 if (prev == NULL)
13935 {
13936 prev = vim_strnsave(buf, buflen);
13937 prevlen = buflen;
13938 }
13939 else
13940 {
13941 s = alloc((unsigned)(prevlen + buflen));
13942 if (s != NULL)
13943 {
13944 mch_memmove(s, prev, prevlen);
13945 mch_memmove(s + prevlen, buf, buflen);
13946 vim_free(prev);
13947 prev = s;
13948 prevlen += buflen;
13949 }
13950 }
13951 filtd = 0;
13952 }
13953 else
13954 {
13955 mch_memmove(buf, buf + tolist, buflen - tolist);
13956 filtd -= tolist;
13957 }
13958 }
13959
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013960 /*
13961 * For a negative line count use only the lines at the end of the file,
13962 * free the rest.
13963 */
13964 if (maxline < 0)
13965 while (cnt > -maxline)
13966 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013967 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013968 --cnt;
13969 }
13970
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013971 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013972 fclose(fd);
13973}
13974
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013975#if defined(FEAT_RELTIME)
13976static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13977
13978/*
13979 * Convert a List to proftime_T.
13980 * Return FAIL when there is something wrong.
13981 */
13982 static int
13983list2proftime(arg, tm)
13984 typval_T *arg;
13985 proftime_T *tm;
13986{
13987 long n1, n2;
13988 int error = FALSE;
13989
13990 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13991 || arg->vval.v_list->lv_len != 2)
13992 return FAIL;
13993 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13994 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13995# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013996 tm->HighPart = n1;
13997 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013998# else
13999 tm->tv_sec = n1;
14000 tm->tv_usec = n2;
14001# endif
14002 return error ? FAIL : OK;
14003}
14004#endif /* FEAT_RELTIME */
14005
14006/*
14007 * "reltime()" function
14008 */
14009 static void
14010f_reltime(argvars, rettv)
14011 typval_T *argvars;
14012 typval_T *rettv;
14013{
14014#ifdef FEAT_RELTIME
14015 proftime_T res;
14016 proftime_T start;
14017
14018 if (argvars[0].v_type == VAR_UNKNOWN)
14019 {
14020 /* No arguments: get current time. */
14021 profile_start(&res);
14022 }
14023 else if (argvars[1].v_type == VAR_UNKNOWN)
14024 {
14025 if (list2proftime(&argvars[0], &res) == FAIL)
14026 return;
14027 profile_end(&res);
14028 }
14029 else
14030 {
14031 /* Two arguments: compute the difference. */
14032 if (list2proftime(&argvars[0], &start) == FAIL
14033 || list2proftime(&argvars[1], &res) == FAIL)
14034 return;
14035 profile_sub(&res, &start);
14036 }
14037
14038 if (rettv_list_alloc(rettv) == OK)
14039 {
14040 long n1, n2;
14041
14042# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014043 n1 = res.HighPart;
14044 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014045# else
14046 n1 = res.tv_sec;
14047 n2 = res.tv_usec;
14048# endif
14049 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14050 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14051 }
14052#endif
14053}
14054
14055/*
14056 * "reltimestr()" function
14057 */
14058 static void
14059f_reltimestr(argvars, rettv)
14060 typval_T *argvars;
14061 typval_T *rettv;
14062{
14063#ifdef FEAT_RELTIME
14064 proftime_T tm;
14065#endif
14066
14067 rettv->v_type = VAR_STRING;
14068 rettv->vval.v_string = NULL;
14069#ifdef FEAT_RELTIME
14070 if (list2proftime(&argvars[0], &tm) == OK)
14071 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14072#endif
14073}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014074
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14076static void make_connection __ARGS((void));
14077static int check_connection __ARGS((void));
14078
14079 static void
14080make_connection()
14081{
14082 if (X_DISPLAY == NULL
14083# ifdef FEAT_GUI
14084 && !gui.in_use
14085# endif
14086 )
14087 {
14088 x_force_connect = TRUE;
14089 setup_term_clip();
14090 x_force_connect = FALSE;
14091 }
14092}
14093
14094 static int
14095check_connection()
14096{
14097 make_connection();
14098 if (X_DISPLAY == NULL)
14099 {
14100 EMSG(_("E240: No connection to Vim server"));
14101 return FAIL;
14102 }
14103 return OK;
14104}
14105#endif
14106
14107#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014108static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014109
14110 static void
14111remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114 int expr;
14115{
14116 char_u *server_name;
14117 char_u *keys;
14118 char_u *r = NULL;
14119 char_u buf[NUMBUFLEN];
14120# ifdef WIN32
14121 HWND w;
14122# else
14123 Window w;
14124# endif
14125
14126 if (check_restricted() || check_secure())
14127 return;
14128
14129# ifdef FEAT_X11
14130 if (check_connection() == FAIL)
14131 return;
14132# endif
14133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014134 server_name = get_tv_string_chk(&argvars[0]);
14135 if (server_name == NULL)
14136 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014137 keys = get_tv_string_buf(&argvars[1], buf);
14138# ifdef WIN32
14139 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14140# else
14141 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14142 < 0)
14143# endif
14144 {
14145 if (r != NULL)
14146 EMSG(r); /* sending worked but evaluation failed */
14147 else
14148 EMSG2(_("E241: Unable to send to %s"), server_name);
14149 return;
14150 }
14151
14152 rettv->vval.v_string = r;
14153
14154 if (argvars[2].v_type != VAR_UNKNOWN)
14155 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014157 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014160 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 v.di_tv.v_type = VAR_STRING;
14162 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014163 idvar = get_tv_string_chk(&argvars[2]);
14164 if (idvar != NULL)
14165 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014167 }
14168}
14169#endif
14170
14171/*
14172 * "remote_expr()" function
14173 */
14174/*ARGSUSED*/
14175 static void
14176f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 typval_T *argvars;
14178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179{
14180 rettv->v_type = VAR_STRING;
14181 rettv->vval.v_string = NULL;
14182#ifdef FEAT_CLIENTSERVER
14183 remote_common(argvars, rettv, TRUE);
14184#endif
14185}
14186
14187/*
14188 * "remote_foreground()" function
14189 */
14190/*ARGSUSED*/
14191 static void
14192f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195{
14196 rettv->vval.v_number = 0;
14197#ifdef FEAT_CLIENTSERVER
14198# ifdef WIN32
14199 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 {
14201 char_u *server_name = get_tv_string_chk(&argvars[0]);
14202
14203 if (server_name != NULL)
14204 serverForeground(server_name);
14205 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206# else
14207 /* Send a foreground() expression to the server. */
14208 argvars[1].v_type = VAR_STRING;
14209 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14210 argvars[2].v_type = VAR_UNKNOWN;
14211 remote_common(argvars, rettv, TRUE);
14212 vim_free(argvars[1].vval.v_string);
14213# endif
14214#endif
14215}
14216
14217/*ARGSUSED*/
14218 static void
14219f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014220 typval_T *argvars;
14221 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222{
14223#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014224 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014225 char_u *s = NULL;
14226# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014227 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014228# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230
14231 if (check_restricted() || check_secure())
14232 {
14233 rettv->vval.v_number = -1;
14234 return;
14235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 serverid = get_tv_string_chk(&argvars[0]);
14237 if (serverid == NULL)
14238 {
14239 rettv->vval.v_number = -1;
14240 return; /* type error; errmsg already given */
14241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014242# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014243 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 if (n == 0)
14245 rettv->vval.v_number = -1;
14246 else
14247 {
14248 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14249 rettv->vval.v_number = (s != NULL);
14250 }
14251# else
14252 rettv->vval.v_number = 0;
14253 if (check_connection() == FAIL)
14254 return;
14255
14256 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258# endif
14259
14260 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 char_u *retvar;
14263
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 v.di_tv.v_type = VAR_STRING;
14265 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 retvar = get_tv_string_chk(&argvars[1]);
14267 if (retvar != NULL)
14268 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014269 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014270 }
14271#else
14272 rettv->vval.v_number = -1;
14273#endif
14274}
14275
14276/*ARGSUSED*/
14277 static void
14278f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 typval_T *argvars;
14280 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281{
14282 char_u *r = NULL;
14283
14284#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 char_u *serverid = get_tv_string_chk(&argvars[0]);
14286
14287 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288 {
14289# ifdef WIN32
14290 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014291 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014292
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014293 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014294 if (n != 0)
14295 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14296 if (r == NULL)
14297# else
14298 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014300# endif
14301 EMSG(_("E277: Unable to read a server reply"));
14302 }
14303#endif
14304 rettv->v_type = VAR_STRING;
14305 rettv->vval.v_string = r;
14306}
14307
14308/*
14309 * "remote_send()" function
14310 */
14311/*ARGSUSED*/
14312 static void
14313f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014314 typval_T *argvars;
14315 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316{
14317 rettv->v_type = VAR_STRING;
14318 rettv->vval.v_string = NULL;
14319#ifdef FEAT_CLIENTSERVER
14320 remote_common(argvars, rettv, FALSE);
14321#endif
14322}
14323
14324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014325 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014326 */
14327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014331{
Bram Moolenaar33570922005-01-25 22:26:29 +000014332 list_T *l;
14333 listitem_T *item, *item2;
14334 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014335 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014336 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014337 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 dict_T *d;
14339 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014340
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014341 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014342 if (argvars[0].v_type == VAR_DICT)
14343 {
14344 if (argvars[2].v_type != VAR_UNKNOWN)
14345 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014346 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014347 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 key = get_tv_string_chk(&argvars[1]);
14350 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 di = dict_find(d, key, -1);
14353 if (di == NULL)
14354 EMSG2(_(e_dictkey), key);
14355 else
14356 {
14357 *rettv = di->di_tv;
14358 init_tv(&di->di_tv);
14359 dictitem_remove(d, di);
14360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014361 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014362 }
14363 }
14364 else if (argvars[0].v_type != VAR_LIST)
14365 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014366 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014367 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 int error = FALSE;
14370
14371 idx = get_tv_number_chk(&argvars[1], &error);
14372 if (error)
14373 ; /* type error: do nothing, errmsg already given */
14374 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014375 EMSGN(_(e_listidx), idx);
14376 else
14377 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014378 if (argvars[2].v_type == VAR_UNKNOWN)
14379 {
14380 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014381 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014382 *rettv = item->li_tv;
14383 vim_free(item);
14384 }
14385 else
14386 {
14387 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 end = get_tv_number_chk(&argvars[2], &error);
14389 if (error)
14390 ; /* type error: do nothing */
14391 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014392 EMSGN(_(e_listidx), end);
14393 else
14394 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014395 int cnt = 0;
14396
14397 for (li = item; li != NULL; li = li->li_next)
14398 {
14399 ++cnt;
14400 if (li == item2)
14401 break;
14402 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014403 if (li == NULL) /* didn't find "item2" after "item" */
14404 EMSG(_(e_invrange));
14405 else
14406 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014407 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014408 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014409 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014410 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014411 l->lv_first = item;
14412 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014413 item->li_prev = NULL;
14414 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014415 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014416 }
14417 }
14418 }
14419 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014420 }
14421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422}
14423
14424/*
14425 * "rename({from}, {to})" function
14426 */
14427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *argvars;
14430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431{
14432 char_u buf[NUMBUFLEN];
14433
14434 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14438 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439}
14440
14441/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014442 * "repeat()" function
14443 */
14444/*ARGSUSED*/
14445 static void
14446f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 typval_T *argvars;
14448 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014449{
14450 char_u *p;
14451 int n;
14452 int slen;
14453 int len;
14454 char_u *r;
14455 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014456
14457 n = get_tv_number(&argvars[1]);
14458 if (argvars[0].v_type == VAR_LIST)
14459 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014460 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014461 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014462 if (list_extend(rettv->vval.v_list,
14463 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014464 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014465 }
14466 else
14467 {
14468 p = get_tv_string(&argvars[0]);
14469 rettv->v_type = VAR_STRING;
14470 rettv->vval.v_string = NULL;
14471
14472 slen = (int)STRLEN(p);
14473 len = slen * n;
14474 if (len <= 0)
14475 return;
14476
14477 r = alloc(len + 1);
14478 if (r != NULL)
14479 {
14480 for (i = 0; i < n; i++)
14481 mch_memmove(r + i * slen, p, (size_t)slen);
14482 r[len] = NUL;
14483 }
14484
14485 rettv->vval.v_string = r;
14486 }
14487}
14488
14489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 * "resolve()" function
14491 */
14492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014493f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *argvars;
14495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496{
14497 char_u *p;
14498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500#ifdef FEAT_SHORTCUT
14501 {
14502 char_u *v = NULL;
14503
14504 v = mch_resolve_shortcut(p);
14505 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 }
14510#else
14511# ifdef HAVE_READLINK
14512 {
14513 char_u buf[MAXPATHL + 1];
14514 char_u *cpy;
14515 int len;
14516 char_u *remain = NULL;
14517 char_u *q;
14518 int is_relative_to_current = FALSE;
14519 int has_trailing_pathsep = FALSE;
14520 int limit = 100;
14521
14522 p = vim_strsave(p);
14523
14524 if (p[0] == '.' && (vim_ispathsep(p[1])
14525 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14526 is_relative_to_current = TRUE;
14527
14528 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014529 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 has_trailing_pathsep = TRUE;
14531
14532 q = getnextcomp(p);
14533 if (*q != NUL)
14534 {
14535 /* Separate the first path component in "p", and keep the
14536 * remainder (beginning with the path separator). */
14537 remain = vim_strsave(q - 1);
14538 q[-1] = NUL;
14539 }
14540
14541 for (;;)
14542 {
14543 for (;;)
14544 {
14545 len = readlink((char *)p, (char *)buf, MAXPATHL);
14546 if (len <= 0)
14547 break;
14548 buf[len] = NUL;
14549
14550 if (limit-- == 0)
14551 {
14552 vim_free(p);
14553 vim_free(remain);
14554 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 goto fail;
14557 }
14558
14559 /* Ensure that the result will have a trailing path separator
14560 * if the argument has one. */
14561 if (remain == NULL && has_trailing_pathsep)
14562 add_pathsep(buf);
14563
14564 /* Separate the first path component in the link value and
14565 * concatenate the remainders. */
14566 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14567 if (*q != NUL)
14568 {
14569 if (remain == NULL)
14570 remain = vim_strsave(q - 1);
14571 else
14572 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014573 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 if (cpy != NULL)
14575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 vim_free(remain);
14577 remain = cpy;
14578 }
14579 }
14580 q[-1] = NUL;
14581 }
14582
14583 q = gettail(p);
14584 if (q > p && *q == NUL)
14585 {
14586 /* Ignore trailing path separator. */
14587 q[-1] = NUL;
14588 q = gettail(p);
14589 }
14590 if (q > p && !mch_isFullName(buf))
14591 {
14592 /* symlink is relative to directory of argument */
14593 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14594 if (cpy != NULL)
14595 {
14596 STRCPY(cpy, p);
14597 STRCPY(gettail(cpy), buf);
14598 vim_free(p);
14599 p = cpy;
14600 }
14601 }
14602 else
14603 {
14604 vim_free(p);
14605 p = vim_strsave(buf);
14606 }
14607 }
14608
14609 if (remain == NULL)
14610 break;
14611
14612 /* Append the first path component of "remain" to "p". */
14613 q = getnextcomp(remain + 1);
14614 len = q - remain - (*q != NUL);
14615 cpy = vim_strnsave(p, STRLEN(p) + len);
14616 if (cpy != NULL)
14617 {
14618 STRNCAT(cpy, remain, len);
14619 vim_free(p);
14620 p = cpy;
14621 }
14622 /* Shorten "remain". */
14623 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014624 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 else
14626 {
14627 vim_free(remain);
14628 remain = NULL;
14629 }
14630 }
14631
14632 /* If the result is a relative path name, make it explicitly relative to
14633 * the current directory if and only if the argument had this form. */
14634 if (!vim_ispathsep(*p))
14635 {
14636 if (is_relative_to_current
14637 && *p != NUL
14638 && !(p[0] == '.'
14639 && (p[1] == NUL
14640 || vim_ispathsep(p[1])
14641 || (p[1] == '.'
14642 && (p[2] == NUL
14643 || vim_ispathsep(p[2]))))))
14644 {
14645 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014646 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 if (cpy != NULL)
14648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 vim_free(p);
14650 p = cpy;
14651 }
14652 }
14653 else if (!is_relative_to_current)
14654 {
14655 /* Strip leading "./". */
14656 q = p;
14657 while (q[0] == '.' && vim_ispathsep(q[1]))
14658 q += 2;
14659 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014660 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 }
14662 }
14663
14664 /* Ensure that the result will have no trailing path separator
14665 * if the argument had none. But keep "/" or "//". */
14666 if (!has_trailing_pathsep)
14667 {
14668 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014669 if (after_pathsep(p, q))
14670 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 }
14672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 }
14675# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677# endif
14678#endif
14679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014680 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681
14682#ifdef HAVE_READLINK
14683fail:
14684#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686}
14687
14688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 */
14691 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014693 typval_T *argvars;
14694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695{
Bram Moolenaar33570922005-01-25 22:26:29 +000014696 list_T *l;
14697 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 rettv->vval.v_number = 0;
14700 if (argvars[0].v_type != VAR_LIST)
14701 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014702 else if ((l = argvars[0].vval.v_list) != NULL
14703 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704 {
14705 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014706 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014707 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708 while (li != NULL)
14709 {
14710 ni = li->li_prev;
14711 list_append(l, li);
14712 li = ni;
14713 }
14714 rettv->vval.v_list = l;
14715 rettv->v_type = VAR_LIST;
14716 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014717 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719}
14720
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014721#define SP_NOMOVE 0x01 /* don't move cursor */
14722#define SP_REPEAT 0x02 /* repeat to find outer pair */
14723#define SP_RETCOUNT 0x04 /* return matchcount */
14724#define SP_SETPCMARK 0x08 /* set previous context mark */
14725#define SP_START 0x10 /* accept match at start position */
14726#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14727#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014728
Bram Moolenaar33570922005-01-25 22:26:29 +000014729static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730
14731/*
14732 * Get flags for a search function.
14733 * Possibly sets "p_ws".
14734 * Returns BACKWARD, FORWARD or zero (for an error).
14735 */
14736 static int
14737get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014739 int *flagsp;
14740{
14741 int dir = FORWARD;
14742 char_u *flags;
14743 char_u nbuf[NUMBUFLEN];
14744 int mask;
14745
14746 if (varp->v_type != VAR_UNKNOWN)
14747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 flags = get_tv_string_buf_chk(varp, nbuf);
14749 if (flags == NULL)
14750 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 while (*flags != NUL)
14752 {
14753 switch (*flags)
14754 {
14755 case 'b': dir = BACKWARD; break;
14756 case 'w': p_ws = TRUE; break;
14757 case 'W': p_ws = FALSE; break;
14758 default: mask = 0;
14759 if (flagsp != NULL)
14760 switch (*flags)
14761 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014762 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014763 case 'e': mask = SP_END; break;
14764 case 'm': mask = SP_RETCOUNT; break;
14765 case 'n': mask = SP_NOMOVE; break;
14766 case 'p': mask = SP_SUBPAT; break;
14767 case 'r': mask = SP_REPEAT; break;
14768 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014769 }
14770 if (mask == 0)
14771 {
14772 EMSG2(_(e_invarg2), flags);
14773 dir = 0;
14774 }
14775 else
14776 *flagsp |= mask;
14777 }
14778 if (dir == 0)
14779 break;
14780 ++flags;
14781 }
14782 }
14783 return dir;
14784}
14785
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014787 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014789 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014790search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014792 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014793 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014795 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 char_u *pat;
14797 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014798 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 int save_p_ws = p_ws;
14800 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014801 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014802 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014803 proftime_T tm;
14804#ifdef FEAT_RELTIME
14805 long time_limit = 0;
14806#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014807 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014808 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014810 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014811 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014812 if (dir == 0)
14813 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014814 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014815 if (flags & SP_START)
14816 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014817 if (flags & SP_END)
14818 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014819
Bram Moolenaar76929292008-01-06 19:07:36 +000014820 /* Optional arguments: line number to stop searching and timeout. */
14821 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014822 {
14823 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14824 if (lnum_stop < 0)
14825 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014826#ifdef FEAT_RELTIME
14827 if (argvars[3].v_type != VAR_UNKNOWN)
14828 {
14829 time_limit = get_tv_number_chk(&argvars[3], NULL);
14830 if (time_limit < 0)
14831 goto theend;
14832 }
14833#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014834 }
14835
Bram Moolenaar76929292008-01-06 19:07:36 +000014836#ifdef FEAT_RELTIME
14837 /* Set the time limit, if there is one. */
14838 profile_setlimit(time_limit, &tm);
14839#endif
14840
Bram Moolenaar231334e2005-07-25 20:46:57 +000014841 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014842 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014843 * Check to make sure only those flags are set.
14844 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14845 * flags cannot be set. Check for that condition also.
14846 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014847 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014848 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014851 goto theend;
14852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014854 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014855 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014856 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014857 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014859 if (flags & SP_SUBPAT)
14860 retval = subpatnum;
14861 else
14862 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014863 if (flags & SP_SETPCMARK)
14864 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014866 if (match_pos != NULL)
14867 {
14868 /* Store the match cursor position */
14869 match_pos->lnum = pos.lnum;
14870 match_pos->col = pos.col + 1;
14871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 /* "/$" will put the cursor after the end of the line, may need to
14873 * correct that here */
14874 check_cursor();
14875 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014876
14877 /* If 'n' flag is used: restore cursor position. */
14878 if (flags & SP_NOMOVE)
14879 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014880 else
14881 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014882theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014884
14885 return retval;
14886}
14887
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014888#ifdef FEAT_FLOAT
14889/*
14890 * "round({float})" function
14891 */
14892 static void
14893f_round(argvars, rettv)
14894 typval_T *argvars;
14895 typval_T *rettv;
14896{
14897 float_T f;
14898
14899 rettv->v_type = VAR_FLOAT;
14900 if (get_float_arg(argvars, &f) == OK)
14901 /* round() is not in C90, use ceil() or floor() instead. */
14902 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14903 else
14904 rettv->vval.v_float = 0.0;
14905}
14906#endif
14907
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014908/*
14909 * "search()" function
14910 */
14911 static void
14912f_search(argvars, rettv)
14913 typval_T *argvars;
14914 typval_T *rettv;
14915{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014916 int flags = 0;
14917
14918 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919}
14920
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014922 * "searchdecl()" function
14923 */
14924 static void
14925f_searchdecl(argvars, rettv)
14926 typval_T *argvars;
14927 typval_T *rettv;
14928{
14929 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014930 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014931 int error = FALSE;
14932 char_u *name;
14933
14934 rettv->vval.v_number = 1; /* default: FAIL */
14935
14936 name = get_tv_string_chk(&argvars[0]);
14937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014938 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014939 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014940 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14941 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14942 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014943 if (!error && name != NULL)
14944 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014945 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014946}
14947
14948/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014949 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014951 static int
14952searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014954 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955{
14956 char_u *spat, *mpat, *epat;
14957 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 int dir;
14960 int flags = 0;
14961 char_u nbuf1[NUMBUFLEN];
14962 char_u nbuf2[NUMBUFLEN];
14963 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014964 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014965 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014966 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014969 spat = get_tv_string_chk(&argvars[0]);
14970 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14971 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14972 if (spat == NULL || mpat == NULL || epat == NULL)
14973 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 /* Handle the optional fourth argument: flags */
14976 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014977 if (dir == 0)
14978 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014979
14980 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014981 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14982 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014983 if ((flags & (SP_END | SP_SUBPAT)) != 0
14984 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014985 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014986 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014987 goto theend;
14988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014990 /* Using 'r' implies 'W', otherwise it doesn't work. */
14991 if (flags & SP_REPEAT)
14992 p_ws = FALSE;
14993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014994 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014995 if (argvars[3].v_type == VAR_UNKNOWN
14996 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 skip = (char_u *)"";
14998 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015001 if (argvars[5].v_type != VAR_UNKNOWN)
15002 {
15003 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15004 if (lnum_stop < 0)
15005 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015006#ifdef FEAT_RELTIME
15007 if (argvars[6].v_type != VAR_UNKNOWN)
15008 {
15009 time_limit = get_tv_number_chk(&argvars[6], NULL);
15010 if (time_limit < 0)
15011 goto theend;
15012 }
15013#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015014 }
15015 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 if (skip == NULL)
15017 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015019 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015020 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015021
15022theend:
15023 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015024
15025 return retval;
15026}
15027
15028/*
15029 * "searchpair()" function
15030 */
15031 static void
15032f_searchpair(argvars, rettv)
15033 typval_T *argvars;
15034 typval_T *rettv;
15035{
15036 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15037}
15038
15039/*
15040 * "searchpairpos()" function
15041 */
15042 static void
15043f_searchpairpos(argvars, rettv)
15044 typval_T *argvars;
15045 typval_T *rettv;
15046{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015047 pos_T match_pos;
15048 int lnum = 0;
15049 int col = 0;
15050
15051 rettv->vval.v_number = 0;
15052
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015053 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015054 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015055
15056 if (searchpair_cmn(argvars, &match_pos) > 0)
15057 {
15058 lnum = match_pos.lnum;
15059 col = match_pos.col;
15060 }
15061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015062 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15063 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015064}
15065
15066/*
15067 * Search for a start/middle/end thing.
15068 * Used by searchpair(), see its documentation for the details.
15069 * Returns 0 or -1 for no match,
15070 */
15071 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015072do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15073 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015074 char_u *spat; /* start pattern */
15075 char_u *mpat; /* middle pattern */
15076 char_u *epat; /* end pattern */
15077 int dir; /* BACKWARD or FORWARD */
15078 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015079 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015080 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015081 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015082 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015083{
15084 char_u *save_cpo;
15085 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15086 long retval = 0;
15087 pos_T pos;
15088 pos_T firstpos;
15089 pos_T foundpos;
15090 pos_T save_cursor;
15091 pos_T save_pos;
15092 int n;
15093 int r;
15094 int nest = 1;
15095 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015096 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015097 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015098
15099 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15100 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015101 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015102
Bram Moolenaar76929292008-01-06 19:07:36 +000015103#ifdef FEAT_RELTIME
15104 /* Set the time limit, if there is one. */
15105 profile_setlimit(time_limit, &tm);
15106#endif
15107
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015108 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15109 * start/middle/end (pat3, for the top pair). */
15110 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15111 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15112 if (pat2 == NULL || pat3 == NULL)
15113 goto theend;
15114 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15115 if (*mpat == NUL)
15116 STRCPY(pat3, pat2);
15117 else
15118 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15119 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015120 if (flags & SP_START)
15121 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015122
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 save_cursor = curwin->w_cursor;
15124 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015125 clearpos(&firstpos);
15126 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 pat = pat3;
15128 for (;;)
15129 {
15130 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015131 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15133 /* didn't find it or found the first match again: FAIL */
15134 break;
15135
15136 if (firstpos.lnum == 0)
15137 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015138 if (equalpos(pos, foundpos))
15139 {
15140 /* Found the same position again. Can happen with a pattern that
15141 * has "\zs" at the end and searching backwards. Advance one
15142 * character and try again. */
15143 if (dir == BACKWARD)
15144 decl(&pos);
15145 else
15146 incl(&pos);
15147 }
15148 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015150 /* clear the start flag to avoid getting stuck here */
15151 options &= ~SEARCH_START;
15152
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 /* If the skip pattern matches, ignore this match. */
15154 if (*skip != NUL)
15155 {
15156 save_pos = curwin->w_cursor;
15157 curwin->w_cursor = pos;
15158 r = eval_to_bool(skip, &err, NULL, FALSE);
15159 curwin->w_cursor = save_pos;
15160 if (err)
15161 {
15162 /* Evaluating {skip} caused an error, break here. */
15163 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015164 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 break;
15166 }
15167 if (r)
15168 continue;
15169 }
15170
15171 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15172 {
15173 /* Found end when searching backwards or start when searching
15174 * forward: nested pair. */
15175 ++nest;
15176 pat = pat2; /* nested, don't search for middle */
15177 }
15178 else
15179 {
15180 /* Found end when searching forward or start when searching
15181 * backward: end of (nested) pair; or found middle in outer pair. */
15182 if (--nest == 1)
15183 pat = pat3; /* outer level, search for middle */
15184 }
15185
15186 if (nest == 0)
15187 {
15188 /* Found the match: return matchcount or line number. */
15189 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015190 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015192 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015193 if (flags & SP_SETPCMARK)
15194 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 curwin->w_cursor = pos;
15196 if (!(flags & SP_REPEAT))
15197 break;
15198 nest = 1; /* search for next unmatched */
15199 }
15200 }
15201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015202 if (match_pos != NULL)
15203 {
15204 /* Store the match cursor position */
15205 match_pos->lnum = curwin->w_cursor.lnum;
15206 match_pos->col = curwin->w_cursor.col + 1;
15207 }
15208
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015210 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 curwin->w_cursor = save_cursor;
15212
15213theend:
15214 vim_free(pat2);
15215 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015216 if (p_cpo == empty_option)
15217 p_cpo = save_cpo;
15218 else
15219 /* Darn, evaluating the {skip} expression changed the value. */
15220 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015221
15222 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223}
15224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015225/*
15226 * "searchpos()" function
15227 */
15228 static void
15229f_searchpos(argvars, rettv)
15230 typval_T *argvars;
15231 typval_T *rettv;
15232{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015233 pos_T match_pos;
15234 int lnum = 0;
15235 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015236 int n;
15237 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015238
15239 rettv->vval.v_number = 0;
15240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015241 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015242 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015243
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015244 n = search_cmn(argvars, &match_pos, &flags);
15245 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015246 {
15247 lnum = match_pos.lnum;
15248 col = match_pos.col;
15249 }
15250
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015251 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15252 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015253 if (flags & SP_SUBPAT)
15254 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015255}
15256
15257
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258/*ARGSUSED*/
15259 static void
15260f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015261 typval_T *argvars;
15262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015264#ifdef FEAT_CLIENTSERVER
15265 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 char_u *server = get_tv_string_chk(&argvars[0]);
15267 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268
Bram Moolenaar0d660222005-01-07 21:51:51 +000015269 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015270 if (server == NULL || reply == NULL)
15271 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272 if (check_restricted() || check_secure())
15273 return;
15274# ifdef FEAT_X11
15275 if (check_connection() == FAIL)
15276 return;
15277# endif
15278
15279 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 EMSG(_("E258: Unable to send to client"));
15282 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 rettv->vval.v_number = 0;
15285#else
15286 rettv->vval.v_number = -1;
15287#endif
15288}
15289
15290/*ARGSUSED*/
15291 static void
15292f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *argvars;
15294 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015295{
15296 char_u *r = NULL;
15297
15298#ifdef FEAT_CLIENTSERVER
15299# ifdef WIN32
15300 r = serverGetVimNames();
15301# else
15302 make_connection();
15303 if (X_DISPLAY != NULL)
15304 r = serverGetVimNames(X_DISPLAY);
15305# endif
15306#endif
15307 rettv->v_type = VAR_STRING;
15308 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309}
15310
15311/*
15312 * "setbufvar()" function
15313 */
15314/*ARGSUSED*/
15315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 typval_T *argvars;
15318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319{
15320 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 char_u nbuf[NUMBUFLEN];
15325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 rettv->vval.v_number = 0;
15327
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 if (check_restricted() || check_secure())
15329 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015330 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15331 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 varp = &argvars[2];
15334
15335 if (buf != NULL && varname != NULL && varp != NULL)
15336 {
15337 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339
15340 if (*varname == '&')
15341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015342 long numval;
15343 char_u *strval;
15344 int error = FALSE;
15345
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015347 numval = get_tv_number_chk(varp, &error);
15348 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015349 if (!error && strval != NULL)
15350 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 }
15352 else
15353 {
15354 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15355 if (bufvarname != NULL)
15356 {
15357 STRCPY(bufvarname, "b:");
15358 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015359 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 vim_free(bufvarname);
15361 }
15362 }
15363
15364 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367}
15368
15369/*
15370 * "setcmdpos()" function
15371 */
15372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015373f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015374 typval_T *argvars;
15375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015377 int pos = (int)get_tv_number(&argvars[0]) - 1;
15378
15379 if (pos >= 0)
15380 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381}
15382
15383/*
15384 * "setline()" function
15385 */
15386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015387f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 typval_T *argvars;
15389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390{
15391 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015392 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015393 list_T *l = NULL;
15394 listitem_T *li = NULL;
15395 long added = 0;
15396 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015398 lnum = get_tv_lnum(&argvars[0]);
15399 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015401 l = argvars[1].vval.v_list;
15402 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015404 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015405 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015406
15407 rettv->vval.v_number = 0; /* OK */
15408 for (;;)
15409 {
15410 if (l != NULL)
15411 {
15412 /* list argument, get next string */
15413 if (li == NULL)
15414 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015416 li = li->li_next;
15417 }
15418
15419 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015420 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015421 break;
15422 if (lnum <= curbuf->b_ml.ml_line_count)
15423 {
15424 /* existing line, replace it */
15425 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15426 {
15427 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015428 if (lnum == curwin->w_cursor.lnum)
15429 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015430 rettv->vval.v_number = 0; /* OK */
15431 }
15432 }
15433 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15434 {
15435 /* lnum is one past the last line, append the line */
15436 ++added;
15437 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15438 rettv->vval.v_number = 0; /* OK */
15439 }
15440
15441 if (l == NULL) /* only one string argument */
15442 break;
15443 ++lnum;
15444 }
15445
15446 if (added > 0)
15447 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448}
15449
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015450static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15451
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015453 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015454 */
15455/*ARGSUSED*/
15456 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015457set_qf_ll_list(wp, list_arg, action_arg, rettv)
15458 win_T *wp;
15459 typval_T *list_arg;
15460 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015461 typval_T *rettv;
15462{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015463#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015464 char_u *act;
15465 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015466#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015467
Bram Moolenaar2641f772005-03-25 21:58:17 +000015468 rettv->vval.v_number = -1;
15469
15470#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015471 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015472 EMSG(_(e_listreq));
15473 else
15474 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015475 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015476
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015477 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015478 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015479 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015480 if (act == NULL)
15481 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015482 if (*act == 'a' || *act == 'r')
15483 action = *act;
15484 }
15485
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015486 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015487 rettv->vval.v_number = 0;
15488 }
15489#endif
15490}
15491
15492/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015493 * "setloclist()" function
15494 */
15495/*ARGSUSED*/
15496 static void
15497f_setloclist(argvars, rettv)
15498 typval_T *argvars;
15499 typval_T *rettv;
15500{
15501 win_T *win;
15502
15503 rettv->vval.v_number = -1;
15504
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015505 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015506 if (win != NULL)
15507 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15508}
15509
15510/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015511 * "setmatches()" function
15512 */
15513 static void
15514f_setmatches(argvars, rettv)
15515 typval_T *argvars;
15516 typval_T *rettv;
15517{
15518#ifdef FEAT_SEARCH_EXTRA
15519 list_T *l;
15520 listitem_T *li;
15521 dict_T *d;
15522
15523 rettv->vval.v_number = -1;
15524 if (argvars[0].v_type != VAR_LIST)
15525 {
15526 EMSG(_(e_listreq));
15527 return;
15528 }
15529 if ((l = argvars[0].vval.v_list) != NULL)
15530 {
15531
15532 /* To some extent make sure that we are dealing with a list from
15533 * "getmatches()". */
15534 li = l->lv_first;
15535 while (li != NULL)
15536 {
15537 if (li->li_tv.v_type != VAR_DICT
15538 || (d = li->li_tv.vval.v_dict) == NULL)
15539 {
15540 EMSG(_(e_invarg));
15541 return;
15542 }
15543 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15544 && dict_find(d, (char_u *)"pattern", -1) != NULL
15545 && dict_find(d, (char_u *)"priority", -1) != NULL
15546 && dict_find(d, (char_u *)"id", -1) != NULL))
15547 {
15548 EMSG(_(e_invarg));
15549 return;
15550 }
15551 li = li->li_next;
15552 }
15553
15554 clear_matches(curwin);
15555 li = l->lv_first;
15556 while (li != NULL)
15557 {
15558 d = li->li_tv.vval.v_dict;
15559 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15560 get_dict_string(d, (char_u *)"pattern", FALSE),
15561 (int)get_dict_number(d, (char_u *)"priority"),
15562 (int)get_dict_number(d, (char_u *)"id"));
15563 li = li->li_next;
15564 }
15565 rettv->vval.v_number = 0;
15566 }
15567#endif
15568}
15569
15570/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015571 * "setpos()" function
15572 */
15573/*ARGSUSED*/
15574 static void
15575f_setpos(argvars, rettv)
15576 typval_T *argvars;
15577 typval_T *rettv;
15578{
15579 pos_T pos;
15580 int fnum;
15581 char_u *name;
15582
Bram Moolenaar08250432008-02-13 11:42:46 +000015583 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015584 name = get_tv_string_chk(argvars);
15585 if (name != NULL)
15586 {
15587 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15588 {
15589 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015590 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015591 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015592 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015593 if (fnum == curbuf->b_fnum)
15594 {
15595 curwin->w_cursor = pos;
15596 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015597 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015598 }
15599 else
15600 EMSG(_(e_invarg));
15601 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015602 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15603 {
15604 /* set mark */
15605 if (setmark_pos(name[1], &pos, fnum) == OK)
15606 rettv->vval.v_number = 0;
15607 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015608 else
15609 EMSG(_(e_invarg));
15610 }
15611 }
15612}
15613
15614/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015615 * "setqflist()" function
15616 */
15617/*ARGSUSED*/
15618 static void
15619f_setqflist(argvars, rettv)
15620 typval_T *argvars;
15621 typval_T *rettv;
15622{
15623 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15624}
15625
15626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 * "setreg()" function
15628 */
15629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 typval_T *argvars;
15632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633{
15634 int regname;
15635 char_u *strregname;
15636 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 int append;
15639 char_u yank_type;
15640 long block_len;
15641
15642 block_len = -1;
15643 yank_type = MAUTO;
15644 append = FALSE;
15645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015647 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015649 if (strregname == NULL)
15650 return; /* type error; errmsg already given */
15651 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 if (regname == 0 || regname == '@')
15653 regname = '"';
15654 else if (regname == '=')
15655 return;
15656
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015659 stropt = get_tv_string_chk(&argvars[2]);
15660 if (stropt == NULL)
15661 return; /* type error */
15662 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 switch (*stropt)
15664 {
15665 case 'a': case 'A': /* append */
15666 append = TRUE;
15667 break;
15668 case 'v': case 'c': /* character-wise selection */
15669 yank_type = MCHAR;
15670 break;
15671 case 'V': case 'l': /* line-wise selection */
15672 yank_type = MLINE;
15673 break;
15674#ifdef FEAT_VISUAL
15675 case 'b': case Ctrl_V: /* block-wise selection */
15676 yank_type = MBLOCK;
15677 if (VIM_ISDIGIT(stropt[1]))
15678 {
15679 ++stropt;
15680 block_len = getdigits(&stropt) - 1;
15681 --stropt;
15682 }
15683 break;
15684#endif
15685 }
15686 }
15687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015688 strval = get_tv_string_chk(&argvars[1]);
15689 if (strval != NULL)
15690 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693}
15694
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015695/*
15696 * "settabwinvar()" function
15697 */
15698 static void
15699f_settabwinvar(argvars, rettv)
15700 typval_T *argvars;
15701 typval_T *rettv;
15702{
15703 setwinvar(argvars, rettv, 1);
15704}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705
15706/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015707 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015710f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015711 typval_T *argvars;
15712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015714 setwinvar(argvars, rettv, 0);
15715}
15716
15717/*
15718 * "setwinvar()" and "settabwinvar()" functions
15719 */
15720 static void
15721setwinvar(argvars, rettv, off)
15722 typval_T *argvars;
15723 typval_T *rettv;
15724 int off;
15725{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 win_T *win;
15727#ifdef FEAT_WINDOWS
15728 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015729 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730#endif
15731 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015732 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015734 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015736 rettv->vval.v_number = 0;
15737
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 if (check_restricted() || check_secure())
15739 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015740
15741#ifdef FEAT_WINDOWS
15742 if (off == 1)
15743 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15744 else
15745 tp = curtab;
15746#endif
15747 win = find_win_by_nr(&argvars[off], tp);
15748 varname = get_tv_string_chk(&argvars[off + 1]);
15749 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750
15751 if (win != NULL && varname != NULL && varp != NULL)
15752 {
15753#ifdef FEAT_WINDOWS
15754 /* set curwin to be our win, temporarily */
15755 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015756 save_curtab = curtab;
15757 goto_tabpage_tp(tp);
15758 if (!win_valid(win))
15759 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 curwin = win;
15761 curbuf = curwin->w_buffer;
15762#endif
15763
15764 if (*varname == '&')
15765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015766 long numval;
15767 char_u *strval;
15768 int error = FALSE;
15769
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015771 numval = get_tv_number_chk(varp, &error);
15772 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015773 if (!error && strval != NULL)
15774 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 }
15776 else
15777 {
15778 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15779 if (winvarname != NULL)
15780 {
15781 STRCPY(winvarname, "w:");
15782 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015783 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 vim_free(winvarname);
15785 }
15786 }
15787
15788#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015789 /* Restore current tabpage and window, if still valid (autocomands can
15790 * make them invalid). */
15791 if (valid_tabpage(save_curtab))
15792 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 if (win_valid(save_curwin))
15794 {
15795 curwin = save_curwin;
15796 curbuf = curwin->w_buffer;
15797 }
15798#endif
15799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800}
15801
15802/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015803 * "shellescape({string})" function
15804 */
15805 static void
15806f_shellescape(argvars, rettv)
15807 typval_T *argvars;
15808 typval_T *rettv;
15809{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015810 rettv->vval.v_string = vim_strsave_shellescape(
15811 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015812 rettv->v_type = VAR_STRING;
15813}
15814
15815/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 */
15818 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 typval_T *argvars;
15821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015823 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824
Bram Moolenaar0d660222005-01-07 21:51:51 +000015825 p = get_tv_string(&argvars[0]);
15826 rettv->vval.v_string = vim_strsave(p);
15827 simplify_filename(rettv->vval.v_string); /* simplify in place */
15828 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829}
15830
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015831#ifdef FEAT_FLOAT
15832/*
15833 * "sin()" function
15834 */
15835 static void
15836f_sin(argvars, rettv)
15837 typval_T *argvars;
15838 typval_T *rettv;
15839{
15840 float_T f;
15841
15842 rettv->v_type = VAR_FLOAT;
15843 if (get_float_arg(argvars, &f) == OK)
15844 rettv->vval.v_float = sin(f);
15845 else
15846 rettv->vval.v_float = 0.0;
15847}
15848#endif
15849
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850static int
15851#ifdef __BORLANDC__
15852 _RTLENTRYF
15853#endif
15854 item_compare __ARGS((const void *s1, const void *s2));
15855static int
15856#ifdef __BORLANDC__
15857 _RTLENTRYF
15858#endif
15859 item_compare2 __ARGS((const void *s1, const void *s2));
15860
15861static int item_compare_ic;
15862static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864#define ITEM_COMPARE_FAIL 999
15865
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869 static int
15870#ifdef __BORLANDC__
15871_RTLENTRYF
15872#endif
15873item_compare(s1, s2)
15874 const void *s1;
15875 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015877 char_u *p1, *p2;
15878 char_u *tofree1, *tofree2;
15879 int res;
15880 char_u numbuf1[NUMBUFLEN];
15881 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015883 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15884 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015885 if (p1 == NULL)
15886 p1 = (char_u *)"";
15887 if (p2 == NULL)
15888 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889 if (item_compare_ic)
15890 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892 res = STRCMP(p1, p2);
15893 vim_free(tofree1);
15894 vim_free(tofree2);
15895 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896}
15897
15898 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015899#ifdef __BORLANDC__
15900_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902item_compare2(s1, s2)
15903 const void *s1;
15904 const void *s2;
15905{
15906 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015908 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015909 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015911 /* shortcut after failure in previous call; compare all items equal */
15912 if (item_compare_func_err)
15913 return 0;
15914
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015915 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15916 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015917 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15918 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919
15920 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015921 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015922 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015923 clear_tv(&argv[0]);
15924 clear_tv(&argv[1]);
15925
15926 if (res == FAIL)
15927 res = ITEM_COMPARE_FAIL;
15928 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15930 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015931 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932 clear_tv(&rettv);
15933 return res;
15934}
15935
15936/*
15937 * "sort({list})" function
15938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015941 typval_T *argvars;
15942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943{
Bram Moolenaar33570922005-01-25 22:26:29 +000015944 list_T *l;
15945 listitem_T *li;
15946 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947 long len;
15948 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950 rettv->vval.v_number = 0;
15951 if (argvars[0].v_type != VAR_LIST)
15952 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953 else
15954 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015956 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015957 return;
15958 rettv->vval.v_list = l;
15959 rettv->v_type = VAR_LIST;
15960 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961
Bram Moolenaar0d660222005-01-07 21:51:51 +000015962 len = list_len(l);
15963 if (len <= 1)
15964 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965
Bram Moolenaar0d660222005-01-07 21:51:51 +000015966 item_compare_ic = FALSE;
15967 item_compare_func = NULL;
15968 if (argvars[1].v_type != VAR_UNKNOWN)
15969 {
15970 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015971 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015972 else
15973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015974 int error = FALSE;
15975
15976 i = get_tv_number_chk(&argvars[1], &error);
15977 if (error)
15978 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979 if (i == 1)
15980 item_compare_ic = TRUE;
15981 else
15982 item_compare_func = get_tv_string(&argvars[1]);
15983 }
15984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985
Bram Moolenaar0d660222005-01-07 21:51:51 +000015986 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015987 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015988 if (ptrs == NULL)
15989 return;
15990 i = 0;
15991 for (li = l->lv_first; li != NULL; li = li->li_next)
15992 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015994 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015995 /* test the compare function */
15996 if (item_compare_func != NULL
15997 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15998 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015999 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016001 {
16002 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016003 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016004 item_compare_func == NULL ? item_compare : item_compare2);
16005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 if (!item_compare_func_err)
16007 {
16008 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016009 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016010 l->lv_len = 0;
16011 for (i = 0; i < len; ++i)
16012 list_append(l, ptrs[i]);
16013 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016014 }
16015
16016 vim_free(ptrs);
16017 }
16018}
16019
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016020/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016021 * "soundfold({word})" function
16022 */
16023 static void
16024f_soundfold(argvars, rettv)
16025 typval_T *argvars;
16026 typval_T *rettv;
16027{
16028 char_u *s;
16029
16030 rettv->v_type = VAR_STRING;
16031 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016032#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016033 rettv->vval.v_string = eval_soundfold(s);
16034#else
16035 rettv->vval.v_string = vim_strsave(s);
16036#endif
16037}
16038
16039/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016040 * "spellbadword()" function
16041 */
16042/* ARGSUSED */
16043 static void
16044f_spellbadword(argvars, rettv)
16045 typval_T *argvars;
16046 typval_T *rettv;
16047{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016048 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016049 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016051
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016052 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016053 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016054
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016055#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016056 if (argvars[0].v_type == VAR_UNKNOWN)
16057 {
16058 /* Find the start and length of the badly spelled word. */
16059 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16060 if (len != 0)
16061 word = ml_get_cursor();
16062 }
16063 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16064 {
16065 char_u *str = get_tv_string_chk(&argvars[0]);
16066 int capcol = -1;
16067
16068 if (str != NULL)
16069 {
16070 /* Check the argument for spelling. */
16071 while (*str != NUL)
16072 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016073 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016074 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016075 {
16076 word = str;
16077 break;
16078 }
16079 str += len;
16080 }
16081 }
16082 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016083#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016084
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 list_append_string(rettv->vval.v_list, word, len);
16086 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016087 attr == HLF_SPB ? "bad" :
16088 attr == HLF_SPR ? "rare" :
16089 attr == HLF_SPL ? "local" :
16090 attr == HLF_SPC ? "caps" :
16091 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016092}
16093
16094/*
16095 * "spellsuggest()" function
16096 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016097/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016098 static void
16099f_spellsuggest(argvars, rettv)
16100 typval_T *argvars;
16101 typval_T *rettv;
16102{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016103#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016104 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016105 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016106 int maxcount;
16107 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016108 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016109 listitem_T *li;
16110 int need_capital = FALSE;
16111#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016112
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016113 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016114 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016115
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016116#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016117 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16118 {
16119 str = get_tv_string(&argvars[0]);
16120 if (argvars[1].v_type != VAR_UNKNOWN)
16121 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016122 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016123 if (maxcount <= 0)
16124 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016125 if (argvars[2].v_type != VAR_UNKNOWN)
16126 {
16127 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16128 if (typeerr)
16129 return;
16130 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016131 }
16132 else
16133 maxcount = 25;
16134
Bram Moolenaar4770d092006-01-12 23:22:24 +000016135 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016136
16137 for (i = 0; i < ga.ga_len; ++i)
16138 {
16139 str = ((char_u **)ga.ga_data)[i];
16140
16141 li = listitem_alloc();
16142 if (li == NULL)
16143 vim_free(str);
16144 else
16145 {
16146 li->li_tv.v_type = VAR_STRING;
16147 li->li_tv.v_lock = 0;
16148 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016149 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016150 }
16151 }
16152 ga_clear(&ga);
16153 }
16154#endif
16155}
16156
Bram Moolenaar0d660222005-01-07 21:51:51 +000016157 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016158f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *argvars;
16160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161{
16162 char_u *str;
16163 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016164 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 regmatch_T regmatch;
16166 char_u patbuf[NUMBUFLEN];
16167 char_u *save_cpo;
16168 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016170 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016171 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172
16173 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16174 save_cpo = p_cpo;
16175 p_cpo = (char_u *)"";
16176
16177 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016178 if (argvars[1].v_type != VAR_UNKNOWN)
16179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16181 if (pat == NULL)
16182 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016183 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016184 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016185 }
16186 if (pat == NULL || *pat == NUL)
16187 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016189 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016191 if (typeerr)
16192 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16195 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016198 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016200 if (*str == NUL)
16201 match = FALSE; /* empty item at the end */
16202 else
16203 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204 if (match)
16205 end = regmatch.startp[0];
16206 else
16207 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016208 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16209 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016210 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016211 if (list_append_string(rettv->vval.v_list, str,
16212 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214 }
16215 if (!match)
16216 break;
16217 /* Advance to just after the match. */
16218 if (regmatch.endp[0] > str)
16219 col = 0;
16220 else
16221 {
16222 /* Don't get stuck at the same match. */
16223#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016224 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225#else
16226 col = 1;
16227#endif
16228 }
16229 str = regmatch.endp[0];
16230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231
Bram Moolenaar0d660222005-01-07 21:51:51 +000016232 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236}
16237
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016238#ifdef FEAT_FLOAT
16239/*
16240 * "sqrt()" function
16241 */
16242 static void
16243f_sqrt(argvars, rettv)
16244 typval_T *argvars;
16245 typval_T *rettv;
16246{
16247 float_T f;
16248
16249 rettv->v_type = VAR_FLOAT;
16250 if (get_float_arg(argvars, &f) == OK)
16251 rettv->vval.v_float = sqrt(f);
16252 else
16253 rettv->vval.v_float = 0.0;
16254}
16255
16256/*
16257 * "str2float()" function
16258 */
16259 static void
16260f_str2float(argvars, rettv)
16261 typval_T *argvars;
16262 typval_T *rettv;
16263{
16264 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16265
16266 if (*p == '+')
16267 p = skipwhite(p + 1);
16268 (void)string2float(p, &rettv->vval.v_float);
16269 rettv->v_type = VAR_FLOAT;
16270}
16271#endif
16272
Bram Moolenaar2c932302006-03-18 21:42:09 +000016273/*
16274 * "str2nr()" function
16275 */
16276 static void
16277f_str2nr(argvars, rettv)
16278 typval_T *argvars;
16279 typval_T *rettv;
16280{
16281 int base = 10;
16282 char_u *p;
16283 long n;
16284
16285 if (argvars[1].v_type != VAR_UNKNOWN)
16286 {
16287 base = get_tv_number(&argvars[1]);
16288 if (base != 8 && base != 10 && base != 16)
16289 {
16290 EMSG(_(e_invarg));
16291 return;
16292 }
16293 }
16294
16295 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016296 if (*p == '+')
16297 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016298 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16299 rettv->vval.v_number = n;
16300}
16301
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302#ifdef HAVE_STRFTIME
16303/*
16304 * "strftime({format}[, {time}])" function
16305 */
16306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016307f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 typval_T *argvars;
16309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
16311 char_u result_buf[256];
16312 struct tm *curtime;
16313 time_t seconds;
16314 char_u *p;
16315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016319 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 seconds = time(NULL);
16321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016322 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 curtime = localtime(&seconds);
16324 /* MSVC returns NULL for an invalid value of seconds. */
16325 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016326 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 else
16328 {
16329# ifdef FEAT_MBYTE
16330 vimconv_T conv;
16331 char_u *enc;
16332
16333 conv.vc_type = CONV_NONE;
16334 enc = enc_locale();
16335 convert_setup(&conv, p_enc, enc);
16336 if (conv.vc_type != CONV_NONE)
16337 p = string_convert(&conv, p, NULL);
16338# endif
16339 if (p != NULL)
16340 (void)strftime((char *)result_buf, sizeof(result_buf),
16341 (char *)p, curtime);
16342 else
16343 result_buf[0] = NUL;
16344
16345# ifdef FEAT_MBYTE
16346 if (conv.vc_type != CONV_NONE)
16347 vim_free(p);
16348 convert_setup(&conv, enc, p_enc);
16349 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 else
16352# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016353 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
16355# ifdef FEAT_MBYTE
16356 /* Release conversion descriptors */
16357 convert_setup(&conv, NULL, NULL);
16358 vim_free(enc);
16359# endif
16360 }
16361}
16362#endif
16363
16364/*
16365 * "stridx()" function
16366 */
16367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 typval_T *argvars;
16370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371{
16372 char_u buf[NUMBUFLEN];
16373 char_u *needle;
16374 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016375 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016379 needle = get_tv_string_chk(&argvars[1]);
16380 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 if (needle == NULL || haystack == NULL)
16383 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 if (argvars[2].v_type != VAR_UNKNOWN)
16386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016387 int error = FALSE;
16388
16389 start_idx = get_tv_number_chk(&argvars[2], &error);
16390 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016392 if (start_idx >= 0)
16393 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 }
16395
16396 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16397 if (pos != NULL)
16398 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399}
16400
16401/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016402 * "string()" function
16403 */
16404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016405f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016406 typval_T *argvars;
16407 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016408{
16409 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016410 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016413 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016414 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016415 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016416 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417}
16418
16419/*
16420 * "strlen()" function
16421 */
16422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 typval_T *argvars;
16425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016427 rettv->vval.v_number = (varnumber_T)(STRLEN(
16428 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429}
16430
16431/*
16432 * "strpart()" function
16433 */
16434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016435f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 typval_T *argvars;
16437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438{
16439 char_u *p;
16440 int n;
16441 int len;
16442 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016445 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 slen = (int)STRLEN(p);
16447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 n = get_tv_number_chk(&argvars[1], &error);
16449 if (error)
16450 len = 0;
16451 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 else
16454 len = slen - n; /* default len: all bytes that are available. */
16455
16456 /*
16457 * Only return the overlap between the specified part and the actual
16458 * string.
16459 */
16460 if (n < 0)
16461 {
16462 len += n;
16463 n = 0;
16464 }
16465 else if (n > slen)
16466 n = slen;
16467 if (len < 0)
16468 len = 0;
16469 else if (n + len > slen)
16470 len = slen - n;
16471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016472 rettv->v_type = VAR_STRING;
16473 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474}
16475
16476/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 * "strridx()" function
16478 */
16479 static void
16480f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016481 typval_T *argvars;
16482 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483{
16484 char_u buf[NUMBUFLEN];
16485 char_u *needle;
16486 char_u *haystack;
16487 char_u *rest;
16488 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016489 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 needle = get_tv_string_chk(&argvars[1]);
16492 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016493
16494 rettv->vval.v_number = -1;
16495 if (needle == NULL || haystack == NULL)
16496 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016497
16498 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016499 if (argvars[2].v_type != VAR_UNKNOWN)
16500 {
16501 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016503 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016504 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016505 }
16506 else
16507 end_idx = haystack_len;
16508
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016510 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016512 lastmatch = haystack + end_idx;
16513 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016515 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516 for (rest = haystack; *rest != '\0'; ++rest)
16517 {
16518 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016519 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520 break;
16521 lastmatch = rest;
16522 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016523 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524
16525 if (lastmatch == NULL)
16526 rettv->vval.v_number = -1;
16527 else
16528 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16529}
16530
16531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 * "strtrans()" function
16533 */
16534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016535f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016536 typval_T *argvars;
16537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 rettv->v_type = VAR_STRING;
16540 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541}
16542
16543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544 * "submatch()" function
16545 */
16546 static void
16547f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016548 typval_T *argvars;
16549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016550{
16551 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 rettv->vval.v_string =
16553 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554}
16555
16556/*
16557 * "substitute()" function
16558 */
16559 static void
16560f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016561 typval_T *argvars;
16562 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563{
16564 char_u patbuf[NUMBUFLEN];
16565 char_u subbuf[NUMBUFLEN];
16566 char_u flagsbuf[NUMBUFLEN];
16567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016568 char_u *str = get_tv_string_chk(&argvars[0]);
16569 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16570 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16571 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16572
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016574 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16575 rettv->vval.v_string = NULL;
16576 else
16577 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016578}
16579
16580/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016581 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 */
16583/*ARGSUSED*/
16584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016585f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016586 typval_T *argvars;
16587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588{
16589 int id = 0;
16590#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016591 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 long col;
16593 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016594 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016596 lnum = get_tv_lnum(argvars); /* -1 on type error */
16597 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16598 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016600 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016601 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016602 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603#endif
16604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016605 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606}
16607
16608/*
16609 * "synIDattr(id, what [, mode])" function
16610 */
16611/*ARGSUSED*/
16612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 typval_T *argvars;
16615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616{
16617 char_u *p = NULL;
16618#ifdef FEAT_SYN_HL
16619 int id;
16620 char_u *what;
16621 char_u *mode;
16622 char_u modebuf[NUMBUFLEN];
16623 int modec;
16624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016625 id = get_tv_number(&argvars[0]);
16626 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016627 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 modec = TOLOWER_ASC(mode[0]);
16631 if (modec != 't' && modec != 'c'
16632#ifdef FEAT_GUI
16633 && modec != 'g'
16634#endif
16635 )
16636 modec = 0; /* replace invalid with current */
16637 }
16638 else
16639 {
16640#ifdef FEAT_GUI
16641 if (gui.in_use)
16642 modec = 'g';
16643 else
16644#endif
16645 if (t_colors > 1)
16646 modec = 'c';
16647 else
16648 modec = 't';
16649 }
16650
16651
16652 switch (TOLOWER_ASC(what[0]))
16653 {
16654 case 'b':
16655 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16656 p = highlight_color(id, what, modec);
16657 else /* bold */
16658 p = highlight_has_attr(id, HL_BOLD, modec);
16659 break;
16660
16661 case 'f': /* fg[#] */
16662 p = highlight_color(id, what, modec);
16663 break;
16664
16665 case 'i':
16666 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16667 p = highlight_has_attr(id, HL_INVERSE, modec);
16668 else /* italic */
16669 p = highlight_has_attr(id, HL_ITALIC, modec);
16670 break;
16671
16672 case 'n': /* name */
16673 p = get_highlight_name(NULL, id - 1);
16674 break;
16675
16676 case 'r': /* reverse */
16677 p = highlight_has_attr(id, HL_INVERSE, modec);
16678 break;
16679
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016680 case 's':
16681 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16682 p = highlight_color(id, what, modec);
16683 else /* standout */
16684 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 break;
16686
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016687 case 'u':
16688 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16689 /* underline */
16690 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16691 else
16692 /* undercurl */
16693 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 break;
16695 }
16696
16697 if (p != NULL)
16698 p = vim_strsave(p);
16699#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700 rettv->v_type = VAR_STRING;
16701 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702}
16703
16704/*
16705 * "synIDtrans(id)" function
16706 */
16707/*ARGSUSED*/
16708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016710 typval_T *argvars;
16711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712{
16713 int id;
16714
16715#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717
16718 if (id > 0)
16719 id = syn_get_final_id(id);
16720 else
16721#endif
16722 id = 0;
16723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016724 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725}
16726
16727/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016728 * "synstack(lnum, col)" function
16729 */
16730/*ARGSUSED*/
16731 static void
16732f_synstack(argvars, rettv)
16733 typval_T *argvars;
16734 typval_T *rettv;
16735{
16736#ifdef FEAT_SYN_HL
16737 long lnum;
16738 long col;
16739 int i;
16740 int id;
16741#endif
16742
16743 rettv->v_type = VAR_LIST;
16744 rettv->vval.v_list = NULL;
16745
16746#ifdef FEAT_SYN_HL
16747 lnum = get_tv_lnum(argvars); /* -1 on type error */
16748 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16749
16750 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016751 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016752 && rettv_list_alloc(rettv) != FAIL)
16753 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016754 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016755 for (i = 0; ; ++i)
16756 {
16757 id = syn_get_stack_item(i);
16758 if (id < 0)
16759 break;
16760 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16761 break;
16762 }
16763 }
16764#endif
16765}
16766
16767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 * "system()" function
16769 */
16770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016771f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016772 typval_T *argvars;
16773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016775 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016777 char_u *infile = NULL;
16778 char_u buf[NUMBUFLEN];
16779 int err = FALSE;
16780 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016782 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016783 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016784
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016786 {
16787 /*
16788 * Write the string to a temp file, to be used for input of the shell
16789 * command.
16790 */
16791 if ((infile = vim_tempname('i')) == NULL)
16792 {
16793 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016794 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016795 }
16796
16797 fd = mch_fopen((char *)infile, WRITEBIN);
16798 if (fd == NULL)
16799 {
16800 EMSG2(_(e_notopen), infile);
16801 goto done;
16802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016803 p = get_tv_string_buf_chk(&argvars[1], buf);
16804 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016805 {
16806 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016807 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016808 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016809 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16810 err = TRUE;
16811 if (fclose(fd) != 0)
16812 err = TRUE;
16813 if (err)
16814 {
16815 EMSG(_("E677: Error writing temp file"));
16816 goto done;
16817 }
16818 }
16819
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016820 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16821 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016822
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823#ifdef USE_CR
16824 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016825 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 {
16827 char_u *s;
16828
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016829 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 {
16831 if (*s == CAR)
16832 *s = NL;
16833 }
16834 }
16835#else
16836# ifdef USE_CRNL
16837 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016838 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 {
16840 char_u *s, *d;
16841
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016842 d = res;
16843 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 {
16845 if (s[0] == CAR && s[1] == NL)
16846 ++s;
16847 *d++ = *s;
16848 }
16849 *d = NUL;
16850 }
16851# endif
16852#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016853
16854done:
16855 if (infile != NULL)
16856 {
16857 mch_remove(infile);
16858 vim_free(infile);
16859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016860 rettv->v_type = VAR_STRING;
16861 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862}
16863
16864/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016865 * "tabpagebuflist()" function
16866 */
16867/* ARGSUSED */
16868 static void
16869f_tabpagebuflist(argvars, rettv)
16870 typval_T *argvars;
16871 typval_T *rettv;
16872{
16873#ifndef FEAT_WINDOWS
16874 rettv->vval.v_number = 0;
16875#else
16876 tabpage_T *tp;
16877 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016878
16879 if (argvars[0].v_type == VAR_UNKNOWN)
16880 wp = firstwin;
16881 else
16882 {
16883 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16884 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016885 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016886 }
16887 if (wp == NULL)
16888 rettv->vval.v_number = 0;
16889 else
16890 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016891 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016892 rettv->vval.v_number = 0;
16893 else
16894 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016895 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016896 if (list_append_number(rettv->vval.v_list,
16897 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016898 break;
16899 }
16900 }
16901#endif
16902}
16903
16904
16905/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016906 * "tabpagenr()" function
16907 */
16908/* ARGSUSED */
16909 static void
16910f_tabpagenr(argvars, rettv)
16911 typval_T *argvars;
16912 typval_T *rettv;
16913{
16914 int nr = 1;
16915#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016916 char_u *arg;
16917
16918 if (argvars[0].v_type != VAR_UNKNOWN)
16919 {
16920 arg = get_tv_string_chk(&argvars[0]);
16921 nr = 0;
16922 if (arg != NULL)
16923 {
16924 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016925 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016926 else
16927 EMSG2(_(e_invexpr2), arg);
16928 }
16929 }
16930 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016931 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016932#endif
16933 rettv->vval.v_number = nr;
16934}
16935
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016936
16937#ifdef FEAT_WINDOWS
16938static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16939
16940/*
16941 * Common code for tabpagewinnr() and winnr().
16942 */
16943 static int
16944get_winnr(tp, argvar)
16945 tabpage_T *tp;
16946 typval_T *argvar;
16947{
16948 win_T *twin;
16949 int nr = 1;
16950 win_T *wp;
16951 char_u *arg;
16952
16953 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16954 if (argvar->v_type != VAR_UNKNOWN)
16955 {
16956 arg = get_tv_string_chk(argvar);
16957 if (arg == NULL)
16958 nr = 0; /* type error; errmsg already given */
16959 else if (STRCMP(arg, "$") == 0)
16960 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16961 else if (STRCMP(arg, "#") == 0)
16962 {
16963 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16964 if (twin == NULL)
16965 nr = 0;
16966 }
16967 else
16968 {
16969 EMSG2(_(e_invexpr2), arg);
16970 nr = 0;
16971 }
16972 }
16973
16974 if (nr > 0)
16975 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16976 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016977 {
16978 if (wp == NULL)
16979 {
16980 /* didn't find it in this tabpage */
16981 nr = 0;
16982 break;
16983 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016984 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016985 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016986 return nr;
16987}
16988#endif
16989
16990/*
16991 * "tabpagewinnr()" function
16992 */
16993/* ARGSUSED */
16994 static void
16995f_tabpagewinnr(argvars, rettv)
16996 typval_T *argvars;
16997 typval_T *rettv;
16998{
16999 int nr = 1;
17000#ifdef FEAT_WINDOWS
17001 tabpage_T *tp;
17002
17003 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17004 if (tp == NULL)
17005 nr = 0;
17006 else
17007 nr = get_winnr(tp, &argvars[1]);
17008#endif
17009 rettv->vval.v_number = nr;
17010}
17011
17012
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017013/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017014 * "tagfiles()" function
17015 */
17016/*ARGSUSED*/
17017 static void
17018f_tagfiles(argvars, rettv)
17019 typval_T *argvars;
17020 typval_T *rettv;
17021{
17022 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017023 tagname_T tn;
17024 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017025
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017026 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017027 {
17028 rettv->vval.v_number = 0;
17029 return;
17030 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017031
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017032 for (first = TRUE; ; first = FALSE)
17033 if (get_tagfname(&tn, first, fname) == FAIL
17034 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017035 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017036 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017037}
17038
17039/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017040 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017041 */
17042 static void
17043f_taglist(argvars, rettv)
17044 typval_T *argvars;
17045 typval_T *rettv;
17046{
17047 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017048
17049 tag_pattern = get_tv_string(&argvars[0]);
17050
17051 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017052 if (*tag_pattern == NUL)
17053 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017054
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017055 if (rettv_list_alloc(rettv) == OK)
17056 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017057}
17058
17059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 * "tempname()" function
17061 */
17062/*ARGSUSED*/
17063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 typval_T *argvars;
17066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067{
17068 static int x = 'A';
17069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 rettv->v_type = VAR_STRING;
17071 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072
17073 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17074 * names. Skip 'I' and 'O', they are used for shell redirection. */
17075 do
17076 {
17077 if (x == 'Z')
17078 x = '0';
17079 else if (x == '9')
17080 x = 'A';
17081 else
17082 {
17083#ifdef EBCDIC
17084 if (x == 'I')
17085 x = 'J';
17086 else if (x == 'R')
17087 x = 'S';
17088 else
17089#endif
17090 ++x;
17091 }
17092 } while (x == 'I' || x == 'O');
17093}
17094
17095/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017096 * "test(list)" function: Just checking the walls...
17097 */
17098/*ARGSUSED*/
17099 static void
17100f_test(argvars, rettv)
17101 typval_T *argvars;
17102 typval_T *rettv;
17103{
17104 /* Used for unit testing. Change the code below to your liking. */
17105#if 0
17106 listitem_T *li;
17107 list_T *l;
17108 char_u *bad, *good;
17109
17110 if (argvars[0].v_type != VAR_LIST)
17111 return;
17112 l = argvars[0].vval.v_list;
17113 if (l == NULL)
17114 return;
17115 li = l->lv_first;
17116 if (li == NULL)
17117 return;
17118 bad = get_tv_string(&li->li_tv);
17119 li = li->li_next;
17120 if (li == NULL)
17121 return;
17122 good = get_tv_string(&li->li_tv);
17123 rettv->vval.v_number = test_edit_score(bad, good);
17124#endif
17125}
17126
17127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 * "tolower(string)" function
17129 */
17130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 typval_T *argvars;
17133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134{
17135 char_u *p;
17136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 p = vim_strsave(get_tv_string(&argvars[0]));
17138 rettv->v_type = VAR_STRING;
17139 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140
17141 if (p != NULL)
17142 while (*p != NUL)
17143 {
17144#ifdef FEAT_MBYTE
17145 int l;
17146
17147 if (enc_utf8)
17148 {
17149 int c, lc;
17150
17151 c = utf_ptr2char(p);
17152 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017153 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 /* TODO: reallocate string when byte count changes. */
17155 if (utf_char2len(lc) == l)
17156 utf_char2bytes(lc, p);
17157 p += l;
17158 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017159 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 p += l; /* skip multi-byte character */
17161 else
17162#endif
17163 {
17164 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17165 ++p;
17166 }
17167 }
17168}
17169
17170/*
17171 * "toupper(string)" function
17172 */
17173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 typval_T *argvars;
17176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180}
17181
17182/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017183 * "tr(string, fromstr, tostr)" function
17184 */
17185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017187 typval_T *argvars;
17188 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017189{
17190 char_u *instr;
17191 char_u *fromstr;
17192 char_u *tostr;
17193 char_u *p;
17194#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017195 int inlen;
17196 int fromlen;
17197 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017198 int idx;
17199 char_u *cpstr;
17200 int cplen;
17201 int first = TRUE;
17202#endif
17203 char_u buf[NUMBUFLEN];
17204 char_u buf2[NUMBUFLEN];
17205 garray_T ga;
17206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017207 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017208 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17209 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017210
17211 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 rettv->v_type = VAR_STRING;
17213 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 if (fromstr == NULL || tostr == NULL)
17215 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017216 ga_init2(&ga, (int)sizeof(char), 80);
17217
17218#ifdef FEAT_MBYTE
17219 if (!has_mbyte)
17220#endif
17221 /* not multi-byte: fromstr and tostr must be the same length */
17222 if (STRLEN(fromstr) != STRLEN(tostr))
17223 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017224#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017225error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017226#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017227 EMSG2(_(e_invarg2), fromstr);
17228 ga_clear(&ga);
17229 return;
17230 }
17231
17232 /* fromstr and tostr have to contain the same number of chars */
17233 while (*instr != NUL)
17234 {
17235#ifdef FEAT_MBYTE
17236 if (has_mbyte)
17237 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017238 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017239 cpstr = instr;
17240 cplen = inlen;
17241 idx = 0;
17242 for (p = fromstr; *p != NUL; p += fromlen)
17243 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017244 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017245 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17246 {
17247 for (p = tostr; *p != NUL; p += tolen)
17248 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017249 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017250 if (idx-- == 0)
17251 {
17252 cplen = tolen;
17253 cpstr = p;
17254 break;
17255 }
17256 }
17257 if (*p == NUL) /* tostr is shorter than fromstr */
17258 goto error;
17259 break;
17260 }
17261 ++idx;
17262 }
17263
17264 if (first && cpstr == instr)
17265 {
17266 /* Check that fromstr and tostr have the same number of
17267 * (multi-byte) characters. Done only once when a character
17268 * of instr doesn't appear in fromstr. */
17269 first = FALSE;
17270 for (p = tostr; *p != NUL; p += tolen)
17271 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017272 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017273 --idx;
17274 }
17275 if (idx != 0)
17276 goto error;
17277 }
17278
17279 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017280 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017281 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017282
17283 instr += inlen;
17284 }
17285 else
17286#endif
17287 {
17288 /* When not using multi-byte chars we can do it faster. */
17289 p = vim_strchr(fromstr, *instr);
17290 if (p != NULL)
17291 ga_append(&ga, tostr[p - fromstr]);
17292 else
17293 ga_append(&ga, *instr);
17294 ++instr;
17295 }
17296 }
17297
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017298 /* add a terminating NUL */
17299 ga_grow(&ga, 1);
17300 ga_append(&ga, NUL);
17301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017303}
17304
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017305#ifdef FEAT_FLOAT
17306/*
17307 * "trunc({float})" function
17308 */
17309 static void
17310f_trunc(argvars, rettv)
17311 typval_T *argvars;
17312 typval_T *rettv;
17313{
17314 float_T f;
17315
17316 rettv->v_type = VAR_FLOAT;
17317 if (get_float_arg(argvars, &f) == OK)
17318 /* trunc() is not in C90, use floor() or ceil() instead. */
17319 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17320 else
17321 rettv->vval.v_float = 0.0;
17322}
17323#endif
17324
Bram Moolenaar8299df92004-07-10 09:47:34 +000017325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 * "type(expr)" function
17327 */
17328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 typval_T *argvars;
17331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017333 int n;
17334
17335 switch (argvars[0].v_type)
17336 {
17337 case VAR_NUMBER: n = 0; break;
17338 case VAR_STRING: n = 1; break;
17339 case VAR_FUNC: n = 2; break;
17340 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017341 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017342#ifdef FEAT_FLOAT
17343 case VAR_FLOAT: n = 5; break;
17344#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017345 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17346 }
17347 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348}
17349
17350/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017351 * "values(dict)" function
17352 */
17353 static void
17354f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 typval_T *argvars;
17356 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017357{
17358 dict_list(argvars, rettv, 1);
17359}
17360
17361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 * "virtcol(string)" function
17363 */
17364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017365f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017366 typval_T *argvars;
17367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368{
17369 colnr_T vcol = 0;
17370 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017371 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 fp = var2fpos(&argvars[0], FALSE, &fnum);
17374 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17375 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 {
17377 getvvcol(curwin, fp, NULL, NULL, &vcol);
17378 ++vcol;
17379 }
17380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382}
17383
17384/*
17385 * "visualmode()" function
17386 */
17387/*ARGSUSED*/
17388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 typval_T *argvars;
17391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392{
17393#ifdef FEAT_VISUAL
17394 char_u str[2];
17395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017396 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 str[0] = curbuf->b_visual_mode_eval;
17398 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017399 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400
17401 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017402 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 curbuf->b_visual_mode_eval = NUL;
17404#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017405 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406#endif
17407}
17408
17409/*
17410 * "winbufnr(nr)" function
17411 */
17412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 typval_T *argvars;
17415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416{
17417 win_T *wp;
17418
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017419 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424}
17425
17426/*
17427 * "wincol()" function
17428 */
17429/*ARGSUSED*/
17430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 typval_T *argvars;
17433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434{
17435 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437}
17438
17439/*
17440 * "winheight(nr)" function
17441 */
17442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017443f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017444 typval_T *argvars;
17445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446{
17447 win_T *wp;
17448
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017449 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017451 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017453 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454}
17455
17456/*
17457 * "winline()" function
17458 */
17459/*ARGSUSED*/
17460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017461f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 typval_T *argvars;
17463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464{
17465 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017466 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467}
17468
17469/*
17470 * "winnr()" function
17471 */
17472/* ARGSUSED */
17473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017474f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 typval_T *argvars;
17476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477{
17478 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017479
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017481 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017483 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484}
17485
17486/*
17487 * "winrestcmd()" function
17488 */
17489/* ARGSUSED */
17490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 typval_T *argvars;
17493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494{
17495#ifdef FEAT_WINDOWS
17496 win_T *wp;
17497 int winnr = 1;
17498 garray_T ga;
17499 char_u buf[50];
17500
17501 ga_init2(&ga, (int)sizeof(char), 70);
17502 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17503 {
17504 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17505 ga_concat(&ga, buf);
17506# ifdef FEAT_VERTSPLIT
17507 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17508 ga_concat(&ga, buf);
17509# endif
17510 ++winnr;
17511 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017512 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017516 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017518 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519}
17520
17521/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017522 * "winrestview()" function
17523 */
17524/* ARGSUSED */
17525 static void
17526f_winrestview(argvars, rettv)
17527 typval_T *argvars;
17528 typval_T *rettv;
17529{
17530 dict_T *dict;
17531
17532 if (argvars[0].v_type != VAR_DICT
17533 || (dict = argvars[0].vval.v_dict) == NULL)
17534 EMSG(_(e_invarg));
17535 else
17536 {
17537 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17538 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17539#ifdef FEAT_VIRTUALEDIT
17540 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17541#endif
17542 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017543 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017544
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017545 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017546#ifdef FEAT_DIFF
17547 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17548#endif
17549 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17550 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17551
17552 check_cursor();
17553 changed_cline_bef_curs();
17554 invalidate_botline();
17555 redraw_later(VALID);
17556
17557 if (curwin->w_topline == 0)
17558 curwin->w_topline = 1;
17559 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17560 curwin->w_topline = curbuf->b_ml.ml_line_count;
17561#ifdef FEAT_DIFF
17562 check_topfill(curwin, TRUE);
17563#endif
17564 }
17565}
17566
17567/*
17568 * "winsaveview()" function
17569 */
17570/* ARGSUSED */
17571 static void
17572f_winsaveview(argvars, rettv)
17573 typval_T *argvars;
17574 typval_T *rettv;
17575{
17576 dict_T *dict;
17577
17578 dict = dict_alloc();
17579 if (dict == NULL)
17580 return;
17581 rettv->v_type = VAR_DICT;
17582 rettv->vval.v_dict = dict;
17583 ++dict->dv_refcount;
17584
17585 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17586 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17587#ifdef FEAT_VIRTUALEDIT
17588 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17589#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017590 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017591 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17592
17593 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17594#ifdef FEAT_DIFF
17595 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17596#endif
17597 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17598 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17599}
17600
17601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 * "winwidth(nr)" function
17603 */
17604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017605f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017606 typval_T *argvars;
17607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608{
17609 win_T *wp;
17610
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017611 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 else
17615#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017616 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619#endif
17620}
17621
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017623 * "writefile()" function
17624 */
17625 static void
17626f_writefile(argvars, rettv)
17627 typval_T *argvars;
17628 typval_T *rettv;
17629{
17630 int binary = FALSE;
17631 char_u *fname;
17632 FILE *fd;
17633 listitem_T *li;
17634 char_u *s;
17635 int ret = 0;
17636 int c;
17637
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017638 if (check_restricted() || check_secure())
17639 return;
17640
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017641 if (argvars[0].v_type != VAR_LIST)
17642 {
17643 EMSG2(_(e_listarg), "writefile()");
17644 return;
17645 }
17646 if (argvars[0].vval.v_list == NULL)
17647 return;
17648
17649 if (argvars[2].v_type != VAR_UNKNOWN
17650 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17651 binary = TRUE;
17652
17653 /* Always open the file in binary mode, library functions have a mind of
17654 * their own about CR-LF conversion. */
17655 fname = get_tv_string(&argvars[1]);
17656 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17657 {
17658 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17659 ret = -1;
17660 }
17661 else
17662 {
17663 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17664 li = li->li_next)
17665 {
17666 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17667 {
17668 if (*s == '\n')
17669 c = putc(NUL, fd);
17670 else
17671 c = putc(*s, fd);
17672 if (c == EOF)
17673 {
17674 ret = -1;
17675 break;
17676 }
17677 }
17678 if (!binary || li->li_next != NULL)
17679 if (putc('\n', fd) == EOF)
17680 {
17681 ret = -1;
17682 break;
17683 }
17684 if (ret < 0)
17685 {
17686 EMSG(_(e_write));
17687 break;
17688 }
17689 }
17690 fclose(fd);
17691 }
17692
17693 rettv->vval.v_number = ret;
17694}
17695
17696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017698 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 */
17700 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017701var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017702 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017703 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017704 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017706 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017708 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709
Bram Moolenaara5525202006-03-02 22:52:09 +000017710 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017711 if (varp->v_type == VAR_LIST)
17712 {
17713 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017714 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017715 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017716 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017717
17718 l = varp->vval.v_list;
17719 if (l == NULL)
17720 return NULL;
17721
17722 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017723 pos.lnum = list_find_nr(l, 0L, &error);
17724 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017725 return NULL; /* invalid line number */
17726
17727 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017728 pos.col = list_find_nr(l, 1L, &error);
17729 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017730 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017731 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017732
17733 /* We accept "$" for the column number: last column. */
17734 li = list_find(l, 1L);
17735 if (li != NULL && li->li_tv.v_type == VAR_STRING
17736 && li->li_tv.vval.v_string != NULL
17737 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17738 pos.col = len + 1;
17739
Bram Moolenaara5525202006-03-02 22:52:09 +000017740 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017741 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017742 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017743 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017744
Bram Moolenaara5525202006-03-02 22:52:09 +000017745#ifdef FEAT_VIRTUALEDIT
17746 /* Get the virtual offset. Defaults to zero. */
17747 pos.coladd = list_find_nr(l, 2L, &error);
17748 if (error)
17749 pos.coladd = 0;
17750#endif
17751
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017752 return &pos;
17753 }
17754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017755 name = get_tv_string_chk(varp);
17756 if (name == NULL)
17757 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017758 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017760#ifdef FEAT_VISUAL
17761 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17762 {
17763 if (VIsual_active)
17764 return &VIsual;
17765 return &curwin->w_cursor;
17766 }
17767#endif
17768 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017770 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17772 return NULL;
17773 return pp;
17774 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017775
17776#ifdef FEAT_VIRTUALEDIT
17777 pos.coladd = 0;
17778#endif
17779
Bram Moolenaar477933c2007-07-17 14:32:23 +000017780 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017781 {
17782 pos.col = 0;
17783 if (name[1] == '0') /* "w0": first visible line */
17784 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017785 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017786 pos.lnum = curwin->w_topline;
17787 return &pos;
17788 }
17789 else if (name[1] == '$') /* "w$": last visible line */
17790 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017791 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017792 pos.lnum = curwin->w_botline - 1;
17793 return &pos;
17794 }
17795 }
17796 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017798 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 {
17800 pos.lnum = curbuf->b_ml.ml_line_count;
17801 pos.col = 0;
17802 }
17803 else
17804 {
17805 pos.lnum = curwin->w_cursor.lnum;
17806 pos.col = (colnr_T)STRLEN(ml_get_curline());
17807 }
17808 return &pos;
17809 }
17810 return NULL;
17811}
17812
17813/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017814 * Convert list in "arg" into a position and optional file number.
17815 * When "fnump" is NULL there is no file number, only 3 items.
17816 * Note that the column is passed on as-is, the caller may want to decrement
17817 * it to use 1 for the first column.
17818 * Return FAIL when conversion is not possible, doesn't check the position for
17819 * validity.
17820 */
17821 static int
17822list2fpos(arg, posp, fnump)
17823 typval_T *arg;
17824 pos_T *posp;
17825 int *fnump;
17826{
17827 list_T *l = arg->vval.v_list;
17828 long i = 0;
17829 long n;
17830
Bram Moolenaarbde35262006-07-23 20:12:24 +000017831 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17832 * when "fnump" isn't NULL and "coladd" is optional. */
17833 if (arg->v_type != VAR_LIST
17834 || l == NULL
17835 || l->lv_len < (fnump == NULL ? 2 : 3)
17836 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017837 return FAIL;
17838
17839 if (fnump != NULL)
17840 {
17841 n = list_find_nr(l, i++, NULL); /* fnum */
17842 if (n < 0)
17843 return FAIL;
17844 if (n == 0)
17845 n = curbuf->b_fnum; /* current buffer */
17846 *fnump = n;
17847 }
17848
17849 n = list_find_nr(l, i++, NULL); /* lnum */
17850 if (n < 0)
17851 return FAIL;
17852 posp->lnum = n;
17853
17854 n = list_find_nr(l, i++, NULL); /* col */
17855 if (n < 0)
17856 return FAIL;
17857 posp->col = n;
17858
17859#ifdef FEAT_VIRTUALEDIT
17860 n = list_find_nr(l, i, NULL);
17861 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017862 posp->coladd = 0;
17863 else
17864 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017865#endif
17866
17867 return OK;
17868}
17869
17870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 * Get the length of an environment variable name.
17872 * Advance "arg" to the first character after the name.
17873 * Return 0 for error.
17874 */
17875 static int
17876get_env_len(arg)
17877 char_u **arg;
17878{
17879 char_u *p;
17880 int len;
17881
17882 for (p = *arg; vim_isIDc(*p); ++p)
17883 ;
17884 if (p == *arg) /* no name found */
17885 return 0;
17886
17887 len = (int)(p - *arg);
17888 *arg = p;
17889 return len;
17890}
17891
17892/*
17893 * Get the length of the name of a function or internal variable.
17894 * "arg" is advanced to the first non-white character after the name.
17895 * Return 0 if something is wrong.
17896 */
17897 static int
17898get_id_len(arg)
17899 char_u **arg;
17900{
17901 char_u *p;
17902 int len;
17903
17904 /* Find the end of the name. */
17905 for (p = *arg; eval_isnamec(*p); ++p)
17906 ;
17907 if (p == *arg) /* no name found */
17908 return 0;
17909
17910 len = (int)(p - *arg);
17911 *arg = skipwhite(p);
17912
17913 return len;
17914}
17915
17916/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017917 * Get the length of the name of a variable or function.
17918 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017920 * Return -1 if curly braces expansion failed.
17921 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 * If the name contains 'magic' {}'s, expand them and return the
17923 * expanded name in an allocated string via 'alias' - caller must free.
17924 */
17925 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017926get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 char_u **arg;
17928 char_u **alias;
17929 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017930 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931{
17932 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 char_u *p;
17934 char_u *expr_start;
17935 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936
17937 *alias = NULL; /* default to no alias */
17938
17939 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17940 && (*arg)[2] == (int)KE_SNR)
17941 {
17942 /* hard coded <SNR>, already translated */
17943 *arg += 3;
17944 return get_id_len(arg) + 3;
17945 }
17946 len = eval_fname_script(*arg);
17947 if (len > 0)
17948 {
17949 /* literal "<SID>", "s:" or "<SNR>" */
17950 *arg += len;
17951 }
17952
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017954 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017956 p = find_name_end(*arg, &expr_start, &expr_end,
17957 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 if (expr_start != NULL)
17959 {
17960 char_u *temp_string;
17961
17962 if (!evaluate)
17963 {
17964 len += (int)(p - *arg);
17965 *arg = skipwhite(p);
17966 return len;
17967 }
17968
17969 /*
17970 * Include any <SID> etc in the expanded string:
17971 * Thus the -len here.
17972 */
17973 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17974 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017975 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 *alias = temp_string;
17977 *arg = skipwhite(p);
17978 return (int)STRLEN(temp_string);
17979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980
17981 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017982 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 EMSG2(_(e_invexpr2), *arg);
17984
17985 return len;
17986}
17987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017988/*
17989 * Find the end of a variable or function name, taking care of magic braces.
17990 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17991 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017992 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017993 * Return a pointer to just after the name. Equal to "arg" if there is no
17994 * valid name.
17995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017997find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 char_u *arg;
17999 char_u **expr_start;
18000 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018001 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018003 int mb_nest = 0;
18004 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 char_u *p;
18006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018007 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018009 *expr_start = NULL;
18010 *expr_end = NULL;
18011 }
18012
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018013 /* Quick check for valid starting character. */
18014 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18015 return arg;
18016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 for (p = arg; *p != NUL
18018 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018019 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018020 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018021 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018022 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018023 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018024 if (*p == '\'')
18025 {
18026 /* skip over 'string' to avoid counting [ and ] inside it. */
18027 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18028 ;
18029 if (*p == NUL)
18030 break;
18031 }
18032 else if (*p == '"')
18033 {
18034 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18035 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18036 if (*p == '\\' && p[1] != NUL)
18037 ++p;
18038 if (*p == NUL)
18039 break;
18040 }
18041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018042 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018044 if (*p == '[')
18045 ++br_nest;
18046 else if (*p == ']')
18047 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018050 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018052 if (*p == '{')
18053 {
18054 mb_nest++;
18055 if (expr_start != NULL && *expr_start == NULL)
18056 *expr_start = p;
18057 }
18058 else if (*p == '}')
18059 {
18060 mb_nest--;
18061 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18062 *expr_end = p;
18063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 }
18066
18067 return p;
18068}
18069
18070/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018071 * Expands out the 'magic' {}'s in a variable/function name.
18072 * Note that this can call itself recursively, to deal with
18073 * constructs like foo{bar}{baz}{bam}
18074 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18075 * "in_start" ^
18076 * "expr_start" ^
18077 * "expr_end" ^
18078 * "in_end" ^
18079 *
18080 * Returns a new allocated string, which the caller must free.
18081 * Returns NULL for failure.
18082 */
18083 static char_u *
18084make_expanded_name(in_start, expr_start, expr_end, in_end)
18085 char_u *in_start;
18086 char_u *expr_start;
18087 char_u *expr_end;
18088 char_u *in_end;
18089{
18090 char_u c1;
18091 char_u *retval = NULL;
18092 char_u *temp_result;
18093 char_u *nextcmd = NULL;
18094
18095 if (expr_end == NULL || in_end == NULL)
18096 return NULL;
18097 *expr_start = NUL;
18098 *expr_end = NUL;
18099 c1 = *in_end;
18100 *in_end = NUL;
18101
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018102 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018103 if (temp_result != NULL && nextcmd == NULL)
18104 {
18105 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18106 + (in_end - expr_end) + 1));
18107 if (retval != NULL)
18108 {
18109 STRCPY(retval, in_start);
18110 STRCAT(retval, temp_result);
18111 STRCAT(retval, expr_end + 1);
18112 }
18113 }
18114 vim_free(temp_result);
18115
18116 *in_end = c1; /* put char back for error messages */
18117 *expr_start = '{';
18118 *expr_end = '}';
18119
18120 if (retval != NULL)
18121 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018122 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018123 if (expr_start != NULL)
18124 {
18125 /* Further expansion! */
18126 temp_result = make_expanded_name(retval, expr_start,
18127 expr_end, temp_result);
18128 vim_free(retval);
18129 retval = temp_result;
18130 }
18131 }
18132
18133 return retval;
18134}
18135
18136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018138 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 */
18140 static int
18141eval_isnamec(c)
18142 int c;
18143{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018144 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18145}
18146
18147/*
18148 * Return TRUE if character "c" can be used as the first character in a
18149 * variable or function name (excluding '{' and '}').
18150 */
18151 static int
18152eval_isnamec1(c)
18153 int c;
18154{
18155 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156}
18157
18158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 * Set number v: variable to "val".
18160 */
18161 void
18162set_vim_var_nr(idx, val)
18163 int idx;
18164 long val;
18165{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018166 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167}
18168
18169/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018170 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 */
18172 long
18173get_vim_var_nr(idx)
18174 int idx;
18175{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018176 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018179/*
18180 * Get string v: variable value. Uses a static buffer, can only be used once.
18181 */
18182 char_u *
18183get_vim_var_str(idx)
18184 int idx;
18185{
18186 return get_tv_string(&vimvars[idx].vv_tv);
18187}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018188
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018190 * Get List v: variable value. Caller must take care of reference count when
18191 * needed.
18192 */
18193 list_T *
18194get_vim_var_list(idx)
18195 int idx;
18196{
18197 return vimvars[idx].vv_list;
18198}
18199
18200/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018201 * Set v:count to "count" and v:count1 to "count1".
18202 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 */
18204 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018205set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 long count;
18207 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018208 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018210 if (set_prevcount)
18211 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018212 vimvars[VV_COUNT].vv_nr = count;
18213 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214}
18215
18216/*
18217 * Set string v: variable to a copy of "val".
18218 */
18219 void
18220set_vim_var_string(idx, val, len)
18221 int idx;
18222 char_u *val;
18223 int len; /* length of "val" to use or -1 (whole string) */
18224{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018225 /* Need to do this (at least) once, since we can't initialize a union.
18226 * Will always be invoked when "v:progname" is set. */
18227 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18228
Bram Moolenaare9a41262005-01-15 22:18:47 +000018229 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018231 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018233 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018235 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236}
18237
18238/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018239 * Set List v: variable to "val".
18240 */
18241 void
18242set_vim_var_list(idx, val)
18243 int idx;
18244 list_T *val;
18245{
18246 list_unref(vimvars[idx].vv_list);
18247 vimvars[idx].vv_list = val;
18248 if (val != NULL)
18249 ++val->lv_refcount;
18250}
18251
18252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 * Set v:register if needed.
18254 */
18255 void
18256set_reg_var(c)
18257 int c;
18258{
18259 char_u regname;
18260
18261 if (c == 0 || c == ' ')
18262 regname = '"';
18263 else
18264 regname = c;
18265 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018266 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 set_vim_var_string(VV_REG, &regname, 1);
18268}
18269
18270/*
18271 * Get or set v:exception. If "oldval" == NULL, return the current value.
18272 * Otherwise, restore the value to "oldval" and return NULL.
18273 * Must always be called in pairs to save and restore v:exception! Does not
18274 * take care of memory allocations.
18275 */
18276 char_u *
18277v_exception(oldval)
18278 char_u *oldval;
18279{
18280 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018281 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282
Bram Moolenaare9a41262005-01-15 22:18:47 +000018283 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 return NULL;
18285}
18286
18287/*
18288 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18289 * Otherwise, restore the value to "oldval" and return NULL.
18290 * Must always be called in pairs to save and restore v:throwpoint! Does not
18291 * take care of memory allocations.
18292 */
18293 char_u *
18294v_throwpoint(oldval)
18295 char_u *oldval;
18296{
18297 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018298 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299
Bram Moolenaare9a41262005-01-15 22:18:47 +000018300 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 return NULL;
18302}
18303
18304#if defined(FEAT_AUTOCMD) || defined(PROTO)
18305/*
18306 * Set v:cmdarg.
18307 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18308 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18309 * Must always be called in pairs!
18310 */
18311 char_u *
18312set_cmdarg(eap, oldarg)
18313 exarg_T *eap;
18314 char_u *oldarg;
18315{
18316 char_u *oldval;
18317 char_u *newval;
18318 unsigned len;
18319
Bram Moolenaare9a41262005-01-15 22:18:47 +000018320 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018321 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018323 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018324 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018325 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 }
18327
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018328 if (eap->force_bin == FORCE_BIN)
18329 len = 6;
18330 else if (eap->force_bin == FORCE_NOBIN)
18331 len = 8;
18332 else
18333 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018334
18335 if (eap->read_edit)
18336 len += 7;
18337
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018338 if (eap->force_ff != 0)
18339 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18340# ifdef FEAT_MBYTE
18341 if (eap->force_enc != 0)
18342 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018343 if (eap->bad_char != 0)
18344 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018345# endif
18346
18347 newval = alloc(len + 1);
18348 if (newval == NULL)
18349 return NULL;
18350
18351 if (eap->force_bin == FORCE_BIN)
18352 sprintf((char *)newval, " ++bin");
18353 else if (eap->force_bin == FORCE_NOBIN)
18354 sprintf((char *)newval, " ++nobin");
18355 else
18356 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018357
18358 if (eap->read_edit)
18359 STRCAT(newval, " ++edit");
18360
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018361 if (eap->force_ff != 0)
18362 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18363 eap->cmd + eap->force_ff);
18364# ifdef FEAT_MBYTE
18365 if (eap->force_enc != 0)
18366 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18367 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018368 if (eap->bad_char != 0)
18369 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18370 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018371# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018372 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018373 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374}
18375#endif
18376
18377/*
18378 * Get the value of internal variable "name".
18379 * Return OK or FAIL.
18380 */
18381 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018382get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 char_u *name;
18384 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018385 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018386 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387{
18388 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018389 typval_T *tv = NULL;
18390 typval_T atv;
18391 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393
18394 /* truncate the name, so that we can use strcmp() */
18395 cc = name[len];
18396 name[len] = NUL;
18397
18398 /*
18399 * Check for "b:changedtick".
18400 */
18401 if (STRCMP(name, "b:changedtick") == 0)
18402 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018403 atv.v_type = VAR_NUMBER;
18404 atv.vval.v_number = curbuf->b_changedtick;
18405 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 }
18407
18408 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 * Check for user-defined variables.
18410 */
18411 else
18412 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018413 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018415 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 }
18417
Bram Moolenaare9a41262005-01-15 22:18:47 +000018418 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018420 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422 ret = FAIL;
18423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018425 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426
18427 name[len] = cc;
18428
18429 return ret;
18430}
18431
18432/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018433 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18434 * Also handle function call with Funcref variable: func(expr)
18435 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18436 */
18437 static int
18438handle_subscript(arg, rettv, evaluate, verbose)
18439 char_u **arg;
18440 typval_T *rettv;
18441 int evaluate; /* do more than finding the end */
18442 int verbose; /* give error messages */
18443{
18444 int ret = OK;
18445 dict_T *selfdict = NULL;
18446 char_u *s;
18447 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018448 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018449
18450 while (ret == OK
18451 && (**arg == '['
18452 || (**arg == '.' && rettv->v_type == VAR_DICT)
18453 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18454 && !vim_iswhite(*(*arg - 1)))
18455 {
18456 if (**arg == '(')
18457 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018458 /* need to copy the funcref so that we can clear rettv */
18459 functv = *rettv;
18460 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018461
18462 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018463 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018464 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018465 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18466 &len, evaluate, selfdict);
18467
18468 /* Clear the funcref afterwards, so that deleting it while
18469 * evaluating the arguments is possible (see test55). */
18470 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018471
18472 /* Stop the expression evaluation when immediately aborting on
18473 * error, or when an interrupt occurred or an exception was thrown
18474 * but not caught. */
18475 if (aborting())
18476 {
18477 if (ret == OK)
18478 clear_tv(rettv);
18479 ret = FAIL;
18480 }
18481 dict_unref(selfdict);
18482 selfdict = NULL;
18483 }
18484 else /* **arg == '[' || **arg == '.' */
18485 {
18486 dict_unref(selfdict);
18487 if (rettv->v_type == VAR_DICT)
18488 {
18489 selfdict = rettv->vval.v_dict;
18490 if (selfdict != NULL)
18491 ++selfdict->dv_refcount;
18492 }
18493 else
18494 selfdict = NULL;
18495 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18496 {
18497 clear_tv(rettv);
18498 ret = FAIL;
18499 }
18500 }
18501 }
18502 dict_unref(selfdict);
18503 return ret;
18504}
18505
18506/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018507 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018508 * value).
18509 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018510 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018511alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018512{
Bram Moolenaar33570922005-01-25 22:26:29 +000018513 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018514}
18515
18516/*
18517 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 * The string "s" must have been allocated, it is consumed.
18519 * Return NULL for out of memory, the variable otherwise.
18520 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018521 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018522alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 char_u *s;
18524{
Bram Moolenaar33570922005-01-25 22:26:29 +000018525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527 rettv = alloc_tv();
18528 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530 rettv->v_type = VAR_STRING;
18531 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 }
18533 else
18534 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536}
18537
18538/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018539 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018541 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018542free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018543 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544{
18545 if (varp != NULL)
18546 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018547 switch (varp->v_type)
18548 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018549 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018550 func_unref(varp->vval.v_string);
18551 /*FALLTHROUGH*/
18552 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018553 vim_free(varp->vval.v_string);
18554 break;
18555 case VAR_LIST:
18556 list_unref(varp->vval.v_list);
18557 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018558 case VAR_DICT:
18559 dict_unref(varp->vval.v_dict);
18560 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018561 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018562#ifdef FEAT_FLOAT
18563 case VAR_FLOAT:
18564#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018565 case VAR_UNKNOWN:
18566 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018567 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018568 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018569 break;
18570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 vim_free(varp);
18572 }
18573}
18574
18575/*
18576 * Free the memory for a variable value and set the value to NULL or 0.
18577 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018578 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018580 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581{
18582 if (varp != NULL)
18583 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018584 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018586 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018587 func_unref(varp->vval.v_string);
18588 /*FALLTHROUGH*/
18589 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018590 vim_free(varp->vval.v_string);
18591 varp->vval.v_string = NULL;
18592 break;
18593 case VAR_LIST:
18594 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018595 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018596 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018597 case VAR_DICT:
18598 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018599 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018600 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018601 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018602 varp->vval.v_number = 0;
18603 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018604#ifdef FEAT_FLOAT
18605 case VAR_FLOAT:
18606 varp->vval.v_float = 0.0;
18607 break;
18608#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018609 case VAR_UNKNOWN:
18610 break;
18611 default:
18612 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018614 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 }
18616}
18617
18618/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018619 * Set the value of a variable to NULL without freeing items.
18620 */
18621 static void
18622init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018623 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624{
18625 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018626 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627}
18628
18629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 * Get the number value of a variable.
18631 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018632 * For incompatible types, return 0.
18633 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18634 * caller of incompatible types: it sets *denote to TRUE if "denote"
18635 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 */
18637 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018641 int error = FALSE;
18642
18643 return get_tv_number_chk(varp, &error); /* return 0L on error */
18644}
18645
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018646 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018647get_tv_number_chk(varp, denote)
18648 typval_T *varp;
18649 int *denote;
18650{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018651 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018653 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018655 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018656 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018657#ifdef FEAT_FLOAT
18658 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018659 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018660 break;
18661#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018662 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018663 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018664 break;
18665 case VAR_STRING:
18666 if (varp->vval.v_string != NULL)
18667 vim_str2nr(varp->vval.v_string, NULL, NULL,
18668 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018669 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018670 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018671 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018672 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018673 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018674 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018675 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018676 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018677 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018678 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018680 if (denote == NULL) /* useful for values that must be unsigned */
18681 n = -1;
18682 else
18683 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018684 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685}
18686
18687/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018688 * Get the lnum from the first argument.
18689 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018690 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 */
18692 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018693get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695{
Bram Moolenaar33570922005-01-25 22:26:29 +000018696 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 linenr_T lnum;
18698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018699 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 if (lnum == 0) /* no valid number, try using line() */
18701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702 rettv.v_type = VAR_NUMBER;
18703 f_line(argvars, &rettv);
18704 lnum = rettv.vval.v_number;
18705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 }
18707 return lnum;
18708}
18709
18710/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018711 * Get the lnum from the first argument.
18712 * Also accepts "$", then "buf" is used.
18713 * Returns 0 on error.
18714 */
18715 static linenr_T
18716get_tv_lnum_buf(argvars, buf)
18717 typval_T *argvars;
18718 buf_T *buf;
18719{
18720 if (argvars[0].v_type == VAR_STRING
18721 && argvars[0].vval.v_string != NULL
18722 && argvars[0].vval.v_string[0] == '$'
18723 && buf != NULL)
18724 return buf->b_ml.ml_line_count;
18725 return get_tv_number_chk(&argvars[0], NULL);
18726}
18727
18728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 * Get the string value of a variable.
18730 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018731 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18732 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 * If the String variable has never been set, return an empty string.
18734 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018735 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18736 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 */
18738 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018740 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741{
18742 static char_u mybuf[NUMBUFLEN];
18743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745}
18746
18747 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018749 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018750 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018752 char_u *res = get_tv_string_buf_chk(varp, buf);
18753
18754 return res != NULL ? res : (char_u *)"";
18755}
18756
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018757 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018758get_tv_string_chk(varp)
18759 typval_T *varp;
18760{
18761 static char_u mybuf[NUMBUFLEN];
18762
18763 return get_tv_string_buf_chk(varp, mybuf);
18764}
18765
18766 static char_u *
18767get_tv_string_buf_chk(varp, buf)
18768 typval_T *varp;
18769 char_u *buf;
18770{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018771 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018773 case VAR_NUMBER:
18774 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18775 return buf;
18776 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018777 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018778 break;
18779 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018781 break;
18782 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018783 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018784 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018785#ifdef FEAT_FLOAT
18786 case VAR_FLOAT:
18787 EMSG(_("E806: using Float as a String"));
18788 break;
18789#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018790 case VAR_STRING:
18791 if (varp->vval.v_string != NULL)
18792 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018793 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018794 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018795 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018798 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799}
18800
18801/*
18802 * Find variable "name" in the list of variables.
18803 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018804 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018805 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018806 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018809find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018811 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815
Bram Moolenaara7043832005-01-21 11:56:39 +000018816 ht = find_var_ht(name, &varname);
18817 if (htp != NULL)
18818 *htp = ht;
18819 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018821 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822}
18823
18824/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018825 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018826 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018829find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018830 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018831 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018832 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018833{
Bram Moolenaar33570922005-01-25 22:26:29 +000018834 hashitem_T *hi;
18835
18836 if (*varname == NUL)
18837 {
18838 /* Must be something like "s:", otherwise "ht" would be NULL. */
18839 switch (varname[-2])
18840 {
18841 case 's': return &SCRIPT_SV(current_SID).sv_var;
18842 case 'g': return &globvars_var;
18843 case 'v': return &vimvars_var;
18844 case 'b': return &curbuf->b_bufvar;
18845 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018846#ifdef FEAT_WINDOWS
18847 case 't': return &curtab->tp_winvar;
18848#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018849 case 'l': return current_funccal == NULL
18850 ? NULL : &current_funccal->l_vars_var;
18851 case 'a': return current_funccal == NULL
18852 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018853 }
18854 return NULL;
18855 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018856
18857 hi = hash_find(ht, varname);
18858 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018859 {
18860 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018861 * worked find the variable again. Don't auto-load a script if it was
18862 * loaded already, otherwise it would be loaded every time when
18863 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018864 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018865 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018866 hi = hash_find(ht, varname);
18867 if (HASHITEM_EMPTY(hi))
18868 return NULL;
18869 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018870 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018871}
18872
18873/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018875 * Set "varname" to the start of name without ':'.
18876 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018878find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 char_u *name;
18880 char_u **varname;
18881{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018882 hashitem_T *hi;
18883
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 if (name[1] != ':')
18885 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018886 /* The name must not start with a colon or #. */
18887 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 return NULL;
18889 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018890
18891 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018892 hi = hash_find(&compat_hashtab, name);
18893 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018894 return &compat_hashtab;
18895
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018897 return &globvarht; /* global variable */
18898 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 }
18900 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018901 if (*name == 'g') /* global variable */
18902 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018903 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18904 */
18905 if (vim_strchr(name + 2, ':') != NULL
18906 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018907 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018909 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018911 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018912#ifdef FEAT_WINDOWS
18913 if (*name == 't') /* tab page variable */
18914 return &curtab->tp_vars.dv_hashtab;
18915#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018916 if (*name == 'v') /* v: variable */
18917 return &vimvarht;
18918 if (*name == 'a' && current_funccal != NULL) /* function argument */
18919 return &current_funccal->l_avars.dv_hashtab;
18920 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18921 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 if (*name == 's' /* script variable */
18923 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18924 return &SCRIPT_VARS(current_SID);
18925 return NULL;
18926}
18927
18928/*
18929 * Get the string value of a (global/local) variable.
18930 * Returns NULL when it doesn't exist.
18931 */
18932 char_u *
18933get_var_value(name)
18934 char_u *name;
18935{
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937
Bram Moolenaara7043832005-01-21 11:56:39 +000018938 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 if (v == NULL)
18940 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018941 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018945 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 * sourcing this script and when executing functions defined in the script.
18947 */
18948 void
18949new_script_vars(id)
18950 scid_T id;
18951{
Bram Moolenaara7043832005-01-21 11:56:39 +000018952 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018953 hashtab_T *ht;
18954 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018955
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18957 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018958 /* Re-allocating ga_data means that an ht_array pointing to
18959 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018960 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018961 for (i = 1; i <= ga_scripts.ga_len; ++i)
18962 {
18963 ht = &SCRIPT_VARS(i);
18964 if (ht->ht_mask == HT_INIT_SIZE - 1)
18965 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018966 sv = &SCRIPT_SV(i);
18967 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018968 }
18969
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 while (ga_scripts.ga_len < id)
18971 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018972 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18973 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 }
18976 }
18977}
18978
18979/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018980 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18981 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 */
18983 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018984init_var_dict(dict, dict_var)
18985 dict_T *dict;
18986 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987{
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018989 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaar33570922005-01-25 22:26:29 +000018990 dict_var->di_tv.vval.v_dict = dict;
18991 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018992 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18994 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995}
18996
18997/*
18998 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018999 * Frees all allocated variables and the value they contain.
19000 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 */
19002 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019003vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 hashtab_T *ht;
19005{
19006 vars_clear_ext(ht, TRUE);
19007}
19008
19009/*
19010 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19011 */
19012 static void
19013vars_clear_ext(ht, free_val)
19014 hashtab_T *ht;
19015 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016{
Bram Moolenaara7043832005-01-21 11:56:39 +000019017 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019018 hashitem_T *hi;
19019 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
Bram Moolenaar33570922005-01-25 22:26:29 +000019021 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019022 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019023 for (hi = ht->ht_array; todo > 0; ++hi)
19024 {
19025 if (!HASHITEM_EMPTY(hi))
19026 {
19027 --todo;
19028
Bram Moolenaar33570922005-01-25 22:26:29 +000019029 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019030 * ht_array might change then. hash_clear() takes care of it
19031 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019032 v = HI2DI(hi);
19033 if (free_val)
19034 clear_tv(&v->di_tv);
19035 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19036 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019037 }
19038 }
19039 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019040 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041}
19042
Bram Moolenaara7043832005-01-21 11:56:39 +000019043/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019044 * Delete a variable from hashtab "ht" at item "hi".
19045 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019048delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019049 hashtab_T *ht;
19050 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051{
Bram Moolenaar33570922005-01-25 22:26:29 +000019052 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019053
19054 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019055 clear_tv(&di->di_tv);
19056 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057}
19058
19059/*
19060 * List the value of one internal variable.
19061 */
19062 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019063list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019064 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019066 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019068 char_u *tofree;
19069 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019071
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019072 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019073 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019074 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019075 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076}
19077
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019079list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 char_u *prefix;
19081 char_u *name;
19082 int type;
19083 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019084 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085{
Bram Moolenaar31859182007-08-14 20:41:13 +000019086 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19087 msg_start();
19088 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 if (name != NULL) /* "a:" vars don't have a name stored */
19090 msg_puts(name);
19091 msg_putchar(' ');
19092 msg_advance(22);
19093 if (type == VAR_NUMBER)
19094 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019095 else if (type == VAR_FUNC)
19096 msg_putchar('*');
19097 else if (type == VAR_LIST)
19098 {
19099 msg_putchar('[');
19100 if (*string == '[')
19101 ++string;
19102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019103 else if (type == VAR_DICT)
19104 {
19105 msg_putchar('{');
19106 if (*string == '{')
19107 ++string;
19108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 else
19110 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019111
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019113
19114 if (type == VAR_FUNC)
19115 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019116 if (*first)
19117 {
19118 msg_clr_eos();
19119 *first = FALSE;
19120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121}
19122
19123/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 * If the variable already exists, the value is updated.
19126 * Otherwise the variable is created.
19127 */
19128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019131 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019132 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133{
Bram Moolenaar33570922005-01-25 22:26:29 +000019134 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019137 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019140 {
19141 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19142 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19143 ? name[2] : name[0]))
19144 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019145 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019146 return;
19147 }
19148 if (function_exists(name))
19149 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019150 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019151 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019152 return;
19153 }
19154 }
19155
Bram Moolenaara7043832005-01-21 11:56:39 +000019156 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019158 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019159 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019160 return;
19161 }
19162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019163 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019166 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019167 if (var_check_ro(v->di_flags, name)
19168 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019169 return;
19170 if (v->di_tv.v_type != tv->v_type
19171 && !((v->di_tv.v_type == VAR_STRING
19172 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019174 || tv->v_type == VAR_NUMBER))
19175#ifdef FEAT_FLOAT
19176 && !((v->di_tv.v_type == VAR_NUMBER
19177 || v->di_tv.v_type == VAR_FLOAT)
19178 && (tv->v_type == VAR_NUMBER
19179 || tv->v_type == VAR_FLOAT))
19180#endif
19181 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019183 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 return;
19185 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019186
19187 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019188 * Handle setting internal v: variables separately: we don't change
19189 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019190 */
19191 if (ht == &vimvarht)
19192 {
19193 if (v->di_tv.v_type == VAR_STRING)
19194 {
19195 vim_free(v->di_tv.vval.v_string);
19196 if (copy || tv->v_type != VAR_STRING)
19197 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19198 else
19199 {
19200 /* Take over the string to avoid an extra alloc/free. */
19201 v->di_tv.vval.v_string = tv->vval.v_string;
19202 tv->vval.v_string = NULL;
19203 }
19204 }
19205 else if (v->di_tv.v_type != VAR_NUMBER)
19206 EMSG2(_(e_intern2), "set_var()");
19207 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019209 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019210 if (STRCMP(varname, "searchforward") == 0)
19211 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19212 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 return;
19214 }
19215
19216 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 }
19218 else /* add a new variable */
19219 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019220 /* Can't add "v:" variable. */
19221 if (ht == &vimvarht)
19222 {
19223 EMSG2(_(e_illvar), name);
19224 return;
19225 }
19226
Bram Moolenaar92124a32005-06-17 22:03:40 +000019227 /* Make sure the variable name is valid. */
19228 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019229 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19230 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019231 {
19232 EMSG2(_(e_illvar), varname);
19233 return;
19234 }
19235
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019236 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19237 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019238 if (v == NULL)
19239 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019240 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019243 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 return;
19245 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019246 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019248
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019249 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019250 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019251 else
19252 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019253 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019254 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019255 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257}
19258
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019259/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019260 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 * Also give an error message.
19262 */
19263 static int
19264var_check_ro(flags, name)
19265 int flags;
19266 char_u *name;
19267{
19268 if (flags & DI_FLAGS_RO)
19269 {
19270 EMSG2(_(e_readonlyvar), name);
19271 return TRUE;
19272 }
19273 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19274 {
19275 EMSG2(_(e_readonlysbx), name);
19276 return TRUE;
19277 }
19278 return FALSE;
19279}
19280
19281/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019282 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19283 * Also give an error message.
19284 */
19285 static int
19286var_check_fixed(flags, name)
19287 int flags;
19288 char_u *name;
19289{
19290 if (flags & DI_FLAGS_FIX)
19291 {
19292 EMSG2(_("E795: Cannot delete variable %s"), name);
19293 return TRUE;
19294 }
19295 return FALSE;
19296}
19297
19298/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019299 * Return TRUE if typeval "tv" is set to be locked (immutable).
19300 * Also give an error message, using "name".
19301 */
19302 static int
19303tv_check_lock(lock, name)
19304 int lock;
19305 char_u *name;
19306{
19307 if (lock & VAR_LOCKED)
19308 {
19309 EMSG2(_("E741: Value is locked: %s"),
19310 name == NULL ? (char_u *)_("Unknown") : name);
19311 return TRUE;
19312 }
19313 if (lock & VAR_FIXED)
19314 {
19315 EMSG2(_("E742: Cannot change value of %s"),
19316 name == NULL ? (char_u *)_("Unknown") : name);
19317 return TRUE;
19318 }
19319 return FALSE;
19320}
19321
19322/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019323 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019324 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019325 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019326 * It is OK for "from" and "to" to point to the same item. This is used to
19327 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019330copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 typval_T *from;
19332 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019334 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019335 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019336 switch (from->v_type)
19337 {
19338 case VAR_NUMBER:
19339 to->vval.v_number = from->vval.v_number;
19340 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019341#ifdef FEAT_FLOAT
19342 case VAR_FLOAT:
19343 to->vval.v_float = from->vval.v_float;
19344 break;
19345#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019346 case VAR_STRING:
19347 case VAR_FUNC:
19348 if (from->vval.v_string == NULL)
19349 to->vval.v_string = NULL;
19350 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019351 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019353 if (from->v_type == VAR_FUNC)
19354 func_ref(to->vval.v_string);
19355 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019356 break;
19357 case VAR_LIST:
19358 if (from->vval.v_list == NULL)
19359 to->vval.v_list = NULL;
19360 else
19361 {
19362 to->vval.v_list = from->vval.v_list;
19363 ++to->vval.v_list->lv_refcount;
19364 }
19365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019366 case VAR_DICT:
19367 if (from->vval.v_dict == NULL)
19368 to->vval.v_dict = NULL;
19369 else
19370 {
19371 to->vval.v_dict = from->vval.v_dict;
19372 ++to->vval.v_dict->dv_refcount;
19373 }
19374 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019375 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019377 break;
19378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379}
19380
19381/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019382 * Make a copy of an item.
19383 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019384 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19385 * reference to an already copied list/dict can be used.
19386 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019387 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019388 static int
19389item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019390 typval_T *from;
19391 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019392 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019393 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019394{
19395 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019396 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019397
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019399 {
19400 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019401 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019402 }
19403 ++recurse;
19404
19405 switch (from->v_type)
19406 {
19407 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019408#ifdef FEAT_FLOAT
19409 case VAR_FLOAT:
19410#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019411 case VAR_STRING:
19412 case VAR_FUNC:
19413 copy_tv(from, to);
19414 break;
19415 case VAR_LIST:
19416 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019417 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019418 if (from->vval.v_list == NULL)
19419 to->vval.v_list = NULL;
19420 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19421 {
19422 /* use the copy made earlier */
19423 to->vval.v_list = from->vval.v_list->lv_copylist;
19424 ++to->vval.v_list->lv_refcount;
19425 }
19426 else
19427 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19428 if (to->vval.v_list == NULL)
19429 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019430 break;
19431 case VAR_DICT:
19432 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019433 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019434 if (from->vval.v_dict == NULL)
19435 to->vval.v_dict = NULL;
19436 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19437 {
19438 /* use the copy made earlier */
19439 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19440 ++to->vval.v_dict->dv_refcount;
19441 }
19442 else
19443 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19444 if (to->vval.v_dict == NULL)
19445 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019446 break;
19447 default:
19448 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019449 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019450 }
19451 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019452 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019453}
19454
19455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 * ":echo expr1 ..." print each argument separated with a space, add a
19457 * newline at the end.
19458 * ":echon expr1 ..." print each argument plain.
19459 */
19460 void
19461ex_echo(eap)
19462 exarg_T *eap;
19463{
19464 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019465 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019466 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 char_u *p;
19468 int needclr = TRUE;
19469 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471
19472 if (eap->skip)
19473 ++emsg_skip;
19474 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19475 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019476 /* If eval1() causes an error message the text from the command may
19477 * still need to be cleared. E.g., "echo 22,44". */
19478 need_clr_eos = needclr;
19479
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 {
19483 /*
19484 * Report the invalid expression unless the expression evaluation
19485 * has been cancelled due to an aborting error, an interrupt, or an
19486 * exception.
19487 */
19488 if (!aborting())
19489 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019490 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 break;
19492 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019493 need_clr_eos = FALSE;
19494
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 if (!eap->skip)
19496 {
19497 if (atstart)
19498 {
19499 atstart = FALSE;
19500 /* Call msg_start() after eval1(), evaluating the expression
19501 * may cause a message to appear. */
19502 if (eap->cmdidx == CMD_echo)
19503 msg_start();
19504 }
19505 else if (eap->cmdidx == CMD_echo)
19506 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019507 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019508 if (p != NULL)
19509 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019511 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019513 if (*p != TAB && needclr)
19514 {
19515 /* remove any text still there from the command */
19516 msg_clr_eos();
19517 needclr = FALSE;
19518 }
19519 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 }
19521 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019522 {
19523#ifdef FEAT_MBYTE
19524 if (has_mbyte)
19525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019526 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019527
19528 (void)msg_outtrans_len_attr(p, i, echo_attr);
19529 p += i - 1;
19530 }
19531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019533 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019536 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 arg = skipwhite(arg);
19540 }
19541 eap->nextcmd = check_nextcmd(arg);
19542
19543 if (eap->skip)
19544 --emsg_skip;
19545 else
19546 {
19547 /* remove text that may still be there from the command */
19548 if (needclr)
19549 msg_clr_eos();
19550 if (eap->cmdidx == CMD_echo)
19551 msg_end();
19552 }
19553}
19554
19555/*
19556 * ":echohl {name}".
19557 */
19558 void
19559ex_echohl(eap)
19560 exarg_T *eap;
19561{
19562 int id;
19563
19564 id = syn_name2id(eap->arg);
19565 if (id == 0)
19566 echo_attr = 0;
19567 else
19568 echo_attr = syn_id2attr(id);
19569}
19570
19571/*
19572 * ":execute expr1 ..." execute the result of an expression.
19573 * ":echomsg expr1 ..." Print a message
19574 * ":echoerr expr1 ..." Print an error
19575 * Each gets spaces around each argument and a newline at the end for
19576 * echo commands
19577 */
19578 void
19579ex_execute(eap)
19580 exarg_T *eap;
19581{
19582 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 int ret = OK;
19585 char_u *p;
19586 garray_T ga;
19587 int len;
19588 int save_did_emsg;
19589
19590 ga_init2(&ga, 1, 80);
19591
19592 if (eap->skip)
19593 ++emsg_skip;
19594 while (*arg != NUL && *arg != '|' && *arg != '\n')
19595 {
19596 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 {
19599 /*
19600 * Report the invalid expression unless the expression evaluation
19601 * has been cancelled due to an aborting error, an interrupt, or an
19602 * exception.
19603 */
19604 if (!aborting())
19605 EMSG2(_(e_invexpr2), p);
19606 ret = FAIL;
19607 break;
19608 }
19609
19610 if (!eap->skip)
19611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 len = (int)STRLEN(p);
19614 if (ga_grow(&ga, len + 2) == FAIL)
19615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019616 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 ret = FAIL;
19618 break;
19619 }
19620 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 ga.ga_len += len;
19624 }
19625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019626 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 arg = skipwhite(arg);
19628 }
19629
19630 if (ret != FAIL && ga.ga_data != NULL)
19631 {
19632 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019635 out_flush();
19636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 else if (eap->cmdidx == CMD_echoerr)
19638 {
19639 /* We don't want to abort following commands, restore did_emsg. */
19640 save_did_emsg = did_emsg;
19641 EMSG((char_u *)ga.ga_data);
19642 if (!force_abort)
19643 did_emsg = save_did_emsg;
19644 }
19645 else if (eap->cmdidx == CMD_execute)
19646 do_cmdline((char_u *)ga.ga_data,
19647 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19648 }
19649
19650 ga_clear(&ga);
19651
19652 if (eap->skip)
19653 --emsg_skip;
19654
19655 eap->nextcmd = check_nextcmd(arg);
19656}
19657
19658/*
19659 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19660 * "arg" points to the "&" or '+' when called, to "option" when returning.
19661 * Returns NULL when no option name found. Otherwise pointer to the char
19662 * after the option name.
19663 */
19664 static char_u *
19665find_option_end(arg, opt_flags)
19666 char_u **arg;
19667 int *opt_flags;
19668{
19669 char_u *p = *arg;
19670
19671 ++p;
19672 if (*p == 'g' && p[1] == ':')
19673 {
19674 *opt_flags = OPT_GLOBAL;
19675 p += 2;
19676 }
19677 else if (*p == 'l' && p[1] == ':')
19678 {
19679 *opt_flags = OPT_LOCAL;
19680 p += 2;
19681 }
19682 else
19683 *opt_flags = 0;
19684
19685 if (!ASCII_ISALPHA(*p))
19686 return NULL;
19687 *arg = p;
19688
19689 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19690 p += 4; /* termcap option */
19691 else
19692 while (ASCII_ISALPHA(*p))
19693 ++p;
19694 return p;
19695}
19696
19697/*
19698 * ":function"
19699 */
19700 void
19701ex_function(eap)
19702 exarg_T *eap;
19703{
19704 char_u *theline;
19705 int j;
19706 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 char_u *name = NULL;
19709 char_u *p;
19710 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019711 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 garray_T newargs;
19713 garray_T newlines;
19714 int varargs = FALSE;
19715 int mustend = FALSE;
19716 int flags = 0;
19717 ufunc_T *fp;
19718 int indent;
19719 int nesting;
19720 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 dictitem_T *v;
19722 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019723 static int func_nr = 0; /* number for nameless function */
19724 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019725 hashtab_T *ht;
19726 int todo;
19727 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019728 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729
19730 /*
19731 * ":function" without argument: list functions.
19732 */
19733 if (ends_excmd(*eap->arg))
19734 {
19735 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019736 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019737 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019738 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019739 {
19740 if (!HASHITEM_EMPTY(hi))
19741 {
19742 --todo;
19743 fp = HI2UF(hi);
19744 if (!isdigit(*fp->uf_name))
19745 list_func_head(fp, FALSE);
19746 }
19747 }
19748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 eap->nextcmd = check_nextcmd(eap->arg);
19750 return;
19751 }
19752
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019753 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019754 * ":function /pat": list functions matching pattern.
19755 */
19756 if (*eap->arg == '/')
19757 {
19758 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19759 if (!eap->skip)
19760 {
19761 regmatch_T regmatch;
19762
19763 c = *p;
19764 *p = NUL;
19765 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19766 *p = c;
19767 if (regmatch.regprog != NULL)
19768 {
19769 regmatch.rm_ic = p_ic;
19770
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019771 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019772 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19773 {
19774 if (!HASHITEM_EMPTY(hi))
19775 {
19776 --todo;
19777 fp = HI2UF(hi);
19778 if (!isdigit(*fp->uf_name)
19779 && vim_regexec(&regmatch, fp->uf_name, 0))
19780 list_func_head(fp, FALSE);
19781 }
19782 }
19783 }
19784 }
19785 if (*p == '/')
19786 ++p;
19787 eap->nextcmd = check_nextcmd(p);
19788 return;
19789 }
19790
19791 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019792 * Get the function name. There are these situations:
19793 * func normal function name
19794 * "name" == func, "fudi.fd_dict" == NULL
19795 * dict.func new dictionary entry
19796 * "name" == NULL, "fudi.fd_dict" set,
19797 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19798 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019799 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019800 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19801 * dict.func existing dict entry that's not a Funcref
19802 * "name" == NULL, "fudi.fd_dict" set,
19803 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019806 name = trans_function_name(&p, eap->skip, 0, &fudi);
19807 paren = (vim_strchr(p, '(') != NULL);
19808 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 {
19810 /*
19811 * Return on an invalid expression in braces, unless the expression
19812 * evaluation has been cancelled due to an aborting error, an
19813 * interrupt, or an exception.
19814 */
19815 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019816 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019817 if (!eap->skip && fudi.fd_newkey != NULL)
19818 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019819 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 else
19823 eap->skip = TRUE;
19824 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019825
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 /* An error in a function call during evaluation of an expression in magic
19827 * braces should not cause the function not to be defined. */
19828 saved_did_emsg = did_emsg;
19829 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830
19831 /*
19832 * ":function func" with only function name: list function.
19833 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019834 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 {
19836 if (!ends_excmd(*skipwhite(p)))
19837 {
19838 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019839 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 }
19841 eap->nextcmd = check_nextcmd(p);
19842 if (eap->nextcmd != NULL)
19843 *p = NUL;
19844 if (!eap->skip && !got_int)
19845 {
19846 fp = find_func(name);
19847 if (fp != NULL)
19848 {
19849 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019850 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019852 if (FUNCLINE(fp, j) == NULL)
19853 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 msg_putchar('\n');
19855 msg_outnum((long)(j + 1));
19856 if (j < 9)
19857 msg_putchar(' ');
19858 if (j < 99)
19859 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019860 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 out_flush(); /* show a line at a time */
19862 ui_breakcheck();
19863 }
19864 if (!got_int)
19865 {
19866 msg_putchar('\n');
19867 msg_puts((char_u *)" endfunction");
19868 }
19869 }
19870 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019871 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019873 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874 }
19875
19876 /*
19877 * ":function name(arg1, arg2)" Define function.
19878 */
19879 p = skipwhite(p);
19880 if (*p != '(')
19881 {
19882 if (!eap->skip)
19883 {
19884 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019885 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 }
19887 /* attempt to continue by skipping some text */
19888 if (vim_strchr(p, '(') != NULL)
19889 p = vim_strchr(p, '(');
19890 }
19891 p = skipwhite(p + 1);
19892
19893 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19894 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19895
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019896 if (!eap->skip)
19897 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019898 /* Check the name of the function. Unless it's a dictionary function
19899 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019900 if (name != NULL)
19901 arg = name;
19902 else
19903 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019904 if (arg != NULL && (fudi.fd_di == NULL
19905 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019906 {
19907 if (*arg == K_SPECIAL)
19908 j = 3;
19909 else
19910 j = 0;
19911 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19912 : eval_isnamec(arg[j])))
19913 ++j;
19914 if (arg[j] != NUL)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019915 emsg_funcname(e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019916 }
19917 }
19918
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 /*
19920 * Isolate the arguments: "arg1, arg2, ...)"
19921 */
19922 while (*p != ')')
19923 {
19924 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19925 {
19926 varargs = TRUE;
19927 p += 3;
19928 mustend = TRUE;
19929 }
19930 else
19931 {
19932 arg = p;
19933 while (ASCII_ISALNUM(*p) || *p == '_')
19934 ++p;
19935 if (arg == p || isdigit(*arg)
19936 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19937 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19938 {
19939 if (!eap->skip)
19940 EMSG2(_("E125: Illegal argument: %s"), arg);
19941 break;
19942 }
19943 if (ga_grow(&newargs, 1) == FAIL)
19944 goto erret;
19945 c = *p;
19946 *p = NUL;
19947 arg = vim_strsave(arg);
19948 if (arg == NULL)
19949 goto erret;
19950 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19951 *p = c;
19952 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 if (*p == ',')
19954 ++p;
19955 else
19956 mustend = TRUE;
19957 }
19958 p = skipwhite(p);
19959 if (mustend && *p != ')')
19960 {
19961 if (!eap->skip)
19962 EMSG2(_(e_invarg2), eap->arg);
19963 break;
19964 }
19965 }
19966 ++p; /* skip the ')' */
19967
Bram Moolenaare9a41262005-01-15 22:18:47 +000019968 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 for (;;)
19970 {
19971 p = skipwhite(p);
19972 if (STRNCMP(p, "range", 5) == 0)
19973 {
19974 flags |= FC_RANGE;
19975 p += 5;
19976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019977 else if (STRNCMP(p, "dict", 4) == 0)
19978 {
19979 flags |= FC_DICT;
19980 p += 4;
19981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 else if (STRNCMP(p, "abort", 5) == 0)
19983 {
19984 flags |= FC_ABORT;
19985 p += 5;
19986 }
19987 else
19988 break;
19989 }
19990
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019991 /* When there is a line break use what follows for the function body.
19992 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19993 if (*p == '\n')
19994 line_arg = p + 1;
19995 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 EMSG(_(e_trailing));
19997
19998 /*
19999 * Read the body of the function, until ":endfunction" is found.
20000 */
20001 if (KeyTyped)
20002 {
20003 /* Check if the function already exists, don't let the user type the
20004 * whole function before telling him it doesn't work! For a script we
20005 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020006 if (!eap->skip && !eap->forceit)
20007 {
20008 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20009 EMSG(_(e_funcdict));
20010 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020011 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020014 if (!eap->skip && did_emsg)
20015 goto erret;
20016
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 msg_putchar('\n'); /* don't overwrite the function name */
20018 cmdline_row = msg_row;
20019 }
20020
20021 indent = 2;
20022 nesting = 0;
20023 for (;;)
20024 {
20025 msg_scroll = TRUE;
20026 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020027 sourcing_lnum_off = sourcing_lnum;
20028
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020029 if (line_arg != NULL)
20030 {
20031 /* Use eap->arg, split up in parts by line breaks. */
20032 theline = line_arg;
20033 p = vim_strchr(theline, '\n');
20034 if (p == NULL)
20035 line_arg += STRLEN(line_arg);
20036 else
20037 {
20038 *p = NUL;
20039 line_arg = p + 1;
20040 }
20041 }
20042 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 theline = getcmdline(':', 0L, indent);
20044 else
20045 theline = eap->getline(':', eap->cookie, indent);
20046 if (KeyTyped)
20047 lines_left = Rows - 1;
20048 if (theline == NULL)
20049 {
20050 EMSG(_("E126: Missing :endfunction"));
20051 goto erret;
20052 }
20053
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020054 /* Detect line continuation: sourcing_lnum increased more than one. */
20055 if (sourcing_lnum > sourcing_lnum_off + 1)
20056 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20057 else
20058 sourcing_lnum_off = 0;
20059
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 if (skip_until != NULL)
20061 {
20062 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20063 * don't check for ":endfunc". */
20064 if (STRCMP(theline, skip_until) == 0)
20065 {
20066 vim_free(skip_until);
20067 skip_until = NULL;
20068 }
20069 }
20070 else
20071 {
20072 /* skip ':' and blanks*/
20073 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20074 ;
20075
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020076 /* Check for "endfunction". */
20077 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020079 if (line_arg == NULL)
20080 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 break;
20082 }
20083
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020084 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 * at "end". */
20086 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20087 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020088 else if (STRNCMP(p, "if", 2) == 0
20089 || STRNCMP(p, "wh", 2) == 0
20090 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 || STRNCMP(p, "try", 3) == 0)
20092 indent += 2;
20093
20094 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020095 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020097 if (*p == '!')
20098 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 p += eval_fname_script(p);
20100 if (ASCII_ISALPHA(*p))
20101 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020102 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 if (*skipwhite(p) == '(')
20104 {
20105 ++nesting;
20106 indent += 2;
20107 }
20108 }
20109 }
20110
20111 /* Check for ":append" or ":insert". */
20112 p = skip_range(p, NULL);
20113 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20114 || (p[0] == 'i'
20115 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20116 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20117 skip_until = vim_strsave((char_u *)".");
20118
20119 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20120 arg = skipwhite(skiptowhite(p));
20121 if (arg[0] == '<' && arg[1] =='<'
20122 && ((p[0] == 'p' && p[1] == 'y'
20123 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20124 || (p[0] == 'p' && p[1] == 'e'
20125 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20126 || (p[0] == 't' && p[1] == 'c'
20127 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20128 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20129 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020130 || (p[0] == 'm' && p[1] == 'z'
20131 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 ))
20133 {
20134 /* ":python <<" continues until a dot, like ":append" */
20135 p = skipwhite(arg + 2);
20136 if (*p == NUL)
20137 skip_until = vim_strsave((char_u *)".");
20138 else
20139 skip_until = vim_strsave(p);
20140 }
20141 }
20142
20143 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020144 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020145 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020146 if (line_arg == NULL)
20147 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020149 }
20150
20151 /* Copy the line to newly allocated memory. get_one_sourceline()
20152 * allocates 250 bytes per line, this saves 80% on average. The cost
20153 * is an extra alloc/free. */
20154 p = vim_strsave(theline);
20155 if (p != NULL)
20156 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020157 if (line_arg == NULL)
20158 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020159 theline = p;
20160 }
20161
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020162 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20163
20164 /* Add NULL lines for continuation lines, so that the line count is
20165 * equal to the index in the growarray. */
20166 while (sourcing_lnum_off-- > 0)
20167 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020168
20169 /* Check for end of eap->arg. */
20170 if (line_arg != NULL && *line_arg == NUL)
20171 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 }
20173
20174 /* Don't define the function when skipping commands or when an error was
20175 * detected. */
20176 if (eap->skip || did_emsg)
20177 goto erret;
20178
20179 /*
20180 * If there are no errors, add the function
20181 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020182 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020183 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020184 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020186 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020187 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020188 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020189 goto erret;
20190 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020191
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020192 fp = find_func(name);
20193 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020195 if (!eap->forceit)
20196 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020197 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020198 goto erret;
20199 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020200 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020201 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020202 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020203 name);
20204 goto erret;
20205 }
20206 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020207 ga_clear_strings(&(fp->uf_args));
20208 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020209 vim_free(name);
20210 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 }
20213 else
20214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020215 char numbuf[20];
20216
20217 fp = NULL;
20218 if (fudi.fd_newkey == NULL && !eap->forceit)
20219 {
20220 EMSG(_(e_funcdict));
20221 goto erret;
20222 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020223 if (fudi.fd_di == NULL)
20224 {
20225 /* Can't add a function to a locked dictionary */
20226 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20227 goto erret;
20228 }
20229 /* Can't change an existing function if it is locked */
20230 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20231 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020232
20233 /* Give the function a sequential number. Can only be used with a
20234 * Funcref! */
20235 vim_free(name);
20236 sprintf(numbuf, "%d", ++func_nr);
20237 name = vim_strsave((char_u *)numbuf);
20238 if (name == NULL)
20239 goto erret;
20240 }
20241
20242 if (fp == NULL)
20243 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020244 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020245 {
20246 int slen, plen;
20247 char_u *scriptname;
20248
20249 /* Check that the autoload name matches the script name. */
20250 j = FAIL;
20251 if (sourcing_name != NULL)
20252 {
20253 scriptname = autoload_name(name);
20254 if (scriptname != NULL)
20255 {
20256 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020257 plen = (int)STRLEN(p);
20258 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020259 if (slen > plen && fnamecmp(p,
20260 sourcing_name + slen - plen) == 0)
20261 j = OK;
20262 vim_free(scriptname);
20263 }
20264 }
20265 if (j == FAIL)
20266 {
20267 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20268 goto erret;
20269 }
20270 }
20271
20272 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 if (fp == NULL)
20274 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020275
20276 if (fudi.fd_dict != NULL)
20277 {
20278 if (fudi.fd_di == NULL)
20279 {
20280 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020281 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020282 if (fudi.fd_di == NULL)
20283 {
20284 vim_free(fp);
20285 goto erret;
20286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020287 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20288 {
20289 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020290 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020291 goto erret;
20292 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020293 }
20294 else
20295 /* overwrite existing dict entry */
20296 clear_tv(&fudi.fd_di->di_tv);
20297 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020298 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020299 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020300 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020301
20302 /* behave like "dict" was used */
20303 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020304 }
20305
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020307 STRCPY(fp->uf_name, name);
20308 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020310 fp->uf_args = newargs;
20311 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020312#ifdef FEAT_PROFILE
20313 fp->uf_tml_count = NULL;
20314 fp->uf_tml_total = NULL;
20315 fp->uf_tml_self = NULL;
20316 fp->uf_profiling = FALSE;
20317 if (prof_def_func())
20318 func_do_profile(fp);
20319#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020320 fp->uf_varargs = varargs;
20321 fp->uf_flags = flags;
20322 fp->uf_calls = 0;
20323 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020324 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325
20326erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 ga_clear_strings(&newargs);
20328 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020329ret_free:
20330 vim_free(skip_until);
20331 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334}
20335
20336/*
20337 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020338 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020340 * flags:
20341 * TFN_INT: internal function name OK
20342 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 * Advances "pp" to just after the function name (if no error).
20344 */
20345 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020346trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 char_u **pp;
20348 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020349 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020350 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020352 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 char_u *start;
20354 char_u *end;
20355 int lead;
20356 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020358 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020359
20360 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020363
20364 /* Check for hard coded <SNR>: already translated function ID (from a user
20365 * command). */
20366 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20367 && (*pp)[2] == (int)KE_SNR)
20368 {
20369 *pp += 3;
20370 len = get_id_len(pp) + 3;
20371 return vim_strnsave(start, len);
20372 }
20373
20374 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20375 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020377 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020379
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020380 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20381 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020382 if (end == start)
20383 {
20384 if (!skip)
20385 EMSG(_("E129: Function name required"));
20386 goto theend;
20387 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020388 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020389 {
20390 /*
20391 * Report an invalid expression in braces, unless the expression
20392 * evaluation has been cancelled due to an aborting error, an
20393 * interrupt, or an exception.
20394 */
20395 if (!aborting())
20396 {
20397 if (end != NULL)
20398 EMSG2(_(e_invarg2), start);
20399 }
20400 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020401 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020402 goto theend;
20403 }
20404
20405 if (lv.ll_tv != NULL)
20406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020407 if (fdp != NULL)
20408 {
20409 fdp->fd_dict = lv.ll_dict;
20410 fdp->fd_newkey = lv.ll_newkey;
20411 lv.ll_newkey = NULL;
20412 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020413 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020414 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20415 {
20416 name = vim_strsave(lv.ll_tv->vval.v_string);
20417 *pp = end;
20418 }
20419 else
20420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020421 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20422 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020423 EMSG(_(e_funcref));
20424 else
20425 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020426 name = NULL;
20427 }
20428 goto theend;
20429 }
20430
20431 if (lv.ll_name == NULL)
20432 {
20433 /* Error found, but continue after the function name. */
20434 *pp = end;
20435 goto theend;
20436 }
20437
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020438 /* Check if the name is a Funcref. If so, use the value. */
20439 if (lv.ll_exp_name != NULL)
20440 {
20441 len = (int)STRLEN(lv.ll_exp_name);
20442 name = deref_func_name(lv.ll_exp_name, &len);
20443 if (name == lv.ll_exp_name)
20444 name = NULL;
20445 }
20446 else
20447 {
20448 len = (int)(end - *pp);
20449 name = deref_func_name(*pp, &len);
20450 if (name == *pp)
20451 name = NULL;
20452 }
20453 if (name != NULL)
20454 {
20455 name = vim_strsave(name);
20456 *pp = end;
20457 goto theend;
20458 }
20459
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020460 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020461 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020462 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020463 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20464 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20465 {
20466 /* When there was "s:" already or the name expanded to get a
20467 * leading "s:" then remove it. */
20468 lv.ll_name += 2;
20469 len -= 2;
20470 lead = 2;
20471 }
20472 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020473 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020474 {
20475 if (lead == 2) /* skip over "s:" */
20476 lv.ll_name += 2;
20477 len = (int)(end - lv.ll_name);
20478 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020479
20480 /*
20481 * Copy the function name to allocated memory.
20482 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20483 * Accept <SNR>123_name() outside a script.
20484 */
20485 if (skip)
20486 lead = 0; /* do nothing */
20487 else if (lead > 0)
20488 {
20489 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020490 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20491 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020492 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020493 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020494 if (current_SID <= 0)
20495 {
20496 EMSG(_(e_usingsid));
20497 goto theend;
20498 }
20499 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20500 lead += (int)STRLEN(sid_buf);
20501 }
20502 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020503 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020504 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020505 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020506 goto theend;
20507 }
20508 name = alloc((unsigned)(len + lead + 1));
20509 if (name != NULL)
20510 {
20511 if (lead > 0)
20512 {
20513 name[0] = K_SPECIAL;
20514 name[1] = KS_EXTRA;
20515 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020516 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020517 STRCPY(name + 3, sid_buf);
20518 }
20519 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20520 name[len + lead] = NUL;
20521 }
20522 *pp = end;
20523
20524theend:
20525 clear_lval(&lv);
20526 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527}
20528
20529/*
20530 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20531 * Return 2 if "p" starts with "s:".
20532 * Return 0 otherwise.
20533 */
20534 static int
20535eval_fname_script(p)
20536 char_u *p;
20537{
20538 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20539 || STRNICMP(p + 1, "SNR>", 4) == 0))
20540 return 5;
20541 if (p[0] == 's' && p[1] == ':')
20542 return 2;
20543 return 0;
20544}
20545
20546/*
20547 * Return TRUE if "p" starts with "<SID>" or "s:".
20548 * Only works if eval_fname_script() returned non-zero for "p"!
20549 */
20550 static int
20551eval_fname_sid(p)
20552 char_u *p;
20553{
20554 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20555}
20556
20557/*
20558 * List the head of the function: "name(arg1, arg2)".
20559 */
20560 static void
20561list_func_head(fp, indent)
20562 ufunc_T *fp;
20563 int indent;
20564{
20565 int j;
20566
20567 msg_start();
20568 if (indent)
20569 MSG_PUTS(" ");
20570 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020571 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 {
20573 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020574 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 }
20576 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020577 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020579 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 {
20581 if (j)
20582 MSG_PUTS(", ");
20583 msg_puts(FUNCARG(fp, j));
20584 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020585 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 {
20587 if (j)
20588 MSG_PUTS(", ");
20589 MSG_PUTS("...");
20590 }
20591 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020592 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020593 if (p_verbose > 0)
20594 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595}
20596
20597/*
20598 * Find a function by name, return pointer to it in ufuncs.
20599 * Return NULL for unknown function.
20600 */
20601 static ufunc_T *
20602find_func(name)
20603 char_u *name;
20604{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020605 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020607 hi = hash_find(&func_hashtab, name);
20608 if (!HASHITEM_EMPTY(hi))
20609 return HI2UF(hi);
20610 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611}
20612
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020613#if defined(EXITFREE) || defined(PROTO)
20614 void
20615free_all_functions()
20616{
20617 hashitem_T *hi;
20618
20619 /* Need to start all over every time, because func_free() may change the
20620 * hash table. */
20621 while (func_hashtab.ht_used > 0)
20622 for (hi = func_hashtab.ht_array; ; ++hi)
20623 if (!HASHITEM_EMPTY(hi))
20624 {
20625 func_free(HI2UF(hi));
20626 break;
20627 }
20628}
20629#endif
20630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020631/*
20632 * Return TRUE if a function "name" exists.
20633 */
20634 static int
20635function_exists(name)
20636 char_u *name;
20637{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020638 char_u *nm = name;
20639 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020640 int n = FALSE;
20641
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020642 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020643 nm = skipwhite(nm);
20644
20645 /* Only accept "funcname", "funcname ", "funcname (..." and
20646 * "funcname(...", not "funcname!...". */
20647 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020648 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020649 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020650 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020651 else
20652 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020653 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020654 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020655 return n;
20656}
20657
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020658/*
20659 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020660 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020661 */
20662 static int
20663builtin_function(name)
20664 char_u *name;
20665{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020666 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20667 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020668}
20669
Bram Moolenaar05159a02005-02-26 23:04:13 +000020670#if defined(FEAT_PROFILE) || defined(PROTO)
20671/*
20672 * Start profiling function "fp".
20673 */
20674 static void
20675func_do_profile(fp)
20676 ufunc_T *fp;
20677{
20678 fp->uf_tm_count = 0;
20679 profile_zero(&fp->uf_tm_self);
20680 profile_zero(&fp->uf_tm_total);
20681 if (fp->uf_tml_count == NULL)
20682 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20683 (sizeof(int) * fp->uf_lines.ga_len));
20684 if (fp->uf_tml_total == NULL)
20685 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20686 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20687 if (fp->uf_tml_self == NULL)
20688 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20689 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20690 fp->uf_tml_idx = -1;
20691 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20692 || fp->uf_tml_self == NULL)
20693 return; /* out of memory */
20694
20695 fp->uf_profiling = TRUE;
20696}
20697
20698/*
20699 * Dump the profiling results for all functions in file "fd".
20700 */
20701 void
20702func_dump_profile(fd)
20703 FILE *fd;
20704{
20705 hashitem_T *hi;
20706 int todo;
20707 ufunc_T *fp;
20708 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020709 ufunc_T **sorttab;
20710 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020711
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020712 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020713 if (todo == 0)
20714 return; /* nothing to dump */
20715
Bram Moolenaar73830342005-02-28 22:48:19 +000020716 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20717
Bram Moolenaar05159a02005-02-26 23:04:13 +000020718 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20719 {
20720 if (!HASHITEM_EMPTY(hi))
20721 {
20722 --todo;
20723 fp = HI2UF(hi);
20724 if (fp->uf_profiling)
20725 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020726 if (sorttab != NULL)
20727 sorttab[st_len++] = fp;
20728
Bram Moolenaar05159a02005-02-26 23:04:13 +000020729 if (fp->uf_name[0] == K_SPECIAL)
20730 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20731 else
20732 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20733 if (fp->uf_tm_count == 1)
20734 fprintf(fd, "Called 1 time\n");
20735 else
20736 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20737 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20738 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20739 fprintf(fd, "\n");
20740 fprintf(fd, "count total (s) self (s)\n");
20741
20742 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20743 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020744 if (FUNCLINE(fp, i) == NULL)
20745 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020746 prof_func_line(fd, fp->uf_tml_count[i],
20747 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020748 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20749 }
20750 fprintf(fd, "\n");
20751 }
20752 }
20753 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020754
20755 if (sorttab != NULL && st_len > 0)
20756 {
20757 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20758 prof_total_cmp);
20759 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20760 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20761 prof_self_cmp);
20762 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20763 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020764
20765 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020766}
Bram Moolenaar73830342005-02-28 22:48:19 +000020767
20768 static void
20769prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20770 FILE *fd;
20771 ufunc_T **sorttab;
20772 int st_len;
20773 char *title;
20774 int prefer_self; /* when equal print only self time */
20775{
20776 int i;
20777 ufunc_T *fp;
20778
20779 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20780 fprintf(fd, "count total (s) self (s) function\n");
20781 for (i = 0; i < 20 && i < st_len; ++i)
20782 {
20783 fp = sorttab[i];
20784 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20785 prefer_self);
20786 if (fp->uf_name[0] == K_SPECIAL)
20787 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20788 else
20789 fprintf(fd, " %s()\n", fp->uf_name);
20790 }
20791 fprintf(fd, "\n");
20792}
20793
20794/*
20795 * Print the count and times for one function or function line.
20796 */
20797 static void
20798prof_func_line(fd, count, total, self, prefer_self)
20799 FILE *fd;
20800 int count;
20801 proftime_T *total;
20802 proftime_T *self;
20803 int prefer_self; /* when equal print only self time */
20804{
20805 if (count > 0)
20806 {
20807 fprintf(fd, "%5d ", count);
20808 if (prefer_self && profile_equal(total, self))
20809 fprintf(fd, " ");
20810 else
20811 fprintf(fd, "%s ", profile_msg(total));
20812 if (!prefer_self && profile_equal(total, self))
20813 fprintf(fd, " ");
20814 else
20815 fprintf(fd, "%s ", profile_msg(self));
20816 }
20817 else
20818 fprintf(fd, " ");
20819}
20820
20821/*
20822 * Compare function for total time sorting.
20823 */
20824 static int
20825#ifdef __BORLANDC__
20826_RTLENTRYF
20827#endif
20828prof_total_cmp(s1, s2)
20829 const void *s1;
20830 const void *s2;
20831{
20832 ufunc_T *p1, *p2;
20833
20834 p1 = *(ufunc_T **)s1;
20835 p2 = *(ufunc_T **)s2;
20836 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20837}
20838
20839/*
20840 * Compare function for self time sorting.
20841 */
20842 static int
20843#ifdef __BORLANDC__
20844_RTLENTRYF
20845#endif
20846prof_self_cmp(s1, s2)
20847 const void *s1;
20848 const void *s2;
20849{
20850 ufunc_T *p1, *p2;
20851
20852 p1 = *(ufunc_T **)s1;
20853 p2 = *(ufunc_T **)s2;
20854 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20855}
20856
Bram Moolenaar05159a02005-02-26 23:04:13 +000020857#endif
20858
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020859/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020860 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020861 * Return TRUE if a package was loaded.
20862 */
20863 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020864script_autoload(name, reload)
20865 char_u *name;
20866 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020867{
20868 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020869 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020870 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020871 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020872
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020873 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020874 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020875 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020876 return FALSE;
20877
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020878 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020879
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020880 /* Find the name in the list of previously loaded package names. Skip
20881 * "autoload/", it's always the same. */
20882 for (i = 0; i < ga_loaded.ga_len; ++i)
20883 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20884 break;
20885 if (!reload && i < ga_loaded.ga_len)
20886 ret = FALSE; /* was loaded already */
20887 else
20888 {
20889 /* Remember the name if it wasn't loaded already. */
20890 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20891 {
20892 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20893 tofree = NULL;
20894 }
20895
20896 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020897 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020898 ret = TRUE;
20899 }
20900
20901 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020902 return ret;
20903}
20904
20905/*
20906 * Return the autoload script name for a function or variable name.
20907 * Returns NULL when out of memory.
20908 */
20909 static char_u *
20910autoload_name(name)
20911 char_u *name;
20912{
20913 char_u *p;
20914 char_u *scriptname;
20915
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020916 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020917 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20918 if (scriptname == NULL)
20919 return FALSE;
20920 STRCPY(scriptname, "autoload/");
20921 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020922 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020923 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020924 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020925 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020926 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020927}
20928
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20930
20931/*
20932 * Function given to ExpandGeneric() to obtain the list of user defined
20933 * function names.
20934 */
20935 char_u *
20936get_user_func_name(xp, idx)
20937 expand_T *xp;
20938 int idx;
20939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020940 static long_u done;
20941 static hashitem_T *hi;
20942 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943
20944 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020946 done = 0;
20947 hi = func_hashtab.ht_array;
20948 }
20949 if (done < func_hashtab.ht_used)
20950 {
20951 if (done++ > 0)
20952 ++hi;
20953 while (HASHITEM_EMPTY(hi))
20954 ++hi;
20955 fp = HI2UF(hi);
20956
20957 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20958 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959
20960 cat_func_name(IObuff, fp);
20961 if (xp->xp_context != EXPAND_USER_FUNC)
20962 {
20963 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 STRCAT(IObuff, ")");
20966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 return IObuff;
20968 }
20969 return NULL;
20970}
20971
20972#endif /* FEAT_CMDL_COMPL */
20973
20974/*
20975 * Copy the function name of "fp" to buffer "buf".
20976 * "buf" must be able to hold the function name plus three bytes.
20977 * Takes care of script-local function names.
20978 */
20979 static void
20980cat_func_name(buf, fp)
20981 char_u *buf;
20982 ufunc_T *fp;
20983{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020984 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 {
20986 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020987 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 }
20989 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020990 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991}
20992
20993/*
20994 * ":delfunction {name}"
20995 */
20996 void
20997ex_delfunction(eap)
20998 exarg_T *eap;
20999{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021000 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 char_u *p;
21002 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021003 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004
21005 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021006 name = trans_function_name(&p, eap->skip, 0, &fudi);
21007 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021009 {
21010 if (fudi.fd_dict != NULL && !eap->skip)
21011 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 if (!ends_excmd(*skipwhite(p)))
21015 {
21016 vim_free(name);
21017 EMSG(_(e_trailing));
21018 return;
21019 }
21020 eap->nextcmd = check_nextcmd(p);
21021 if (eap->nextcmd != NULL)
21022 *p = NUL;
21023
21024 if (!eap->skip)
21025 fp = find_func(name);
21026 vim_free(name);
21027
21028 if (!eap->skip)
21029 {
21030 if (fp == NULL)
21031 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021032 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 return;
21034 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021035 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 {
21037 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21038 return;
21039 }
21040
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021041 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 /* Delete the dict item that refers to the function, it will
21044 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021045 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021047 else
21048 func_free(fp);
21049 }
21050}
21051
21052/*
21053 * Free a function and remove it from the list of functions.
21054 */
21055 static void
21056func_free(fp)
21057 ufunc_T *fp;
21058{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021059 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060
21061 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021062 ga_clear_strings(&(fp->uf_args));
21063 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021064#ifdef FEAT_PROFILE
21065 vim_free(fp->uf_tml_count);
21066 vim_free(fp->uf_tml_total);
21067 vim_free(fp->uf_tml_self);
21068#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021069
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021070 /* remove the function from the function hashtable */
21071 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21072 if (HASHITEM_EMPTY(hi))
21073 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021074 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021075 hash_remove(&func_hashtab, hi);
21076
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021077 vim_free(fp);
21078}
21079
21080/*
21081 * Unreference a Function: decrement the reference count and free it when it
21082 * becomes zero. Only for numbered functions.
21083 */
21084 static void
21085func_unref(name)
21086 char_u *name;
21087{
21088 ufunc_T *fp;
21089
21090 if (name != NULL && isdigit(*name))
21091 {
21092 fp = find_func(name);
21093 if (fp == NULL)
21094 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021095 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021096 {
21097 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021098 * when "uf_calls" becomes zero. */
21099 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021100 func_free(fp);
21101 }
21102 }
21103}
21104
21105/*
21106 * Count a reference to a Function.
21107 */
21108 static void
21109func_ref(name)
21110 char_u *name;
21111{
21112 ufunc_T *fp;
21113
21114 if (name != NULL && isdigit(*name))
21115 {
21116 fp = find_func(name);
21117 if (fp == NULL)
21118 EMSG2(_(e_intern2), "func_ref()");
21119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021120 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 }
21122}
21123
21124/*
21125 * Call a user function.
21126 */
21127 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021128call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 ufunc_T *fp; /* pointer to function */
21130 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 typval_T *argvars; /* arguments */
21132 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 linenr_T firstline; /* first line of range */
21134 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136{
Bram Moolenaar33570922005-01-25 22:26:29 +000021137 char_u *save_sourcing_name;
21138 linenr_T save_sourcing_lnum;
21139 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021140 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021141 int save_did_emsg;
21142 static int depth = 0;
21143 dictitem_T *v;
21144 int fixvar_idx = 0; /* index in fixvar[] */
21145 int i;
21146 int ai;
21147 char_u numbuf[NUMBUFLEN];
21148 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021149#ifdef FEAT_PROFILE
21150 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021151 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153
21154 /* If depth of calling is getting too high, don't execute the function */
21155 if (depth >= p_mfd)
21156 {
21157 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->v_type = VAR_NUMBER;
21159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 return;
21161 }
21162 ++depth;
21163
21164 line_breakcheck(); /* check for CTRL-C hit */
21165
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021166 fc = (funccall_T *)alloc(sizeof(funccall_T));
21167 fc->caller = current_funccal;
21168 current_funccal = fc;
21169 fc->func = fp;
21170 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021171 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021172 fc->linenr = 0;
21173 fc->returned = FALSE;
21174 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021176 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21177 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178
Bram Moolenaar33570922005-01-25 22:26:29 +000021179 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021180 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021181 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21182 * each argument variable and saves a lot of time.
21183 */
21184 /*
21185 * Init l: variables.
21186 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021187 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021188 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021189 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021190 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21191 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021192 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021193 name = v->di_key;
21194 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021195 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021196 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021197 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021198 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021199 v->di_tv.vval.v_dict = selfdict;
21200 ++selfdict->dv_refcount;
21201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021202
Bram Moolenaar33570922005-01-25 22:26:29 +000021203 /*
21204 * Init a: variables.
21205 * Set a:0 to "argcount".
21206 * Set a:000 to a list with room for the "..." arguments.
21207 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021208 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21209 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021210 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021211 /* Use "name" to avoid a warning from some compiler that checks the
21212 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021213 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021214 name = v->di_key;
21215 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021216 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021217 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021218 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021219 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021220 v->di_tv.vval.v_list = &fc->l_varlist;
21221 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21222 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21223 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021224
21225 /*
21226 * Set a:firstline to "firstline" and a:lastline to "lastline".
21227 * Set a:name to named arguments.
21228 * Set a:N to the "..." arguments.
21229 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021230 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021231 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021232 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021233 (varnumber_T)lastline);
21234 for (i = 0; i < argcount; ++i)
21235 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021236 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021237 if (ai < 0)
21238 /* named argument a:name */
21239 name = FUNCARG(fp, i);
21240 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021241 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021242 /* "..." argument a:1, a:2, etc. */
21243 sprintf((char *)numbuf, "%d", ai + 1);
21244 name = numbuf;
21245 }
21246 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21247 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021248 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021249 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21250 }
21251 else
21252 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021253 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21254 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021255 if (v == NULL)
21256 break;
21257 v->di_flags = DI_FLAGS_RO;
21258 }
21259 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021260 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021261
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021262 /* Note: the values are copied directly to avoid alloc/free.
21263 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021265 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021266
21267 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21268 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021269 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21270 fc->l_listitems[ai].li_tv = argvars[i];
21271 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021272 }
21273 }
21274
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 /* Don't redraw while executing the function. */
21276 ++RedrawingDisabled;
21277 save_sourcing_name = sourcing_name;
21278 save_sourcing_lnum = sourcing_lnum;
21279 sourcing_lnum = 1;
21280 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021281 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 if (sourcing_name != NULL)
21283 {
21284 if (save_sourcing_name != NULL
21285 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21286 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21287 else
21288 STRCPY(sourcing_name, "function ");
21289 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21290
21291 if (p_verbose >= 12)
21292 {
21293 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021294 verbose_enter_scroll();
21295
Bram Moolenaar555b2802005-05-19 21:08:39 +000021296 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 if (p_verbose >= 14)
21298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021300 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021301 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021302 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303
21304 msg_puts((char_u *)"(");
21305 for (i = 0; i < argcount; ++i)
21306 {
21307 if (i > 0)
21308 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021309 if (argvars[i].v_type == VAR_NUMBER)
21310 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 else
21312 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021313 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21314 if (s != NULL)
21315 {
21316 trunc_string(s, buf, MSG_BUF_CLEN);
21317 msg_puts(buf);
21318 vim_free(tofree);
21319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021320 }
21321 }
21322 msg_puts((char_u *)")");
21323 }
21324 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021325
21326 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 --no_wait_return;
21328 }
21329 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021330#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021331 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021332 {
21333 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21334 func_do_profile(fp);
21335 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021336 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021337 {
21338 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021339 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021340 profile_zero(&fp->uf_tm_children);
21341 }
21342 script_prof_save(&wait_start);
21343 }
21344#endif
21345
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021347 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 save_did_emsg = did_emsg;
21349 did_emsg = FALSE;
21350
21351 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021352 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21354
21355 --RedrawingDisabled;
21356
21357 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021358 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 clear_tv(rettv);
21361 rettv->v_type = VAR_NUMBER;
21362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 }
21364
Bram Moolenaar05159a02005-02-26 23:04:13 +000021365#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021366 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021367 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021368 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021369 profile_end(&call_start);
21370 profile_sub_wait(&wait_start, &call_start);
21371 profile_add(&fp->uf_tm_total, &call_start);
21372 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021373 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021374 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021375 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21376 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021377 }
21378 }
21379#endif
21380
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 /* when being verbose, mention the return value */
21382 if (p_verbose >= 12)
21383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021385 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021388 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021389 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021390 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021391 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021394 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021395 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021396 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021397 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021398
Bram Moolenaar555b2802005-05-19 21:08:39 +000021399 /* The value may be very long. Skip the middle part, so that we
21400 * have some idea how it starts and ends. smsg() would always
21401 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021402 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021403 if (s != NULL)
21404 {
21405 trunc_string(s, buf, MSG_BUF_CLEN);
21406 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21407 vim_free(tofree);
21408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 }
21410 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021411
21412 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 --no_wait_return;
21414 }
21415
21416 vim_free(sourcing_name);
21417 sourcing_name = save_sourcing_name;
21418 sourcing_lnum = save_sourcing_lnum;
21419 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021420#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021421 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021422 script_prof_restore(&wait_start);
21423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424
21425 if (p_verbose >= 12 && sourcing_name != NULL)
21426 {
21427 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021428 verbose_enter_scroll();
21429
Bram Moolenaar555b2802005-05-19 21:08:39 +000021430 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021432
21433 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 --no_wait_return;
21435 }
21436
21437 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021438 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021440
21441 /* if the a:000 list and the a: dict are not referenced we can free the
21442 * funccall_T and what's in it. */
21443 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21444 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21445 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21446 {
21447 free_funccal(fc, FALSE);
21448 }
21449 else
21450 {
21451 hashitem_T *hi;
21452 listitem_T *li;
21453 int todo;
21454
21455 /* "fc" is still in use. This can happen when returning "a:000" or
21456 * assigning "l:" to a global variable.
21457 * Link "fc" in the list for garbage collection later. */
21458 fc->caller = previous_funccal;
21459 previous_funccal = fc;
21460
21461 /* Make a copy of the a: variables, since we didn't do that above. */
21462 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21463 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21464 {
21465 if (!HASHITEM_EMPTY(hi))
21466 {
21467 --todo;
21468 v = HI2DI(hi);
21469 copy_tv(&v->di_tv, &v->di_tv);
21470 }
21471 }
21472
21473 /* Make a copy of the a:000 items, since we didn't do that above. */
21474 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21475 copy_tv(&li->li_tv, &li->li_tv);
21476 }
21477}
21478
21479/*
21480 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021481 * referenced from anywhere.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021482 */
21483 static int
21484can_free_funccal(fc, copyID)
21485 funccall_T *fc;
21486 int copyID;
21487{
21488 return (fc->l_varlist.lv_copyID != copyID
21489 && fc->l_vars.dv_copyID != copyID
21490 && fc->l_avars.dv_copyID != copyID);
21491}
21492
21493/*
21494 * Free "fc" and what it contains.
21495 */
21496 static void
21497free_funccal(fc, free_val)
21498 funccall_T *fc;
21499 int free_val; /* a: vars were allocated */
21500{
21501 listitem_T *li;
21502
21503 /* The a: variables typevals may not have been allocated, only free the
21504 * allocated variables. */
21505 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21506
21507 /* free all l: variables */
21508 vars_clear(&fc->l_vars.dv_hashtab);
21509
21510 /* Free the a:000 variables if they were allocated. */
21511 if (free_val)
21512 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21513 clear_tv(&li->li_tv);
21514
21515 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516}
21517
21518/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021519 * Add a number variable "name" to dict "dp" with value "nr".
21520 */
21521 static void
21522add_nr_var(dp, v, name, nr)
21523 dict_T *dp;
21524 dictitem_T *v;
21525 char *name;
21526 varnumber_T nr;
21527{
21528 STRCPY(v->di_key, name);
21529 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21530 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21531 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021532 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 v->di_tv.vval.v_number = nr;
21534}
21535
21536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 * ":return [expr]"
21538 */
21539 void
21540ex_return(eap)
21541 exarg_T *eap;
21542{
21543 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021544 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 int returning = FALSE;
21546
21547 if (current_funccal == NULL)
21548 {
21549 EMSG(_("E133: :return not inside a function"));
21550 return;
21551 }
21552
21553 if (eap->skip)
21554 ++emsg_skip;
21555
21556 eap->nextcmd = NULL;
21557 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021558 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 {
21560 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021561 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021563 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 }
21565 /* It's safer to return also on error. */
21566 else if (!eap->skip)
21567 {
21568 /*
21569 * Return unless the expression evaluation has been cancelled due to an
21570 * aborting error, an interrupt, or an exception.
21571 */
21572 if (!aborting())
21573 returning = do_return(eap, FALSE, TRUE, NULL);
21574 }
21575
21576 /* When skipping or the return gets pending, advance to the next command
21577 * in this line (!returning). Otherwise, ignore the rest of the line.
21578 * Following lines will be ignored by get_func_line(). */
21579 if (returning)
21580 eap->nextcmd = NULL;
21581 else if (eap->nextcmd == NULL) /* no argument */
21582 eap->nextcmd = check_nextcmd(arg);
21583
21584 if (eap->skip)
21585 --emsg_skip;
21586}
21587
21588/*
21589 * Return from a function. Possibly makes the return pending. Also called
21590 * for a pending return at the ":endtry" or after returning from an extra
21591 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021592 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021593 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 * FALSE when the return gets pending.
21595 */
21596 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021597do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 exarg_T *eap;
21599 int reanimate;
21600 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021601 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602{
21603 int idx;
21604 struct condstack *cstack = eap->cstack;
21605
21606 if (reanimate)
21607 /* Undo the return. */
21608 current_funccal->returned = FALSE;
21609
21610 /*
21611 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21612 * not in its finally clause (which then is to be executed next) is found.
21613 * In this case, make the ":return" pending for execution at the ":endtry".
21614 * Otherwise, return normally.
21615 */
21616 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21617 if (idx >= 0)
21618 {
21619 cstack->cs_pending[idx] = CSTP_RETURN;
21620
21621 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622 /* A pending return again gets pending. "rettv" points to an
21623 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 else
21627 {
21628 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021629 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021631 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021633 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 {
21635 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021636 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021637 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 else
21639 EMSG(_(e_outofmem));
21640 }
21641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021642 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643
21644 if (reanimate)
21645 {
21646 /* The pending return value could be overwritten by a ":return"
21647 * without argument in a finally clause; reset the default
21648 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021649 current_funccal->rettv->v_type = VAR_NUMBER;
21650 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 }
21652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021653 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 }
21655 else
21656 {
21657 current_funccal->returned = TRUE;
21658
21659 /* If the return is carried out now, store the return value. For
21660 * a return immediately after reanimation, the value is already
21661 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021662 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021664 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021667 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 }
21669 }
21670
21671 return idx < 0;
21672}
21673
21674/*
21675 * Free the variable with a pending return value.
21676 */
21677 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021678discard_pending_return(rettv)
21679 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680{
Bram Moolenaar33570922005-01-25 22:26:29 +000021681 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682}
21683
21684/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021685 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 * is an allocated string. Used by report_pending() for verbose messages.
21687 */
21688 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021689get_return_cmd(rettv)
21690 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021692 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021693 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021694 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021696 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021697 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021698 if (s == NULL)
21699 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021700
21701 STRCPY(IObuff, ":return ");
21702 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21703 if (STRLEN(s) + 8 >= IOSIZE)
21704 STRCPY(IObuff + IOSIZE - 4, "...");
21705 vim_free(tofree);
21706 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707}
21708
21709/*
21710 * Get next function line.
21711 * Called by do_cmdline() to get the next line.
21712 * Returns allocated string, or NULL for end of function.
21713 */
21714/* ARGSUSED */
21715 char_u *
21716get_func_line(c, cookie, indent)
21717 int c; /* not used */
21718 void *cookie;
21719 int indent; /* not used */
21720{
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021722 ufunc_T *fp = fcp->func;
21723 char_u *retval;
21724 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725
21726 /* If breakpoints have been added/deleted need to check for it. */
21727 if (fcp->dbg_tick != debug_tick)
21728 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021729 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 sourcing_lnum);
21731 fcp->dbg_tick = debug_tick;
21732 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021733#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021734 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021735 func_line_end(cookie);
21736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737
Bram Moolenaar05159a02005-02-26 23:04:13 +000021738 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021739 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21740 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 retval = NULL;
21742 else
21743 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021744 /* Skip NULL lines (continuation lines). */
21745 while (fcp->linenr < gap->ga_len
21746 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21747 ++fcp->linenr;
21748 if (fcp->linenr >= gap->ga_len)
21749 retval = NULL;
21750 else
21751 {
21752 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21753 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021754#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021755 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021756 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021757#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 }
21760
21761 /* Did we encounter a breakpoint? */
21762 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21763 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021764 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021766 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 sourcing_lnum);
21768 fcp->dbg_tick = debug_tick;
21769 }
21770
21771 return retval;
21772}
21773
Bram Moolenaar05159a02005-02-26 23:04:13 +000021774#if defined(FEAT_PROFILE) || defined(PROTO)
21775/*
21776 * Called when starting to read a function line.
21777 * "sourcing_lnum" must be correct!
21778 * When skipping lines it may not actually be executed, but we won't find out
21779 * until later and we need to store the time now.
21780 */
21781 void
21782func_line_start(cookie)
21783 void *cookie;
21784{
21785 funccall_T *fcp = (funccall_T *)cookie;
21786 ufunc_T *fp = fcp->func;
21787
21788 if (fp->uf_profiling && sourcing_lnum >= 1
21789 && sourcing_lnum <= fp->uf_lines.ga_len)
21790 {
21791 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021792 /* Skip continuation lines. */
21793 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21794 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021795 fp->uf_tml_execed = FALSE;
21796 profile_start(&fp->uf_tml_start);
21797 profile_zero(&fp->uf_tml_children);
21798 profile_get_wait(&fp->uf_tml_wait);
21799 }
21800}
21801
21802/*
21803 * Called when actually executing a function line.
21804 */
21805 void
21806func_line_exec(cookie)
21807 void *cookie;
21808{
21809 funccall_T *fcp = (funccall_T *)cookie;
21810 ufunc_T *fp = fcp->func;
21811
21812 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21813 fp->uf_tml_execed = TRUE;
21814}
21815
21816/*
21817 * Called when done with a function line.
21818 */
21819 void
21820func_line_end(cookie)
21821 void *cookie;
21822{
21823 funccall_T *fcp = (funccall_T *)cookie;
21824 ufunc_T *fp = fcp->func;
21825
21826 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21827 {
21828 if (fp->uf_tml_execed)
21829 {
21830 ++fp->uf_tml_count[fp->uf_tml_idx];
21831 profile_end(&fp->uf_tml_start);
21832 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021833 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021834 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21835 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021836 }
21837 fp->uf_tml_idx = -1;
21838 }
21839}
21840#endif
21841
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842/*
21843 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021844 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 */
21846 int
21847func_has_ended(cookie)
21848 void *cookie;
21849{
Bram Moolenaar33570922005-01-25 22:26:29 +000021850 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851
21852 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21853 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021854 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 || fcp->returned);
21856}
21857
21858/*
21859 * return TRUE if cookie indicates a function which "abort"s on errors.
21860 */
21861 int
21862func_has_abort(cookie)
21863 void *cookie;
21864{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021865 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866}
21867
21868#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21869typedef enum
21870{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021871 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21872 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21873 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874} var_flavour_T;
21875
21876static var_flavour_T var_flavour __ARGS((char_u *varname));
21877
21878 static var_flavour_T
21879var_flavour(varname)
21880 char_u *varname;
21881{
21882 char_u *p = varname;
21883
21884 if (ASCII_ISUPPER(*p))
21885 {
21886 while (*(++p))
21887 if (ASCII_ISLOWER(*p))
21888 return VAR_FLAVOUR_SESSION;
21889 return VAR_FLAVOUR_VIMINFO;
21890 }
21891 else
21892 return VAR_FLAVOUR_DEFAULT;
21893}
21894#endif
21895
21896#if defined(FEAT_VIMINFO) || defined(PROTO)
21897/*
21898 * Restore global vars that start with a capital from the viminfo file
21899 */
21900 int
21901read_viminfo_varlist(virp, writing)
21902 vir_T *virp;
21903 int writing;
21904{
21905 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021906 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908
21909 if (!writing && (find_viminfo_parameter('!') != NULL))
21910 {
21911 tab = vim_strchr(virp->vir_line + 1, '\t');
21912 if (tab != NULL)
21913 {
21914 *tab++ = '\0'; /* isolate the variable name */
21915 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021916 type = VAR_STRING;
21917#ifdef FEAT_FLOAT
21918 else if (*tab == 'F')
21919 type = VAR_FLOAT;
21920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921
21922 tab = vim_strchr(tab, '\t');
21923 if (tab != NULL)
21924 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021925 tv.v_type = type;
21926 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021927 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021929#ifdef FEAT_FLOAT
21930 else if (type == VAR_FLOAT)
21931 (void)string2float(tab + 1, &tv.vval.v_float);
21932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021934 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021935 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021936 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021937 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 }
21939 }
21940 }
21941
21942 return viminfo_readline(virp);
21943}
21944
21945/*
21946 * Write global vars that start with a capital to the viminfo file
21947 */
21948 void
21949write_viminfo_varlist(fp)
21950 FILE *fp;
21951{
Bram Moolenaar33570922005-01-25 22:26:29 +000021952 hashitem_T *hi;
21953 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021954 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021955 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021956 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021957 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021958 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959
21960 if (find_viminfo_parameter('!') == NULL)
21961 return;
21962
21963 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021964
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021965 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021970 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 this_var = HI2DI(hi);
21972 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021975 {
21976 case VAR_STRING: s = "STR"; break;
21977 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021978#ifdef FEAT_FLOAT
21979 case VAR_FLOAT: s = "FLO"; break;
21980#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021981 default: continue;
21982 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021983 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021984 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021985 if (p != NULL)
21986 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021987 vim_free(tofree);
21988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 }
21990 }
21991}
21992#endif
21993
21994#if defined(FEAT_SESSION) || defined(PROTO)
21995 int
21996store_session_globals(fd)
21997 FILE *fd;
21998{
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 hashitem_T *hi;
22000 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022001 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 char_u *p, *t;
22003
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022004 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022007 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022009 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 this_var = HI2DI(hi);
22011 if ((this_var->di_tv.v_type == VAR_NUMBER
22012 || this_var->di_tv.v_type == VAR_STRING)
22013 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022014 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022015 /* Escape special characters with a backslash. Turn a LF and
22016 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022018 (char_u *)"\\\"\n\r");
22019 if (p == NULL) /* out of memory */
22020 break;
22021 for (t = p; *t != NUL; ++t)
22022 if (*t == '\n')
22023 *t = 'n';
22024 else if (*t == '\r')
22025 *t = 'r';
22026 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 this_var->di_key,
22028 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22029 : ' ',
22030 p,
22031 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22032 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022033 || put_eol(fd) == FAIL)
22034 {
22035 vim_free(p);
22036 return FAIL;
22037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 vim_free(p);
22039 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022040#ifdef FEAT_FLOAT
22041 else if (this_var->di_tv.v_type == VAR_FLOAT
22042 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22043 {
22044 float_T f = this_var->di_tv.vval.v_float;
22045 int sign = ' ';
22046
22047 if (f < 0)
22048 {
22049 f = -f;
22050 sign = '-';
22051 }
22052 if ((fprintf(fd, "let %s = %c&%f",
22053 this_var->di_key, sign, f) < 0)
22054 || put_eol(fd) == FAIL)
22055 return FAIL;
22056 }
22057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 }
22059 }
22060 return OK;
22061}
22062#endif
22063
Bram Moolenaar661b1822005-07-28 22:36:45 +000022064/*
22065 * Display script name where an item was last set.
22066 * Should only be invoked when 'verbose' is non-zero.
22067 */
22068 void
22069last_set_msg(scriptID)
22070 scid_T scriptID;
22071{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022072 char_u *p;
22073
Bram Moolenaar661b1822005-07-28 22:36:45 +000022074 if (scriptID != 0)
22075 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022076 p = home_replace_save(NULL, get_scriptname(scriptID));
22077 if (p != NULL)
22078 {
22079 verbose_enter();
22080 MSG_PUTS(_("\n\tLast set from "));
22081 MSG_PUTS(p);
22082 vim_free(p);
22083 verbose_leave();
22084 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022085 }
22086}
22087
Bram Moolenaard812df62008-11-09 12:46:09 +000022088/*
22089 * List v:oldfiles in a nice way.
22090 */
22091/*ARGSUSED*/
22092 void
22093ex_oldfiles(eap)
22094 exarg_T *eap;
22095{
22096 list_T *l = vimvars[VV_OLDFILES].vv_list;
22097 listitem_T *li;
22098 int nr = 0;
22099
22100 if (l == NULL)
22101 msg((char_u *)_("No old files"));
22102 else
22103 {
22104 msg_start();
22105 msg_scroll = TRUE;
22106 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22107 {
22108 msg_outnum((long)++nr);
22109 MSG_PUTS(": ");
22110 msg_outtrans(get_tv_string(&li->li_tv));
22111 msg_putchar('\n');
22112 out_flush(); /* output one line at a time */
22113 ui_breakcheck();
22114 }
22115 /* Assume "got_int" was set to truncate the listing. */
22116 got_int = FALSE;
22117
22118#ifdef FEAT_BROWSE_CMD
22119 if (cmdmod.browse)
22120 {
22121 quit_more = FALSE;
22122 nr = prompt_for_number(FALSE);
22123 msg_starthere();
22124 if (nr > 0)
22125 {
22126 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22127 (long)nr);
22128
22129 if (p != NULL)
22130 {
22131 p = expand_env_save(p);
22132 eap->arg = p;
22133 eap->cmdidx = CMD_edit;
22134 cmdmod.browse = FALSE;
22135 do_exedit(eap, NULL);
22136 vim_free(p);
22137 }
22138 }
22139 }
22140#endif
22141 }
22142}
22143
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144#endif /* FEAT_EVAL */
22145
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022147#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148
22149#ifdef WIN3264
22150/*
22151 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22152 */
22153static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22154static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22155static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22156
22157/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022158 * Get the short path (8.3) for the filename in "fnamep".
22159 * Only works for a valid file name.
22160 * When the path gets longer "fnamep" is changed and the allocated buffer
22161 * is put in "bufp".
22162 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22163 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 */
22165 static int
22166get_short_pathname(fnamep, bufp, fnamelen)
22167 char_u **fnamep;
22168 char_u **bufp;
22169 int *fnamelen;
22170{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022171 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 char_u *newbuf;
22173
22174 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 l = GetShortPathName(*fnamep, *fnamep, len);
22176 if (l > len - 1)
22177 {
22178 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022179 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 newbuf = vim_strnsave(*fnamep, l);
22181 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183
22184 vim_free(*bufp);
22185 *fnamep = *bufp = newbuf;
22186
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022187 /* Really should always succeed, as the buffer is big enough. */
22188 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 }
22190
22191 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022192 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193}
22194
22195/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022196 * Get the short path (8.3) for the filename in "fname". The converted
22197 * path is returned in "bufp".
22198 *
22199 * Some of the directories specified in "fname" may not exist. This function
22200 * will shorten the existing directories at the beginning of the path and then
22201 * append the remaining non-existing path.
22202 *
22203 * fname - Pointer to the filename to shorten. On return, contains the
22204 * pointer to the shortened pathname
22205 * bufp - Pointer to an allocated buffer for the filename.
22206 * fnamelen - Length of the filename pointed to by fname
22207 *
22208 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 */
22210 static int
22211shortpath_for_invalid_fname(fname, bufp, fnamelen)
22212 char_u **fname;
22213 char_u **bufp;
22214 int *fnamelen;
22215{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022216 char_u *short_fname, *save_fname, *pbuf_unused;
22217 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022219 int old_len, len;
22220 int new_len, sfx_len;
22221 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222
22223 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022224 old_len = *fnamelen;
22225 save_fname = vim_strnsave(*fname, old_len);
22226 pbuf_unused = NULL;
22227 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022229 endp = save_fname + old_len - 1; /* Find the end of the copy */
22230 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022232 /*
22233 * Try shortening the supplied path till it succeeds by removing one
22234 * directory at a time from the tail of the path.
22235 */
22236 len = 0;
22237 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022239 /* go back one path-separator */
22240 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22241 --endp;
22242 if (endp <= save_fname)
22243 break; /* processed the complete path */
22244
22245 /*
22246 * Replace the path separator with a NUL and try to shorten the
22247 * resulting path.
22248 */
22249 ch = *endp;
22250 *endp = 0;
22251 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022252 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022253 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22254 {
22255 retval = FAIL;
22256 goto theend;
22257 }
22258 *endp = ch; /* preserve the string */
22259
22260 if (len > 0)
22261 break; /* successfully shortened the path */
22262
22263 /* failed to shorten the path. Skip the path separator */
22264 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 }
22266
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022267 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022269 /*
22270 * Succeeded in shortening the path. Now concatenate the shortened
22271 * path with the remaining path at the tail.
22272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022274 /* Compute the length of the new path. */
22275 sfx_len = (int)(save_endp - endp) + 1;
22276 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022278 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022280 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022282 /* There is not enough space in the currently allocated string,
22283 * copy it to a buffer big enough. */
22284 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022286 {
22287 retval = FAIL;
22288 goto theend;
22289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 }
22291 else
22292 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022293 /* Transfer short_fname to the main buffer (it's big enough),
22294 * unless get_short_pathname() did its work in-place. */
22295 *fname = *bufp = save_fname;
22296 if (short_fname != save_fname)
22297 vim_strncpy(save_fname, short_fname, len);
22298 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022300
22301 /* concat the not-shortened part of the path */
22302 vim_strncpy(*fname + len, endp, sfx_len);
22303 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022305
22306theend:
22307 vim_free(pbuf_unused);
22308 vim_free(save_fname);
22309
22310 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311}
22312
22313/*
22314 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022315 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 */
22317 static int
22318shortpath_for_partial(fnamep, bufp, fnamelen)
22319 char_u **fnamep;
22320 char_u **bufp;
22321 int *fnamelen;
22322{
22323 int sepcount, len, tflen;
22324 char_u *p;
22325 char_u *pbuf, *tfname;
22326 int hasTilde;
22327
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022328 /* Count up the path separators from the RHS.. so we know which part
22329 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022331 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 if (vim_ispathsep(*p))
22333 ++sepcount;
22334
22335 /* Need full path first (use expand_env() to remove a "~/") */
22336 hasTilde = (**fnamep == '~');
22337 if (hasTilde)
22338 pbuf = tfname = expand_env_save(*fnamep);
22339 else
22340 pbuf = tfname = FullName_save(*fnamep, FALSE);
22341
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022342 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022344 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346
22347 if (len == 0)
22348 {
22349 /* Don't have a valid filename, so shorten the rest of the
22350 * path if we can. This CAN give us invalid 8.3 filenames, but
22351 * there's not a lot of point in guessing what it might be.
22352 */
22353 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022354 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 }
22357
22358 /* Count the paths backward to find the beginning of the desired string. */
22359 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022360 {
22361#ifdef FEAT_MBYTE
22362 if (has_mbyte)
22363 p -= mb_head_off(tfname, p);
22364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 if (vim_ispathsep(*p))
22366 {
22367 if (sepcount == 0 || (hasTilde && sepcount == 1))
22368 break;
22369 else
22370 sepcount --;
22371 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 if (hasTilde)
22374 {
22375 --p;
22376 if (p >= tfname)
22377 *p = '~';
22378 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 }
22381 else
22382 ++p;
22383
22384 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22385 vim_free(*bufp);
22386 *fnamelen = (int)STRLEN(p);
22387 *bufp = pbuf;
22388 *fnamep = p;
22389
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022390 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391}
22392#endif /* WIN3264 */
22393
22394/*
22395 * Adjust a filename, according to a string of modifiers.
22396 * *fnamep must be NUL terminated when called. When returning, the length is
22397 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022398 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 * When there is an error, *fnamep is set to NULL.
22400 */
22401 int
22402modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22403 char_u *src; /* string with modifiers */
22404 int *usedlen; /* characters after src that are used */
22405 char_u **fnamep; /* file name so far */
22406 char_u **bufp; /* buffer for allocated file name or NULL */
22407 int *fnamelen; /* length of fnamep */
22408{
22409 int valid = 0;
22410 char_u *tail;
22411 char_u *s, *p, *pbuf;
22412 char_u dirname[MAXPATHL];
22413 int c;
22414 int has_fullname = 0;
22415#ifdef WIN3264
22416 int has_shortname = 0;
22417#endif
22418
22419repeat:
22420 /* ":p" - full path/file_name */
22421 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22422 {
22423 has_fullname = 1;
22424
22425 valid |= VALID_PATH;
22426 *usedlen += 2;
22427
22428 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22429 if ((*fnamep)[0] == '~'
22430#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22431 && ((*fnamep)[1] == '/'
22432# ifdef BACKSLASH_IN_FILENAME
22433 || (*fnamep)[1] == '\\'
22434# endif
22435 || (*fnamep)[1] == NUL)
22436
22437#endif
22438 )
22439 {
22440 *fnamep = expand_env_save(*fnamep);
22441 vim_free(*bufp); /* free any allocated file name */
22442 *bufp = *fnamep;
22443 if (*fnamep == NULL)
22444 return -1;
22445 }
22446
22447 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022448 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 {
22450 if (vim_ispathsep(*p)
22451 && p[1] == '.'
22452 && (p[2] == NUL
22453 || vim_ispathsep(p[2])
22454 || (p[2] == '.'
22455 && (p[3] == NUL || vim_ispathsep(p[3])))))
22456 break;
22457 }
22458
22459 /* FullName_save() is slow, don't use it when not needed. */
22460 if (*p != NUL || !vim_isAbsName(*fnamep))
22461 {
22462 *fnamep = FullName_save(*fnamep, *p != NUL);
22463 vim_free(*bufp); /* free any allocated file name */
22464 *bufp = *fnamep;
22465 if (*fnamep == NULL)
22466 return -1;
22467 }
22468
22469 /* Append a path separator to a directory. */
22470 if (mch_isdir(*fnamep))
22471 {
22472 /* Make room for one or two extra characters. */
22473 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22474 vim_free(*bufp); /* free any allocated file name */
22475 *bufp = *fnamep;
22476 if (*fnamep == NULL)
22477 return -1;
22478 add_pathsep(*fnamep);
22479 }
22480 }
22481
22482 /* ":." - path relative to the current directory */
22483 /* ":~" - path relative to the home directory */
22484 /* ":8" - shortname path - postponed till after */
22485 while (src[*usedlen] == ':'
22486 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22487 {
22488 *usedlen += 2;
22489 if (c == '8')
22490 {
22491#ifdef WIN3264
22492 has_shortname = 1; /* Postpone this. */
22493#endif
22494 continue;
22495 }
22496 pbuf = NULL;
22497 /* Need full path first (use expand_env() to remove a "~/") */
22498 if (!has_fullname)
22499 {
22500 if (c == '.' && **fnamep == '~')
22501 p = pbuf = expand_env_save(*fnamep);
22502 else
22503 p = pbuf = FullName_save(*fnamep, FALSE);
22504 }
22505 else
22506 p = *fnamep;
22507
22508 has_fullname = 0;
22509
22510 if (p != NULL)
22511 {
22512 if (c == '.')
22513 {
22514 mch_dirname(dirname, MAXPATHL);
22515 s = shorten_fname(p, dirname);
22516 if (s != NULL)
22517 {
22518 *fnamep = s;
22519 if (pbuf != NULL)
22520 {
22521 vim_free(*bufp); /* free any allocated file name */
22522 *bufp = pbuf;
22523 pbuf = NULL;
22524 }
22525 }
22526 }
22527 else
22528 {
22529 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22530 /* Only replace it when it starts with '~' */
22531 if (*dirname == '~')
22532 {
22533 s = vim_strsave(dirname);
22534 if (s != NULL)
22535 {
22536 *fnamep = s;
22537 vim_free(*bufp);
22538 *bufp = s;
22539 }
22540 }
22541 }
22542 vim_free(pbuf);
22543 }
22544 }
22545
22546 tail = gettail(*fnamep);
22547 *fnamelen = (int)STRLEN(*fnamep);
22548
22549 /* ":h" - head, remove "/file_name", can be repeated */
22550 /* Don't remove the first "/" or "c:\" */
22551 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22552 {
22553 valid |= VALID_HEAD;
22554 *usedlen += 2;
22555 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022556 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022557 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 *fnamelen = (int)(tail - *fnamep);
22559#ifdef VMS
22560 if (*fnamelen > 0)
22561 *fnamelen += 1; /* the path separator is part of the path */
22562#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022563 if (*fnamelen == 0)
22564 {
22565 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22566 p = vim_strsave((char_u *)".");
22567 if (p == NULL)
22568 return -1;
22569 vim_free(*bufp);
22570 *bufp = *fnamep = tail = p;
22571 *fnamelen = 1;
22572 }
22573 else
22574 {
22575 while (tail > s && !after_pathsep(s, tail))
22576 mb_ptr_back(*fnamep, tail);
22577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 }
22579
22580 /* ":8" - shortname */
22581 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22582 {
22583 *usedlen += 2;
22584#ifdef WIN3264
22585 has_shortname = 1;
22586#endif
22587 }
22588
22589#ifdef WIN3264
22590 /* Check shortname after we have done 'heads' and before we do 'tails'
22591 */
22592 if (has_shortname)
22593 {
22594 pbuf = NULL;
22595 /* Copy the string if it is shortened by :h */
22596 if (*fnamelen < (int)STRLEN(*fnamep))
22597 {
22598 p = vim_strnsave(*fnamep, *fnamelen);
22599 if (p == 0)
22600 return -1;
22601 vim_free(*bufp);
22602 *bufp = *fnamep = p;
22603 }
22604
22605 /* Split into two implementations - makes it easier. First is where
22606 * there isn't a full name already, second is where there is.
22607 */
22608 if (!has_fullname && !vim_isAbsName(*fnamep))
22609 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022610 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 return -1;
22612 }
22613 else
22614 {
22615 int l;
22616
22617 /* Simple case, already have the full-name
22618 * Nearly always shorter, so try first time. */
22619 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022620 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 return -1;
22622
22623 if (l == 0)
22624 {
22625 /* Couldn't find the filename.. search the paths.
22626 */
22627 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022628 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 return -1;
22630 }
22631 *fnamelen = l;
22632 }
22633 }
22634#endif /* WIN3264 */
22635
22636 /* ":t" - tail, just the basename */
22637 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22638 {
22639 *usedlen += 2;
22640 *fnamelen -= (int)(tail - *fnamep);
22641 *fnamep = tail;
22642 }
22643
22644 /* ":e" - extension, can be repeated */
22645 /* ":r" - root, without extension, can be repeated */
22646 while (src[*usedlen] == ':'
22647 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22648 {
22649 /* find a '.' in the tail:
22650 * - for second :e: before the current fname
22651 * - otherwise: The last '.'
22652 */
22653 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22654 s = *fnamep - 2;
22655 else
22656 s = *fnamep + *fnamelen - 1;
22657 for ( ; s > tail; --s)
22658 if (s[0] == '.')
22659 break;
22660 if (src[*usedlen + 1] == 'e') /* :e */
22661 {
22662 if (s > tail)
22663 {
22664 *fnamelen += (int)(*fnamep - (s + 1));
22665 *fnamep = s + 1;
22666#ifdef VMS
22667 /* cut version from the extension */
22668 s = *fnamep + *fnamelen - 1;
22669 for ( ; s > *fnamep; --s)
22670 if (s[0] == ';')
22671 break;
22672 if (s > *fnamep)
22673 *fnamelen = s - *fnamep;
22674#endif
22675 }
22676 else if (*fnamep <= tail)
22677 *fnamelen = 0;
22678 }
22679 else /* :r */
22680 {
22681 if (s > tail) /* remove one extension */
22682 *fnamelen = (int)(s - *fnamep);
22683 }
22684 *usedlen += 2;
22685 }
22686
22687 /* ":s?pat?foo?" - substitute */
22688 /* ":gs?pat?foo?" - global substitute */
22689 if (src[*usedlen] == ':'
22690 && (src[*usedlen + 1] == 's'
22691 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22692 {
22693 char_u *str;
22694 char_u *pat;
22695 char_u *sub;
22696 int sep;
22697 char_u *flags;
22698 int didit = FALSE;
22699
22700 flags = (char_u *)"";
22701 s = src + *usedlen + 2;
22702 if (src[*usedlen + 1] == 'g')
22703 {
22704 flags = (char_u *)"g";
22705 ++s;
22706 }
22707
22708 sep = *s++;
22709 if (sep)
22710 {
22711 /* find end of pattern */
22712 p = vim_strchr(s, sep);
22713 if (p != NULL)
22714 {
22715 pat = vim_strnsave(s, (int)(p - s));
22716 if (pat != NULL)
22717 {
22718 s = p + 1;
22719 /* find end of substitution */
22720 p = vim_strchr(s, sep);
22721 if (p != NULL)
22722 {
22723 sub = vim_strnsave(s, (int)(p - s));
22724 str = vim_strnsave(*fnamep, *fnamelen);
22725 if (sub != NULL && str != NULL)
22726 {
22727 *usedlen = (int)(p + 1 - src);
22728 s = do_string_sub(str, pat, sub, flags);
22729 if (s != NULL)
22730 {
22731 *fnamep = s;
22732 *fnamelen = (int)STRLEN(s);
22733 vim_free(*bufp);
22734 *bufp = s;
22735 didit = TRUE;
22736 }
22737 }
22738 vim_free(sub);
22739 vim_free(str);
22740 }
22741 vim_free(pat);
22742 }
22743 }
22744 /* after using ":s", repeat all the modifiers */
22745 if (didit)
22746 goto repeat;
22747 }
22748 }
22749
22750 return valid;
22751}
22752
22753/*
22754 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22755 * "flags" can be "g" to do a global substitute.
22756 * Returns an allocated string, NULL for error.
22757 */
22758 char_u *
22759do_string_sub(str, pat, sub, flags)
22760 char_u *str;
22761 char_u *pat;
22762 char_u *sub;
22763 char_u *flags;
22764{
22765 int sublen;
22766 regmatch_T regmatch;
22767 int i;
22768 int do_all;
22769 char_u *tail;
22770 garray_T ga;
22771 char_u *ret;
22772 char_u *save_cpo;
22773
22774 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22775 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022776 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777
22778 ga_init2(&ga, 1, 200);
22779
22780 do_all = (flags[0] == 'g');
22781
22782 regmatch.rm_ic = p_ic;
22783 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22784 if (regmatch.regprog != NULL)
22785 {
22786 tail = str;
22787 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22788 {
22789 /*
22790 * Get some space for a temporary buffer to do the substitution
22791 * into. It will contain:
22792 * - The text up to where the match is.
22793 * - The substituted text.
22794 * - The text after the match.
22795 */
22796 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22797 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22798 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22799 {
22800 ga_clear(&ga);
22801 break;
22802 }
22803
22804 /* copy the text up to where the match is */
22805 i = (int)(regmatch.startp[0] - tail);
22806 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22807 /* add the substituted text */
22808 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22809 + ga.ga_len + i, TRUE, TRUE, FALSE);
22810 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 /* avoid getting stuck on a match with an empty string */
22812 if (tail == regmatch.endp[0])
22813 {
22814 if (*tail == NUL)
22815 break;
22816 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22817 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 }
22819 else
22820 {
22821 tail = regmatch.endp[0];
22822 if (*tail == NUL)
22823 break;
22824 }
22825 if (!do_all)
22826 break;
22827 }
22828
22829 if (ga.ga_data != NULL)
22830 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22831
22832 vim_free(regmatch.regprog);
22833 }
22834
22835 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22836 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022837 if (p_cpo == empty_option)
22838 p_cpo = save_cpo;
22839 else
22840 /* Darn, evaluating {sub} expression changed the value. */
22841 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842
22843 return ret;
22844}
22845
22846#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */