blob: bc86a5594e88b431659648f3619df8d266aba327 [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 Moolenaar81bf7082005-02-12 14:31:42 +00007921 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007923 emsg_funcname("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.
8156 */
8157 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008158emsg_funcname(ermsg, name)
8159 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008160 char_u *name;
8161{
8162 char_u *p;
8163
8164 if (*name == K_SPECIAL)
8165 p = concat_str((char_u *)"<SNR>", name + 3);
8166 else
8167 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008168 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008169 if (p != name)
8170 vim_free(p);
8171}
8172
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008173/*
8174 * Return TRUE for a non-zero Number and a non-empty String.
8175 */
8176 static int
8177non_zero_arg(argvars)
8178 typval_T *argvars;
8179{
8180 return ((argvars[0].v_type == VAR_NUMBER
8181 && argvars[0].vval.v_number != 0)
8182 || (argvars[0].v_type == VAR_STRING
8183 && argvars[0].vval.v_string != NULL
8184 && *argvars[0].vval.v_string != NUL));
8185}
8186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187/*********************************************
8188 * Implementation of the built-in functions
8189 */
8190
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008191#ifdef FEAT_FLOAT
8192/*
8193 * "abs(expr)" function
8194 */
8195 static void
8196f_abs(argvars, rettv)
8197 typval_T *argvars;
8198 typval_T *rettv;
8199{
8200 if (argvars[0].v_type == VAR_FLOAT)
8201 {
8202 rettv->v_type = VAR_FLOAT;
8203 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8204 }
8205 else
8206 {
8207 varnumber_T n;
8208 int error = FALSE;
8209
8210 n = get_tv_number_chk(&argvars[0], &error);
8211 if (error)
8212 rettv->vval.v_number = -1;
8213 else if (n > 0)
8214 rettv->vval.v_number = n;
8215 else
8216 rettv->vval.v_number = -n;
8217 }
8218}
8219#endif
8220
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008222 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 */
8224 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008225f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 typval_T *argvars;
8227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008231 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008234 if ((l = argvars[0].vval.v_list) != NULL
8235 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8236 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238 }
8239 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008240 EMSG(_(e_listreq));
8241}
8242
8243/*
8244 * "append(lnum, string/list)" function
8245 */
8246 static void
8247f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008248 typval_T *argvars;
8249 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008250{
8251 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 list_T *l = NULL;
8254 listitem_T *li = NULL;
8255 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008256 long added = 0;
8257
Bram Moolenaar0d660222005-01-07 21:51:51 +00008258 lnum = get_tv_lnum(argvars);
8259 if (lnum >= 0
8260 && lnum <= curbuf->b_ml.ml_line_count
8261 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008263 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008264 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008265 l = argvars[1].vval.v_list;
8266 if (l == NULL)
8267 return;
8268 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008270 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008271 for (;;)
8272 {
8273 if (l == NULL)
8274 tv = &argvars[1]; /* append a string */
8275 else if (li == NULL)
8276 break; /* end of list */
8277 else
8278 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 line = get_tv_string_chk(tv);
8280 if (line == NULL) /* type error */
8281 {
8282 rettv->vval.v_number = 1; /* Failed */
8283 break;
8284 }
8285 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008286 ++added;
8287 if (l == NULL)
8288 break;
8289 li = li->li_next;
8290 }
8291
8292 appended_lines_mark(lnum, added);
8293 if (curwin->w_cursor.lnum > lnum)
8294 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008296 else
8297 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
8299
8300/*
8301 * "argc()" function
8302 */
8303/* ARGSUSED */
8304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 typval_T *argvars;
8307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310}
8311
8312/*
8313 * "argidx()" function
8314 */
8315/* ARGSUSED */
8316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008317f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 typval_T *argvars;
8319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008321 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322}
8323
8324/*
8325 * "argv(nr)" function
8326 */
8327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008328f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 typval_T *argvars;
8330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 int idx;
8333
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008334 if (argvars[0].v_type != VAR_UNKNOWN)
8335 {
8336 idx = get_tv_number_chk(&argvars[0], NULL);
8337 if (idx >= 0 && idx < ARGCOUNT)
8338 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8339 else
8340 rettv->vval.v_string = NULL;
8341 rettv->v_type = VAR_STRING;
8342 }
8343 else if (rettv_list_alloc(rettv) == OK)
8344 for (idx = 0; idx < ARGCOUNT; ++idx)
8345 list_append_string(rettv->vval.v_list,
8346 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347}
8348
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008349#ifdef FEAT_FLOAT
8350static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8351
8352/*
8353 * Get the float value of "argvars[0]" into "f".
8354 * Returns FAIL when the argument is not a Number or Float.
8355 */
8356 static int
8357get_float_arg(argvars, f)
8358 typval_T *argvars;
8359 float_T *f;
8360{
8361 if (argvars[0].v_type == VAR_FLOAT)
8362 {
8363 *f = argvars[0].vval.v_float;
8364 return OK;
8365 }
8366 if (argvars[0].v_type == VAR_NUMBER)
8367 {
8368 *f = (float_T)argvars[0].vval.v_number;
8369 return OK;
8370 }
8371 EMSG(_("E808: Number or Float required"));
8372 return FAIL;
8373}
8374
8375/*
8376 * "atan()" function
8377 */
8378 static void
8379f_atan(argvars, rettv)
8380 typval_T *argvars;
8381 typval_T *rettv;
8382{
8383 float_T f;
8384
8385 rettv->v_type = VAR_FLOAT;
8386 if (get_float_arg(argvars, &f) == OK)
8387 rettv->vval.v_float = atan(f);
8388 else
8389 rettv->vval.v_float = 0.0;
8390}
8391#endif
8392
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393/*
8394 * "browse(save, title, initdir, default)" function
8395 */
8396/* ARGSUSED */
8397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008398f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008399 typval_T *argvars;
8400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401{
8402#ifdef FEAT_BROWSE
8403 int save;
8404 char_u *title;
8405 char_u *initdir;
8406 char_u *defname;
8407 char_u buf[NUMBUFLEN];
8408 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008411 save = get_tv_number_chk(&argvars[0], &error);
8412 title = get_tv_string_chk(&argvars[1]);
8413 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8414 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008416 if (error || title == NULL || initdir == NULL || defname == NULL)
8417 rettv->vval.v_string = NULL;
8418 else
8419 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008420 do_browse(save ? BROWSE_SAVE : 0,
8421 title, defname, NULL, initdir, NULL, curbuf);
8422#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008426}
8427
8428/*
8429 * "browsedir(title, initdir)" function
8430 */
8431/* ARGSUSED */
8432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 typval_T *argvars;
8435 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008436{
8437#ifdef FEAT_BROWSE
8438 char_u *title;
8439 char_u *initdir;
8440 char_u buf[NUMBUFLEN];
8441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 title = get_tv_string_chk(&argvars[0]);
8443 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445 if (title == NULL || initdir == NULL)
8446 rettv->vval.v_string = NULL;
8447 else
8448 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008449 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454}
8455
Bram Moolenaar33570922005-01-25 22:26:29 +00008456static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*
8459 * Find a buffer by number or exact name.
8460 */
8461 static buf_T *
8462find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008467 if (avar->v_type == VAR_NUMBER)
8468 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008469 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008471 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008472 if (buf == NULL)
8473 {
8474 /* No full path name match, try a match with a URL or a "nofile"
8475 * buffer, these don't use the full path. */
8476 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8477 if (buf->b_fname != NULL
8478 && (path_with_url(buf->b_fname)
8479#ifdef FEAT_QUICKFIX
8480 || bt_nofile(buf)
8481#endif
8482 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008483 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008484 break;
8485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 }
8487 return buf;
8488}
8489
8490/*
8491 * "bufexists(expr)" function
8492 */
8493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499}
8500
8501/*
8502 * "buflisted(expr)" function
8503 */
8504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 typval_T *argvars;
8507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 buf_T *buf;
8510
8511 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513}
8514
8515/*
8516 * "bufloaded(expr)" function
8517 */
8518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 typval_T *argvars;
8521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 buf_T *buf;
8524
8525 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527}
8528
Bram Moolenaar33570922005-01-25 22:26:29 +00008529static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531/*
8532 * Get buffer by number or pattern.
8533 */
8534 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008538 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 int save_magic;
8540 char_u *save_cpo;
8541 buf_T *buf;
8542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008543 if (tv->v_type == VAR_NUMBER)
8544 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008545 if (tv->v_type != VAR_STRING)
8546 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 if (name == NULL || *name == NUL)
8548 return curbuf;
8549 if (name[0] == '$' && name[1] == NUL)
8550 return lastbuf;
8551
8552 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8553 save_magic = p_magic;
8554 p_magic = TRUE;
8555 save_cpo = p_cpo;
8556 p_cpo = (char_u *)"";
8557
8558 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8559 TRUE, FALSE));
8560
8561 p_magic = save_magic;
8562 p_cpo = save_cpo;
8563
8564 /* If not found, try expanding the name, like done for bufexists(). */
8565 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
8568 return buf;
8569}
8570
8571/*
8572 * "bufname(expr)" function
8573 */
8574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *argvars;
8577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 buf_T *buf;
8580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008581 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 buf = get_buf_tv(&argvars[0]);
8584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 --emsg_off;
8590}
8591
8592/*
8593 * "bufnr(expr)" function
8594 */
8595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 typval_T *argvars;
8598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008601 int error = FALSE;
8602 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008607 --emsg_off;
8608
8609 /* If the buffer isn't found and the second argument is not zero create a
8610 * new buffer. */
8611 if (buf == NULL
8612 && argvars[1].v_type != VAR_UNKNOWN
8613 && get_tv_number_chk(&argvars[1], &error) != 0
8614 && !error
8615 && (name = get_tv_string_chk(&argvars[0])) != NULL
8616 && !error)
8617 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623}
8624
8625/*
8626 * "bufwinnr(nr)" function
8627 */
8628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *argvars;
8631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633#ifdef FEAT_WINDOWS
8634 win_T *wp;
8635 int winnr = 0;
8636#endif
8637 buf_T *buf;
8638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#ifdef FEAT_WINDOWS
8643 for (wp = firstwin; wp; wp = wp->w_next)
8644 {
8645 ++winnr;
8646 if (wp->w_buffer == buf)
8647 break;
8648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#endif
8653 --emsg_off;
8654}
8655
8656/*
8657 * "byte2line(byte)" function
8658 */
8659/*ARGSUSED*/
8660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008662 typval_T *argvars;
8663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664{
8665#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667#else
8668 long boff = 0;
8669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008670 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 (linenr_T)0, &boff);
8676#endif
8677}
8678
8679/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008680 * "byteidx()" function
8681 */
8682/*ARGSUSED*/
8683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *argvars;
8686 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008687{
8688#ifdef FEAT_MBYTE
8689 char_u *t;
8690#endif
8691 char_u *str;
8692 long idx;
8693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008694 str = get_tv_string_chk(&argvars[0]);
8695 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008697 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008698 return;
8699
8700#ifdef FEAT_MBYTE
8701 t = str;
8702 for ( ; idx > 0; idx--)
8703 {
8704 if (*t == NUL) /* EOL reached */
8705 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008706 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008707 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008708 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008709#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008712#endif
8713}
8714
8715/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008716 * "call(func, arglist)" function
8717 */
8718 static void
8719f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T *argvars;
8721 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008722{
8723 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008724 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008725 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008727 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008729
8730 rettv->vval.v_number = 0;
8731 if (argvars[1].v_type != VAR_LIST)
8732 {
8733 EMSG(_(e_listreq));
8734 return;
8735 }
8736 if (argvars[1].vval.v_list == NULL)
8737 return;
8738
8739 if (argvars[0].v_type == VAR_FUNC)
8740 func = argvars[0].vval.v_string;
8741 else
8742 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 if (*func == NUL)
8744 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008745
Bram Moolenaare9a41262005-01-15 22:18:47 +00008746 if (argvars[2].v_type != VAR_UNKNOWN)
8747 {
8748 if (argvars[2].v_type != VAR_DICT)
8749 {
8750 EMSG(_(e_dictreq));
8751 return;
8752 }
8753 selfdict = argvars[2].vval.v_dict;
8754 }
8755
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008756 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8757 item = item->li_next)
8758 {
8759 if (argc == MAX_FUNC_ARGS)
8760 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008761 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008762 break;
8763 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008764 /* Make a copy of each argument. This is needed to be able to set
8765 * v_lock to VAR_FIXED in the copy without changing the original list.
8766 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008767 copy_tv(&item->li_tv, &argv[argc++]);
8768 }
8769
8770 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008771 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008772 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8773 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008774
8775 /* Free the arguments. */
8776 while (argc > 0)
8777 clear_tv(&argv[--argc]);
8778}
8779
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008780#ifdef FEAT_FLOAT
8781/*
8782 * "ceil({float})" function
8783 */
8784 static void
8785f_ceil(argvars, rettv)
8786 typval_T *argvars;
8787 typval_T *rettv;
8788{
8789 float_T f;
8790
8791 rettv->v_type = VAR_FLOAT;
8792 if (get_float_arg(argvars, &f) == OK)
8793 rettv->vval.v_float = ceil(f);
8794 else
8795 rettv->vval.v_float = 0.0;
8796}
8797#endif
8798
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008799/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008800 * "changenr()" function
8801 */
8802/*ARGSUSED*/
8803 static void
8804f_changenr(argvars, rettv)
8805 typval_T *argvars;
8806 typval_T *rettv;
8807{
8808 rettv->vval.v_number = curbuf->b_u_seq_cur;
8809}
8810
8811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 * "char2nr(string)" function
8813 */
8814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008816 typval_T *argvars;
8817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
8819#ifdef FEAT_MBYTE
8820 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008821 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 else
8823#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
8827/*
8828 * "cindent(lnum)" function
8829 */
8830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 typval_T *argvars;
8833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835#ifdef FEAT_CINDENT
8836 pos_T pos;
8837 linenr_T lnum;
8838
8839 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8842 {
8843 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 curwin->w_cursor = pos;
8846 }
8847 else
8848#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008853 * "clearmatches()" function
8854 */
8855/*ARGSUSED*/
8856 static void
8857f_clearmatches(argvars, rettv)
8858 typval_T *argvars;
8859 typval_T *rettv;
8860{
8861#ifdef FEAT_SEARCH_EXTRA
8862 clear_matches(curwin);
8863#endif
8864}
8865
8866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 * "col(string)" function
8868 */
8869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008871 typval_T *argvars;
8872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 colnr_T col = 0;
8875 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008876 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008878 fp = var2fpos(&argvars[0], FALSE, &fnum);
8879 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 if (fp->col == MAXCOL)
8882 {
8883 /* '> can be MAXCOL, get the length of the line then */
8884 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008885 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 else
8887 col = MAXCOL;
8888 }
8889 else
8890 {
8891 col = fp->col + 1;
8892#ifdef FEAT_VIRTUALEDIT
8893 /* col(".") when the cursor is on the NUL at the end of the line
8894 * because of "coladd" can be seen as an extra column. */
8895 if (virtual_active() && fp == &curwin->w_cursor)
8896 {
8897 char_u *p = ml_get_cursor();
8898
8899 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8900 curwin->w_virtcol - curwin->w_cursor.coladd))
8901 {
8902# ifdef FEAT_MBYTE
8903 int l;
8904
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008905 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 col += l;
8907# else
8908 if (*p != NUL && p[1] == NUL)
8909 ++col;
8910# endif
8911 }
8912 }
8913#endif
8914 }
8915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917}
8918
Bram Moolenaar572cb562005-08-05 21:35:02 +00008919#if defined(FEAT_INS_EXPAND)
8920/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008921 * "complete()" function
8922 */
8923/*ARGSUSED*/
8924 static void
8925f_complete(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 int startcol;
8930
8931 if ((State & INSERT) == 0)
8932 {
8933 EMSG(_("E785: complete() can only be used in Insert mode"));
8934 return;
8935 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008936
8937 /* Check for undo allowed here, because if something was already inserted
8938 * the line was already saved for undo and this check isn't done. */
8939 if (!undo_allowed())
8940 return;
8941
Bram Moolenaarade00832006-03-10 21:46:58 +00008942 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8943 {
8944 EMSG(_(e_invarg));
8945 return;
8946 }
8947
8948 startcol = get_tv_number_chk(&argvars[0], NULL);
8949 if (startcol <= 0)
8950 return;
8951
8952 set_completion(startcol - 1, argvars[1].vval.v_list);
8953}
8954
8955/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008956 * "complete_add()" function
8957 */
8958/*ARGSUSED*/
8959 static void
8960f_complete_add(argvars, rettv)
8961 typval_T *argvars;
8962 typval_T *rettv;
8963{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008964 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008965}
8966
8967/*
8968 * "complete_check()" function
8969 */
8970/*ARGSUSED*/
8971 static void
8972f_complete_check(argvars, rettv)
8973 typval_T *argvars;
8974 typval_T *rettv;
8975{
8976 int saved = RedrawingDisabled;
8977
8978 RedrawingDisabled = 0;
8979 ins_compl_check_keys(0);
8980 rettv->vval.v_number = compl_interrupted;
8981 RedrawingDisabled = saved;
8982}
8983#endif
8984
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985/*
8986 * "confirm(message, buttons[, default [, type]])" function
8987 */
8988/*ARGSUSED*/
8989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8995 char_u *message;
8996 char_u *buttons = NULL;
8997 char_u buf[NUMBUFLEN];
8998 char_u buf2[NUMBUFLEN];
8999 int def = 1;
9000 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 char_u *typestr;
9002 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 message = get_tv_string_chk(&argvars[0]);
9005 if (message == NULL)
9006 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9010 if (buttons == NULL)
9011 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009017 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9018 if (typestr == NULL)
9019 error = TRUE;
9020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022 switch (TOUPPER_ASC(*typestr))
9023 {
9024 case 'E': type = VIM_ERROR; break;
9025 case 'Q': type = VIM_QUESTION; break;
9026 case 'I': type = VIM_INFO; break;
9027 case 'W': type = VIM_WARNING; break;
9028 case 'G': type = VIM_GENERIC; break;
9029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
9031 }
9032 }
9033 }
9034
9035 if (buttons == NULL || *buttons == NUL)
9036 buttons = (char_u *)_("&Ok");
9037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 if (error)
9039 rettv->vval.v_number = 0;
9040 else
9041 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 def, NULL);
9043#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045#endif
9046}
9047
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009048/*
9049 * "copy()" function
9050 */
9051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009053 typval_T *argvars;
9054 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009055{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009056 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009057}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009059#ifdef FEAT_FLOAT
9060/*
9061 * "cos()" function
9062 */
9063 static void
9064f_cos(argvars, rettv)
9065 typval_T *argvars;
9066 typval_T *rettv;
9067{
9068 float_T f;
9069
9070 rettv->v_type = VAR_FLOAT;
9071 if (get_float_arg(argvars, &f) == OK)
9072 rettv->vval.v_float = cos(f);
9073 else
9074 rettv->vval.v_float = 0.0;
9075}
9076#endif
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009079 * "count()" function
9080 */
9081 static void
9082f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 typval_T *argvars;
9084 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009085{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009086 long n = 0;
9087 int ic = FALSE;
9088
Bram Moolenaare9a41262005-01-15 22:18:47 +00009089 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009090 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 listitem_T *li;
9092 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009093 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009094
Bram Moolenaare9a41262005-01-15 22:18:47 +00009095 if ((l = argvars[0].vval.v_list) != NULL)
9096 {
9097 li = l->lv_first;
9098 if (argvars[2].v_type != VAR_UNKNOWN)
9099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 int error = FALSE;
9101
9102 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009103 if (argvars[3].v_type != VAR_UNKNOWN)
9104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 idx = get_tv_number_chk(&argvars[3], &error);
9106 if (!error)
9107 {
9108 li = list_find(l, idx);
9109 if (li == NULL)
9110 EMSGN(_(e_listidx), idx);
9111 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 if (error)
9114 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009115 }
9116
9117 for ( ; li != NULL; li = li->li_next)
9118 if (tv_equal(&li->li_tv, &argvars[1], ic))
9119 ++n;
9120 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009122 else if (argvars[0].v_type == VAR_DICT)
9123 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 int todo;
9125 dict_T *d;
9126 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009127
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009128 if ((d = argvars[0].vval.v_dict) != NULL)
9129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 int error = FALSE;
9131
Bram Moolenaare9a41262005-01-15 22:18:47 +00009132 if (argvars[2].v_type != VAR_UNKNOWN)
9133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009135 if (argvars[3].v_type != VAR_UNKNOWN)
9136 EMSG(_(e_invarg));
9137 }
9138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009139 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009140 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009141 {
9142 if (!HASHITEM_EMPTY(hi))
9143 {
9144 --todo;
9145 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9146 ++n;
9147 }
9148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009149 }
9150 }
9151 else
9152 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009153 rettv->vval.v_number = n;
9154}
9155
9156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9158 *
9159 * Checks the existence of a cscope connection.
9160 */
9161/*ARGSUSED*/
9162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167#ifdef FEAT_CSCOPE
9168 int num = 0;
9169 char_u *dbpath = NULL;
9170 char_u *prepend = NULL;
9171 char_u buf[NUMBUFLEN];
9172
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009173 if (argvars[0].v_type != VAR_UNKNOWN
9174 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 num = (int)get_tv_number(&argvars[0]);
9177 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009178 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 }
9181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#endif
9186}
9187
9188/*
9189 * "cursor(lnum, col)" function
9190 *
9191 * Moves the cursor to the specified line and column
9192 */
9193/*ARGSUSED*/
9194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009200#ifdef FEAT_VIRTUALEDIT
9201 long coladd = 0;
9202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203
Bram Moolenaara5525202006-03-02 22:52:09 +00009204 if (argvars[1].v_type == VAR_UNKNOWN)
9205 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009206 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009207
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009208 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009209 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009210 line = pos.lnum;
9211 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009212#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009213 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009214#endif
9215 }
9216 else
9217 {
9218 line = get_tv_lnum(argvars);
9219 col = get_tv_number_chk(&argvars[1], NULL);
9220#ifdef FEAT_VIRTUALEDIT
9221 if (argvars[2].v_type != VAR_UNKNOWN)
9222 coladd = get_tv_number_chk(&argvars[2], NULL);
9223#endif
9224 }
9225 if (line < 0 || col < 0
9226#ifdef FEAT_VIRTUALEDIT
9227 || coladd < 0
9228#endif
9229 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 if (line > 0)
9232 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 if (col > 0)
9234 curwin->w_cursor.col = col - 1;
9235#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009236 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#endif
9238
9239 /* Make sure the cursor is in a valid position. */
9240 check_cursor();
9241#ifdef FEAT_MBYTE
9242 /* Correct cursor for multi-byte character. */
9243 if (has_mbyte)
9244 mb_adjust_cursor();
9245#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009246
9247 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248}
9249
9250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009251 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 */
9253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009255 typval_T *argvars;
9256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009258 int noref = 0;
9259
9260 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009262 if (noref < 0 || noref > 1)
9263 EMSG(_(e_invarg));
9264 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009265 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266}
9267
9268/*
9269 * "delete()" function
9270 */
9271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009272f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 typval_T *argvars;
9274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280}
9281
9282/*
9283 * "did_filetype()" function
9284 */
9285/*ARGSUSED*/
9286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009288 typval_T *argvars;
9289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295#endif
9296}
9297
9298/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009299 * "diff_filler()" function
9300 */
9301/*ARGSUSED*/
9302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009306{
9307#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009309#endif
9310}
9311
9312/*
9313 * "diff_hlID()" function
9314 */
9315/*ARGSUSED*/
9316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 typval_T *argvars;
9319 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009320{
9321#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009323 static linenr_T prev_lnum = 0;
9324 static int changedtick = 0;
9325 static int fnum = 0;
9326 static int change_start = 0;
9327 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009328 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009329 int filler_lines;
9330 int col;
9331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 if (lnum < 0) /* ignore type error in {lnum} arg */
9333 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009334 if (lnum != prev_lnum
9335 || changedtick != curbuf->b_changedtick
9336 || fnum != curbuf->b_fnum)
9337 {
9338 /* New line, buffer, change: need to get the values. */
9339 filler_lines = diff_check(curwin, lnum);
9340 if (filler_lines < 0)
9341 {
9342 if (filler_lines == -1)
9343 {
9344 change_start = MAXCOL;
9345 change_end = -1;
9346 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9347 hlID = HLF_ADD; /* added line */
9348 else
9349 hlID = HLF_CHD; /* changed line */
9350 }
9351 else
9352 hlID = HLF_ADD; /* added line */
9353 }
9354 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009355 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009356 prev_lnum = lnum;
9357 changedtick = curbuf->b_changedtick;
9358 fnum = curbuf->b_fnum;
9359 }
9360
9361 if (hlID == HLF_CHD || hlID == HLF_TXD)
9362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009364 if (col >= change_start && col <= change_end)
9365 hlID = HLF_TXD; /* changed text */
9366 else
9367 hlID = HLF_CHD; /* changed line */
9368 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009369 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009370#endif
9371}
9372
9373/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009374 * "empty({expr})" function
9375 */
9376 static void
9377f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 typval_T *argvars;
9379 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009380{
9381 int n;
9382
9383 switch (argvars[0].v_type)
9384 {
9385 case VAR_STRING:
9386 case VAR_FUNC:
9387 n = argvars[0].vval.v_string == NULL
9388 || *argvars[0].vval.v_string == NUL;
9389 break;
9390 case VAR_NUMBER:
9391 n = argvars[0].vval.v_number == 0;
9392 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009393#ifdef FEAT_FLOAT
9394 case VAR_FLOAT:
9395 n = argvars[0].vval.v_float == 0.0;
9396 break;
9397#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009398 case VAR_LIST:
9399 n = argvars[0].vval.v_list == NULL
9400 || argvars[0].vval.v_list->lv_first == NULL;
9401 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 case VAR_DICT:
9403 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009404 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009406 default:
9407 EMSG2(_(e_intern2), "f_empty()");
9408 n = 0;
9409 }
9410
9411 rettv->vval.v_number = n;
9412}
9413
9414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 * "escape({string}, {chars})" function
9416 */
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
9422 char_u buf[NUMBUFLEN];
9423
Bram Moolenaar758711c2005-02-02 23:11:38 +00009424 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9425 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
9429/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009430 * "eval()" function
9431 */
9432/*ARGSUSED*/
9433 static void
9434f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *argvars;
9436 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009437{
9438 char_u *s;
9439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 s = get_tv_string_chk(&argvars[0]);
9441 if (s != NULL)
9442 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009444 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9445 {
9446 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009447 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009448 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009449 else if (*s != NUL)
9450 EMSG(_(e_trailing));
9451}
9452
9453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 * "eventhandler()" function
9455 */
9456/*ARGSUSED*/
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
9466 * "executable()" function
9467 */
9468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009470 typval_T *argvars;
9471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474}
9475
9476/*
9477 * "exists()" function
9478 */
9479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009481 typval_T *argvars;
9482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484 char_u *p;
9485 char_u *name;
9486 int n = FALSE;
9487 int len = 0;
9488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 if (*p == '$') /* environment variable */
9491 {
9492 /* first try "normal" environment variables (fast) */
9493 if (mch_getenv(p + 1) != NULL)
9494 n = TRUE;
9495 else
9496 {
9497 /* try expanding things like $VIM and ${HOME} */
9498 p = expand_env_save(p);
9499 if (p != NULL && *p != '$')
9500 n = TRUE;
9501 vim_free(p);
9502 }
9503 }
9504 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009507 if (*skipwhite(p) != NUL)
9508 n = FALSE; /* trailing garbage */
9509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 else if (*p == '*') /* internal or user defined function */
9511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
9514 else if (*p == ':')
9515 {
9516 n = cmd_exists(p + 1);
9517 }
9518 else if (*p == '#')
9519 {
9520#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009521 if (p[1] == '#')
9522 n = autocmd_supported(p + 2);
9523 else
9524 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525#endif
9526 }
9527 else /* internal variable */
9528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009529 char_u *tofree;
9530 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009532 /* get_name_len() takes care of expanding curly braces */
9533 name = p;
9534 len = get_name_len(&p, &tofree, TRUE, FALSE);
9535 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009537 if (tofree != NULL)
9538 name = tofree;
9539 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9540 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009542 /* handle d.key, l[idx], f(expr) */
9543 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9544 if (n)
9545 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 }
9547 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009548 if (*p != NUL)
9549 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009551 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 }
9553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
9558 * "expand()" function
9559 */
9560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 typval_T *argvars;
9563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 char_u *s;
9566 int len;
9567 char_u *errormsg;
9568 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9569 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->v_type = VAR_STRING;
9573 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 if (*s == '%' || *s == '#' || *s == '<')
9575 {
9576 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009577 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 --emsg_off;
9579 }
9580 else
9581 {
9582 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009583 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 if (argvars[1].v_type != VAR_UNKNOWN
9585 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 if (!error)
9588 {
9589 ExpandInit(&xpc);
9590 xpc.xp_context = EXPAND_FILES;
9591 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 }
9593 else
9594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 }
9596}
9597
9598/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009600 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601 */
9602 static void
9603f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009604 typval_T *argvars;
9605 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009609 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 list_T *l1, *l2;
9611 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009614
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 l1 = argvars[0].vval.v_list;
9616 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009617 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9618 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 {
9620 if (argvars[2].v_type != VAR_UNKNOWN)
9621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 before = get_tv_number_chk(&argvars[2], &error);
9623 if (error)
9624 return; /* type error; errmsg already given */
9625
Bram Moolenaar758711c2005-02-02 23:11:38 +00009626 if (before == l1->lv_len)
9627 item = NULL;
9628 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009630 item = list_find(l1, before);
9631 if (item == NULL)
9632 {
9633 EMSGN(_(e_listidx), before);
9634 return;
9635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 }
9637 }
9638 else
9639 item = NULL;
9640 list_extend(l1, l2, item);
9641
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 copy_tv(&argvars[0], rettv);
9643 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009647 dict_T *d1, *d2;
9648 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 char_u *action;
9650 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009652 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653
9654 d1 = argvars[0].vval.v_dict;
9655 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009656 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9657 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 {
9659 /* Check the third argument. */
9660 if (argvars[2].v_type != VAR_UNKNOWN)
9661 {
9662 static char *(av[]) = {"keep", "force", "error"};
9663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 action = get_tv_string_chk(&argvars[2]);
9665 if (action == NULL)
9666 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009667 for (i = 0; i < 3; ++i)
9668 if (STRCMP(action, av[i]) == 0)
9669 break;
9670 if (i == 3)
9671 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009672 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009673 return;
9674 }
9675 }
9676 else
9677 action = (char_u *)"force";
9678
9679 /* Go over all entries in the second dict and add them to the
9680 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009681 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009684 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009686 --todo;
9687 di1 = dict_find(d1, hi2->hi_key, -1);
9688 if (di1 == NULL)
9689 {
9690 di1 = dictitem_copy(HI2DI(hi2));
9691 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9692 dictitem_free(di1);
9693 }
9694 else if (*action == 'e')
9695 {
9696 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9697 break;
9698 }
9699 else if (*action == 'f')
9700 {
9701 clear_tv(&di1->di_tv);
9702 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009704 }
9705 }
9706
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 copy_tv(&argvars[0], rettv);
9708 }
9709 }
9710 else
9711 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009712}
9713
9714/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009715 * "feedkeys()" function
9716 */
9717/*ARGSUSED*/
9718 static void
9719f_feedkeys(argvars, rettv)
9720 typval_T *argvars;
9721 typval_T *rettv;
9722{
9723 int remap = TRUE;
9724 char_u *keys, *flags;
9725 char_u nbuf[NUMBUFLEN];
9726 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009727 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009728
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009729 /* This is not allowed in the sandbox. If the commands would still be
9730 * executed in the sandbox it would be OK, but it probably happens later,
9731 * when "sandbox" is no longer set. */
9732 if (check_secure())
9733 return;
9734
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009735 rettv->vval.v_number = 0;
9736 keys = get_tv_string(&argvars[0]);
9737 if (*keys != NUL)
9738 {
9739 if (argvars[1].v_type != VAR_UNKNOWN)
9740 {
9741 flags = get_tv_string_buf(&argvars[1], nbuf);
9742 for ( ; *flags != NUL; ++flags)
9743 {
9744 switch (*flags)
9745 {
9746 case 'n': remap = FALSE; break;
9747 case 'm': remap = TRUE; break;
9748 case 't': typed = TRUE; break;
9749 }
9750 }
9751 }
9752
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009753 /* Need to escape K_SPECIAL and CSI before putting the string in the
9754 * typeahead buffer. */
9755 keys_esc = vim_strsave_escape_csi(keys);
9756 if (keys_esc != NULL)
9757 {
9758 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009759 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009760 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009761 if (vgetc_busy)
9762 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009763 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009764 }
9765}
9766
9767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 * "filereadable()" function
9769 */
9770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009771f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009772 typval_T *argvars;
9773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009775 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 char_u *p;
9777 int n;
9778
Bram Moolenaarc236c162008-07-13 17:41:49 +00009779#ifndef O_NONBLOCK
9780# define O_NONBLOCK 0
9781#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009783 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9784 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
9786 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009787 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 }
9789 else
9790 n = FALSE;
9791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
9795/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009796 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 * rights to write into.
9798 */
9799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009801 typval_T *argvars;
9802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009804 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009807static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009808
9809 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009810findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009811 typval_T *argvars;
9812 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009813 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009814{
9815#ifdef FEAT_SEARCHPATH
9816 char_u *fname;
9817 char_u *fresult = NULL;
9818 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9819 char_u *p;
9820 char_u pathbuf[NUMBUFLEN];
9821 int count = 1;
9822 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009823 int error = FALSE;
9824#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009825
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009826 rettv->vval.v_string = NULL;
9827 rettv->v_type = VAR_STRING;
9828
9829#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9835 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009836 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 else
9838 {
9839 if (*p != NUL)
9840 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009843 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009845 }
9846
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009847 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9848 error = TRUE;
9849
9850 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 do
9853 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009854 if (rettv->v_type == VAR_STRING)
9855 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 fresult = find_file_in_path_option(first ? fname : NULL,
9857 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009858 0, first, path,
9859 find_what,
9860 curbuf->b_ffname,
9861 find_what == FINDFILE_DIR
9862 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009864
9865 if (fresult != NULL && rettv->v_type == VAR_LIST)
9866 list_append_string(rettv->vval.v_list, fresult, -1);
9867
9868 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009870
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009871 if (rettv->v_type == VAR_STRING)
9872 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009873#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009874}
9875
Bram Moolenaar33570922005-01-25 22:26:29 +00009876static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9877static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009878
9879/*
9880 * Implementation of map() and filter().
9881 */
9882 static void
9883filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009884 typval_T *argvars;
9885 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009886 int map;
9887{
9888 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009889 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 listitem_T *li, *nli;
9891 list_T *l = NULL;
9892 dictitem_T *di;
9893 hashtab_T *ht;
9894 hashitem_T *hi;
9895 dict_T *d = NULL;
9896 typval_T save_val;
9897 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009898 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009899 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009900 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009901 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009902
9903 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009905 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009906 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009907 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 return;
9909 }
9910 else if (argvars[0].v_type == VAR_DICT)
9911 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009912 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009913 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 return;
9915 }
9916 else
9917 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009918 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919 return;
9920 }
9921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 expr = get_tv_string_buf_chk(&argvars[1], buf);
9923 /* On type errors, the preceding call has already displayed an error
9924 * message. Avoid a misleading error message for an empty string that
9925 * was not passed as argument. */
9926 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 prepare_vimvar(VV_VAL, &save_val);
9929 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009930
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009931 /* We reset "did_emsg" to be able to detect whether an error
9932 * occurred during evaluation of the expression. */
9933 save_did_emsg = did_emsg;
9934 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 prepare_vimvar(VV_KEY, &save_key);
9939 vimvars[VV_KEY].vv_type = VAR_STRING;
9940
9941 ht = &d->dv_hashtab;
9942 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009943 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 if (!HASHITEM_EMPTY(hi))
9947 {
9948 --todo;
9949 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009950 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 break;
9952 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009953 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009954 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 break;
9956 if (!map && rem)
9957 dictitem_remove(d, di);
9958 clear_tv(&vimvars[VV_KEY].vv_tv);
9959 }
9960 }
9961 hash_unlock(ht);
9962
9963 restore_vimvar(VV_KEY, &save_key);
9964 }
9965 else
9966 {
9967 for (li = l->lv_first; li != NULL; li = nli)
9968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009969 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009970 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009971 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009972 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009973 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009974 break;
9975 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009977 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009978 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009981
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009982 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009983 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984
9985 copy_tv(&argvars[0], rettv);
9986}
9987
9988 static int
9989filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009990 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 char_u *expr;
9992 int map;
9993 int *remp;
9994{
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009997 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009998
Bram Moolenaar33570922005-01-25 22:26:29 +00009999 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010000 s = expr;
10001 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010002 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 if (*s != NUL) /* check for trailing chars after expr */
10004 {
10005 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010006 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007 }
10008 if (map)
10009 {
10010 /* map(): replace the list item value */
10011 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010012 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010013 *tv = rettv;
10014 }
10015 else
10016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010017 int error = FALSE;
10018
Bram Moolenaare9a41262005-01-15 22:18:47 +000010019 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010021 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010022 /* On type error, nothing has been removed; return FAIL to stop the
10023 * loop. The error message was given by get_tv_number_chk(). */
10024 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010025 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010026 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010027 retval = OK;
10028theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010029 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010030 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010031}
10032
10033/*
10034 * "filter()" function
10035 */
10036 static void
10037f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010038 typval_T *argvars;
10039 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010040{
10041 filter_map(argvars, rettv, FALSE);
10042}
10043
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010045 * "finddir({fname}[, {path}[, {count}]])" function
10046 */
10047 static void
10048f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *argvars;
10050 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010051{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010052 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010053}
10054
10055/*
10056 * "findfile({fname}[, {path}[, {count}]])" function
10057 */
10058 static void
10059f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010062{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010063 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010064}
10065
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010066#ifdef FEAT_FLOAT
10067/*
10068 * "float2nr({float})" function
10069 */
10070 static void
10071f_float2nr(argvars, rettv)
10072 typval_T *argvars;
10073 typval_T *rettv;
10074{
10075 float_T f;
10076
10077 if (get_float_arg(argvars, &f) == OK)
10078 {
10079 if (f < -0x7fffffff)
10080 rettv->vval.v_number = -0x7fffffff;
10081 else if (f > 0x7fffffff)
10082 rettv->vval.v_number = 0x7fffffff;
10083 else
10084 rettv->vval.v_number = (varnumber_T)f;
10085 }
10086 else
10087 rettv->vval.v_number = 0;
10088}
10089
10090/*
10091 * "floor({float})" function
10092 */
10093 static void
10094f_floor(argvars, rettv)
10095 typval_T *argvars;
10096 typval_T *rettv;
10097{
10098 float_T f;
10099
10100 rettv->v_type = VAR_FLOAT;
10101 if (get_float_arg(argvars, &f) == OK)
10102 rettv->vval.v_float = floor(f);
10103 else
10104 rettv->vval.v_float = 0.0;
10105}
10106#endif
10107
Bram Moolenaar0d660222005-01-07 21:51:51 +000010108/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010109 * "fnameescape({string})" function
10110 */
10111 static void
10112f_fnameescape(argvars, rettv)
10113 typval_T *argvars;
10114 typval_T *rettv;
10115{
10116 rettv->vval.v_string = vim_strsave_fnameescape(
10117 get_tv_string(&argvars[0]), FALSE);
10118 rettv->v_type = VAR_STRING;
10119}
10120
10121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 * "fnamemodify({fname}, {mods})" function
10123 */
10124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010126 typval_T *argvars;
10127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128{
10129 char_u *fname;
10130 char_u *mods;
10131 int usedlen = 0;
10132 int len;
10133 char_u *fbuf = NULL;
10134 char_u buf[NUMBUFLEN];
10135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 fname = get_tv_string_chk(&argvars[0]);
10137 mods = get_tv_string_buf_chk(&argvars[1], buf);
10138 if (fname == NULL || mods == NULL)
10139 fname = NULL;
10140 else
10141 {
10142 len = (int)STRLEN(fname);
10143 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 vim_free(fbuf);
10152}
10153
Bram Moolenaar33570922005-01-25 22:26:29 +000010154static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
10156/*
10157 * "foldclosed()" function
10158 */
10159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010161 typval_T *argvars;
10162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 int end;
10164{
10165#ifdef FEAT_FOLDING
10166 linenr_T lnum;
10167 linenr_T first, last;
10168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10171 {
10172 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10173 {
10174 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 return;
10179 }
10180 }
10181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183}
10184
10185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010186 * "foldclosed()" function
10187 */
10188 static void
10189f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010190 typval_T *argvars;
10191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010192{
10193 foldclosed_both(argvars, rettv, FALSE);
10194}
10195
10196/*
10197 * "foldclosedend()" function
10198 */
10199 static void
10200f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 typval_T *argvars;
10202 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010203{
10204 foldclosed_both(argvars, rettv, TRUE);
10205}
10206
10207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 * "foldlevel()" function
10209 */
10210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010212 typval_T *argvars;
10213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215#ifdef FEAT_FOLDING
10216 linenr_T lnum;
10217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010218 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 else
10222#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224}
10225
10226/*
10227 * "foldtext()" function
10228 */
10229/*ARGSUSED*/
10230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010231f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 typval_T *argvars;
10233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235#ifdef FEAT_FOLDING
10236 linenr_T lnum;
10237 char_u *s;
10238 char_u *r;
10239 int len;
10240 char *txt;
10241#endif
10242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010243 rettv->v_type = VAR_STRING;
10244 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010246 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10247 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10248 <= curbuf->b_ml.ml_line_count
10249 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 {
10251 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10253 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 {
10255 if (!linewhite(lnum))
10256 break;
10257 ++lnum;
10258 }
10259
10260 /* Find interesting text in this line. */
10261 s = skipwhite(ml_get(lnum));
10262 /* skip C comment-start */
10263 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010266 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010268 {
10269 s = skipwhite(ml_get(lnum + 1));
10270 if (*s == '*')
10271 s = skipwhite(s + 1);
10272 }
10273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 txt = _("+-%s%3ld lines: ");
10275 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 + 20 /* for %3ld */
10278 + STRLEN(s))); /* concatenated */
10279 if (r != NULL)
10280 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10282 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10283 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 len = (int)STRLEN(r);
10285 STRCAT(r, s);
10286 /* remove 'foldmarker' and 'commentstring' */
10287 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 }
10290 }
10291#endif
10292}
10293
10294/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010295 * "foldtextresult(lnum)" function
10296 */
10297/*ARGSUSED*/
10298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010300 typval_T *argvars;
10301 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010302{
10303#ifdef FEAT_FOLDING
10304 linenr_T lnum;
10305 char_u *text;
10306 char_u buf[51];
10307 foldinfo_T foldinfo;
10308 int fold_count;
10309#endif
10310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 rettv->v_type = VAR_STRING;
10312 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010313#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 /* treat illegal types and illegal string values for {lnum} the same */
10316 if (lnum < 0)
10317 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010318 fold_count = foldedCount(curwin, lnum, &foldinfo);
10319 if (fold_count > 0)
10320 {
10321 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10322 &foldinfo, buf);
10323 if (text == buf)
10324 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010326 }
10327#endif
10328}
10329
10330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 * "foreground()" function
10332 */
10333/*ARGSUSED*/
10334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#ifdef FEAT_GUI
10341 if (gui.in_use)
10342 gui_mch_set_foreground();
10343#else
10344# ifdef WIN32
10345 win32_set_foreground();
10346# endif
10347#endif
10348}
10349
10350/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010351 * "function()" function
10352 */
10353/*ARGSUSED*/
10354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 typval_T *argvars;
10357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010358{
10359 char_u *s;
10360
Bram Moolenaara7043832005-01-21 11:56:39 +000010361 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010363 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010364 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010365 /* Don't check an autoload name for existence here. */
10366 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010367 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368 else
10369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370 rettv->vval.v_string = vim_strsave(s);
10371 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010372 }
10373}
10374
10375/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010376 * "garbagecollect()" function
10377 */
10378/*ARGSUSED*/
10379 static void
10380f_garbagecollect(argvars, rettv)
10381 typval_T *argvars;
10382 typval_T *rettv;
10383{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010384 /* This is postponed until we are back at the toplevel, because we may be
10385 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10386 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010387
10388 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10389 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010390}
10391
10392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010393 * "get()" function
10394 */
10395 static void
10396f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010397 typval_T *argvars;
10398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010399{
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 listitem_T *li;
10401 list_T *l;
10402 dictitem_T *di;
10403 dict_T *d;
10404 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010405
Bram Moolenaare9a41262005-01-15 22:18:47 +000010406 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010407 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010408 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 int error = FALSE;
10411
10412 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10413 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010414 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010415 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 else if (argvars[0].v_type == VAR_DICT)
10418 {
10419 if ((d = argvars[0].vval.v_dict) != NULL)
10420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010421 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 if (di != NULL)
10423 tv = &di->di_tv;
10424 }
10425 }
10426 else
10427 EMSG2(_(e_listdictarg), "get()");
10428
10429 if (tv == NULL)
10430 {
10431 if (argvars[2].v_type == VAR_UNKNOWN)
10432 rettv->vval.v_number = 0;
10433 else
10434 copy_tv(&argvars[2], rettv);
10435 }
10436 else
10437 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010438}
10439
Bram Moolenaar342337a2005-07-21 21:11:17 +000010440static 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 +000010441
10442/*
10443 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010444 * Return a range (from start to end) of lines in rettv from the specified
10445 * buffer.
10446 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010447 */
10448 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010449get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010450 buf_T *buf;
10451 linenr_T start;
10452 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010453 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010454 typval_T *rettv;
10455{
10456 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010457
Bram Moolenaar342337a2005-07-21 21:11:17 +000010458 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010459 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010460 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010461 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010462 }
10463 else
10464 rettv->vval.v_number = 0;
10465
10466 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10467 return;
10468
10469 if (!retlist)
10470 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010471 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10472 p = ml_get_buf(buf, start, FALSE);
10473 else
10474 p = (char_u *)"";
10475
10476 rettv->v_type = VAR_STRING;
10477 rettv->vval.v_string = vim_strsave(p);
10478 }
10479 else
10480 {
10481 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010482 return;
10483
10484 if (start < 1)
10485 start = 1;
10486 if (end > buf->b_ml.ml_line_count)
10487 end = buf->b_ml.ml_line_count;
10488 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010489 if (list_append_string(rettv->vval.v_list,
10490 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010491 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010492 }
10493}
10494
10495/*
10496 * "getbufline()" function
10497 */
10498 static void
10499f_getbufline(argvars, rettv)
10500 typval_T *argvars;
10501 typval_T *rettv;
10502{
10503 linenr_T lnum;
10504 linenr_T end;
10505 buf_T *buf;
10506
10507 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10508 ++emsg_off;
10509 buf = get_buf_tv(&argvars[0]);
10510 --emsg_off;
10511
Bram Moolenaar661b1822005-07-28 22:36:45 +000010512 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010513 if (argvars[2].v_type == VAR_UNKNOWN)
10514 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010515 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010516 end = get_tv_lnum_buf(&argvars[2], buf);
10517
Bram Moolenaar342337a2005-07-21 21:11:17 +000010518 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010519}
10520
Bram Moolenaar0d660222005-01-07 21:51:51 +000010521/*
10522 * "getbufvar()" function
10523 */
10524 static void
10525f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010526 typval_T *argvars;
10527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010528{
10529 buf_T *buf;
10530 buf_T *save_curbuf;
10531 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10535 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010536 ++emsg_off;
10537 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010538
10539 rettv->v_type = VAR_STRING;
10540 rettv->vval.v_string = NULL;
10541
10542 if (buf != NULL && varname != NULL)
10543 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010544 /* set curbuf to be our buf, temporarily */
10545 save_curbuf = curbuf;
10546 curbuf = buf;
10547
Bram Moolenaar0d660222005-01-07 21:51:51 +000010548 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010549 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010550 else
10551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 if (*varname == NUL)
10553 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10554 * scope prefix before the NUL byte is required by
10555 * find_var_in_ht(). */
10556 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010557 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010558 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010559 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010562
10563 /* restore previous notion of curbuf */
10564 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010565 }
10566
10567 --emsg_off;
10568}
10569
10570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 * "getchar()" function
10572 */
10573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *argvars;
10576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
10578 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010581 /* Position the cursor. Needed after a message that ends in a space. */
10582 windgoto(msg_row, msg_col);
10583
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 ++no_mapping;
10585 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010586 for (;;)
10587 {
10588 if (argvars[0].v_type == VAR_UNKNOWN)
10589 /* getchar(): blocking wait. */
10590 n = safe_vgetc();
10591 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10592 /* getchar(1): only check if char avail */
10593 n = vpeekc();
10594 else if (error || vpeekc() == NUL)
10595 /* illegal argument or getchar(0) and no char avail: return zero */
10596 n = 0;
10597 else
10598 /* getchar(0) and char avail: return char */
10599 n = safe_vgetc();
10600 if (n == K_IGNORE)
10601 continue;
10602 break;
10603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 --no_mapping;
10605 --allow_keys;
10606
Bram Moolenaar219b8702006-11-01 14:32:36 +000010607 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10608 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10609 vimvars[VV_MOUSE_COL].vv_nr = 0;
10610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 if (IS_SPECIAL(n) || mod_mask != 0)
10613 {
10614 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10615 int i = 0;
10616
10617 /* Turn a special key into three bytes, plus modifier. */
10618 if (mod_mask != 0)
10619 {
10620 temp[i++] = K_SPECIAL;
10621 temp[i++] = KS_MODIFIER;
10622 temp[i++] = mod_mask;
10623 }
10624 if (IS_SPECIAL(n))
10625 {
10626 temp[i++] = K_SPECIAL;
10627 temp[i++] = K_SECOND(n);
10628 temp[i++] = K_THIRD(n);
10629 }
10630#ifdef FEAT_MBYTE
10631 else if (has_mbyte)
10632 i += (*mb_char2bytes)(n, temp + i);
10633#endif
10634 else
10635 temp[i++] = n;
10636 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 rettv->v_type = VAR_STRING;
10638 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010639
10640#ifdef FEAT_MOUSE
10641 if (n == K_LEFTMOUSE
10642 || n == K_LEFTMOUSE_NM
10643 || n == K_LEFTDRAG
10644 || n == K_LEFTRELEASE
10645 || n == K_LEFTRELEASE_NM
10646 || n == K_MIDDLEMOUSE
10647 || n == K_MIDDLEDRAG
10648 || n == K_MIDDLERELEASE
10649 || n == K_RIGHTMOUSE
10650 || n == K_RIGHTDRAG
10651 || n == K_RIGHTRELEASE
10652 || n == K_X1MOUSE
10653 || n == K_X1DRAG
10654 || n == K_X1RELEASE
10655 || n == K_X2MOUSE
10656 || n == K_X2DRAG
10657 || n == K_X2RELEASE
10658 || n == K_MOUSEDOWN
10659 || n == K_MOUSEUP)
10660 {
10661 int row = mouse_row;
10662 int col = mouse_col;
10663 win_T *win;
10664 linenr_T lnum;
10665# ifdef FEAT_WINDOWS
10666 win_T *wp;
10667# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010668 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010669
10670 if (row >= 0 && col >= 0)
10671 {
10672 /* Find the window at the mouse coordinates and compute the
10673 * text position. */
10674 win = mouse_find_win(&row, &col);
10675 (void)mouse_comp_pos(win, &row, &col, &lnum);
10676# ifdef FEAT_WINDOWS
10677 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010678 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010679# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010680 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010681 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10682 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10683 }
10684 }
10685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 }
10687}
10688
10689/*
10690 * "getcharmod()" function
10691 */
10692/*ARGSUSED*/
10693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010695 typval_T *argvars;
10696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699}
10700
10701/*
10702 * "getcmdline()" function
10703 */
10704/*ARGSUSED*/
10705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010707 typval_T *argvars;
10708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 rettv->v_type = VAR_STRING;
10711 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712}
10713
10714/*
10715 * "getcmdpos()" function
10716 */
10717/*ARGSUSED*/
10718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *argvars;
10721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725
10726/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010727 * "getcmdtype()" function
10728 */
10729/*ARGSUSED*/
10730 static void
10731f_getcmdtype(argvars, rettv)
10732 typval_T *argvars;
10733 typval_T *rettv;
10734{
10735 rettv->v_type = VAR_STRING;
10736 rettv->vval.v_string = alloc(2);
10737 if (rettv->vval.v_string != NULL)
10738 {
10739 rettv->vval.v_string[0] = get_cmdline_type();
10740 rettv->vval.v_string[1] = NUL;
10741 }
10742}
10743
10744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 * "getcwd()" function
10746 */
10747/*ARGSUSED*/
10748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752{
10753 char_u cwd[MAXPATHL];
10754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 else
10759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010762 if (rettv->vval.v_string != NULL)
10763 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764#endif
10765 }
10766}
10767
10768/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010769 * "getfontname()" function
10770 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010771/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010774 typval_T *argvars;
10775 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->v_type = VAR_STRING;
10778 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010779#ifdef FEAT_GUI
10780 if (gui.in_use)
10781 {
10782 GuiFont font;
10783 char_u *name = NULL;
10784
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010785 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010786 {
10787 /* Get the "Normal" font. Either the name saved by
10788 * hl_set_font_name() or from the font ID. */
10789 font = gui.norm_font;
10790 name = hl_get_font_name();
10791 }
10792 else
10793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010795 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10796 return;
10797 font = gui_mch_get_font(name, FALSE);
10798 if (font == NOFONT)
10799 return; /* Invalid font name, return empty string. */
10800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010802 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010803 gui_mch_free_font(font);
10804 }
10805#endif
10806}
10807
10808/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010809 * "getfperm({fname})" function
10810 */
10811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010813 typval_T *argvars;
10814 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010815{
10816 char_u *fname;
10817 struct stat st;
10818 char_u *perm = NULL;
10819 char_u flags[] = "rwx";
10820 int i;
10821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010822 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010825 if (mch_stat((char *)fname, &st) >= 0)
10826 {
10827 perm = vim_strsave((char_u *)"---------");
10828 if (perm != NULL)
10829 {
10830 for (i = 0; i < 9; i++)
10831 {
10832 if (st.st_mode & (1 << (8 - i)))
10833 perm[i] = flags[i % 3];
10834 }
10835 }
10836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010837 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010838}
10839
10840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 * "getfsize({fname})" function
10842 */
10843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 typval_T *argvars;
10846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
10848 char_u *fname;
10849 struct stat st;
10850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854
10855 if (mch_stat((char *)fname, &st) >= 0)
10856 {
10857 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010862
10863 /* non-perfect check for overflow */
10864 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10865 rettv->vval.v_number = -2;
10866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 }
10868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870}
10871
10872/*
10873 * "getftime({fname})" function
10874 */
10875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
10880 char_u *fname;
10881 struct stat st;
10882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884
10885 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889}
10890
10891/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010892 * "getftype({fname})" function
10893 */
10894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 typval_T *argvars;
10897 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010898{
10899 char_u *fname;
10900 struct stat st;
10901 char_u *type = NULL;
10902 char *t;
10903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010907 if (mch_lstat((char *)fname, &st) >= 0)
10908 {
10909#ifdef S_ISREG
10910 if (S_ISREG(st.st_mode))
10911 t = "file";
10912 else if (S_ISDIR(st.st_mode))
10913 t = "dir";
10914# ifdef S_ISLNK
10915 else if (S_ISLNK(st.st_mode))
10916 t = "link";
10917# endif
10918# ifdef S_ISBLK
10919 else if (S_ISBLK(st.st_mode))
10920 t = "bdev";
10921# endif
10922# ifdef S_ISCHR
10923 else if (S_ISCHR(st.st_mode))
10924 t = "cdev";
10925# endif
10926# ifdef S_ISFIFO
10927 else if (S_ISFIFO(st.st_mode))
10928 t = "fifo";
10929# endif
10930# ifdef S_ISSOCK
10931 else if (S_ISSOCK(st.st_mode))
10932 t = "fifo";
10933# endif
10934 else
10935 t = "other";
10936#else
10937# ifdef S_IFMT
10938 switch (st.st_mode & S_IFMT)
10939 {
10940 case S_IFREG: t = "file"; break;
10941 case S_IFDIR: t = "dir"; break;
10942# ifdef S_IFLNK
10943 case S_IFLNK: t = "link"; break;
10944# endif
10945# ifdef S_IFBLK
10946 case S_IFBLK: t = "bdev"; break;
10947# endif
10948# ifdef S_IFCHR
10949 case S_IFCHR: t = "cdev"; break;
10950# endif
10951# ifdef S_IFIFO
10952 case S_IFIFO: t = "fifo"; break;
10953# endif
10954# ifdef S_IFSOCK
10955 case S_IFSOCK: t = "socket"; break;
10956# endif
10957 default: t = "other";
10958 }
10959# else
10960 if (mch_isdir(fname))
10961 t = "dir";
10962 else
10963 t = "file";
10964# endif
10965#endif
10966 type = vim_strsave((char_u *)t);
10967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010969}
10970
10971/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010972 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010973 */
10974 static void
10975f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *argvars;
10977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010978{
10979 linenr_T lnum;
10980 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010981 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010982
10983 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010984 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010985 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010986 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010987 retlist = FALSE;
10988 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010989 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010990 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010992 retlist = TRUE;
10993 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010994
Bram Moolenaar342337a2005-07-21 21:11:17 +000010995 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010996}
10997
10998/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010999 * "getmatches()" function
11000 */
11001/*ARGSUSED*/
11002 static void
11003f_getmatches(argvars, rettv)
11004 typval_T *argvars;
11005 typval_T *rettv;
11006{
11007#ifdef FEAT_SEARCH_EXTRA
11008 dict_T *dict;
11009 matchitem_T *cur = curwin->w_match_head;
11010
11011 rettv->vval.v_number = 0;
11012
11013 if (rettv_list_alloc(rettv) == OK)
11014 {
11015 while (cur != NULL)
11016 {
11017 dict = dict_alloc();
11018 if (dict == NULL)
11019 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011020 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11021 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11022 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11023 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11024 list_append_dict(rettv->vval.v_list, dict);
11025 cur = cur->next;
11026 }
11027 }
11028#endif
11029}
11030
11031/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011032 * "getpid()" function
11033 */
11034/*ARGSUSED*/
11035 static void
11036f_getpid(argvars, rettv)
11037 typval_T *argvars;
11038 typval_T *rettv;
11039{
11040 rettv->vval.v_number = mch_get_pid();
11041}
11042
11043/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011044 * "getpos(string)" function
11045 */
11046 static void
11047f_getpos(argvars, rettv)
11048 typval_T *argvars;
11049 typval_T *rettv;
11050{
11051 pos_T *fp;
11052 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011053 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011054
11055 if (rettv_list_alloc(rettv) == OK)
11056 {
11057 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011058 fp = var2fpos(&argvars[0], TRUE, &fnum);
11059 if (fnum != -1)
11060 list_append_number(l, (varnumber_T)fnum);
11061 else
11062 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011063 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11064 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011065 list_append_number(l, (fp != NULL)
11066 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011067 : (varnumber_T)0);
11068 list_append_number(l,
11069#ifdef FEAT_VIRTUALEDIT
11070 (fp != NULL) ? (varnumber_T)fp->coladd :
11071#endif
11072 (varnumber_T)0);
11073 }
11074 else
11075 rettv->vval.v_number = FALSE;
11076}
11077
11078/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011079 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011080 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000011081/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000011082 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011083f_getqflist(argvars, rettv)
11084 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011085 typval_T *rettv;
11086{
11087#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011088 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011089#endif
11090
Bram Moolenaar77f66d62007-02-20 02:16:18 +000011091 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011092#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011093 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011094 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011095 wp = NULL;
11096 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11097 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011098 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011099 if (wp == NULL)
11100 return;
11101 }
11102
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011103 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011104 }
11105#endif
11106}
11107
11108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 * "getreg()" function
11110 */
11111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 char_u *strregname;
11117 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011118 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011121 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 strregname = get_tv_string_chk(&argvars[0]);
11124 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011125 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 regname = (strregname == NULL ? '"' : *strregname);
11131 if (regname == 0)
11132 regname = '"';
11133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 rettv->vval.v_string = error ? NULL :
11136 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137}
11138
11139/*
11140 * "getregtype()" function
11141 */
11142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011144 typval_T *argvars;
11145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
11147 char_u *strregname;
11148 int regname;
11149 char_u buf[NUMBUFLEN + 2];
11150 long reglen = 0;
11151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011152 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153 {
11154 strregname = get_tv_string_chk(&argvars[0]);
11155 if (strregname == NULL) /* type error; errmsg already given */
11156 {
11157 rettv->v_type = VAR_STRING;
11158 rettv->vval.v_string = NULL;
11159 return;
11160 }
11161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 else
11163 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165
11166 regname = (strregname == NULL ? '"' : *strregname);
11167 if (regname == 0)
11168 regname = '"';
11169
11170 buf[0] = NUL;
11171 buf[1] = NUL;
11172 switch (get_reg_type(regname, &reglen))
11173 {
11174 case MLINE: buf[0] = 'V'; break;
11175 case MCHAR: buf[0] = 'v'; break;
11176#ifdef FEAT_VISUAL
11177 case MBLOCK:
11178 buf[0] = Ctrl_V;
11179 sprintf((char *)buf + 1, "%ld", reglen + 1);
11180 break;
11181#endif
11182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->v_type = VAR_STRING;
11184 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185}
11186
11187/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011188 * "gettabwinvar()" function
11189 */
11190 static void
11191f_gettabwinvar(argvars, rettv)
11192 typval_T *argvars;
11193 typval_T *rettv;
11194{
11195 getwinvar(argvars, rettv, 1);
11196}
11197
11198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 * "getwinposx()" function
11200 */
11201/*ARGSUSED*/
11202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011204 typval_T *argvars;
11205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208#ifdef FEAT_GUI
11209 if (gui.in_use)
11210 {
11211 int x, y;
11212
11213 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 }
11216#endif
11217}
11218
11219/*
11220 * "getwinposy()" function
11221 */
11222/*ARGSUSED*/
11223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011225 typval_T *argvars;
11226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229#ifdef FEAT_GUI
11230 if (gui.in_use)
11231 {
11232 int x, y;
11233
11234 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 }
11237#endif
11238}
11239
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011240/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011241 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011242 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011243 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011244find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011245 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011246 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011247{
11248#ifdef FEAT_WINDOWS
11249 win_T *wp;
11250#endif
11251 int nr;
11252
11253 nr = get_tv_number_chk(vp, NULL);
11254
11255#ifdef FEAT_WINDOWS
11256 if (nr < 0)
11257 return NULL;
11258 if (nr == 0)
11259 return curwin;
11260
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011261 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11262 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011263 if (--nr <= 0)
11264 break;
11265 return wp;
11266#else
11267 if (nr == 0 || nr == 1)
11268 return curwin;
11269 return NULL;
11270#endif
11271}
11272
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273/*
11274 * "getwinvar()" function
11275 */
11276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011278 typval_T *argvars;
11279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011281 getwinvar(argvars, rettv, 0);
11282}
11283
11284/*
11285 * getwinvar() and gettabwinvar()
11286 */
11287 static void
11288getwinvar(argvars, rettv, off)
11289 typval_T *argvars;
11290 typval_T *rettv;
11291 int off; /* 1 for gettabwinvar() */
11292{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 win_T *win, *oldcurwin;
11294 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011295 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011296 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011298#ifdef FEAT_WINDOWS
11299 if (off == 1)
11300 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11301 else
11302 tp = curtab;
11303#endif
11304 win = find_win_by_nr(&argvars[off], tp);
11305 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308 rettv->v_type = VAR_STRING;
11309 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310
11311 if (win != NULL && varname != NULL)
11312 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011313 /* Set curwin to be our win, temporarily. Also set curbuf, so
11314 * that we can get buffer-local options. */
11315 oldcurwin = curwin;
11316 curwin = win;
11317 curbuf = win->w_buffer;
11318
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 else
11322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323 if (*varname == NUL)
11324 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11325 * scope prefix before the NUL byte is required by
11326 * find_var_in_ht(). */
11327 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011329 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011333
11334 /* restore previous notion of curwin */
11335 curwin = oldcurwin;
11336 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 }
11338
11339 --emsg_off;
11340}
11341
11342/*
11343 * "glob()" function
11344 */
11345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011350 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011352 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011354 /* When the optional second argument is non-zero, don't remove matches
11355 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11356 if (argvars[1].v_type != VAR_UNKNOWN
11357 && get_tv_number_chk(&argvars[1], &error))
11358 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011360 if (!error)
11361 {
11362 ExpandInit(&xpc);
11363 xpc.xp_context = EXPAND_FILES;
11364 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11365 NULL, flags, WILD_ALL);
11366 }
11367 else
11368 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369}
11370
11371/*
11372 * "globpath()" function
11373 */
11374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011379 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011382 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011384 /* When the optional second argument is non-zero, don't remove matches
11385 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11386 if (argvars[2].v_type != VAR_UNKNOWN
11387 && get_tv_number_chk(&argvars[2], &error))
11388 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011390 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 rettv->vval.v_string = NULL;
11392 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011393 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11394 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395}
11396
11397/*
11398 * "has()" function
11399 */
11400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 typval_T *argvars;
11403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404{
11405 int i;
11406 char_u *name;
11407 int n = FALSE;
11408 static char *(has_list[]) =
11409 {
11410#ifdef AMIGA
11411 "amiga",
11412# ifdef FEAT_ARP
11413 "arp",
11414# endif
11415#endif
11416#ifdef __BEOS__
11417 "beos",
11418#endif
11419#ifdef MSDOS
11420# ifdef DJGPP
11421 "dos32",
11422# else
11423 "dos16",
11424# endif
11425#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011426#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 "mac",
11428#endif
11429#if defined(MACOS_X_UNIX)
11430 "macunix",
11431#endif
11432#ifdef OS2
11433 "os2",
11434#endif
11435#ifdef __QNX__
11436 "qnx",
11437#endif
11438#ifdef RISCOS
11439 "riscos",
11440#endif
11441#ifdef UNIX
11442 "unix",
11443#endif
11444#ifdef VMS
11445 "vms",
11446#endif
11447#ifdef WIN16
11448 "win16",
11449#endif
11450#ifdef WIN32
11451 "win32",
11452#endif
11453#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11454 "win32unix",
11455#endif
11456#ifdef WIN64
11457 "win64",
11458#endif
11459#ifdef EBCDIC
11460 "ebcdic",
11461#endif
11462#ifndef CASE_INSENSITIVE_FILENAME
11463 "fname_case",
11464#endif
11465#ifdef FEAT_ARABIC
11466 "arabic",
11467#endif
11468#ifdef FEAT_AUTOCMD
11469 "autocmd",
11470#endif
11471#ifdef FEAT_BEVAL
11472 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011473# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11474 "balloon_multiline",
11475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476#endif
11477#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11478 "builtin_terms",
11479# ifdef ALL_BUILTIN_TCAPS
11480 "all_builtin_terms",
11481# endif
11482#endif
11483#ifdef FEAT_BYTEOFF
11484 "byte_offset",
11485#endif
11486#ifdef FEAT_CINDENT
11487 "cindent",
11488#endif
11489#ifdef FEAT_CLIENTSERVER
11490 "clientserver",
11491#endif
11492#ifdef FEAT_CLIPBOARD
11493 "clipboard",
11494#endif
11495#ifdef FEAT_CMDL_COMPL
11496 "cmdline_compl",
11497#endif
11498#ifdef FEAT_CMDHIST
11499 "cmdline_hist",
11500#endif
11501#ifdef FEAT_COMMENTS
11502 "comments",
11503#endif
11504#ifdef FEAT_CRYPT
11505 "cryptv",
11506#endif
11507#ifdef FEAT_CSCOPE
11508 "cscope",
11509#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011510#ifdef CURSOR_SHAPE
11511 "cursorshape",
11512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513#ifdef DEBUG
11514 "debug",
11515#endif
11516#ifdef FEAT_CON_DIALOG
11517 "dialog_con",
11518#endif
11519#ifdef FEAT_GUI_DIALOG
11520 "dialog_gui",
11521#endif
11522#ifdef FEAT_DIFF
11523 "diff",
11524#endif
11525#ifdef FEAT_DIGRAPHS
11526 "digraphs",
11527#endif
11528#ifdef FEAT_DND
11529 "dnd",
11530#endif
11531#ifdef FEAT_EMACS_TAGS
11532 "emacs_tags",
11533#endif
11534 "eval", /* always present, of course! */
11535#ifdef FEAT_EX_EXTRA
11536 "ex_extra",
11537#endif
11538#ifdef FEAT_SEARCH_EXTRA
11539 "extra_search",
11540#endif
11541#ifdef FEAT_FKMAP
11542 "farsi",
11543#endif
11544#ifdef FEAT_SEARCHPATH
11545 "file_in_path",
11546#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011547#if defined(UNIX) && !defined(USE_SYSTEM)
11548 "filterpipe",
11549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550#ifdef FEAT_FIND_ID
11551 "find_in_path",
11552#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011553#ifdef FEAT_FLOAT
11554 "float",
11555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556#ifdef FEAT_FOLDING
11557 "folding",
11558#endif
11559#ifdef FEAT_FOOTER
11560 "footer",
11561#endif
11562#if !defined(USE_SYSTEM) && defined(UNIX)
11563 "fork",
11564#endif
11565#ifdef FEAT_GETTEXT
11566 "gettext",
11567#endif
11568#ifdef FEAT_GUI
11569 "gui",
11570#endif
11571#ifdef FEAT_GUI_ATHENA
11572# ifdef FEAT_GUI_NEXTAW
11573 "gui_neXtaw",
11574# else
11575 "gui_athena",
11576# endif
11577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578#ifdef FEAT_GUI_GTK
11579 "gui_gtk",
11580# ifdef HAVE_GTK2
11581 "gui_gtk2",
11582# endif
11583#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011584#ifdef FEAT_GUI_GNOME
11585 "gui_gnome",
11586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587#ifdef FEAT_GUI_MAC
11588 "gui_mac",
11589#endif
11590#ifdef FEAT_GUI_MOTIF
11591 "gui_motif",
11592#endif
11593#ifdef FEAT_GUI_PHOTON
11594 "gui_photon",
11595#endif
11596#ifdef FEAT_GUI_W16
11597 "gui_win16",
11598#endif
11599#ifdef FEAT_GUI_W32
11600 "gui_win32",
11601#endif
11602#ifdef FEAT_HANGULIN
11603 "hangul_input",
11604#endif
11605#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11606 "iconv",
11607#endif
11608#ifdef FEAT_INS_EXPAND
11609 "insert_expand",
11610#endif
11611#ifdef FEAT_JUMPLIST
11612 "jumplist",
11613#endif
11614#ifdef FEAT_KEYMAP
11615 "keymap",
11616#endif
11617#ifdef FEAT_LANGMAP
11618 "langmap",
11619#endif
11620#ifdef FEAT_LIBCALL
11621 "libcall",
11622#endif
11623#ifdef FEAT_LINEBREAK
11624 "linebreak",
11625#endif
11626#ifdef FEAT_LISP
11627 "lispindent",
11628#endif
11629#ifdef FEAT_LISTCMDS
11630 "listcmds",
11631#endif
11632#ifdef FEAT_LOCALMAP
11633 "localmap",
11634#endif
11635#ifdef FEAT_MENU
11636 "menu",
11637#endif
11638#ifdef FEAT_SESSION
11639 "mksession",
11640#endif
11641#ifdef FEAT_MODIFY_FNAME
11642 "modify_fname",
11643#endif
11644#ifdef FEAT_MOUSE
11645 "mouse",
11646#endif
11647#ifdef FEAT_MOUSESHAPE
11648 "mouseshape",
11649#endif
11650#if defined(UNIX) || defined(VMS)
11651# ifdef FEAT_MOUSE_DEC
11652 "mouse_dec",
11653# endif
11654# ifdef FEAT_MOUSE_GPM
11655 "mouse_gpm",
11656# endif
11657# ifdef FEAT_MOUSE_JSB
11658 "mouse_jsbterm",
11659# endif
11660# ifdef FEAT_MOUSE_NET
11661 "mouse_netterm",
11662# endif
11663# ifdef FEAT_MOUSE_PTERM
11664 "mouse_pterm",
11665# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011666# ifdef FEAT_SYSMOUSE
11667 "mouse_sysmouse",
11668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669# ifdef FEAT_MOUSE_XTERM
11670 "mouse_xterm",
11671# endif
11672#endif
11673#ifdef FEAT_MBYTE
11674 "multi_byte",
11675#endif
11676#ifdef FEAT_MBYTE_IME
11677 "multi_byte_ime",
11678#endif
11679#ifdef FEAT_MULTI_LANG
11680 "multi_lang",
11681#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011682#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011683#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011684 "mzscheme",
11685#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687#ifdef FEAT_OLE
11688 "ole",
11689#endif
11690#ifdef FEAT_OSFILETYPE
11691 "osfiletype",
11692#endif
11693#ifdef FEAT_PATH_EXTRA
11694 "path_extra",
11695#endif
11696#ifdef FEAT_PERL
11697#ifndef DYNAMIC_PERL
11698 "perl",
11699#endif
11700#endif
11701#ifdef FEAT_PYTHON
11702#ifndef DYNAMIC_PYTHON
11703 "python",
11704#endif
11705#endif
11706#ifdef FEAT_POSTSCRIPT
11707 "postscript",
11708#endif
11709#ifdef FEAT_PRINTER
11710 "printer",
11711#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011712#ifdef FEAT_PROFILE
11713 "profile",
11714#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011715#ifdef FEAT_RELTIME
11716 "reltime",
11717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718#ifdef FEAT_QUICKFIX
11719 "quickfix",
11720#endif
11721#ifdef FEAT_RIGHTLEFT
11722 "rightleft",
11723#endif
11724#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11725 "ruby",
11726#endif
11727#ifdef FEAT_SCROLLBIND
11728 "scrollbind",
11729#endif
11730#ifdef FEAT_CMDL_INFO
11731 "showcmd",
11732 "cmdline_info",
11733#endif
11734#ifdef FEAT_SIGNS
11735 "signs",
11736#endif
11737#ifdef FEAT_SMARTINDENT
11738 "smartindent",
11739#endif
11740#ifdef FEAT_SNIFF
11741 "sniff",
11742#endif
11743#ifdef FEAT_STL_OPT
11744 "statusline",
11745#endif
11746#ifdef FEAT_SUN_WORKSHOP
11747 "sun_workshop",
11748#endif
11749#ifdef FEAT_NETBEANS_INTG
11750 "netbeans_intg",
11751#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011752#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011753 "spell",
11754#endif
11755#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 "syntax",
11757#endif
11758#if defined(USE_SYSTEM) || !defined(UNIX)
11759 "system",
11760#endif
11761#ifdef FEAT_TAG_BINS
11762 "tag_binary",
11763#endif
11764#ifdef FEAT_TAG_OLDSTATIC
11765 "tag_old_static",
11766#endif
11767#ifdef FEAT_TAG_ANYWHITE
11768 "tag_any_white",
11769#endif
11770#ifdef FEAT_TCL
11771# ifndef DYNAMIC_TCL
11772 "tcl",
11773# endif
11774#endif
11775#ifdef TERMINFO
11776 "terminfo",
11777#endif
11778#ifdef FEAT_TERMRESPONSE
11779 "termresponse",
11780#endif
11781#ifdef FEAT_TEXTOBJ
11782 "textobjects",
11783#endif
11784#ifdef HAVE_TGETENT
11785 "tgetent",
11786#endif
11787#ifdef FEAT_TITLE
11788 "title",
11789#endif
11790#ifdef FEAT_TOOLBAR
11791 "toolbar",
11792#endif
11793#ifdef FEAT_USR_CMDS
11794 "user-commands", /* was accidentally included in 5.4 */
11795 "user_commands",
11796#endif
11797#ifdef FEAT_VIMINFO
11798 "viminfo",
11799#endif
11800#ifdef FEAT_VERTSPLIT
11801 "vertsplit",
11802#endif
11803#ifdef FEAT_VIRTUALEDIT
11804 "virtualedit",
11805#endif
11806#ifdef FEAT_VISUAL
11807 "visual",
11808#endif
11809#ifdef FEAT_VISUALEXTRA
11810 "visualextra",
11811#endif
11812#ifdef FEAT_VREPLACE
11813 "vreplace",
11814#endif
11815#ifdef FEAT_WILDIGN
11816 "wildignore",
11817#endif
11818#ifdef FEAT_WILDMENU
11819 "wildmenu",
11820#endif
11821#ifdef FEAT_WINDOWS
11822 "windows",
11823#endif
11824#ifdef FEAT_WAK
11825 "winaltkeys",
11826#endif
11827#ifdef FEAT_WRITEBACKUP
11828 "writebackup",
11829#endif
11830#ifdef FEAT_XIM
11831 "xim",
11832#endif
11833#ifdef FEAT_XFONTSET
11834 "xfontset",
11835#endif
11836#ifdef USE_XSMP
11837 "xsmp",
11838#endif
11839#ifdef USE_XSMP_INTERACT
11840 "xsmp_interact",
11841#endif
11842#ifdef FEAT_XCLIPBOARD
11843 "xterm_clipboard",
11844#endif
11845#ifdef FEAT_XTERM_SAVE
11846 "xterm_save",
11847#endif
11848#if defined(UNIX) && defined(FEAT_X11)
11849 "X11",
11850#endif
11851 NULL
11852 };
11853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 for (i = 0; has_list[i] != NULL; ++i)
11856 if (STRICMP(name, has_list[i]) == 0)
11857 {
11858 n = TRUE;
11859 break;
11860 }
11861
11862 if (n == FALSE)
11863 {
11864 if (STRNICMP(name, "patch", 5) == 0)
11865 n = has_patch(atoi((char *)name + 5));
11866 else if (STRICMP(name, "vim_starting") == 0)
11867 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011868#ifdef FEAT_MBYTE
11869 else if (STRICMP(name, "multi_byte_encoding") == 0)
11870 n = has_mbyte;
11871#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011872#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11873 else if (STRICMP(name, "balloon_multiline") == 0)
11874 n = multiline_balloon_available();
11875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876#ifdef DYNAMIC_TCL
11877 else if (STRICMP(name, "tcl") == 0)
11878 n = tcl_enabled(FALSE);
11879#endif
11880#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11881 else if (STRICMP(name, "iconv") == 0)
11882 n = iconv_enabled(FALSE);
11883#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011884#ifdef DYNAMIC_MZSCHEME
11885 else if (STRICMP(name, "mzscheme") == 0)
11886 n = mzscheme_enabled(FALSE);
11887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888#ifdef DYNAMIC_RUBY
11889 else if (STRICMP(name, "ruby") == 0)
11890 n = ruby_enabled(FALSE);
11891#endif
11892#ifdef DYNAMIC_PYTHON
11893 else if (STRICMP(name, "python") == 0)
11894 n = python_enabled(FALSE);
11895#endif
11896#ifdef DYNAMIC_PERL
11897 else if (STRICMP(name, "perl") == 0)
11898 n = perl_enabled(FALSE);
11899#endif
11900#ifdef FEAT_GUI
11901 else if (STRICMP(name, "gui_running") == 0)
11902 n = (gui.in_use || gui.starting);
11903# ifdef FEAT_GUI_W32
11904 else if (STRICMP(name, "gui_win32s") == 0)
11905 n = gui_is_win32s();
11906# endif
11907# ifdef FEAT_BROWSE
11908 else if (STRICMP(name, "browse") == 0)
11909 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11910# endif
11911#endif
11912#ifdef FEAT_SYN_HL
11913 else if (STRICMP(name, "syntax_items") == 0)
11914 n = syntax_present(curbuf);
11915#endif
11916#if defined(WIN3264)
11917 else if (STRICMP(name, "win95") == 0)
11918 n = mch_windows95();
11919#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011920#ifdef FEAT_NETBEANS_INTG
11921 else if (STRICMP(name, "netbeans_enabled") == 0)
11922 n = usingNetbeans;
11923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 }
11925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927}
11928
11929/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011930 * "has_key()" function
11931 */
11932 static void
11933f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011934 typval_T *argvars;
11935 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011936{
11937 rettv->vval.v_number = 0;
11938 if (argvars[0].v_type != VAR_DICT)
11939 {
11940 EMSG(_(e_dictreq));
11941 return;
11942 }
11943 if (argvars[0].vval.v_dict == NULL)
11944 return;
11945
11946 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011947 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011948}
11949
11950/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011951 * "haslocaldir()" function
11952 */
11953/*ARGSUSED*/
11954 static void
11955f_haslocaldir(argvars, rettv)
11956 typval_T *argvars;
11957 typval_T *rettv;
11958{
11959 rettv->vval.v_number = (curwin->w_localdir != NULL);
11960}
11961
11962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 * "hasmapto()" function
11964 */
11965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011967 typval_T *argvars;
11968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970 char_u *name;
11971 char_u *mode;
11972 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011973 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011976 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 mode = (char_u *)"nvo";
11978 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011981 if (argvars[2].v_type != VAR_UNKNOWN)
11982 abbr = get_tv_number(&argvars[2]);
11983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984
Bram Moolenaar2c932302006-03-18 21:42:09 +000011985 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989}
11990
11991/*
11992 * "histadd()" function
11993 */
11994/*ARGSUSED*/
11995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 typval_T *argvars;
11998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999{
12000#ifdef FEAT_CMDHIST
12001 int histype;
12002 char_u *str;
12003 char_u buf[NUMBUFLEN];
12004#endif
12005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012006 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 if (check_restricted() || check_secure())
12008 return;
12009#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12011 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 if (histype >= 0)
12013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 if (*str != NUL)
12016 {
12017 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 return;
12020 }
12021 }
12022#endif
12023}
12024
12025/*
12026 * "histdel()" function
12027 */
12028/*ARGSUSED*/
12029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012031 typval_T *argvars;
12032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033{
12034#ifdef FEAT_CMDHIST
12035 int n;
12036 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12040 if (str == NULL)
12041 n = 0;
12042 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012044 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012045 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 else
12050 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012051 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 get_tv_string_buf(&argvars[1], buf));
12053 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056#endif
12057}
12058
12059/*
12060 * "histget()" function
12061 */
12062/*ARGSUSED*/
12063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
12068#ifdef FEAT_CMDHIST
12069 int type;
12070 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12074 if (str == NULL)
12075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 {
12078 type = get_histtype(str);
12079 if (argvars[1].v_type == VAR_UNKNOWN)
12080 idx = get_history_idx(type);
12081 else
12082 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12083 /* -1 on type error */
12084 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090}
12091
12092/*
12093 * "histnr()" function
12094 */
12095/*ARGSUSED*/
12096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012098 typval_T *argvars;
12099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100{
12101 int i;
12102
12103#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 char_u *history = get_tv_string_chk(&argvars[0]);
12105
12106 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 if (i >= HIST_CMD && i < HIST_COUNT)
12108 i = get_history_idx(i);
12109 else
12110#endif
12111 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113}
12114
12115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 * "highlightID(name)" function
12117 */
12118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 typval_T *argvars;
12121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124}
12125
12126/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127 * "highlight_exists()" function
12128 */
12129 static void
12130f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133{
12134 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12135}
12136
12137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138 * "hostname()" function
12139 */
12140/*ARGSUSED*/
12141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
12146 char_u hostname[256];
12147
12148 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->v_type = VAR_STRING;
12150 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151}
12152
12153/*
12154 * iconv() function
12155 */
12156/*ARGSUSED*/
12157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012159 typval_T *argvars;
12160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161{
12162#ifdef FEAT_MBYTE
12163 char_u buf1[NUMBUFLEN];
12164 char_u buf2[NUMBUFLEN];
12165 char_u *from, *to, *str;
12166 vimconv_T vimconv;
12167#endif
12168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->v_type = VAR_STRING;
12170 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171
12172#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 str = get_tv_string(&argvars[0]);
12174 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12175 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 vimconv.vc_type = CONV_NONE;
12177 convert_setup(&vimconv, from, to);
12178
12179 /* If the encodings are equal, no conversion needed. */
12180 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184
12185 convert_setup(&vimconv, NULL, NULL);
12186 vim_free(from);
12187 vim_free(to);
12188#endif
12189}
12190
12191/*
12192 * "indent()" function
12193 */
12194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012196 typval_T *argvars;
12197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 linenr_T lnum;
12200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206}
12207
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012208/*
12209 * "index()" function
12210 */
12211 static void
12212f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012213 typval_T *argvars;
12214 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012215{
Bram Moolenaar33570922005-01-25 22:26:29 +000012216 list_T *l;
12217 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012218 long idx = 0;
12219 int ic = FALSE;
12220
12221 rettv->vval.v_number = -1;
12222 if (argvars[0].v_type != VAR_LIST)
12223 {
12224 EMSG(_(e_listreq));
12225 return;
12226 }
12227 l = argvars[0].vval.v_list;
12228 if (l != NULL)
12229 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012230 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012231 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012233 int error = FALSE;
12234
Bram Moolenaar758711c2005-02-02 23:11:38 +000012235 /* Start at specified item. Use the cached index that list_find()
12236 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012238 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012239 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012240 ic = get_tv_number_chk(&argvars[3], &error);
12241 if (error)
12242 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012243 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012244
Bram Moolenaar758711c2005-02-02 23:11:38 +000012245 for ( ; item != NULL; item = item->li_next, ++idx)
12246 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012247 {
12248 rettv->vval.v_number = idx;
12249 break;
12250 }
12251 }
12252}
12253
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254static int inputsecret_flag = 0;
12255
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012256static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12257
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012259 * This function is used by f_input() and f_inputdialog() functions. The third
12260 * argument to f_input() specifies the type of completion to use at the
12261 * prompt. The third argument to f_inputdialog() specifies the value to return
12262 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 */
12264 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012265get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012266 typval_T *argvars;
12267 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012268 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012270 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 char_u *p = NULL;
12272 int c;
12273 char_u buf[NUMBUFLEN];
12274 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012276 int xp_type = EXPAND_NOTHING;
12277 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012280 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281
12282#ifdef NO_CONSOLE_INPUT
12283 /* While starting up, there is no place to enter text. */
12284 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286#endif
12287
12288 cmd_silent = FALSE; /* Want to see the prompt. */
12289 if (prompt != NULL)
12290 {
12291 /* Only the part of the message after the last NL is considered as
12292 * prompt for the command line */
12293 p = vim_strrchr(prompt, '\n');
12294 if (p == NULL)
12295 p = prompt;
12296 else
12297 {
12298 ++p;
12299 c = *p;
12300 *p = NUL;
12301 msg_start();
12302 msg_clr_eos();
12303 msg_puts_attr(prompt, echo_attr);
12304 msg_didout = FALSE;
12305 msg_starthere();
12306 *p = c;
12307 }
12308 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012310 if (argvars[1].v_type != VAR_UNKNOWN)
12311 {
12312 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12313 if (defstr != NULL)
12314 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012316 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012317 {
12318 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012319 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012320 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012321
Bram Moolenaar4463f292005-09-25 22:20:24 +000012322 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012323
Bram Moolenaar4463f292005-09-25 22:20:24 +000012324 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12325 if (xp_name == NULL)
12326 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012327
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012328 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012329
Bram Moolenaar4463f292005-09-25 22:20:24 +000012330 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12331 &xp_arg) == FAIL)
12332 return;
12333 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012334 }
12335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012336 if (defstr != NULL)
12337 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012338 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12339 xp_type, xp_arg);
12340
12341 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012343 /* since the user typed this, no need to wait for return */
12344 need_wait_return = FALSE;
12345 msg_didout = FALSE;
12346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 cmd_silent = cmd_silent_save;
12348}
12349
12350/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012351 * "input()" function
12352 * Also handles inputsecret() when inputsecret is set.
12353 */
12354 static void
12355f_input(argvars, rettv)
12356 typval_T *argvars;
12357 typval_T *rettv;
12358{
12359 get_user_input(argvars, rettv, FALSE);
12360}
12361
12362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 * "inputdialog()" function
12364 */
12365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012367 typval_T *argvars;
12368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369{
12370#if defined(FEAT_GUI_TEXTDIALOG)
12371 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12372 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12373 {
12374 char_u *message;
12375 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012376 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012378 message = get_tv_string_chk(&argvars[0]);
12379 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012380 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012381 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 else
12383 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 if (message != NULL && defstr != NULL
12385 && do_dialog(VIM_QUESTION, NULL, message,
12386 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 else
12389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 if (message != NULL && defstr != NULL
12391 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012392 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 rettv->vval.v_string = vim_strsave(
12394 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 }
12400 else
12401#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012402 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403}
12404
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012405/*
12406 * "inputlist()" function
12407 */
12408 static void
12409f_inputlist(argvars, rettv)
12410 typval_T *argvars;
12411 typval_T *rettv;
12412{
12413 listitem_T *li;
12414 int selected;
12415 int mouse_used;
12416
12417 rettv->vval.v_number = 0;
12418#ifdef NO_CONSOLE_INPUT
12419 /* While starting up, there is no place to enter text. */
12420 if (no_console_input())
12421 return;
12422#endif
12423 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12424 {
12425 EMSG2(_(e_listarg), "inputlist()");
12426 return;
12427 }
12428
12429 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012430 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012431 lines_left = Rows; /* avoid more prompt */
12432 msg_scroll = TRUE;
12433 msg_clr_eos();
12434
12435 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12436 {
12437 msg_puts(get_tv_string(&li->li_tv));
12438 msg_putchar('\n');
12439 }
12440
12441 /* Ask for choice. */
12442 selected = prompt_for_number(&mouse_used);
12443 if (mouse_used)
12444 selected -= lines_left;
12445
12446 rettv->vval.v_number = selected;
12447}
12448
12449
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12451
12452/*
12453 * "inputrestore()" function
12454 */
12455/*ARGSUSED*/
12456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012458 typval_T *argvars;
12459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460{
12461 if (ga_userinput.ga_len > 0)
12462 {
12463 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12465 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 }
12468 else if (p_verbose > 1)
12469 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012470 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 }
12473}
12474
12475/*
12476 * "inputsave()" function
12477 */
12478/*ARGSUSED*/
12479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012481 typval_T *argvars;
12482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012484 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 if (ga_grow(&ga_userinput, 1) == OK)
12486 {
12487 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12488 + ga_userinput.ga_len);
12489 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 }
12492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494}
12495
12496/*
12497 * "inputsecret()" function
12498 */
12499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012501 typval_T *argvars;
12502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
12504 ++cmdline_star;
12505 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 --cmdline_star;
12508 --inputsecret_flag;
12509}
12510
12511/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012512 * "insert()" function
12513 */
12514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012516 typval_T *argvars;
12517 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012518{
12519 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012520 listitem_T *item;
12521 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012523
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012524 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012525 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012527 else if ((l = argvars[0].vval.v_list) != NULL
12528 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012529 {
12530 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012531 before = get_tv_number_chk(&argvars[2], &error);
12532 if (error)
12533 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012534
Bram Moolenaar758711c2005-02-02 23:11:38 +000012535 if (before == l->lv_len)
12536 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012537 else
12538 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012539 item = list_find(l, before);
12540 if (item == NULL)
12541 {
12542 EMSGN(_(e_listidx), before);
12543 l = NULL;
12544 }
12545 }
12546 if (l != NULL)
12547 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012548 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012549 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012550 }
12551 }
12552}
12553
12554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 * "isdirectory()" function
12556 */
12557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012559 typval_T *argvars;
12560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563}
12564
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012565/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012566 * "islocked()" function
12567 */
12568 static void
12569f_islocked(argvars, rettv)
12570 typval_T *argvars;
12571 typval_T *rettv;
12572{
12573 lval_T lv;
12574 char_u *end;
12575 dictitem_T *di;
12576
12577 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012578 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12579 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012580 if (end != NULL && lv.ll_name != NULL)
12581 {
12582 if (*end != NUL)
12583 EMSG(_(e_trailing));
12584 else
12585 {
12586 if (lv.ll_tv == NULL)
12587 {
12588 if (check_changedtick(lv.ll_name))
12589 rettv->vval.v_number = 1; /* always locked */
12590 else
12591 {
12592 di = find_var(lv.ll_name, NULL);
12593 if (di != NULL)
12594 {
12595 /* Consider a variable locked when:
12596 * 1. the variable itself is locked
12597 * 2. the value of the variable is locked.
12598 * 3. the List or Dict value is locked.
12599 */
12600 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12601 || tv_islocked(&di->di_tv));
12602 }
12603 }
12604 }
12605 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012606 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012607 else if (lv.ll_newkey != NULL)
12608 EMSG2(_(e_dictkey), lv.ll_newkey);
12609 else if (lv.ll_list != NULL)
12610 /* List item. */
12611 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12612 else
12613 /* Dictionary item. */
12614 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12615 }
12616 }
12617
12618 clear_lval(&lv);
12619}
12620
Bram Moolenaar33570922005-01-25 22:26:29 +000012621static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012622
12623/*
12624 * Turn a dict into a list:
12625 * "what" == 0: list of keys
12626 * "what" == 1: list of values
12627 * "what" == 2: list of items
12628 */
12629 static void
12630dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012631 typval_T *argvars;
12632 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012633 int what;
12634{
Bram Moolenaar33570922005-01-25 22:26:29 +000012635 list_T *l2;
12636 dictitem_T *di;
12637 hashitem_T *hi;
12638 listitem_T *li;
12639 listitem_T *li2;
12640 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012641 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012642
12643 rettv->vval.v_number = 0;
12644 if (argvars[0].v_type != VAR_DICT)
12645 {
12646 EMSG(_(e_dictreq));
12647 return;
12648 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012649 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012650 return;
12651
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012652 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012653 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012654
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012655 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012656 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012658 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012659 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012660 --todo;
12661 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012662
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012663 li = listitem_alloc();
12664 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012665 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012666 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012667
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012668 if (what == 0)
12669 {
12670 /* keys() */
12671 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012672 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012673 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12674 }
12675 else if (what == 1)
12676 {
12677 /* values() */
12678 copy_tv(&di->di_tv, &li->li_tv);
12679 }
12680 else
12681 {
12682 /* items() */
12683 l2 = list_alloc();
12684 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012685 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012686 li->li_tv.vval.v_list = l2;
12687 if (l2 == NULL)
12688 break;
12689 ++l2->lv_refcount;
12690
12691 li2 = listitem_alloc();
12692 if (li2 == NULL)
12693 break;
12694 list_append(l2, li2);
12695 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012696 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012697 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12698
12699 li2 = listitem_alloc();
12700 if (li2 == NULL)
12701 break;
12702 list_append(l2, li2);
12703 copy_tv(&di->di_tv, &li2->li_tv);
12704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012705 }
12706 }
12707}
12708
12709/*
12710 * "items(dict)" function
12711 */
12712 static void
12713f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012714 typval_T *argvars;
12715 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012716{
12717 dict_list(argvars, rettv, 2);
12718}
12719
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012721 * "join()" function
12722 */
12723 static void
12724f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 typval_T *argvars;
12726 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012727{
12728 garray_T ga;
12729 char_u *sep;
12730
12731 rettv->vval.v_number = 0;
12732 if (argvars[0].v_type != VAR_LIST)
12733 {
12734 EMSG(_(e_listreq));
12735 return;
12736 }
12737 if (argvars[0].vval.v_list == NULL)
12738 return;
12739 if (argvars[1].v_type == VAR_UNKNOWN)
12740 sep = (char_u *)" ";
12741 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012743
12744 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745
12746 if (sep != NULL)
12747 {
12748 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012749 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 ga_append(&ga, NUL);
12751 rettv->vval.v_string = (char_u *)ga.ga_data;
12752 }
12753 else
12754 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012755}
12756
12757/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012758 * "keys()" function
12759 */
12760 static void
12761f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *argvars;
12763 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012764{
12765 dict_list(argvars, rettv, 0);
12766}
12767
12768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 * "last_buffer_nr()" function.
12770 */
12771/*ARGSUSED*/
12772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012774 typval_T *argvars;
12775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776{
12777 int n = 0;
12778 buf_T *buf;
12779
12780 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12781 if (n < buf->b_fnum)
12782 n = buf->b_fnum;
12783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012785}
12786
12787/*
12788 * "len()" function
12789 */
12790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012794{
12795 switch (argvars[0].v_type)
12796 {
12797 case VAR_STRING:
12798 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->vval.v_number = (varnumber_T)STRLEN(
12800 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012801 break;
12802 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012804 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012805 case VAR_DICT:
12806 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12807 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012808 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012809 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810 break;
12811 }
12812}
12813
Bram Moolenaar33570922005-01-25 22:26:29 +000012814static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012815
12816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012818 typval_T *argvars;
12819 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012820 int type;
12821{
12822#ifdef FEAT_LIBCALL
12823 char_u *string_in;
12824 char_u **string_result;
12825 int nr_result;
12826#endif
12827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012829 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012831 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012833
12834 if (check_restricted() || check_secure())
12835 return;
12836
12837#ifdef FEAT_LIBCALL
12838 /* The first two args must be strings, otherwise its meaningless */
12839 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12840 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012841 string_in = NULL;
12842 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012843 string_in = argvars[2].vval.v_string;
12844 if (type == VAR_NUMBER)
12845 string_result = NULL;
12846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012848 if (mch_libcall(argvars[0].vval.v_string,
12849 argvars[1].vval.v_string,
12850 string_in,
12851 argvars[2].vval.v_number,
12852 string_result,
12853 &nr_result) == OK
12854 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012856 }
12857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858}
12859
12860/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012861 * "libcall()" function
12862 */
12863 static void
12864f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 typval_T *argvars;
12866 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867{
12868 libcall_common(argvars, rettv, VAR_STRING);
12869}
12870
12871/*
12872 * "libcallnr()" function
12873 */
12874 static void
12875f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012878{
12879 libcall_common(argvars, rettv, VAR_NUMBER);
12880}
12881
12882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 * "line(string)" function
12884 */
12885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889{
12890 linenr_T lnum = 0;
12891 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012892 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012894 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 if (fp != NULL)
12896 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
12901 * "line2byte(lnum)" function
12902 */
12903/*ARGSUSED*/
12904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 typval_T *argvars;
12907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
12909#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911#else
12912 linenr_T lnum;
12913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12919 if (rettv->vval.v_number >= 0)
12920 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921#endif
12922}
12923
12924/*
12925 * "lispindent(lnum)" function
12926 */
12927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012929 typval_T *argvars;
12930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931{
12932#ifdef FEAT_LISP
12933 pos_T pos;
12934 linenr_T lnum;
12935
12936 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12939 {
12940 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942 curwin->w_cursor = pos;
12943 }
12944 else
12945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947}
12948
12949/*
12950 * "localtime()" function
12951 */
12952/*ARGSUSED*/
12953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012955 typval_T *argvars;
12956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012958 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959}
12960
Bram Moolenaar33570922005-01-25 22:26:29 +000012961static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962
12963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012965 typval_T *argvars;
12966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 int exact;
12968{
12969 char_u *keys;
12970 char_u *which;
12971 char_u buf[NUMBUFLEN];
12972 char_u *keys_buf = NULL;
12973 char_u *rhs;
12974 int mode;
12975 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012976 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977
12978 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->v_type = VAR_STRING;
12980 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 if (*keys == NUL)
12984 return;
12985
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012986 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012988 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012989 if (argvars[2].v_type != VAR_UNKNOWN)
12990 abbr = get_tv_number(&argvars[2]);
12991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 else
12993 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012994 if (which == NULL)
12995 return;
12996
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 mode = get_map_mode(&which, 0);
12998
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012999 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013000 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001 vim_free(keys_buf);
13002 if (rhs != NULL)
13003 {
13004 ga_init(&ga);
13005 ga.ga_itemsize = 1;
13006 ga.ga_growsize = 40;
13007
13008 while (*rhs != NUL)
13009 ga_concat(&ga, str2special(&rhs, FALSE));
13010
13011 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 }
13014}
13015
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013016#ifdef FEAT_FLOAT
13017/*
13018 * "log10()" function
13019 */
13020 static void
13021f_log10(argvars, rettv)
13022 typval_T *argvars;
13023 typval_T *rettv;
13024{
13025 float_T f;
13026
13027 rettv->v_type = VAR_FLOAT;
13028 if (get_float_arg(argvars, &f) == OK)
13029 rettv->vval.v_float = log10(f);
13030 else
13031 rettv->vval.v_float = 0.0;
13032}
13033#endif
13034
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013036 * "map()" function
13037 */
13038 static void
13039f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 typval_T *argvars;
13041 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013042{
13043 filter_map(argvars, rettv, TRUE);
13044}
13045
13046/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 */
13049 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013050f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013054 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055}
13056
13057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013058 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 */
13060 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013061f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *argvars;
13063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013065 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066}
13067
Bram Moolenaar33570922005-01-25 22:26:29 +000013068static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069
13070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013072 typval_T *argvars;
13073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 int type;
13075{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013076 char_u *str = NULL;
13077 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 char_u *pat;
13079 regmatch_T regmatch;
13080 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013081 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 char_u *save_cpo;
13083 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013084 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013085 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013086 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 list_T *l = NULL;
13088 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013089 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013090 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091
13092 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13093 save_cpo = p_cpo;
13094 p_cpo = (char_u *)"";
13095
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013096 rettv->vval.v_number = -1;
13097 if (type == 3)
13098 {
13099 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013100 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013101 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013102 }
13103 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->v_type = VAR_STRING;
13106 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013109 if (argvars[0].v_type == VAR_LIST)
13110 {
13111 if ((l = argvars[0].vval.v_list) == NULL)
13112 goto theend;
13113 li = l->lv_first;
13114 }
13115 else
13116 expr = str = get_tv_string(&argvars[0]);
13117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13119 if (pat == NULL)
13120 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013121
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013122 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013124 int error = FALSE;
13125
13126 start = get_tv_number_chk(&argvars[2], &error);
13127 if (error)
13128 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013129 if (l != NULL)
13130 {
13131 li = list_find(l, start);
13132 if (li == NULL)
13133 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013134 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013135 }
13136 else
13137 {
13138 if (start < 0)
13139 start = 0;
13140 if (start > (long)STRLEN(str))
13141 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013142 /* When "count" argument is there ignore matches before "start",
13143 * otherwise skip part of the string. Differs when pattern is "^"
13144 * or "\<". */
13145 if (argvars[3].v_type != VAR_UNKNOWN)
13146 startcol = start;
13147 else
13148 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013149 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013150
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013151 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 nth = get_tv_number_chk(&argvars[3], &error);
13153 if (error)
13154 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 }
13156
13157 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13158 if (regmatch.regprog != NULL)
13159 {
13160 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013161
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013162 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013163 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013164 if (l != NULL)
13165 {
13166 if (li == NULL)
13167 {
13168 match = FALSE;
13169 break;
13170 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013171 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013172 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013173 if (str == NULL)
13174 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013175 }
13176
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013177 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013178
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013179 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013180 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013181 if (l == NULL && !match)
13182 break;
13183
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013184 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013185 if (l != NULL)
13186 {
13187 li = li->li_next;
13188 ++idx;
13189 }
13190 else
13191 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013192#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013193 startcol = (colnr_T)(regmatch.startp[0]
13194 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013195#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013196 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013197#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013198 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013199 }
13200
13201 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013203 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013205 int i;
13206
13207 /* return list with matched string and submatches */
13208 for (i = 0; i < NSUBEXP; ++i)
13209 {
13210 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013211 {
13212 if (list_append_string(rettv->vval.v_list,
13213 (char_u *)"", 0) == FAIL)
13214 break;
13215 }
13216 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013217 regmatch.startp[i],
13218 (int)(regmatch.endp[i] - regmatch.startp[i]))
13219 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013220 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013221 }
13222 }
13223 else if (type == 2)
13224 {
13225 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013226 if (l != NULL)
13227 copy_tv(&li->li_tv, rettv);
13228 else
13229 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013231 }
13232 else if (l != NULL)
13233 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 else
13235 {
13236 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 (varnumber_T)(regmatch.startp[0] - str);
13239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013242 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 }
13244 }
13245 vim_free(regmatch.regprog);
13246 }
13247
13248theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013249 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 p_cpo = save_cpo;
13251}
13252
13253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013254 * "match()" function
13255 */
13256 static void
13257f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *argvars;
13259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260{
13261 find_some_match(argvars, rettv, 1);
13262}
13263
13264/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013265 * "matchadd()" function
13266 */
13267 static void
13268f_matchadd(argvars, rettv)
13269 typval_T *argvars;
13270 typval_T *rettv;
13271{
13272#ifdef FEAT_SEARCH_EXTRA
13273 char_u buf[NUMBUFLEN];
13274 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13275 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13276 int prio = 10; /* default priority */
13277 int id = -1;
13278 int error = FALSE;
13279
13280 rettv->vval.v_number = -1;
13281
13282 if (grp == NULL || pat == NULL)
13283 return;
13284 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013285 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013286 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013287 if (argvars[3].v_type != VAR_UNKNOWN)
13288 id = get_tv_number_chk(&argvars[3], &error);
13289 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013290 if (error == TRUE)
13291 return;
13292 if (id >= 1 && id <= 3)
13293 {
13294 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13295 return;
13296 }
13297
13298 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13299#endif
13300}
13301
13302/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013303 * "matcharg()" function
13304 */
13305 static void
13306f_matcharg(argvars, rettv)
13307 typval_T *argvars;
13308 typval_T *rettv;
13309{
13310 if (rettv_list_alloc(rettv) == OK)
13311 {
13312#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013313 int id = get_tv_number(&argvars[0]);
13314 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013315
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013316 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013317 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013318 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13319 {
13320 list_append_string(rettv->vval.v_list,
13321 syn_id2name(m->hlg_id), -1);
13322 list_append_string(rettv->vval.v_list, m->pattern, -1);
13323 }
13324 else
13325 {
13326 list_append_string(rettv->vval.v_list, NUL, -1);
13327 list_append_string(rettv->vval.v_list, NUL, -1);
13328 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013329 }
13330#endif
13331 }
13332}
13333
13334/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013335 * "matchdelete()" function
13336 */
13337 static void
13338f_matchdelete(argvars, rettv)
13339 typval_T *argvars;
13340 typval_T *rettv;
13341{
13342#ifdef FEAT_SEARCH_EXTRA
13343 rettv->vval.v_number = match_delete(curwin,
13344 (int)get_tv_number(&argvars[0]), TRUE);
13345#endif
13346}
13347
13348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349 * "matchend()" function
13350 */
13351 static void
13352f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355{
13356 find_some_match(argvars, rettv, 0);
13357}
13358
13359/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013360 * "matchlist()" function
13361 */
13362 static void
13363f_matchlist(argvars, rettv)
13364 typval_T *argvars;
13365 typval_T *rettv;
13366{
13367 find_some_match(argvars, rettv, 3);
13368}
13369
13370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013371 * "matchstr()" function
13372 */
13373 static void
13374f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013375 typval_T *argvars;
13376 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377{
13378 find_some_match(argvars, rettv, 2);
13379}
13380
Bram Moolenaar33570922005-01-25 22:26:29 +000013381static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013382
13383 static void
13384max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013385 typval_T *argvars;
13386 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013387 int domax;
13388{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013389 long n = 0;
13390 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013392
13393 if (argvars[0].v_type == VAR_LIST)
13394 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013395 list_T *l;
13396 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013397
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013398 l = argvars[0].vval.v_list;
13399 if (l != NULL)
13400 {
13401 li = l->lv_first;
13402 if (li != NULL)
13403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013404 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013405 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013406 {
13407 li = li->li_next;
13408 if (li == NULL)
13409 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013411 if (domax ? i > n : i < n)
13412 n = i;
13413 }
13414 }
13415 }
13416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013417 else if (argvars[0].v_type == VAR_DICT)
13418 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013420 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013422 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013423
13424 d = argvars[0].vval.v_dict;
13425 if (d != NULL)
13426 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013427 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013430 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013432 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013434 if (first)
13435 {
13436 n = i;
13437 first = FALSE;
13438 }
13439 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013440 n = i;
13441 }
13442 }
13443 }
13444 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013445 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013446 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013448}
13449
13450/*
13451 * "max()" function
13452 */
13453 static void
13454f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 typval_T *argvars;
13456 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013457{
13458 max_min(argvars, rettv, TRUE);
13459}
13460
13461/*
13462 * "min()" function
13463 */
13464 static void
13465f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013466 typval_T *argvars;
13467 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013468{
13469 max_min(argvars, rettv, FALSE);
13470}
13471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013472static int mkdir_recurse __ARGS((char_u *dir, int prot));
13473
13474/*
13475 * Create the directory in which "dir" is located, and higher levels when
13476 * needed.
13477 */
13478 static int
13479mkdir_recurse(dir, prot)
13480 char_u *dir;
13481 int prot;
13482{
13483 char_u *p;
13484 char_u *updir;
13485 int r = FAIL;
13486
13487 /* Get end of directory name in "dir".
13488 * We're done when it's "/" or "c:/". */
13489 p = gettail_sep(dir);
13490 if (p <= get_past_head(dir))
13491 return OK;
13492
13493 /* If the directory exists we're done. Otherwise: create it.*/
13494 updir = vim_strnsave(dir, (int)(p - dir));
13495 if (updir == NULL)
13496 return FAIL;
13497 if (mch_isdir(updir))
13498 r = OK;
13499 else if (mkdir_recurse(updir, prot) == OK)
13500 r = vim_mkdir_emsg(updir, prot);
13501 vim_free(updir);
13502 return r;
13503}
13504
13505#ifdef vim_mkdir
13506/*
13507 * "mkdir()" function
13508 */
13509 static void
13510f_mkdir(argvars, rettv)
13511 typval_T *argvars;
13512 typval_T *rettv;
13513{
13514 char_u *dir;
13515 char_u buf[NUMBUFLEN];
13516 int prot = 0755;
13517
13518 rettv->vval.v_number = FAIL;
13519 if (check_restricted() || check_secure())
13520 return;
13521
13522 dir = get_tv_string_buf(&argvars[0], buf);
13523 if (argvars[1].v_type != VAR_UNKNOWN)
13524 {
13525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 prot = get_tv_number_chk(&argvars[2], NULL);
13527 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013528 mkdir_recurse(dir, prot);
13529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013531}
13532#endif
13533
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 * "mode()" function
13536 */
13537/*ARGSUSED*/
13538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 typval_T *argvars;
13541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013543 char_u buf[3];
13544
13545 buf[1] = NUL;
13546 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
13548#ifdef FEAT_VISUAL
13549 if (VIsual_active)
13550 {
13551 if (VIsual_select)
13552 buf[0] = VIsual_mode + 's' - 'v';
13553 else
13554 buf[0] = VIsual_mode;
13555 }
13556 else
13557#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013558 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13559 || State == CONFIRM)
13560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013562 if (State == ASKMORE)
13563 buf[1] = 'm';
13564 else if (State == CONFIRM)
13565 buf[1] = '?';
13566 }
13567 else if (State == EXTERNCMD)
13568 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 else if (State & INSERT)
13570 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013571#ifdef FEAT_VREPLACE
13572 if (State & VREPLACE_FLAG)
13573 {
13574 buf[0] = 'R';
13575 buf[1] = 'v';
13576 }
13577 else
13578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 if (State & REPLACE_FLAG)
13580 buf[0] = 'R';
13581 else
13582 buf[0] = 'i';
13583 }
13584 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013587 if (exmode_active)
13588 buf[1] = 'v';
13589 }
13590 else if (exmode_active)
13591 {
13592 buf[0] = 'c';
13593 buf[1] = 'e';
13594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013598 if (finish_op)
13599 buf[1] = 'o';
13600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013602 /* Clear out the minor mode when the argument is not a non-zero number or
13603 * non-empty string. */
13604 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013605 buf[1] = NUL;
13606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_string = vim_strsave(buf);
13608 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609}
13610
13611/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013612 * "nextnonblank()" function
13613 */
13614 static void
13615f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *argvars;
13617 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013618{
13619 linenr_T lnum;
13620
13621 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013624 {
13625 lnum = 0;
13626 break;
13627 }
13628 if (*skipwhite(ml_get(lnum)) != NUL)
13629 break;
13630 }
13631 rettv->vval.v_number = lnum;
13632}
13633
13634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 * "nr2char()" function
13636 */
13637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013638f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013639 typval_T *argvars;
13640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641{
13642 char_u buf[NUMBUFLEN];
13643
13644#ifdef FEAT_MBYTE
13645 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 else
13648#endif
13649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 buf[1] = NUL;
13652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->v_type = VAR_STRING;
13654 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013655}
13656
13657/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013658 * "pathshorten()" function
13659 */
13660 static void
13661f_pathshorten(argvars, rettv)
13662 typval_T *argvars;
13663 typval_T *rettv;
13664{
13665 char_u *p;
13666
13667 rettv->v_type = VAR_STRING;
13668 p = get_tv_string_chk(&argvars[0]);
13669 if (p == NULL)
13670 rettv->vval.v_string = NULL;
13671 else
13672 {
13673 p = vim_strsave(p);
13674 rettv->vval.v_string = p;
13675 if (p != NULL)
13676 shorten_dir(p);
13677 }
13678}
13679
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013680#ifdef FEAT_FLOAT
13681/*
13682 * "pow()" function
13683 */
13684 static void
13685f_pow(argvars, rettv)
13686 typval_T *argvars;
13687 typval_T *rettv;
13688{
13689 float_T fx, fy;
13690
13691 rettv->v_type = VAR_FLOAT;
13692 if (get_float_arg(argvars, &fx) == OK
13693 && get_float_arg(&argvars[1], &fy) == OK)
13694 rettv->vval.v_float = pow(fx, fy);
13695 else
13696 rettv->vval.v_float = 0.0;
13697}
13698#endif
13699
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 * "prevnonblank()" function
13702 */
13703 static void
13704f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *argvars;
13706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013707{
13708 linenr_T lnum;
13709
13710 lnum = get_tv_lnum(argvars);
13711 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13712 lnum = 0;
13713 else
13714 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13715 --lnum;
13716 rettv->vval.v_number = lnum;
13717}
13718
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013719#ifdef HAVE_STDARG_H
13720/* This dummy va_list is here because:
13721 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13722 * - locally in the function results in a "used before set" warning
13723 * - using va_start() to initialize it gives "function with fixed args" error */
13724static va_list ap;
13725#endif
13726
Bram Moolenaar8c711452005-01-14 21:53:12 +000013727/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013728 * "printf()" function
13729 */
13730 static void
13731f_printf(argvars, rettv)
13732 typval_T *argvars;
13733 typval_T *rettv;
13734{
13735 rettv->v_type = VAR_STRING;
13736 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013737#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013738 {
13739 char_u buf[NUMBUFLEN];
13740 int len;
13741 char_u *s;
13742 int saved_did_emsg = did_emsg;
13743 char *fmt;
13744
13745 /* Get the required length, allocate the buffer and do it for real. */
13746 did_emsg = FALSE;
13747 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013748 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013749 if (!did_emsg)
13750 {
13751 s = alloc(len + 1);
13752 if (s != NULL)
13753 {
13754 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013755 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013756 }
13757 }
13758 did_emsg |= saved_did_emsg;
13759 }
13760#endif
13761}
13762
13763/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013764 * "pumvisible()" function
13765 */
13766/*ARGSUSED*/
13767 static void
13768f_pumvisible(argvars, rettv)
13769 typval_T *argvars;
13770 typval_T *rettv;
13771{
13772 rettv->vval.v_number = 0;
13773#ifdef FEAT_INS_EXPAND
13774 if (pum_visible())
13775 rettv->vval.v_number = 1;
13776#endif
13777}
13778
13779/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013780 * "range()" function
13781 */
13782 static void
13783f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *argvars;
13785 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013786{
13787 long start;
13788 long end;
13789 long stride = 1;
13790 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013791 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013794 if (argvars[1].v_type == VAR_UNKNOWN)
13795 {
13796 end = start - 1;
13797 start = 0;
13798 }
13799 else
13800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013804 }
13805
13806 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013807 if (error)
13808 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013809 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013810 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013811 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013812 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013813 else
13814 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013815 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013816 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013817 if (list_append_number(rettv->vval.v_list,
13818 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013819 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013820 }
13821}
13822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013823/*
13824 * "readfile()" function
13825 */
13826 static void
13827f_readfile(argvars, rettv)
13828 typval_T *argvars;
13829 typval_T *rettv;
13830{
13831 int binary = FALSE;
13832 char_u *fname;
13833 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013834 listitem_T *li;
13835#define FREAD_SIZE 200 /* optimized for text lines */
13836 char_u buf[FREAD_SIZE];
13837 int readlen; /* size of last fread() */
13838 int buflen; /* nr of valid chars in buf[] */
13839 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13840 int tolist; /* first byte in buf[] still to be put in list */
13841 int chop; /* how many CR to chop off */
13842 char_u *prev = NULL; /* previously read bytes, if any */
13843 int prevlen = 0; /* length of "prev" if not NULL */
13844 char_u *s;
13845 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013846 long maxline = MAXLNUM;
13847 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013848
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013849 if (argvars[1].v_type != VAR_UNKNOWN)
13850 {
13851 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13852 binary = TRUE;
13853 if (argvars[2].v_type != VAR_UNKNOWN)
13854 maxline = get_tv_number(&argvars[2]);
13855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013856
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013857 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013858 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013859
13860 /* Always open the file in binary mode, library functions have a mind of
13861 * their own about CR-LF conversion. */
13862 fname = get_tv_string(&argvars[0]);
13863 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13864 {
13865 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13866 return;
13867 }
13868
13869 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013870 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013871 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013872 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013873 buflen = filtd + readlen;
13874 tolist = 0;
13875 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13876 {
13877 if (buf[filtd] == '\n' || readlen <= 0)
13878 {
13879 /* Only when in binary mode add an empty list item when the
13880 * last line ends in a '\n'. */
13881 if (!binary && readlen == 0 && filtd == 0)
13882 break;
13883
13884 /* Found end-of-line or end-of-file: add a text line to the
13885 * list. */
13886 chop = 0;
13887 if (!binary)
13888 while (filtd - chop - 1 >= tolist
13889 && buf[filtd - chop - 1] == '\r')
13890 ++chop;
13891 len = filtd - tolist - chop;
13892 if (prev == NULL)
13893 s = vim_strnsave(buf + tolist, len);
13894 else
13895 {
13896 s = alloc((unsigned)(prevlen + len + 1));
13897 if (s != NULL)
13898 {
13899 mch_memmove(s, prev, prevlen);
13900 vim_free(prev);
13901 prev = NULL;
13902 mch_memmove(s + prevlen, buf + tolist, len);
13903 s[prevlen + len] = NUL;
13904 }
13905 }
13906 tolist = filtd + 1;
13907
13908 li = listitem_alloc();
13909 if (li == NULL)
13910 {
13911 vim_free(s);
13912 break;
13913 }
13914 li->li_tv.v_type = VAR_STRING;
13915 li->li_tv.v_lock = 0;
13916 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013917 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013918
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013919 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013920 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013921 if (readlen <= 0)
13922 break;
13923 }
13924 else if (buf[filtd] == NUL)
13925 buf[filtd] = '\n';
13926 }
13927 if (readlen <= 0)
13928 break;
13929
13930 if (tolist == 0)
13931 {
13932 /* "buf" is full, need to move text to an allocated buffer */
13933 if (prev == NULL)
13934 {
13935 prev = vim_strnsave(buf, buflen);
13936 prevlen = buflen;
13937 }
13938 else
13939 {
13940 s = alloc((unsigned)(prevlen + buflen));
13941 if (s != NULL)
13942 {
13943 mch_memmove(s, prev, prevlen);
13944 mch_memmove(s + prevlen, buf, buflen);
13945 vim_free(prev);
13946 prev = s;
13947 prevlen += buflen;
13948 }
13949 }
13950 filtd = 0;
13951 }
13952 else
13953 {
13954 mch_memmove(buf, buf + tolist, buflen - tolist);
13955 filtd -= tolist;
13956 }
13957 }
13958
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013959 /*
13960 * For a negative line count use only the lines at the end of the file,
13961 * free the rest.
13962 */
13963 if (maxline < 0)
13964 while (cnt > -maxline)
13965 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013966 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013967 --cnt;
13968 }
13969
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013970 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013971 fclose(fd);
13972}
13973
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013974#if defined(FEAT_RELTIME)
13975static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13976
13977/*
13978 * Convert a List to proftime_T.
13979 * Return FAIL when there is something wrong.
13980 */
13981 static int
13982list2proftime(arg, tm)
13983 typval_T *arg;
13984 proftime_T *tm;
13985{
13986 long n1, n2;
13987 int error = FALSE;
13988
13989 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13990 || arg->vval.v_list->lv_len != 2)
13991 return FAIL;
13992 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13993 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13994# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013995 tm->HighPart = n1;
13996 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013997# else
13998 tm->tv_sec = n1;
13999 tm->tv_usec = n2;
14000# endif
14001 return error ? FAIL : OK;
14002}
14003#endif /* FEAT_RELTIME */
14004
14005/*
14006 * "reltime()" function
14007 */
14008 static void
14009f_reltime(argvars, rettv)
14010 typval_T *argvars;
14011 typval_T *rettv;
14012{
14013#ifdef FEAT_RELTIME
14014 proftime_T res;
14015 proftime_T start;
14016
14017 if (argvars[0].v_type == VAR_UNKNOWN)
14018 {
14019 /* No arguments: get current time. */
14020 profile_start(&res);
14021 }
14022 else if (argvars[1].v_type == VAR_UNKNOWN)
14023 {
14024 if (list2proftime(&argvars[0], &res) == FAIL)
14025 return;
14026 profile_end(&res);
14027 }
14028 else
14029 {
14030 /* Two arguments: compute the difference. */
14031 if (list2proftime(&argvars[0], &start) == FAIL
14032 || list2proftime(&argvars[1], &res) == FAIL)
14033 return;
14034 profile_sub(&res, &start);
14035 }
14036
14037 if (rettv_list_alloc(rettv) == OK)
14038 {
14039 long n1, n2;
14040
14041# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014042 n1 = res.HighPart;
14043 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014044# else
14045 n1 = res.tv_sec;
14046 n2 = res.tv_usec;
14047# endif
14048 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14049 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14050 }
14051#endif
14052}
14053
14054/*
14055 * "reltimestr()" function
14056 */
14057 static void
14058f_reltimestr(argvars, rettv)
14059 typval_T *argvars;
14060 typval_T *rettv;
14061{
14062#ifdef FEAT_RELTIME
14063 proftime_T tm;
14064#endif
14065
14066 rettv->v_type = VAR_STRING;
14067 rettv->vval.v_string = NULL;
14068#ifdef FEAT_RELTIME
14069 if (list2proftime(&argvars[0], &tm) == OK)
14070 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14071#endif
14072}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014073
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14075static void make_connection __ARGS((void));
14076static int check_connection __ARGS((void));
14077
14078 static void
14079make_connection()
14080{
14081 if (X_DISPLAY == NULL
14082# ifdef FEAT_GUI
14083 && !gui.in_use
14084# endif
14085 )
14086 {
14087 x_force_connect = TRUE;
14088 setup_term_clip();
14089 x_force_connect = FALSE;
14090 }
14091}
14092
14093 static int
14094check_connection()
14095{
14096 make_connection();
14097 if (X_DISPLAY == NULL)
14098 {
14099 EMSG(_("E240: No connection to Vim server"));
14100 return FAIL;
14101 }
14102 return OK;
14103}
14104#endif
14105
14106#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014107static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108
14109 static void
14110remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 typval_T *argvars;
14112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113 int expr;
14114{
14115 char_u *server_name;
14116 char_u *keys;
14117 char_u *r = NULL;
14118 char_u buf[NUMBUFLEN];
14119# ifdef WIN32
14120 HWND w;
14121# else
14122 Window w;
14123# endif
14124
14125 if (check_restricted() || check_secure())
14126 return;
14127
14128# ifdef FEAT_X11
14129 if (check_connection() == FAIL)
14130 return;
14131# endif
14132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133 server_name = get_tv_string_chk(&argvars[0]);
14134 if (server_name == NULL)
14135 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136 keys = get_tv_string_buf(&argvars[1], buf);
14137# ifdef WIN32
14138 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14139# else
14140 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14141 < 0)
14142# endif
14143 {
14144 if (r != NULL)
14145 EMSG(r); /* sending worked but evaluation failed */
14146 else
14147 EMSG2(_("E241: Unable to send to %s"), server_name);
14148 return;
14149 }
14150
14151 rettv->vval.v_string = r;
14152
14153 if (argvars[2].v_type != VAR_UNKNOWN)
14154 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014156 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014157 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014159 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 v.di_tv.v_type = VAR_STRING;
14161 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 idvar = get_tv_string_chk(&argvars[2]);
14163 if (idvar != NULL)
14164 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166 }
14167}
14168#endif
14169
14170/*
14171 * "remote_expr()" function
14172 */
14173/*ARGSUSED*/
14174 static void
14175f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014178{
14179 rettv->v_type = VAR_STRING;
14180 rettv->vval.v_string = NULL;
14181#ifdef FEAT_CLIENTSERVER
14182 remote_common(argvars, rettv, TRUE);
14183#endif
14184}
14185
14186/*
14187 * "remote_foreground()" function
14188 */
14189/*ARGSUSED*/
14190 static void
14191f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *argvars;
14193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194{
14195 rettv->vval.v_number = 0;
14196#ifdef FEAT_CLIENTSERVER
14197# ifdef WIN32
14198 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 {
14200 char_u *server_name = get_tv_string_chk(&argvars[0]);
14201
14202 if (server_name != NULL)
14203 serverForeground(server_name);
14204 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014205# else
14206 /* Send a foreground() expression to the server. */
14207 argvars[1].v_type = VAR_STRING;
14208 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14209 argvars[2].v_type = VAR_UNKNOWN;
14210 remote_common(argvars, rettv, TRUE);
14211 vim_free(argvars[1].vval.v_string);
14212# endif
14213#endif
14214}
14215
14216/*ARGSUSED*/
14217 static void
14218f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *argvars;
14220 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014221{
14222#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014223 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 char_u *s = NULL;
14225# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014226 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014229
14230 if (check_restricted() || check_secure())
14231 {
14232 rettv->vval.v_number = -1;
14233 return;
14234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 serverid = get_tv_string_chk(&argvars[0]);
14236 if (serverid == NULL)
14237 {
14238 rettv->vval.v_number = -1;
14239 return; /* type error; errmsg already given */
14240 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014241# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014242 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014243 if (n == 0)
14244 rettv->vval.v_number = -1;
14245 else
14246 {
14247 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14248 rettv->vval.v_number = (s != NULL);
14249 }
14250# else
14251 rettv->vval.v_number = 0;
14252 if (check_connection() == FAIL)
14253 return;
14254
14255 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257# endif
14258
14259 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 char_u *retvar;
14262
Bram Moolenaar33570922005-01-25 22:26:29 +000014263 v.di_tv.v_type = VAR_STRING;
14264 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 retvar = get_tv_string_chk(&argvars[1]);
14266 if (retvar != NULL)
14267 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014268 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014269 }
14270#else
14271 rettv->vval.v_number = -1;
14272#endif
14273}
14274
14275/*ARGSUSED*/
14276 static void
14277f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014278 typval_T *argvars;
14279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014280{
14281 char_u *r = NULL;
14282
14283#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 char_u *serverid = get_tv_string_chk(&argvars[0]);
14285
14286 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014287 {
14288# ifdef WIN32
14289 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014290 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014291
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014292 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293 if (n != 0)
14294 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14295 if (r == NULL)
14296# else
14297 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014299# endif
14300 EMSG(_("E277: Unable to read a server reply"));
14301 }
14302#endif
14303 rettv->v_type = VAR_STRING;
14304 rettv->vval.v_string = r;
14305}
14306
14307/*
14308 * "remote_send()" function
14309 */
14310/*ARGSUSED*/
14311 static void
14312f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014313 typval_T *argvars;
14314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014315{
14316 rettv->v_type = VAR_STRING;
14317 rettv->vval.v_string = NULL;
14318#ifdef FEAT_CLIENTSERVER
14319 remote_common(argvars, rettv, FALSE);
14320#endif
14321}
14322
14323/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014324 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014325 */
14326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330{
Bram Moolenaar33570922005-01-25 22:26:29 +000014331 list_T *l;
14332 listitem_T *item, *item2;
14333 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014334 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014335 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014336 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014337 dict_T *d;
14338 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014339
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014340 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014341 if (argvars[0].v_type == VAR_DICT)
14342 {
14343 if (argvars[2].v_type != VAR_UNKNOWN)
14344 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014345 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014346 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 key = get_tv_string_chk(&argvars[1]);
14349 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 di = dict_find(d, key, -1);
14352 if (di == NULL)
14353 EMSG2(_(e_dictkey), key);
14354 else
14355 {
14356 *rettv = di->di_tv;
14357 init_tv(&di->di_tv);
14358 dictitem_remove(d, di);
14359 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014361 }
14362 }
14363 else if (argvars[0].v_type != VAR_LIST)
14364 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014365 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014366 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368 int error = FALSE;
14369
14370 idx = get_tv_number_chk(&argvars[1], &error);
14371 if (error)
14372 ; /* type error: do nothing, errmsg already given */
14373 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014374 EMSGN(_(e_listidx), idx);
14375 else
14376 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014377 if (argvars[2].v_type == VAR_UNKNOWN)
14378 {
14379 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014380 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014381 *rettv = item->li_tv;
14382 vim_free(item);
14383 }
14384 else
14385 {
14386 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 end = get_tv_number_chk(&argvars[2], &error);
14388 if (error)
14389 ; /* type error: do nothing */
14390 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014391 EMSGN(_(e_listidx), end);
14392 else
14393 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014394 int cnt = 0;
14395
14396 for (li = item; li != NULL; li = li->li_next)
14397 {
14398 ++cnt;
14399 if (li == item2)
14400 break;
14401 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014402 if (li == NULL) /* didn't find "item2" after "item" */
14403 EMSG(_(e_invrange));
14404 else
14405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014406 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014407 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014408 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014409 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014410 l->lv_first = item;
14411 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014412 item->li_prev = NULL;
14413 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014414 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014415 }
14416 }
14417 }
14418 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014419 }
14420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421}
14422
14423/*
14424 * "rename({from}, {to})" function
14425 */
14426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014428 typval_T *argvars;
14429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430{
14431 char_u buf[NUMBUFLEN];
14432
14433 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14437 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438}
14439
14440/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014441 * "repeat()" function
14442 */
14443/*ARGSUSED*/
14444 static void
14445f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014446 typval_T *argvars;
14447 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014448{
14449 char_u *p;
14450 int n;
14451 int slen;
14452 int len;
14453 char_u *r;
14454 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014455
14456 n = get_tv_number(&argvars[1]);
14457 if (argvars[0].v_type == VAR_LIST)
14458 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014459 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014460 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014461 if (list_extend(rettv->vval.v_list,
14462 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014463 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014464 }
14465 else
14466 {
14467 p = get_tv_string(&argvars[0]);
14468 rettv->v_type = VAR_STRING;
14469 rettv->vval.v_string = NULL;
14470
14471 slen = (int)STRLEN(p);
14472 len = slen * n;
14473 if (len <= 0)
14474 return;
14475
14476 r = alloc(len + 1);
14477 if (r != NULL)
14478 {
14479 for (i = 0; i < n; i++)
14480 mch_memmove(r + i * slen, p, (size_t)slen);
14481 r[len] = NUL;
14482 }
14483
14484 rettv->vval.v_string = r;
14485 }
14486}
14487
14488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 * "resolve()" function
14490 */
14491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T *argvars;
14494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
14496 char_u *p;
14497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499#ifdef FEAT_SHORTCUT
14500 {
14501 char_u *v = NULL;
14502
14503 v = mch_resolve_shortcut(p);
14504 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 }
14509#else
14510# ifdef HAVE_READLINK
14511 {
14512 char_u buf[MAXPATHL + 1];
14513 char_u *cpy;
14514 int len;
14515 char_u *remain = NULL;
14516 char_u *q;
14517 int is_relative_to_current = FALSE;
14518 int has_trailing_pathsep = FALSE;
14519 int limit = 100;
14520
14521 p = vim_strsave(p);
14522
14523 if (p[0] == '.' && (vim_ispathsep(p[1])
14524 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14525 is_relative_to_current = TRUE;
14526
14527 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014528 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 has_trailing_pathsep = TRUE;
14530
14531 q = getnextcomp(p);
14532 if (*q != NUL)
14533 {
14534 /* Separate the first path component in "p", and keep the
14535 * remainder (beginning with the path separator). */
14536 remain = vim_strsave(q - 1);
14537 q[-1] = NUL;
14538 }
14539
14540 for (;;)
14541 {
14542 for (;;)
14543 {
14544 len = readlink((char *)p, (char *)buf, MAXPATHL);
14545 if (len <= 0)
14546 break;
14547 buf[len] = NUL;
14548
14549 if (limit-- == 0)
14550 {
14551 vim_free(p);
14552 vim_free(remain);
14553 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 goto fail;
14556 }
14557
14558 /* Ensure that the result will have a trailing path separator
14559 * if the argument has one. */
14560 if (remain == NULL && has_trailing_pathsep)
14561 add_pathsep(buf);
14562
14563 /* Separate the first path component in the link value and
14564 * concatenate the remainders. */
14565 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14566 if (*q != NUL)
14567 {
14568 if (remain == NULL)
14569 remain = vim_strsave(q - 1);
14570 else
14571 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014572 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 if (cpy != NULL)
14574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 vim_free(remain);
14576 remain = cpy;
14577 }
14578 }
14579 q[-1] = NUL;
14580 }
14581
14582 q = gettail(p);
14583 if (q > p && *q == NUL)
14584 {
14585 /* Ignore trailing path separator. */
14586 q[-1] = NUL;
14587 q = gettail(p);
14588 }
14589 if (q > p && !mch_isFullName(buf))
14590 {
14591 /* symlink is relative to directory of argument */
14592 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14593 if (cpy != NULL)
14594 {
14595 STRCPY(cpy, p);
14596 STRCPY(gettail(cpy), buf);
14597 vim_free(p);
14598 p = cpy;
14599 }
14600 }
14601 else
14602 {
14603 vim_free(p);
14604 p = vim_strsave(buf);
14605 }
14606 }
14607
14608 if (remain == NULL)
14609 break;
14610
14611 /* Append the first path component of "remain" to "p". */
14612 q = getnextcomp(remain + 1);
14613 len = q - remain - (*q != NUL);
14614 cpy = vim_strnsave(p, STRLEN(p) + len);
14615 if (cpy != NULL)
14616 {
14617 STRNCAT(cpy, remain, len);
14618 vim_free(p);
14619 p = cpy;
14620 }
14621 /* Shorten "remain". */
14622 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014623 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 else
14625 {
14626 vim_free(remain);
14627 remain = NULL;
14628 }
14629 }
14630
14631 /* If the result is a relative path name, make it explicitly relative to
14632 * the current directory if and only if the argument had this form. */
14633 if (!vim_ispathsep(*p))
14634 {
14635 if (is_relative_to_current
14636 && *p != NUL
14637 && !(p[0] == '.'
14638 && (p[1] == NUL
14639 || vim_ispathsep(p[1])
14640 || (p[1] == '.'
14641 && (p[2] == NUL
14642 || vim_ispathsep(p[2]))))))
14643 {
14644 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014645 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 if (cpy != NULL)
14647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 vim_free(p);
14649 p = cpy;
14650 }
14651 }
14652 else if (!is_relative_to_current)
14653 {
14654 /* Strip leading "./". */
14655 q = p;
14656 while (q[0] == '.' && vim_ispathsep(q[1]))
14657 q += 2;
14658 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014659 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 }
14661 }
14662
14663 /* Ensure that the result will have no trailing path separator
14664 * if the argument had none. But keep "/" or "//". */
14665 if (!has_trailing_pathsep)
14666 {
14667 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014668 if (after_pathsep(p, q))
14669 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670 }
14671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 }
14674# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014675 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676# endif
14677#endif
14678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680
14681#ifdef HAVE_READLINK
14682fail:
14683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685}
14686
14687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 */
14690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014692 typval_T *argvars;
14693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694{
Bram Moolenaar33570922005-01-25 22:26:29 +000014695 list_T *l;
14696 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698 rettv->vval.v_number = 0;
14699 if (argvars[0].v_type != VAR_LIST)
14700 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014701 else if ((l = argvars[0].vval.v_list) != NULL
14702 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703 {
14704 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014705 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014706 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 while (li != NULL)
14708 {
14709 ni = li->li_prev;
14710 list_append(l, li);
14711 li = ni;
14712 }
14713 rettv->vval.v_list = l;
14714 rettv->v_type = VAR_LIST;
14715 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014716 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718}
14719
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014720#define SP_NOMOVE 0x01 /* don't move cursor */
14721#define SP_REPEAT 0x02 /* repeat to find outer pair */
14722#define SP_RETCOUNT 0x04 /* return matchcount */
14723#define SP_SETPCMARK 0x08 /* set previous context mark */
14724#define SP_START 0x10 /* accept match at start position */
14725#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14726#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014727
Bram Moolenaar33570922005-01-25 22:26:29 +000014728static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729
14730/*
14731 * Get flags for a search function.
14732 * Possibly sets "p_ws".
14733 * Returns BACKWARD, FORWARD or zero (for an error).
14734 */
14735 static int
14736get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738 int *flagsp;
14739{
14740 int dir = FORWARD;
14741 char_u *flags;
14742 char_u nbuf[NUMBUFLEN];
14743 int mask;
14744
14745 if (varp->v_type != VAR_UNKNOWN)
14746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014747 flags = get_tv_string_buf_chk(varp, nbuf);
14748 if (flags == NULL)
14749 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 while (*flags != NUL)
14751 {
14752 switch (*flags)
14753 {
14754 case 'b': dir = BACKWARD; break;
14755 case 'w': p_ws = TRUE; break;
14756 case 'W': p_ws = FALSE; break;
14757 default: mask = 0;
14758 if (flagsp != NULL)
14759 switch (*flags)
14760 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014761 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014762 case 'e': mask = SP_END; break;
14763 case 'm': mask = SP_RETCOUNT; break;
14764 case 'n': mask = SP_NOMOVE; break;
14765 case 'p': mask = SP_SUBPAT; break;
14766 case 'r': mask = SP_REPEAT; break;
14767 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014768 }
14769 if (mask == 0)
14770 {
14771 EMSG2(_(e_invarg2), flags);
14772 dir = 0;
14773 }
14774 else
14775 *flagsp |= mask;
14776 }
14777 if (dir == 0)
14778 break;
14779 ++flags;
14780 }
14781 }
14782 return dir;
14783}
14784
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014786 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014788 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014789search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014791 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014792 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014794 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 char_u *pat;
14796 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014797 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 int save_p_ws = p_ws;
14799 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014800 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014801 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014802 proftime_T tm;
14803#ifdef FEAT_RELTIME
14804 long time_limit = 0;
14805#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014806 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014807 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014810 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014811 if (dir == 0)
14812 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014813 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014814 if (flags & SP_START)
14815 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014816 if (flags & SP_END)
14817 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014818
Bram Moolenaar76929292008-01-06 19:07:36 +000014819 /* Optional arguments: line number to stop searching and timeout. */
14820 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014821 {
14822 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14823 if (lnum_stop < 0)
14824 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014825#ifdef FEAT_RELTIME
14826 if (argvars[3].v_type != VAR_UNKNOWN)
14827 {
14828 time_limit = get_tv_number_chk(&argvars[3], NULL);
14829 if (time_limit < 0)
14830 goto theend;
14831 }
14832#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014833 }
14834
Bram Moolenaar76929292008-01-06 19:07:36 +000014835#ifdef FEAT_RELTIME
14836 /* Set the time limit, if there is one. */
14837 profile_setlimit(time_limit, &tm);
14838#endif
14839
Bram Moolenaar231334e2005-07-25 20:46:57 +000014840 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014841 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014842 * Check to make sure only those flags are set.
14843 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14844 * flags cannot be set. Check for that condition also.
14845 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014846 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014847 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014849 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014850 goto theend;
14851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014853 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014854 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014855 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014856 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014858 if (flags & SP_SUBPAT)
14859 retval = subpatnum;
14860 else
14861 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014862 if (flags & SP_SETPCMARK)
14863 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014865 if (match_pos != NULL)
14866 {
14867 /* Store the match cursor position */
14868 match_pos->lnum = pos.lnum;
14869 match_pos->col = pos.col + 1;
14870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 /* "/$" will put the cursor after the end of the line, may need to
14872 * correct that here */
14873 check_cursor();
14874 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014875
14876 /* If 'n' flag is used: restore cursor position. */
14877 if (flags & SP_NOMOVE)
14878 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014879 else
14880 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014881theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014883
14884 return retval;
14885}
14886
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014887#ifdef FEAT_FLOAT
14888/*
14889 * "round({float})" function
14890 */
14891 static void
14892f_round(argvars, rettv)
14893 typval_T *argvars;
14894 typval_T *rettv;
14895{
14896 float_T f;
14897
14898 rettv->v_type = VAR_FLOAT;
14899 if (get_float_arg(argvars, &f) == OK)
14900 /* round() is not in C90, use ceil() or floor() instead. */
14901 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14902 else
14903 rettv->vval.v_float = 0.0;
14904}
14905#endif
14906
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014907/*
14908 * "search()" function
14909 */
14910 static void
14911f_search(argvars, rettv)
14912 typval_T *argvars;
14913 typval_T *rettv;
14914{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014915 int flags = 0;
14916
14917 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918}
14919
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014921 * "searchdecl()" function
14922 */
14923 static void
14924f_searchdecl(argvars, rettv)
14925 typval_T *argvars;
14926 typval_T *rettv;
14927{
14928 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014929 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014930 int error = FALSE;
14931 char_u *name;
14932
14933 rettv->vval.v_number = 1; /* default: FAIL */
14934
14935 name = get_tv_string_chk(&argvars[0]);
14936 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014937 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014938 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014939 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14940 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14941 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014942 if (!error && name != NULL)
14943 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014944 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014945}
14946
14947/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014948 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014950 static int
14951searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014952 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014953 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954{
14955 char_u *spat, *mpat, *epat;
14956 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 int dir;
14959 int flags = 0;
14960 char_u nbuf1[NUMBUFLEN];
14961 char_u nbuf2[NUMBUFLEN];
14962 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014963 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014964 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014965 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 spat = get_tv_string_chk(&argvars[0]);
14969 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14970 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14971 if (spat == NULL || mpat == NULL || epat == NULL)
14972 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 /* Handle the optional fourth argument: flags */
14975 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014976 if (dir == 0)
14977 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014978
14979 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014980 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14981 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014982 if ((flags & (SP_END | SP_SUBPAT)) != 0
14983 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014984 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014985 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014986 goto theend;
14987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014989 /* Using 'r' implies 'W', otherwise it doesn't work. */
14990 if (flags & SP_REPEAT)
14991 p_ws = FALSE;
14992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014993 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014994 if (argvars[3].v_type == VAR_UNKNOWN
14995 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996 skip = (char_u *)"";
14997 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015000 if (argvars[5].v_type != VAR_UNKNOWN)
15001 {
15002 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15003 if (lnum_stop < 0)
15004 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015005#ifdef FEAT_RELTIME
15006 if (argvars[6].v_type != VAR_UNKNOWN)
15007 {
15008 time_limit = get_tv_number_chk(&argvars[6], NULL);
15009 if (time_limit < 0)
15010 goto theend;
15011 }
15012#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015013 }
15014 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 if (skip == NULL)
15016 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015018 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015019 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015020
15021theend:
15022 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015023
15024 return retval;
15025}
15026
15027/*
15028 * "searchpair()" function
15029 */
15030 static void
15031f_searchpair(argvars, rettv)
15032 typval_T *argvars;
15033 typval_T *rettv;
15034{
15035 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15036}
15037
15038/*
15039 * "searchpairpos()" function
15040 */
15041 static void
15042f_searchpairpos(argvars, rettv)
15043 typval_T *argvars;
15044 typval_T *rettv;
15045{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015046 pos_T match_pos;
15047 int lnum = 0;
15048 int col = 0;
15049
15050 rettv->vval.v_number = 0;
15051
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015052 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015053 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015054
15055 if (searchpair_cmn(argvars, &match_pos) > 0)
15056 {
15057 lnum = match_pos.lnum;
15058 col = match_pos.col;
15059 }
15060
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015061 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15062 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015063}
15064
15065/*
15066 * Search for a start/middle/end thing.
15067 * Used by searchpair(), see its documentation for the details.
15068 * Returns 0 or -1 for no match,
15069 */
15070 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015071do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15072 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015073 char_u *spat; /* start pattern */
15074 char_u *mpat; /* middle pattern */
15075 char_u *epat; /* end pattern */
15076 int dir; /* BACKWARD or FORWARD */
15077 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015078 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015079 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015080 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015081 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015082{
15083 char_u *save_cpo;
15084 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15085 long retval = 0;
15086 pos_T pos;
15087 pos_T firstpos;
15088 pos_T foundpos;
15089 pos_T save_cursor;
15090 pos_T save_pos;
15091 int n;
15092 int r;
15093 int nest = 1;
15094 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015095 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015096 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015097
15098 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15099 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015100 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015101
Bram Moolenaar76929292008-01-06 19:07:36 +000015102#ifdef FEAT_RELTIME
15103 /* Set the time limit, if there is one. */
15104 profile_setlimit(time_limit, &tm);
15105#endif
15106
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015107 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15108 * start/middle/end (pat3, for the top pair). */
15109 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15110 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15111 if (pat2 == NULL || pat3 == NULL)
15112 goto theend;
15113 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15114 if (*mpat == NUL)
15115 STRCPY(pat3, pat2);
15116 else
15117 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15118 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015119 if (flags & SP_START)
15120 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015121
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 save_cursor = curwin->w_cursor;
15123 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015124 clearpos(&firstpos);
15125 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 pat = pat3;
15127 for (;;)
15128 {
15129 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015130 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15132 /* didn't find it or found the first match again: FAIL */
15133 break;
15134
15135 if (firstpos.lnum == 0)
15136 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015137 if (equalpos(pos, foundpos))
15138 {
15139 /* Found the same position again. Can happen with a pattern that
15140 * has "\zs" at the end and searching backwards. Advance one
15141 * character and try again. */
15142 if (dir == BACKWARD)
15143 decl(&pos);
15144 else
15145 incl(&pos);
15146 }
15147 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015149 /* clear the start flag to avoid getting stuck here */
15150 options &= ~SEARCH_START;
15151
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 /* If the skip pattern matches, ignore this match. */
15153 if (*skip != NUL)
15154 {
15155 save_pos = curwin->w_cursor;
15156 curwin->w_cursor = pos;
15157 r = eval_to_bool(skip, &err, NULL, FALSE);
15158 curwin->w_cursor = save_pos;
15159 if (err)
15160 {
15161 /* Evaluating {skip} caused an error, break here. */
15162 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015163 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 break;
15165 }
15166 if (r)
15167 continue;
15168 }
15169
15170 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15171 {
15172 /* Found end when searching backwards or start when searching
15173 * forward: nested pair. */
15174 ++nest;
15175 pat = pat2; /* nested, don't search for middle */
15176 }
15177 else
15178 {
15179 /* Found end when searching forward or start when searching
15180 * backward: end of (nested) pair; or found middle in outer pair. */
15181 if (--nest == 1)
15182 pat = pat3; /* outer level, search for middle */
15183 }
15184
15185 if (nest == 0)
15186 {
15187 /* Found the match: return matchcount or line number. */
15188 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015189 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015191 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015192 if (flags & SP_SETPCMARK)
15193 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 curwin->w_cursor = pos;
15195 if (!(flags & SP_REPEAT))
15196 break;
15197 nest = 1; /* search for next unmatched */
15198 }
15199 }
15200
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015201 if (match_pos != NULL)
15202 {
15203 /* Store the match cursor position */
15204 match_pos->lnum = curwin->w_cursor.lnum;
15205 match_pos->col = curwin->w_cursor.col + 1;
15206 }
15207
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015209 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 curwin->w_cursor = save_cursor;
15211
15212theend:
15213 vim_free(pat2);
15214 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015215 if (p_cpo == empty_option)
15216 p_cpo = save_cpo;
15217 else
15218 /* Darn, evaluating the {skip} expression changed the value. */
15219 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015220
15221 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222}
15223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015224/*
15225 * "searchpos()" function
15226 */
15227 static void
15228f_searchpos(argvars, rettv)
15229 typval_T *argvars;
15230 typval_T *rettv;
15231{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015232 pos_T match_pos;
15233 int lnum = 0;
15234 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015235 int n;
15236 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015237
15238 rettv->vval.v_number = 0;
15239
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015240 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015241 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015242
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015243 n = search_cmn(argvars, &match_pos, &flags);
15244 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015245 {
15246 lnum = match_pos.lnum;
15247 col = match_pos.col;
15248 }
15249
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015250 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15251 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015252 if (flags & SP_SUBPAT)
15253 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015254}
15255
15256
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257/*ARGSUSED*/
15258 static void
15259f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *argvars;
15261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263#ifdef FEAT_CLIENTSERVER
15264 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015265 char_u *server = get_tv_string_chk(&argvars[0]);
15266 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 if (server == NULL || reply == NULL)
15270 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015271 if (check_restricted() || check_secure())
15272 return;
15273# ifdef FEAT_X11
15274 if (check_connection() == FAIL)
15275 return;
15276# endif
15277
15278 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 EMSG(_("E258: Unable to send to client"));
15281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015283 rettv->vval.v_number = 0;
15284#else
15285 rettv->vval.v_number = -1;
15286#endif
15287}
15288
15289/*ARGSUSED*/
15290 static void
15291f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015292 typval_T *argvars;
15293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294{
15295 char_u *r = NULL;
15296
15297#ifdef FEAT_CLIENTSERVER
15298# ifdef WIN32
15299 r = serverGetVimNames();
15300# else
15301 make_connection();
15302 if (X_DISPLAY != NULL)
15303 r = serverGetVimNames(X_DISPLAY);
15304# endif
15305#endif
15306 rettv->v_type = VAR_STRING;
15307 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308}
15309
15310/*
15311 * "setbufvar()" function
15312 */
15313/*ARGSUSED*/
15314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015315f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015316 typval_T *argvars;
15317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318{
15319 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015322 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 char_u nbuf[NUMBUFLEN];
15324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 rettv->vval.v_number = 0;
15326
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 if (check_restricted() || check_secure())
15328 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015329 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15330 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 varp = &argvars[2];
15333
15334 if (buf != NULL && varname != NULL && varp != NULL)
15335 {
15336 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338
15339 if (*varname == '&')
15340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015341 long numval;
15342 char_u *strval;
15343 int error = FALSE;
15344
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015346 numval = get_tv_number_chk(varp, &error);
15347 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 if (!error && strval != NULL)
15349 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 }
15351 else
15352 {
15353 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15354 if (bufvarname != NULL)
15355 {
15356 STRCPY(bufvarname, "b:");
15357 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015358 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 vim_free(bufvarname);
15360 }
15361 }
15362
15363 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366}
15367
15368/*
15369 * "setcmdpos()" function
15370 */
15371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015373 typval_T *argvars;
15374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015376 int pos = (int)get_tv_number(&argvars[0]) - 1;
15377
15378 if (pos >= 0)
15379 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380}
15381
15382/*
15383 * "setline()" function
15384 */
15385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015387 typval_T *argvars;
15388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389{
15390 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015391 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015392 list_T *l = NULL;
15393 listitem_T *li = NULL;
15394 long added = 0;
15395 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015397 lnum = get_tv_lnum(&argvars[0]);
15398 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015400 l = argvars[1].vval.v_list;
15401 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015403 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015404 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015405
15406 rettv->vval.v_number = 0; /* OK */
15407 for (;;)
15408 {
15409 if (l != NULL)
15410 {
15411 /* list argument, get next string */
15412 if (li == NULL)
15413 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015414 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015415 li = li->li_next;
15416 }
15417
15418 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015419 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015420 break;
15421 if (lnum <= curbuf->b_ml.ml_line_count)
15422 {
15423 /* existing line, replace it */
15424 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15425 {
15426 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015427 if (lnum == curwin->w_cursor.lnum)
15428 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015429 rettv->vval.v_number = 0; /* OK */
15430 }
15431 }
15432 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15433 {
15434 /* lnum is one past the last line, append the line */
15435 ++added;
15436 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15437 rettv->vval.v_number = 0; /* OK */
15438 }
15439
15440 if (l == NULL) /* only one string argument */
15441 break;
15442 ++lnum;
15443 }
15444
15445 if (added > 0)
15446 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447}
15448
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015449static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15450
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015452 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015453 */
15454/*ARGSUSED*/
15455 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015456set_qf_ll_list(wp, list_arg, action_arg, rettv)
15457 win_T *wp;
15458 typval_T *list_arg;
15459 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015460 typval_T *rettv;
15461{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015462#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015463 char_u *act;
15464 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015465#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015466
Bram Moolenaar2641f772005-03-25 21:58:17 +000015467 rettv->vval.v_number = -1;
15468
15469#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015470 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015471 EMSG(_(e_listreq));
15472 else
15473 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015474 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015475
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015476 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015477 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015478 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015479 if (act == NULL)
15480 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015481 if (*act == 'a' || *act == 'r')
15482 action = *act;
15483 }
15484
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015485 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015486 rettv->vval.v_number = 0;
15487 }
15488#endif
15489}
15490
15491/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015492 * "setloclist()" function
15493 */
15494/*ARGSUSED*/
15495 static void
15496f_setloclist(argvars, rettv)
15497 typval_T *argvars;
15498 typval_T *rettv;
15499{
15500 win_T *win;
15501
15502 rettv->vval.v_number = -1;
15503
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015504 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015505 if (win != NULL)
15506 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15507}
15508
15509/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015510 * "setmatches()" function
15511 */
15512 static void
15513f_setmatches(argvars, rettv)
15514 typval_T *argvars;
15515 typval_T *rettv;
15516{
15517#ifdef FEAT_SEARCH_EXTRA
15518 list_T *l;
15519 listitem_T *li;
15520 dict_T *d;
15521
15522 rettv->vval.v_number = -1;
15523 if (argvars[0].v_type != VAR_LIST)
15524 {
15525 EMSG(_(e_listreq));
15526 return;
15527 }
15528 if ((l = argvars[0].vval.v_list) != NULL)
15529 {
15530
15531 /* To some extent make sure that we are dealing with a list from
15532 * "getmatches()". */
15533 li = l->lv_first;
15534 while (li != NULL)
15535 {
15536 if (li->li_tv.v_type != VAR_DICT
15537 || (d = li->li_tv.vval.v_dict) == NULL)
15538 {
15539 EMSG(_(e_invarg));
15540 return;
15541 }
15542 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15543 && dict_find(d, (char_u *)"pattern", -1) != NULL
15544 && dict_find(d, (char_u *)"priority", -1) != NULL
15545 && dict_find(d, (char_u *)"id", -1) != NULL))
15546 {
15547 EMSG(_(e_invarg));
15548 return;
15549 }
15550 li = li->li_next;
15551 }
15552
15553 clear_matches(curwin);
15554 li = l->lv_first;
15555 while (li != NULL)
15556 {
15557 d = li->li_tv.vval.v_dict;
15558 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15559 get_dict_string(d, (char_u *)"pattern", FALSE),
15560 (int)get_dict_number(d, (char_u *)"priority"),
15561 (int)get_dict_number(d, (char_u *)"id"));
15562 li = li->li_next;
15563 }
15564 rettv->vval.v_number = 0;
15565 }
15566#endif
15567}
15568
15569/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015570 * "setpos()" function
15571 */
15572/*ARGSUSED*/
15573 static void
15574f_setpos(argvars, rettv)
15575 typval_T *argvars;
15576 typval_T *rettv;
15577{
15578 pos_T pos;
15579 int fnum;
15580 char_u *name;
15581
Bram Moolenaar08250432008-02-13 11:42:46 +000015582 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015583 name = get_tv_string_chk(argvars);
15584 if (name != NULL)
15585 {
15586 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15587 {
15588 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015589 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015590 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015591 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015592 if (fnum == curbuf->b_fnum)
15593 {
15594 curwin->w_cursor = pos;
15595 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015596 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015597 }
15598 else
15599 EMSG(_(e_invarg));
15600 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015601 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15602 {
15603 /* set mark */
15604 if (setmark_pos(name[1], &pos, fnum) == OK)
15605 rettv->vval.v_number = 0;
15606 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015607 else
15608 EMSG(_(e_invarg));
15609 }
15610 }
15611}
15612
15613/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015614 * "setqflist()" function
15615 */
15616/*ARGSUSED*/
15617 static void
15618f_setqflist(argvars, rettv)
15619 typval_T *argvars;
15620 typval_T *rettv;
15621{
15622 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15623}
15624
15625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 * "setreg()" function
15627 */
15628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 typval_T *argvars;
15631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632{
15633 int regname;
15634 char_u *strregname;
15635 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 int append;
15638 char_u yank_type;
15639 long block_len;
15640
15641 block_len = -1;
15642 yank_type = MAUTO;
15643 append = FALSE;
15644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015645 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015646 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 if (strregname == NULL)
15649 return; /* type error; errmsg already given */
15650 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 if (regname == 0 || regname == '@')
15652 regname = '"';
15653 else if (regname == '=')
15654 return;
15655
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 stropt = get_tv_string_chk(&argvars[2]);
15659 if (stropt == NULL)
15660 return; /* type error */
15661 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 switch (*stropt)
15663 {
15664 case 'a': case 'A': /* append */
15665 append = TRUE;
15666 break;
15667 case 'v': case 'c': /* character-wise selection */
15668 yank_type = MCHAR;
15669 break;
15670 case 'V': case 'l': /* line-wise selection */
15671 yank_type = MLINE;
15672 break;
15673#ifdef FEAT_VISUAL
15674 case 'b': case Ctrl_V: /* block-wise selection */
15675 yank_type = MBLOCK;
15676 if (VIM_ISDIGIT(stropt[1]))
15677 {
15678 ++stropt;
15679 block_len = getdigits(&stropt) - 1;
15680 --stropt;
15681 }
15682 break;
15683#endif
15684 }
15685 }
15686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015687 strval = get_tv_string_chk(&argvars[1]);
15688 if (strval != NULL)
15689 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692}
15693
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015694/*
15695 * "settabwinvar()" function
15696 */
15697 static void
15698f_settabwinvar(argvars, rettv)
15699 typval_T *argvars;
15700 typval_T *rettv;
15701{
15702 setwinvar(argvars, rettv, 1);
15703}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704
15705/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015706 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015709f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 typval_T *argvars;
15711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015713 setwinvar(argvars, rettv, 0);
15714}
15715
15716/*
15717 * "setwinvar()" and "settabwinvar()" functions
15718 */
15719 static void
15720setwinvar(argvars, rettv, off)
15721 typval_T *argvars;
15722 typval_T *rettv;
15723 int off;
15724{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 win_T *win;
15726#ifdef FEAT_WINDOWS
15727 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015728 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729#endif
15730 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015731 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015733 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 rettv->vval.v_number = 0;
15736
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 if (check_restricted() || check_secure())
15738 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015739
15740#ifdef FEAT_WINDOWS
15741 if (off == 1)
15742 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15743 else
15744 tp = curtab;
15745#endif
15746 win = find_win_by_nr(&argvars[off], tp);
15747 varname = get_tv_string_chk(&argvars[off + 1]);
15748 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749
15750 if (win != NULL && varname != NULL && varp != NULL)
15751 {
15752#ifdef FEAT_WINDOWS
15753 /* set curwin to be our win, temporarily */
15754 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015755 save_curtab = curtab;
15756 goto_tabpage_tp(tp);
15757 if (!win_valid(win))
15758 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 curwin = win;
15760 curbuf = curwin->w_buffer;
15761#endif
15762
15763 if (*varname == '&')
15764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015765 long numval;
15766 char_u *strval;
15767 int error = FALSE;
15768
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015770 numval = get_tv_number_chk(varp, &error);
15771 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015772 if (!error && strval != NULL)
15773 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 }
15775 else
15776 {
15777 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15778 if (winvarname != NULL)
15779 {
15780 STRCPY(winvarname, "w:");
15781 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015782 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 vim_free(winvarname);
15784 }
15785 }
15786
15787#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015788 /* Restore current tabpage and window, if still valid (autocomands can
15789 * make them invalid). */
15790 if (valid_tabpage(save_curtab))
15791 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 if (win_valid(save_curwin))
15793 {
15794 curwin = save_curwin;
15795 curbuf = curwin->w_buffer;
15796 }
15797#endif
15798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799}
15800
15801/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015802 * "shellescape({string})" function
15803 */
15804 static void
15805f_shellescape(argvars, rettv)
15806 typval_T *argvars;
15807 typval_T *rettv;
15808{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015809 rettv->vval.v_string = vim_strsave_shellescape(
15810 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015811 rettv->v_type = VAR_STRING;
15812}
15813
15814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 */
15817 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 typval_T *argvars;
15820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 p = get_tv_string(&argvars[0]);
15825 rettv->vval.v_string = vim_strsave(p);
15826 simplify_filename(rettv->vval.v_string); /* simplify in place */
15827 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828}
15829
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015830#ifdef FEAT_FLOAT
15831/*
15832 * "sin()" function
15833 */
15834 static void
15835f_sin(argvars, rettv)
15836 typval_T *argvars;
15837 typval_T *rettv;
15838{
15839 float_T f;
15840
15841 rettv->v_type = VAR_FLOAT;
15842 if (get_float_arg(argvars, &f) == OK)
15843 rettv->vval.v_float = sin(f);
15844 else
15845 rettv->vval.v_float = 0.0;
15846}
15847#endif
15848
Bram Moolenaar0d660222005-01-07 21:51:51 +000015849static int
15850#ifdef __BORLANDC__
15851 _RTLENTRYF
15852#endif
15853 item_compare __ARGS((const void *s1, const void *s2));
15854static int
15855#ifdef __BORLANDC__
15856 _RTLENTRYF
15857#endif
15858 item_compare2 __ARGS((const void *s1, const void *s2));
15859
15860static int item_compare_ic;
15861static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863#define ITEM_COMPARE_FAIL 999
15864
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868 static int
15869#ifdef __BORLANDC__
15870_RTLENTRYF
15871#endif
15872item_compare(s1, s2)
15873 const void *s1;
15874 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015876 char_u *p1, *p2;
15877 char_u *tofree1, *tofree2;
15878 int res;
15879 char_u numbuf1[NUMBUFLEN];
15880 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015882 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15883 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015884 if (p1 == NULL)
15885 p1 = (char_u *)"";
15886 if (p2 == NULL)
15887 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888 if (item_compare_ic)
15889 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015891 res = STRCMP(p1, p2);
15892 vim_free(tofree1);
15893 vim_free(tofree2);
15894 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895}
15896
15897 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015898#ifdef __BORLANDC__
15899_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901item_compare2(s1, s2)
15902 const void *s1;
15903 const void *s2;
15904{
15905 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015906 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015907 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 /* shortcut after failure in previous call; compare all items equal */
15911 if (item_compare_func_err)
15912 return 0;
15913
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015914 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15915 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15917 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015918
15919 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015920 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015921 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 clear_tv(&argv[0]);
15923 clear_tv(&argv[1]);
15924
15925 if (res == FAIL)
15926 res = ITEM_COMPARE_FAIL;
15927 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015928 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15929 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015930 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931 clear_tv(&rettv);
15932 return res;
15933}
15934
15935/*
15936 * "sort({list})" function
15937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015940 typval_T *argvars;
15941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942{
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 list_T *l;
15944 listitem_T *li;
15945 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946 long len;
15947 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948
Bram Moolenaar0d660222005-01-07 21:51:51 +000015949 rettv->vval.v_number = 0;
15950 if (argvars[0].v_type != VAR_LIST)
15951 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 else
15953 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015954 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015955 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015956 return;
15957 rettv->vval.v_list = l;
15958 rettv->v_type = VAR_LIST;
15959 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961 len = list_len(l);
15962 if (len <= 1)
15963 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964
Bram Moolenaar0d660222005-01-07 21:51:51 +000015965 item_compare_ic = FALSE;
15966 item_compare_func = NULL;
15967 if (argvars[1].v_type != VAR_UNKNOWN)
15968 {
15969 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971 else
15972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015973 int error = FALSE;
15974
15975 i = get_tv_number_chk(&argvars[1], &error);
15976 if (error)
15977 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978 if (i == 1)
15979 item_compare_ic = TRUE;
15980 else
15981 item_compare_func = get_tv_string(&argvars[1]);
15982 }
15983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984
Bram Moolenaar0d660222005-01-07 21:51:51 +000015985 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015986 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015987 if (ptrs == NULL)
15988 return;
15989 i = 0;
15990 for (li = l->lv_first; li != NULL; li = li->li_next)
15991 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015994 /* test the compare function */
15995 if (item_compare_func != NULL
15996 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15997 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015998 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016000 {
16001 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016003 item_compare_func == NULL ? item_compare : item_compare2);
16004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016005 if (!item_compare_func_err)
16006 {
16007 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016008 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 l->lv_len = 0;
16010 for (i = 0; i < len; ++i)
16011 list_append(l, ptrs[i]);
16012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016013 }
16014
16015 vim_free(ptrs);
16016 }
16017}
16018
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016019/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016020 * "soundfold({word})" function
16021 */
16022 static void
16023f_soundfold(argvars, rettv)
16024 typval_T *argvars;
16025 typval_T *rettv;
16026{
16027 char_u *s;
16028
16029 rettv->v_type = VAR_STRING;
16030 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016031#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016032 rettv->vval.v_string = eval_soundfold(s);
16033#else
16034 rettv->vval.v_string = vim_strsave(s);
16035#endif
16036}
16037
16038/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016039 * "spellbadword()" function
16040 */
16041/* ARGSUSED */
16042 static void
16043f_spellbadword(argvars, rettv)
16044 typval_T *argvars;
16045 typval_T *rettv;
16046{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016047 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016048 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016049 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016052 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016053
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016054#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016055 if (argvars[0].v_type == VAR_UNKNOWN)
16056 {
16057 /* Find the start and length of the badly spelled word. */
16058 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16059 if (len != 0)
16060 word = ml_get_cursor();
16061 }
16062 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16063 {
16064 char_u *str = get_tv_string_chk(&argvars[0]);
16065 int capcol = -1;
16066
16067 if (str != NULL)
16068 {
16069 /* Check the argument for spelling. */
16070 while (*str != NUL)
16071 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016072 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016073 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016074 {
16075 word = str;
16076 break;
16077 }
16078 str += len;
16079 }
16080 }
16081 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016082#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016084 list_append_string(rettv->vval.v_list, word, len);
16085 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016086 attr == HLF_SPB ? "bad" :
16087 attr == HLF_SPR ? "rare" :
16088 attr == HLF_SPL ? "local" :
16089 attr == HLF_SPC ? "caps" :
16090 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016091}
16092
16093/*
16094 * "spellsuggest()" function
16095 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016096/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016097 static void
16098f_spellsuggest(argvars, rettv)
16099 typval_T *argvars;
16100 typval_T *rettv;
16101{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016102#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016103 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016104 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016105 int maxcount;
16106 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016107 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016108 listitem_T *li;
16109 int need_capital = FALSE;
16110#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016111
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016112 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016113 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016114
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016115#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016116 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16117 {
16118 str = get_tv_string(&argvars[0]);
16119 if (argvars[1].v_type != VAR_UNKNOWN)
16120 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016121 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016122 if (maxcount <= 0)
16123 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016124 if (argvars[2].v_type != VAR_UNKNOWN)
16125 {
16126 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16127 if (typeerr)
16128 return;
16129 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016130 }
16131 else
16132 maxcount = 25;
16133
Bram Moolenaar4770d092006-01-12 23:22:24 +000016134 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016135
16136 for (i = 0; i < ga.ga_len; ++i)
16137 {
16138 str = ((char_u **)ga.ga_data)[i];
16139
16140 li = listitem_alloc();
16141 if (li == NULL)
16142 vim_free(str);
16143 else
16144 {
16145 li->li_tv.v_type = VAR_STRING;
16146 li->li_tv.v_lock = 0;
16147 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016148 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016149 }
16150 }
16151 ga_clear(&ga);
16152 }
16153#endif
16154}
16155
Bram Moolenaar0d660222005-01-07 21:51:51 +000016156 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016157f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016158 typval_T *argvars;
16159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160{
16161 char_u *str;
16162 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016163 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 regmatch_T regmatch;
16165 char_u patbuf[NUMBUFLEN];
16166 char_u *save_cpo;
16167 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016169 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016170 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171
16172 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16173 save_cpo = p_cpo;
16174 p_cpo = (char_u *)"";
16175
16176 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016177 if (argvars[1].v_type != VAR_UNKNOWN)
16178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016179 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16180 if (pat == NULL)
16181 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016183 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016184 }
16185 if (pat == NULL || *pat == NUL)
16186 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016188 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016190 if (typeerr)
16191 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16194 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016197 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016199 if (*str == NUL)
16200 match = FALSE; /* empty item at the end */
16201 else
16202 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203 if (match)
16204 end = regmatch.startp[0];
16205 else
16206 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016207 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16208 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016209 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016210 if (list_append_string(rettv->vval.v_list, str,
16211 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016213 }
16214 if (!match)
16215 break;
16216 /* Advance to just after the match. */
16217 if (regmatch.endp[0] > str)
16218 col = 0;
16219 else
16220 {
16221 /* Don't get stuck at the same match. */
16222#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016223 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224#else
16225 col = 1;
16226#endif
16227 }
16228 str = regmatch.endp[0];
16229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235}
16236
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016237#ifdef FEAT_FLOAT
16238/*
16239 * "sqrt()" function
16240 */
16241 static void
16242f_sqrt(argvars, rettv)
16243 typval_T *argvars;
16244 typval_T *rettv;
16245{
16246 float_T f;
16247
16248 rettv->v_type = VAR_FLOAT;
16249 if (get_float_arg(argvars, &f) == OK)
16250 rettv->vval.v_float = sqrt(f);
16251 else
16252 rettv->vval.v_float = 0.0;
16253}
16254
16255/*
16256 * "str2float()" function
16257 */
16258 static void
16259f_str2float(argvars, rettv)
16260 typval_T *argvars;
16261 typval_T *rettv;
16262{
16263 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16264
16265 if (*p == '+')
16266 p = skipwhite(p + 1);
16267 (void)string2float(p, &rettv->vval.v_float);
16268 rettv->v_type = VAR_FLOAT;
16269}
16270#endif
16271
Bram Moolenaar2c932302006-03-18 21:42:09 +000016272/*
16273 * "str2nr()" function
16274 */
16275 static void
16276f_str2nr(argvars, rettv)
16277 typval_T *argvars;
16278 typval_T *rettv;
16279{
16280 int base = 10;
16281 char_u *p;
16282 long n;
16283
16284 if (argvars[1].v_type != VAR_UNKNOWN)
16285 {
16286 base = get_tv_number(&argvars[1]);
16287 if (base != 8 && base != 10 && base != 16)
16288 {
16289 EMSG(_(e_invarg));
16290 return;
16291 }
16292 }
16293
16294 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016295 if (*p == '+')
16296 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016297 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16298 rettv->vval.v_number = n;
16299}
16300
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301#ifdef HAVE_STRFTIME
16302/*
16303 * "strftime({format}[, {time}])" function
16304 */
16305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016306f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 typval_T *argvars;
16308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309{
16310 char_u result_buf[256];
16311 struct tm *curtime;
16312 time_t seconds;
16313 char_u *p;
16314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016315 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016317 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016318 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319 seconds = time(NULL);
16320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016321 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 curtime = localtime(&seconds);
16323 /* MSVC returns NULL for an invalid value of seconds. */
16324 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 else
16327 {
16328# ifdef FEAT_MBYTE
16329 vimconv_T conv;
16330 char_u *enc;
16331
16332 conv.vc_type = CONV_NONE;
16333 enc = enc_locale();
16334 convert_setup(&conv, p_enc, enc);
16335 if (conv.vc_type != CONV_NONE)
16336 p = string_convert(&conv, p, NULL);
16337# endif
16338 if (p != NULL)
16339 (void)strftime((char *)result_buf, sizeof(result_buf),
16340 (char *)p, curtime);
16341 else
16342 result_buf[0] = NUL;
16343
16344# ifdef FEAT_MBYTE
16345 if (conv.vc_type != CONV_NONE)
16346 vim_free(p);
16347 convert_setup(&conv, enc, p_enc);
16348 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 else
16351# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353
16354# ifdef FEAT_MBYTE
16355 /* Release conversion descriptors */
16356 convert_setup(&conv, NULL, NULL);
16357 vim_free(enc);
16358# endif
16359 }
16360}
16361#endif
16362
16363/*
16364 * "stridx()" function
16365 */
16366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016368 typval_T *argvars;
16369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370{
16371 char_u buf[NUMBUFLEN];
16372 char_u *needle;
16373 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016376 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 needle = get_tv_string_chk(&argvars[1]);
16379 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016381 if (needle == NULL || haystack == NULL)
16382 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383
Bram Moolenaar33570922005-01-25 22:26:29 +000016384 if (argvars[2].v_type != VAR_UNKNOWN)
16385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016386 int error = FALSE;
16387
16388 start_idx = get_tv_number_chk(&argvars[2], &error);
16389 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016391 if (start_idx >= 0)
16392 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016393 }
16394
16395 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16396 if (pos != NULL)
16397 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398}
16399
16400/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016401 * "string()" function
16402 */
16403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016404f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016405 typval_T *argvars;
16406 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016407{
16408 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016409 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016412 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016413 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016414 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016415 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416}
16417
16418/*
16419 * "strlen()" function
16420 */
16421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016422f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016423 typval_T *argvars;
16424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 rettv->vval.v_number = (varnumber_T)(STRLEN(
16427 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428}
16429
16430/*
16431 * "strpart()" function
16432 */
16433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016434f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 typval_T *argvars;
16436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437{
16438 char_u *p;
16439 int n;
16440 int len;
16441 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016442 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016444 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 slen = (int)STRLEN(p);
16446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 n = get_tv_number_chk(&argvars[1], &error);
16448 if (error)
16449 len = 0;
16450 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 else
16453 len = slen - n; /* default len: all bytes that are available. */
16454
16455 /*
16456 * Only return the overlap between the specified part and the actual
16457 * string.
16458 */
16459 if (n < 0)
16460 {
16461 len += n;
16462 n = 0;
16463 }
16464 else if (n > slen)
16465 n = slen;
16466 if (len < 0)
16467 len = 0;
16468 else if (n + len > slen)
16469 len = slen - n;
16470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016471 rettv->v_type = VAR_STRING;
16472 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473}
16474
16475/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476 * "strridx()" function
16477 */
16478 static void
16479f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016480 typval_T *argvars;
16481 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482{
16483 char_u buf[NUMBUFLEN];
16484 char_u *needle;
16485 char_u *haystack;
16486 char_u *rest;
16487 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016488 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016490 needle = get_tv_string_chk(&argvars[1]);
16491 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492
16493 rettv->vval.v_number = -1;
16494 if (needle == NULL || haystack == NULL)
16495 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016496
16497 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016498 if (argvars[2].v_type != VAR_UNKNOWN)
16499 {
16500 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016501 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016502 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016503 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016504 }
16505 else
16506 end_idx = haystack_len;
16507
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016509 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016511 lastmatch = haystack + end_idx;
16512 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016514 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515 for (rest = haystack; *rest != '\0'; ++rest)
16516 {
16517 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016518 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519 break;
16520 lastmatch = rest;
16521 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016522 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523
16524 if (lastmatch == NULL)
16525 rettv->vval.v_number = -1;
16526 else
16527 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16528}
16529
16530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 * "strtrans()" function
16532 */
16533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016535 typval_T *argvars;
16536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538 rettv->v_type = VAR_STRING;
16539 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540}
16541
16542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 * "submatch()" function
16544 */
16545 static void
16546f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016547 typval_T *argvars;
16548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549{
16550 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 rettv->vval.v_string =
16552 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553}
16554
16555/*
16556 * "substitute()" function
16557 */
16558 static void
16559f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 typval_T *argvars;
16561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016562{
16563 char_u patbuf[NUMBUFLEN];
16564 char_u subbuf[NUMBUFLEN];
16565 char_u flagsbuf[NUMBUFLEN];
16566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016567 char_u *str = get_tv_string_chk(&argvars[0]);
16568 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16569 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16570 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16571
Bram Moolenaar0d660222005-01-07 21:51:51 +000016572 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16574 rettv->vval.v_string = NULL;
16575 else
16576 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577}
16578
16579/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016580 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 */
16582/*ARGSUSED*/
16583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016584f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016585 typval_T *argvars;
16586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587{
16588 int id = 0;
16589#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016590 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 long col;
16592 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016593 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016595 lnum = get_tv_lnum(argvars); /* -1 on type error */
16596 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16597 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016599 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016600 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016601 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602#endif
16603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016604 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605}
16606
16607/*
16608 * "synIDattr(id, what [, mode])" function
16609 */
16610/*ARGSUSED*/
16611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016612f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016613 typval_T *argvars;
16614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615{
16616 char_u *p = NULL;
16617#ifdef FEAT_SYN_HL
16618 int id;
16619 char_u *what;
16620 char_u *mode;
16621 char_u modebuf[NUMBUFLEN];
16622 int modec;
16623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016624 id = get_tv_number(&argvars[0]);
16625 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016628 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 modec = TOLOWER_ASC(mode[0]);
16630 if (modec != 't' && modec != 'c'
16631#ifdef FEAT_GUI
16632 && modec != 'g'
16633#endif
16634 )
16635 modec = 0; /* replace invalid with current */
16636 }
16637 else
16638 {
16639#ifdef FEAT_GUI
16640 if (gui.in_use)
16641 modec = 'g';
16642 else
16643#endif
16644 if (t_colors > 1)
16645 modec = 'c';
16646 else
16647 modec = 't';
16648 }
16649
16650
16651 switch (TOLOWER_ASC(what[0]))
16652 {
16653 case 'b':
16654 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16655 p = highlight_color(id, what, modec);
16656 else /* bold */
16657 p = highlight_has_attr(id, HL_BOLD, modec);
16658 break;
16659
16660 case 'f': /* fg[#] */
16661 p = highlight_color(id, what, modec);
16662 break;
16663
16664 case 'i':
16665 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16666 p = highlight_has_attr(id, HL_INVERSE, modec);
16667 else /* italic */
16668 p = highlight_has_attr(id, HL_ITALIC, modec);
16669 break;
16670
16671 case 'n': /* name */
16672 p = get_highlight_name(NULL, id - 1);
16673 break;
16674
16675 case 'r': /* reverse */
16676 p = highlight_has_attr(id, HL_INVERSE, modec);
16677 break;
16678
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016679 case 's':
16680 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16681 p = highlight_color(id, what, modec);
16682 else /* standout */
16683 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 break;
16685
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016686 case 'u':
16687 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16688 /* underline */
16689 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16690 else
16691 /* undercurl */
16692 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 break;
16694 }
16695
16696 if (p != NULL)
16697 p = vim_strsave(p);
16698#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016699 rettv->v_type = VAR_STRING;
16700 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701}
16702
16703/*
16704 * "synIDtrans(id)" function
16705 */
16706/*ARGSUSED*/
16707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016708f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016709 typval_T *argvars;
16710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711{
16712 int id;
16713
16714#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016715 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716
16717 if (id > 0)
16718 id = syn_get_final_id(id);
16719 else
16720#endif
16721 id = 0;
16722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016723 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724}
16725
16726/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016727 * "synstack(lnum, col)" function
16728 */
16729/*ARGSUSED*/
16730 static void
16731f_synstack(argvars, rettv)
16732 typval_T *argvars;
16733 typval_T *rettv;
16734{
16735#ifdef FEAT_SYN_HL
16736 long lnum;
16737 long col;
16738 int i;
16739 int id;
16740#endif
16741
16742 rettv->v_type = VAR_LIST;
16743 rettv->vval.v_list = NULL;
16744
16745#ifdef FEAT_SYN_HL
16746 lnum = get_tv_lnum(argvars); /* -1 on type error */
16747 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16748
16749 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016750 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016751 && rettv_list_alloc(rettv) != FAIL)
16752 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016753 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016754 for (i = 0; ; ++i)
16755 {
16756 id = syn_get_stack_item(i);
16757 if (id < 0)
16758 break;
16759 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16760 break;
16761 }
16762 }
16763#endif
16764}
16765
16766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 * "system()" function
16768 */
16769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016771 typval_T *argvars;
16772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016774 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016776 char_u *infile = NULL;
16777 char_u buf[NUMBUFLEN];
16778 int err = FALSE;
16779 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016781 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016782 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016783
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016785 {
16786 /*
16787 * Write the string to a temp file, to be used for input of the shell
16788 * command.
16789 */
16790 if ((infile = vim_tempname('i')) == NULL)
16791 {
16792 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016793 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016794 }
16795
16796 fd = mch_fopen((char *)infile, WRITEBIN);
16797 if (fd == NULL)
16798 {
16799 EMSG2(_(e_notopen), infile);
16800 goto done;
16801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016802 p = get_tv_string_buf_chk(&argvars[1], buf);
16803 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016804 {
16805 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016806 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016807 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016808 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16809 err = TRUE;
16810 if (fclose(fd) != 0)
16811 err = TRUE;
16812 if (err)
16813 {
16814 EMSG(_("E677: Error writing temp file"));
16815 goto done;
16816 }
16817 }
16818
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016819 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16820 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016821
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822#ifdef USE_CR
16823 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016824 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 {
16826 char_u *s;
16827
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016828 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 {
16830 if (*s == CAR)
16831 *s = NL;
16832 }
16833 }
16834#else
16835# ifdef USE_CRNL
16836 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016837 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 {
16839 char_u *s, *d;
16840
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016841 d = res;
16842 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 {
16844 if (s[0] == CAR && s[1] == NL)
16845 ++s;
16846 *d++ = *s;
16847 }
16848 *d = NUL;
16849 }
16850# endif
16851#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016852
16853done:
16854 if (infile != NULL)
16855 {
16856 mch_remove(infile);
16857 vim_free(infile);
16858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 rettv->v_type = VAR_STRING;
16860 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861}
16862
16863/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016864 * "tabpagebuflist()" function
16865 */
16866/* ARGSUSED */
16867 static void
16868f_tabpagebuflist(argvars, rettv)
16869 typval_T *argvars;
16870 typval_T *rettv;
16871{
16872#ifndef FEAT_WINDOWS
16873 rettv->vval.v_number = 0;
16874#else
16875 tabpage_T *tp;
16876 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016877
16878 if (argvars[0].v_type == VAR_UNKNOWN)
16879 wp = firstwin;
16880 else
16881 {
16882 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16883 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016884 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016885 }
16886 if (wp == NULL)
16887 rettv->vval.v_number = 0;
16888 else
16889 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016890 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016891 rettv->vval.v_number = 0;
16892 else
16893 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016894 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016895 if (list_append_number(rettv->vval.v_list,
16896 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016897 break;
16898 }
16899 }
16900#endif
16901}
16902
16903
16904/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016905 * "tabpagenr()" function
16906 */
16907/* ARGSUSED */
16908 static void
16909f_tabpagenr(argvars, rettv)
16910 typval_T *argvars;
16911 typval_T *rettv;
16912{
16913 int nr = 1;
16914#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016915 char_u *arg;
16916
16917 if (argvars[0].v_type != VAR_UNKNOWN)
16918 {
16919 arg = get_tv_string_chk(&argvars[0]);
16920 nr = 0;
16921 if (arg != NULL)
16922 {
16923 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016924 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016925 else
16926 EMSG2(_(e_invexpr2), arg);
16927 }
16928 }
16929 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016930 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016931#endif
16932 rettv->vval.v_number = nr;
16933}
16934
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016935
16936#ifdef FEAT_WINDOWS
16937static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16938
16939/*
16940 * Common code for tabpagewinnr() and winnr().
16941 */
16942 static int
16943get_winnr(tp, argvar)
16944 tabpage_T *tp;
16945 typval_T *argvar;
16946{
16947 win_T *twin;
16948 int nr = 1;
16949 win_T *wp;
16950 char_u *arg;
16951
16952 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16953 if (argvar->v_type != VAR_UNKNOWN)
16954 {
16955 arg = get_tv_string_chk(argvar);
16956 if (arg == NULL)
16957 nr = 0; /* type error; errmsg already given */
16958 else if (STRCMP(arg, "$") == 0)
16959 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16960 else if (STRCMP(arg, "#") == 0)
16961 {
16962 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16963 if (twin == NULL)
16964 nr = 0;
16965 }
16966 else
16967 {
16968 EMSG2(_(e_invexpr2), arg);
16969 nr = 0;
16970 }
16971 }
16972
16973 if (nr > 0)
16974 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16975 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016976 {
16977 if (wp == NULL)
16978 {
16979 /* didn't find it in this tabpage */
16980 nr = 0;
16981 break;
16982 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016983 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016984 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016985 return nr;
16986}
16987#endif
16988
16989/*
16990 * "tabpagewinnr()" function
16991 */
16992/* ARGSUSED */
16993 static void
16994f_tabpagewinnr(argvars, rettv)
16995 typval_T *argvars;
16996 typval_T *rettv;
16997{
16998 int nr = 1;
16999#ifdef FEAT_WINDOWS
17000 tabpage_T *tp;
17001
17002 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17003 if (tp == NULL)
17004 nr = 0;
17005 else
17006 nr = get_winnr(tp, &argvars[1]);
17007#endif
17008 rettv->vval.v_number = nr;
17009}
17010
17011
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017012/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017013 * "tagfiles()" function
17014 */
17015/*ARGSUSED*/
17016 static void
17017f_tagfiles(argvars, rettv)
17018 typval_T *argvars;
17019 typval_T *rettv;
17020{
17021 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017022 tagname_T tn;
17023 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017024
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017025 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017026 {
17027 rettv->vval.v_number = 0;
17028 return;
17029 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017030
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017031 for (first = TRUE; ; first = FALSE)
17032 if (get_tagfname(&tn, first, fname) == FAIL
17033 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017034 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017035 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017036}
17037
17038/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017039 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017040 */
17041 static void
17042f_taglist(argvars, rettv)
17043 typval_T *argvars;
17044 typval_T *rettv;
17045{
17046 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017047
17048 tag_pattern = get_tv_string(&argvars[0]);
17049
17050 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 if (*tag_pattern == NUL)
17052 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017053
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017054 if (rettv_list_alloc(rettv) == OK)
17055 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017056}
17057
17058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 * "tempname()" function
17060 */
17061/*ARGSUSED*/
17062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017064 typval_T *argvars;
17065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066{
17067 static int x = 'A';
17068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017069 rettv->v_type = VAR_STRING;
17070 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071
17072 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17073 * names. Skip 'I' and 'O', they are used for shell redirection. */
17074 do
17075 {
17076 if (x == 'Z')
17077 x = '0';
17078 else if (x == '9')
17079 x = 'A';
17080 else
17081 {
17082#ifdef EBCDIC
17083 if (x == 'I')
17084 x = 'J';
17085 else if (x == 'R')
17086 x = 'S';
17087 else
17088#endif
17089 ++x;
17090 }
17091 } while (x == 'I' || x == 'O');
17092}
17093
17094/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017095 * "test(list)" function: Just checking the walls...
17096 */
17097/*ARGSUSED*/
17098 static void
17099f_test(argvars, rettv)
17100 typval_T *argvars;
17101 typval_T *rettv;
17102{
17103 /* Used for unit testing. Change the code below to your liking. */
17104#if 0
17105 listitem_T *li;
17106 list_T *l;
17107 char_u *bad, *good;
17108
17109 if (argvars[0].v_type != VAR_LIST)
17110 return;
17111 l = argvars[0].vval.v_list;
17112 if (l == NULL)
17113 return;
17114 li = l->lv_first;
17115 if (li == NULL)
17116 return;
17117 bad = get_tv_string(&li->li_tv);
17118 li = li->li_next;
17119 if (li == NULL)
17120 return;
17121 good = get_tv_string(&li->li_tv);
17122 rettv->vval.v_number = test_edit_score(bad, good);
17123#endif
17124}
17125
17126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 * "tolower(string)" function
17128 */
17129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017130f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017131 typval_T *argvars;
17132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133{
17134 char_u *p;
17135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017136 p = vim_strsave(get_tv_string(&argvars[0]));
17137 rettv->v_type = VAR_STRING;
17138 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139
17140 if (p != NULL)
17141 while (*p != NUL)
17142 {
17143#ifdef FEAT_MBYTE
17144 int l;
17145
17146 if (enc_utf8)
17147 {
17148 int c, lc;
17149
17150 c = utf_ptr2char(p);
17151 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017152 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 /* TODO: reallocate string when byte count changes. */
17154 if (utf_char2len(lc) == l)
17155 utf_char2bytes(lc, p);
17156 p += l;
17157 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017158 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 p += l; /* skip multi-byte character */
17160 else
17161#endif
17162 {
17163 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17164 ++p;
17165 }
17166 }
17167}
17168
17169/*
17170 * "toupper(string)" function
17171 */
17172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017174 typval_T *argvars;
17175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017178 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179}
17180
17181/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017182 * "tr(string, fromstr, tostr)" function
17183 */
17184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017186 typval_T *argvars;
17187 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017188{
17189 char_u *instr;
17190 char_u *fromstr;
17191 char_u *tostr;
17192 char_u *p;
17193#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017194 int inlen;
17195 int fromlen;
17196 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017197 int idx;
17198 char_u *cpstr;
17199 int cplen;
17200 int first = TRUE;
17201#endif
17202 char_u buf[NUMBUFLEN];
17203 char_u buf2[NUMBUFLEN];
17204 garray_T ga;
17205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017206 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017207 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17208 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017209
17210 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211 rettv->v_type = VAR_STRING;
17212 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017213 if (fromstr == NULL || tostr == NULL)
17214 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017215 ga_init2(&ga, (int)sizeof(char), 80);
17216
17217#ifdef FEAT_MBYTE
17218 if (!has_mbyte)
17219#endif
17220 /* not multi-byte: fromstr and tostr must be the same length */
17221 if (STRLEN(fromstr) != STRLEN(tostr))
17222 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017223#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017224error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017225#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017226 EMSG2(_(e_invarg2), fromstr);
17227 ga_clear(&ga);
17228 return;
17229 }
17230
17231 /* fromstr and tostr have to contain the same number of chars */
17232 while (*instr != NUL)
17233 {
17234#ifdef FEAT_MBYTE
17235 if (has_mbyte)
17236 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017237 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017238 cpstr = instr;
17239 cplen = inlen;
17240 idx = 0;
17241 for (p = fromstr; *p != NUL; p += fromlen)
17242 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017243 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017244 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17245 {
17246 for (p = tostr; *p != NUL; p += tolen)
17247 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017248 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017249 if (idx-- == 0)
17250 {
17251 cplen = tolen;
17252 cpstr = p;
17253 break;
17254 }
17255 }
17256 if (*p == NUL) /* tostr is shorter than fromstr */
17257 goto error;
17258 break;
17259 }
17260 ++idx;
17261 }
17262
17263 if (first && cpstr == instr)
17264 {
17265 /* Check that fromstr and tostr have the same number of
17266 * (multi-byte) characters. Done only once when a character
17267 * of instr doesn't appear in fromstr. */
17268 first = FALSE;
17269 for (p = tostr; *p != NUL; p += tolen)
17270 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017271 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017272 --idx;
17273 }
17274 if (idx != 0)
17275 goto error;
17276 }
17277
17278 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017279 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017280 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017281
17282 instr += inlen;
17283 }
17284 else
17285#endif
17286 {
17287 /* When not using multi-byte chars we can do it faster. */
17288 p = vim_strchr(fromstr, *instr);
17289 if (p != NULL)
17290 ga_append(&ga, tostr[p - fromstr]);
17291 else
17292 ga_append(&ga, *instr);
17293 ++instr;
17294 }
17295 }
17296
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017297 /* add a terminating NUL */
17298 ga_grow(&ga, 1);
17299 ga_append(&ga, NUL);
17300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017302}
17303
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017304#ifdef FEAT_FLOAT
17305/*
17306 * "trunc({float})" function
17307 */
17308 static void
17309f_trunc(argvars, rettv)
17310 typval_T *argvars;
17311 typval_T *rettv;
17312{
17313 float_T f;
17314
17315 rettv->v_type = VAR_FLOAT;
17316 if (get_float_arg(argvars, &f) == OK)
17317 /* trunc() is not in C90, use floor() or ceil() instead. */
17318 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17319 else
17320 rettv->vval.v_float = 0.0;
17321}
17322#endif
17323
Bram Moolenaar8299df92004-07-10 09:47:34 +000017324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 * "type(expr)" function
17326 */
17327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 typval_T *argvars;
17330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017332 int n;
17333
17334 switch (argvars[0].v_type)
17335 {
17336 case VAR_NUMBER: n = 0; break;
17337 case VAR_STRING: n = 1; break;
17338 case VAR_FUNC: n = 2; break;
17339 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017340 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017341#ifdef FEAT_FLOAT
17342 case VAR_FLOAT: n = 5; break;
17343#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017344 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17345 }
17346 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347}
17348
17349/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017350 * "values(dict)" function
17351 */
17352 static void
17353f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 typval_T *argvars;
17355 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017356{
17357 dict_list(argvars, rettv, 1);
17358}
17359
17360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 * "virtcol(string)" function
17362 */
17363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 typval_T *argvars;
17366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367{
17368 colnr_T vcol = 0;
17369 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017370 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017372 fp = var2fpos(&argvars[0], FALSE, &fnum);
17373 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17374 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 {
17376 getvvcol(curwin, fp, NULL, NULL, &vcol);
17377 ++vcol;
17378 }
17379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381}
17382
17383/*
17384 * "visualmode()" function
17385 */
17386/*ARGSUSED*/
17387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017389 typval_T *argvars;
17390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391{
17392#ifdef FEAT_VISUAL
17393 char_u str[2];
17394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017395 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 str[0] = curbuf->b_visual_mode_eval;
17397 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017398 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
17400 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017401 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 curbuf->b_visual_mode_eval = NUL;
17403#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017404 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405#endif
17406}
17407
17408/*
17409 * "winbufnr(nr)" function
17410 */
17411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017413 typval_T *argvars;
17414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415{
17416 win_T *wp;
17417
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017418 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017422 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423}
17424
17425/*
17426 * "wincol()" function
17427 */
17428/*ARGSUSED*/
17429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017430f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017431 typval_T *argvars;
17432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433{
17434 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017435 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436}
17437
17438/*
17439 * "winheight(nr)" function
17440 */
17441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017442f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017443 typval_T *argvars;
17444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445{
17446 win_T *wp;
17447
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017448 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017450 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453}
17454
17455/*
17456 * "winline()" function
17457 */
17458/*ARGSUSED*/
17459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *argvars;
17462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463{
17464 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017465 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466}
17467
17468/*
17469 * "winnr()" function
17470 */
17471/* ARGSUSED */
17472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 typval_T *argvars;
17475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476{
17477 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017478
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017480 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017482 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483}
17484
17485/*
17486 * "winrestcmd()" function
17487 */
17488/* ARGSUSED */
17489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T *argvars;
17492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493{
17494#ifdef FEAT_WINDOWS
17495 win_T *wp;
17496 int winnr = 1;
17497 garray_T ga;
17498 char_u buf[50];
17499
17500 ga_init2(&ga, (int)sizeof(char), 70);
17501 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17502 {
17503 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17504 ga_concat(&ga, buf);
17505# ifdef FEAT_VERTSPLIT
17506 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17507 ga_concat(&ga, buf);
17508# endif
17509 ++winnr;
17510 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017511 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017513 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017517 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518}
17519
17520/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017521 * "winrestview()" function
17522 */
17523/* ARGSUSED */
17524 static void
17525f_winrestview(argvars, rettv)
17526 typval_T *argvars;
17527 typval_T *rettv;
17528{
17529 dict_T *dict;
17530
17531 if (argvars[0].v_type != VAR_DICT
17532 || (dict = argvars[0].vval.v_dict) == NULL)
17533 EMSG(_(e_invarg));
17534 else
17535 {
17536 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17537 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17538#ifdef FEAT_VIRTUALEDIT
17539 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17540#endif
17541 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017542 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017543
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017544 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017545#ifdef FEAT_DIFF
17546 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17547#endif
17548 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17549 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17550
17551 check_cursor();
17552 changed_cline_bef_curs();
17553 invalidate_botline();
17554 redraw_later(VALID);
17555
17556 if (curwin->w_topline == 0)
17557 curwin->w_topline = 1;
17558 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17559 curwin->w_topline = curbuf->b_ml.ml_line_count;
17560#ifdef FEAT_DIFF
17561 check_topfill(curwin, TRUE);
17562#endif
17563 }
17564}
17565
17566/*
17567 * "winsaveview()" function
17568 */
17569/* ARGSUSED */
17570 static void
17571f_winsaveview(argvars, rettv)
17572 typval_T *argvars;
17573 typval_T *rettv;
17574{
17575 dict_T *dict;
17576
17577 dict = dict_alloc();
17578 if (dict == NULL)
17579 return;
17580 rettv->v_type = VAR_DICT;
17581 rettv->vval.v_dict = dict;
17582 ++dict->dv_refcount;
17583
17584 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17585 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17586#ifdef FEAT_VIRTUALEDIT
17587 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17588#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017589 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017590 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17591
17592 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17593#ifdef FEAT_DIFF
17594 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17595#endif
17596 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17597 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17598}
17599
17600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 * "winwidth(nr)" function
17602 */
17603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017604f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017605 typval_T *argvars;
17606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607{
17608 win_T *wp;
17609
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017610 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 else
17614#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618#endif
17619}
17620
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017622 * "writefile()" function
17623 */
17624 static void
17625f_writefile(argvars, rettv)
17626 typval_T *argvars;
17627 typval_T *rettv;
17628{
17629 int binary = FALSE;
17630 char_u *fname;
17631 FILE *fd;
17632 listitem_T *li;
17633 char_u *s;
17634 int ret = 0;
17635 int c;
17636
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017637 if (check_restricted() || check_secure())
17638 return;
17639
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017640 if (argvars[0].v_type != VAR_LIST)
17641 {
17642 EMSG2(_(e_listarg), "writefile()");
17643 return;
17644 }
17645 if (argvars[0].vval.v_list == NULL)
17646 return;
17647
17648 if (argvars[2].v_type != VAR_UNKNOWN
17649 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17650 binary = TRUE;
17651
17652 /* Always open the file in binary mode, library functions have a mind of
17653 * their own about CR-LF conversion. */
17654 fname = get_tv_string(&argvars[1]);
17655 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17656 {
17657 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17658 ret = -1;
17659 }
17660 else
17661 {
17662 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17663 li = li->li_next)
17664 {
17665 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17666 {
17667 if (*s == '\n')
17668 c = putc(NUL, fd);
17669 else
17670 c = putc(*s, fd);
17671 if (c == EOF)
17672 {
17673 ret = -1;
17674 break;
17675 }
17676 }
17677 if (!binary || li->li_next != NULL)
17678 if (putc('\n', fd) == EOF)
17679 {
17680 ret = -1;
17681 break;
17682 }
17683 if (ret < 0)
17684 {
17685 EMSG(_(e_write));
17686 break;
17687 }
17688 }
17689 fclose(fd);
17690 }
17691
17692 rettv->vval.v_number = ret;
17693}
17694
17695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017697 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 */
17699 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017700var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017701 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017702 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017703 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017705 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017707 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708
Bram Moolenaara5525202006-03-02 22:52:09 +000017709 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017710 if (varp->v_type == VAR_LIST)
17711 {
17712 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017713 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017714 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017715 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017716
17717 l = varp->vval.v_list;
17718 if (l == NULL)
17719 return NULL;
17720
17721 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017722 pos.lnum = list_find_nr(l, 0L, &error);
17723 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017724 return NULL; /* invalid line number */
17725
17726 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017727 pos.col = list_find_nr(l, 1L, &error);
17728 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017729 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017730 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017731
17732 /* We accept "$" for the column number: last column. */
17733 li = list_find(l, 1L);
17734 if (li != NULL && li->li_tv.v_type == VAR_STRING
17735 && li->li_tv.vval.v_string != NULL
17736 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17737 pos.col = len + 1;
17738
Bram Moolenaara5525202006-03-02 22:52:09 +000017739 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017740 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017741 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017742 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017743
Bram Moolenaara5525202006-03-02 22:52:09 +000017744#ifdef FEAT_VIRTUALEDIT
17745 /* Get the virtual offset. Defaults to zero. */
17746 pos.coladd = list_find_nr(l, 2L, &error);
17747 if (error)
17748 pos.coladd = 0;
17749#endif
17750
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017751 return &pos;
17752 }
17753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017754 name = get_tv_string_chk(varp);
17755 if (name == NULL)
17756 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017757 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017759#ifdef FEAT_VISUAL
17760 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17761 {
17762 if (VIsual_active)
17763 return &VIsual;
17764 return &curwin->w_cursor;
17765 }
17766#endif
17767 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017769 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17771 return NULL;
17772 return pp;
17773 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017774
17775#ifdef FEAT_VIRTUALEDIT
17776 pos.coladd = 0;
17777#endif
17778
Bram Moolenaar477933c2007-07-17 14:32:23 +000017779 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017780 {
17781 pos.col = 0;
17782 if (name[1] == '0') /* "w0": first visible line */
17783 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017784 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017785 pos.lnum = curwin->w_topline;
17786 return &pos;
17787 }
17788 else if (name[1] == '$') /* "w$": last visible line */
17789 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017790 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017791 pos.lnum = curwin->w_botline - 1;
17792 return &pos;
17793 }
17794 }
17795 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017797 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 {
17799 pos.lnum = curbuf->b_ml.ml_line_count;
17800 pos.col = 0;
17801 }
17802 else
17803 {
17804 pos.lnum = curwin->w_cursor.lnum;
17805 pos.col = (colnr_T)STRLEN(ml_get_curline());
17806 }
17807 return &pos;
17808 }
17809 return NULL;
17810}
17811
17812/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017813 * Convert list in "arg" into a position and optional file number.
17814 * When "fnump" is NULL there is no file number, only 3 items.
17815 * Note that the column is passed on as-is, the caller may want to decrement
17816 * it to use 1 for the first column.
17817 * Return FAIL when conversion is not possible, doesn't check the position for
17818 * validity.
17819 */
17820 static int
17821list2fpos(arg, posp, fnump)
17822 typval_T *arg;
17823 pos_T *posp;
17824 int *fnump;
17825{
17826 list_T *l = arg->vval.v_list;
17827 long i = 0;
17828 long n;
17829
Bram Moolenaarbde35262006-07-23 20:12:24 +000017830 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17831 * when "fnump" isn't NULL and "coladd" is optional. */
17832 if (arg->v_type != VAR_LIST
17833 || l == NULL
17834 || l->lv_len < (fnump == NULL ? 2 : 3)
17835 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017836 return FAIL;
17837
17838 if (fnump != NULL)
17839 {
17840 n = list_find_nr(l, i++, NULL); /* fnum */
17841 if (n < 0)
17842 return FAIL;
17843 if (n == 0)
17844 n = curbuf->b_fnum; /* current buffer */
17845 *fnump = n;
17846 }
17847
17848 n = list_find_nr(l, i++, NULL); /* lnum */
17849 if (n < 0)
17850 return FAIL;
17851 posp->lnum = n;
17852
17853 n = list_find_nr(l, i++, NULL); /* col */
17854 if (n < 0)
17855 return FAIL;
17856 posp->col = n;
17857
17858#ifdef FEAT_VIRTUALEDIT
17859 n = list_find_nr(l, i, NULL);
17860 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017861 posp->coladd = 0;
17862 else
17863 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017864#endif
17865
17866 return OK;
17867}
17868
17869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 * Get the length of an environment variable name.
17871 * Advance "arg" to the first character after the name.
17872 * Return 0 for error.
17873 */
17874 static int
17875get_env_len(arg)
17876 char_u **arg;
17877{
17878 char_u *p;
17879 int len;
17880
17881 for (p = *arg; vim_isIDc(*p); ++p)
17882 ;
17883 if (p == *arg) /* no name found */
17884 return 0;
17885
17886 len = (int)(p - *arg);
17887 *arg = p;
17888 return len;
17889}
17890
17891/*
17892 * Get the length of the name of a function or internal variable.
17893 * "arg" is advanced to the first non-white character after the name.
17894 * Return 0 if something is wrong.
17895 */
17896 static int
17897get_id_len(arg)
17898 char_u **arg;
17899{
17900 char_u *p;
17901 int len;
17902
17903 /* Find the end of the name. */
17904 for (p = *arg; eval_isnamec(*p); ++p)
17905 ;
17906 if (p == *arg) /* no name found */
17907 return 0;
17908
17909 len = (int)(p - *arg);
17910 *arg = skipwhite(p);
17911
17912 return len;
17913}
17914
17915/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017916 * Get the length of the name of a variable or function.
17917 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017919 * Return -1 if curly braces expansion failed.
17920 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 * If the name contains 'magic' {}'s, expand them and return the
17922 * expanded name in an allocated string via 'alias' - caller must free.
17923 */
17924 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017925get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 char_u **arg;
17927 char_u **alias;
17928 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017929 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930{
17931 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 char_u *p;
17933 char_u *expr_start;
17934 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935
17936 *alias = NULL; /* default to no alias */
17937
17938 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17939 && (*arg)[2] == (int)KE_SNR)
17940 {
17941 /* hard coded <SNR>, already translated */
17942 *arg += 3;
17943 return get_id_len(arg) + 3;
17944 }
17945 len = eval_fname_script(*arg);
17946 if (len > 0)
17947 {
17948 /* literal "<SID>", "s:" or "<SNR>" */
17949 *arg += len;
17950 }
17951
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017953 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017955 p = find_name_end(*arg, &expr_start, &expr_end,
17956 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 if (expr_start != NULL)
17958 {
17959 char_u *temp_string;
17960
17961 if (!evaluate)
17962 {
17963 len += (int)(p - *arg);
17964 *arg = skipwhite(p);
17965 return len;
17966 }
17967
17968 /*
17969 * Include any <SID> etc in the expanded string:
17970 * Thus the -len here.
17971 */
17972 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17973 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017974 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 *alias = temp_string;
17976 *arg = skipwhite(p);
17977 return (int)STRLEN(temp_string);
17978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979
17980 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017981 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 EMSG2(_(e_invexpr2), *arg);
17983
17984 return len;
17985}
17986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987/*
17988 * Find the end of a variable or function name, taking care of magic braces.
17989 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17990 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017991 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 * Return a pointer to just after the name. Equal to "arg" if there is no
17993 * valid name.
17994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017996find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 char_u *arg;
17998 char_u **expr_start;
17999 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018000 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018002 int mb_nest = 0;
18003 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 char_u *p;
18005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018006 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008 *expr_start = NULL;
18009 *expr_end = NULL;
18010 }
18011
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018012 /* Quick check for valid starting character. */
18013 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18014 return arg;
18015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018016 for (p = arg; *p != NUL
18017 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018018 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018019 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018020 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018021 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018022 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018023 if (*p == '\'')
18024 {
18025 /* skip over 'string' to avoid counting [ and ] inside it. */
18026 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18027 ;
18028 if (*p == NUL)
18029 break;
18030 }
18031 else if (*p == '"')
18032 {
18033 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18034 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18035 if (*p == '\\' && p[1] != NUL)
18036 ++p;
18037 if (*p == NUL)
18038 break;
18039 }
18040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018041 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018043 if (*p == '[')
18044 ++br_nest;
18045 else if (*p == ']')
18046 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 if (*p == '{')
18052 {
18053 mb_nest++;
18054 if (expr_start != NULL && *expr_start == NULL)
18055 *expr_start = p;
18056 }
18057 else if (*p == '}')
18058 {
18059 mb_nest--;
18060 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18061 *expr_end = p;
18062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 }
18065
18066 return p;
18067}
18068
18069/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018070 * Expands out the 'magic' {}'s in a variable/function name.
18071 * Note that this can call itself recursively, to deal with
18072 * constructs like foo{bar}{baz}{bam}
18073 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18074 * "in_start" ^
18075 * "expr_start" ^
18076 * "expr_end" ^
18077 * "in_end" ^
18078 *
18079 * Returns a new allocated string, which the caller must free.
18080 * Returns NULL for failure.
18081 */
18082 static char_u *
18083make_expanded_name(in_start, expr_start, expr_end, in_end)
18084 char_u *in_start;
18085 char_u *expr_start;
18086 char_u *expr_end;
18087 char_u *in_end;
18088{
18089 char_u c1;
18090 char_u *retval = NULL;
18091 char_u *temp_result;
18092 char_u *nextcmd = NULL;
18093
18094 if (expr_end == NULL || in_end == NULL)
18095 return NULL;
18096 *expr_start = NUL;
18097 *expr_end = NUL;
18098 c1 = *in_end;
18099 *in_end = NUL;
18100
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018101 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018102 if (temp_result != NULL && nextcmd == NULL)
18103 {
18104 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18105 + (in_end - expr_end) + 1));
18106 if (retval != NULL)
18107 {
18108 STRCPY(retval, in_start);
18109 STRCAT(retval, temp_result);
18110 STRCAT(retval, expr_end + 1);
18111 }
18112 }
18113 vim_free(temp_result);
18114
18115 *in_end = c1; /* put char back for error messages */
18116 *expr_start = '{';
18117 *expr_end = '}';
18118
18119 if (retval != NULL)
18120 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018121 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018122 if (expr_start != NULL)
18123 {
18124 /* Further expansion! */
18125 temp_result = make_expanded_name(retval, expr_start,
18126 expr_end, temp_result);
18127 vim_free(retval);
18128 retval = temp_result;
18129 }
18130 }
18131
18132 return retval;
18133}
18134
18135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018137 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 */
18139 static int
18140eval_isnamec(c)
18141 int c;
18142{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018143 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18144}
18145
18146/*
18147 * Return TRUE if character "c" can be used as the first character in a
18148 * variable or function name (excluding '{' and '}').
18149 */
18150 static int
18151eval_isnamec1(c)
18152 int c;
18153{
18154 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155}
18156
18157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 * Set number v: variable to "val".
18159 */
18160 void
18161set_vim_var_nr(idx, val)
18162 int idx;
18163 long val;
18164{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018165 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166}
18167
18168/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018169 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 */
18171 long
18172get_vim_var_nr(idx)
18173 int idx;
18174{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018175 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176}
18177
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018178/*
18179 * Get string v: variable value. Uses a static buffer, can only be used once.
18180 */
18181 char_u *
18182get_vim_var_str(idx)
18183 int idx;
18184{
18185 return get_tv_string(&vimvars[idx].vv_tv);
18186}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018187
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018189 * Get List v: variable value. Caller must take care of reference count when
18190 * needed.
18191 */
18192 list_T *
18193get_vim_var_list(idx)
18194 int idx;
18195{
18196 return vimvars[idx].vv_list;
18197}
18198
18199/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018200 * Set v:count to "count" and v:count1 to "count1".
18201 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 */
18203 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018204set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 long count;
18206 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018207 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018209 if (set_prevcount)
18210 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018211 vimvars[VV_COUNT].vv_nr = count;
18212 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213}
18214
18215/*
18216 * Set string v: variable to a copy of "val".
18217 */
18218 void
18219set_vim_var_string(idx, val, len)
18220 int idx;
18221 char_u *val;
18222 int len; /* length of "val" to use or -1 (whole string) */
18223{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018224 /* Need to do this (at least) once, since we can't initialize a union.
18225 * Will always be invoked when "v:progname" is set. */
18226 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18227
Bram Moolenaare9a41262005-01-15 22:18:47 +000018228 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018230 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018232 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018234 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235}
18236
18237/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018238 * Set List v: variable to "val".
18239 */
18240 void
18241set_vim_var_list(idx, val)
18242 int idx;
18243 list_T *val;
18244{
18245 list_unref(vimvars[idx].vv_list);
18246 vimvars[idx].vv_list = val;
18247 if (val != NULL)
18248 ++val->lv_refcount;
18249}
18250
18251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 * Set v:register if needed.
18253 */
18254 void
18255set_reg_var(c)
18256 int c;
18257{
18258 char_u regname;
18259
18260 if (c == 0 || c == ' ')
18261 regname = '"';
18262 else
18263 regname = c;
18264 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018265 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 set_vim_var_string(VV_REG, &regname, 1);
18267}
18268
18269/*
18270 * Get or set v:exception. If "oldval" == NULL, return the current value.
18271 * Otherwise, restore the value to "oldval" and return NULL.
18272 * Must always be called in pairs to save and restore v:exception! Does not
18273 * take care of memory allocations.
18274 */
18275 char_u *
18276v_exception(oldval)
18277 char_u *oldval;
18278{
18279 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018280 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281
Bram Moolenaare9a41262005-01-15 22:18:47 +000018282 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 return NULL;
18284}
18285
18286/*
18287 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18288 * Otherwise, restore the value to "oldval" and return NULL.
18289 * Must always be called in pairs to save and restore v:throwpoint! Does not
18290 * take care of memory allocations.
18291 */
18292 char_u *
18293v_throwpoint(oldval)
18294 char_u *oldval;
18295{
18296 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018297 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298
Bram Moolenaare9a41262005-01-15 22:18:47 +000018299 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 return NULL;
18301}
18302
18303#if defined(FEAT_AUTOCMD) || defined(PROTO)
18304/*
18305 * Set v:cmdarg.
18306 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18307 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18308 * Must always be called in pairs!
18309 */
18310 char_u *
18311set_cmdarg(eap, oldarg)
18312 exarg_T *eap;
18313 char_u *oldarg;
18314{
18315 char_u *oldval;
18316 char_u *newval;
18317 unsigned len;
18318
Bram Moolenaare9a41262005-01-15 22:18:47 +000018319 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018320 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018322 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018323 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018324 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 }
18326
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018327 if (eap->force_bin == FORCE_BIN)
18328 len = 6;
18329 else if (eap->force_bin == FORCE_NOBIN)
18330 len = 8;
18331 else
18332 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018333
18334 if (eap->read_edit)
18335 len += 7;
18336
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018337 if (eap->force_ff != 0)
18338 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18339# ifdef FEAT_MBYTE
18340 if (eap->force_enc != 0)
18341 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018342 if (eap->bad_char != 0)
18343 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018344# endif
18345
18346 newval = alloc(len + 1);
18347 if (newval == NULL)
18348 return NULL;
18349
18350 if (eap->force_bin == FORCE_BIN)
18351 sprintf((char *)newval, " ++bin");
18352 else if (eap->force_bin == FORCE_NOBIN)
18353 sprintf((char *)newval, " ++nobin");
18354 else
18355 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018356
18357 if (eap->read_edit)
18358 STRCAT(newval, " ++edit");
18359
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018360 if (eap->force_ff != 0)
18361 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18362 eap->cmd + eap->force_ff);
18363# ifdef FEAT_MBYTE
18364 if (eap->force_enc != 0)
18365 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18366 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018367 if (eap->bad_char != 0)
18368 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18369 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018370# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018371 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018372 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373}
18374#endif
18375
18376/*
18377 * Get the value of internal variable "name".
18378 * Return OK or FAIL.
18379 */
18380 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018381get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 char_u *name;
18383 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018384 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018385 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
18387 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018388 typval_T *tv = NULL;
18389 typval_T atv;
18390 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392
18393 /* truncate the name, so that we can use strcmp() */
18394 cc = name[len];
18395 name[len] = NUL;
18396
18397 /*
18398 * Check for "b:changedtick".
18399 */
18400 if (STRCMP(name, "b:changedtick") == 0)
18401 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018402 atv.v_type = VAR_NUMBER;
18403 atv.vval.v_number = curbuf->b_changedtick;
18404 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 }
18406
18407 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 * Check for user-defined variables.
18409 */
18410 else
18411 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018412 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018414 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 }
18416
Bram Moolenaare9a41262005-01-15 22:18:47 +000018417 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018419 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 ret = FAIL;
18422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018424 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425
18426 name[len] = cc;
18427
18428 return ret;
18429}
18430
18431/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018432 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18433 * Also handle function call with Funcref variable: func(expr)
18434 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18435 */
18436 static int
18437handle_subscript(arg, rettv, evaluate, verbose)
18438 char_u **arg;
18439 typval_T *rettv;
18440 int evaluate; /* do more than finding the end */
18441 int verbose; /* give error messages */
18442{
18443 int ret = OK;
18444 dict_T *selfdict = NULL;
18445 char_u *s;
18446 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018447 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018448
18449 while (ret == OK
18450 && (**arg == '['
18451 || (**arg == '.' && rettv->v_type == VAR_DICT)
18452 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18453 && !vim_iswhite(*(*arg - 1)))
18454 {
18455 if (**arg == '(')
18456 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018457 /* need to copy the funcref so that we can clear rettv */
18458 functv = *rettv;
18459 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018460
18461 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018462 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018463 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018464 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18465 &len, evaluate, selfdict);
18466
18467 /* Clear the funcref afterwards, so that deleting it while
18468 * evaluating the arguments is possible (see test55). */
18469 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018470
18471 /* Stop the expression evaluation when immediately aborting on
18472 * error, or when an interrupt occurred or an exception was thrown
18473 * but not caught. */
18474 if (aborting())
18475 {
18476 if (ret == OK)
18477 clear_tv(rettv);
18478 ret = FAIL;
18479 }
18480 dict_unref(selfdict);
18481 selfdict = NULL;
18482 }
18483 else /* **arg == '[' || **arg == '.' */
18484 {
18485 dict_unref(selfdict);
18486 if (rettv->v_type == VAR_DICT)
18487 {
18488 selfdict = rettv->vval.v_dict;
18489 if (selfdict != NULL)
18490 ++selfdict->dv_refcount;
18491 }
18492 else
18493 selfdict = NULL;
18494 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18495 {
18496 clear_tv(rettv);
18497 ret = FAIL;
18498 }
18499 }
18500 }
18501 dict_unref(selfdict);
18502 return ret;
18503}
18504
18505/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018506 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018507 * value).
18508 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018509 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018510alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018511{
Bram Moolenaar33570922005-01-25 22:26:29 +000018512 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018513}
18514
18515/*
18516 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 * The string "s" must have been allocated, it is consumed.
18518 * Return NULL for out of memory, the variable otherwise.
18519 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018520 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 char_u *s;
18523{
Bram Moolenaar33570922005-01-25 22:26:29 +000018524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018526 rettv = alloc_tv();
18527 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529 rettv->v_type = VAR_STRING;
18530 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 }
18532 else
18533 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018534 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535}
18536
18537/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018538 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018540 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018542 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543{
18544 if (varp != NULL)
18545 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018546 switch (varp->v_type)
18547 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018548 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018549 func_unref(varp->vval.v_string);
18550 /*FALLTHROUGH*/
18551 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018552 vim_free(varp->vval.v_string);
18553 break;
18554 case VAR_LIST:
18555 list_unref(varp->vval.v_list);
18556 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018557 case VAR_DICT:
18558 dict_unref(varp->vval.v_dict);
18559 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018560 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018561#ifdef FEAT_FLOAT
18562 case VAR_FLOAT:
18563#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018564 case VAR_UNKNOWN:
18565 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018566 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018567 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018568 break;
18569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 vim_free(varp);
18571 }
18572}
18573
18574/*
18575 * Free the memory for a variable value and set the value to NULL or 0.
18576 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018577 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018579 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
18581 if (varp != NULL)
18582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018583 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018585 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018586 func_unref(varp->vval.v_string);
18587 /*FALLTHROUGH*/
18588 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018589 vim_free(varp->vval.v_string);
18590 varp->vval.v_string = NULL;
18591 break;
18592 case VAR_LIST:
18593 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018594 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018595 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018596 case VAR_DICT:
18597 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018598 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018599 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018600 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018601 varp->vval.v_number = 0;
18602 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018603#ifdef FEAT_FLOAT
18604 case VAR_FLOAT:
18605 varp->vval.v_float = 0.0;
18606 break;
18607#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608 case VAR_UNKNOWN:
18609 break;
18610 default:
18611 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018613 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 }
18615}
18616
18617/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618 * Set the value of a variable to NULL without freeing items.
18619 */
18620 static void
18621init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018622 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623{
18624 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018625 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626}
18627
18628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 * Get the number value of a variable.
18630 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 * For incompatible types, return 0.
18632 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18633 * caller of incompatible types: it sets *denote to TRUE if "denote"
18634 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 */
18636 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018638 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640 int error = FALSE;
18641
18642 return get_tv_number_chk(varp, &error); /* return 0L on error */
18643}
18644
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018645 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018646get_tv_number_chk(varp, denote)
18647 typval_T *varp;
18648 int *denote;
18649{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018650 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018652 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018654 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018655 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018656#ifdef FEAT_FLOAT
18657 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018658 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018659 break;
18660#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018661 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018662 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018663 break;
18664 case VAR_STRING:
18665 if (varp->vval.v_string != NULL)
18666 vim_str2nr(varp->vval.v_string, NULL, NULL,
18667 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018668 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018669 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018670 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018671 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018672 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018673 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018674 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018675 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018676 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018677 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018679 if (denote == NULL) /* useful for values that must be unsigned */
18680 n = -1;
18681 else
18682 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018683 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684}
18685
18686/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018687 * Get the lnum from the first argument.
18688 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018689 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 */
18691 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694{
Bram Moolenaar33570922005-01-25 22:26:29 +000018695 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 linenr_T lnum;
18697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018698 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 if (lnum == 0) /* no valid number, try using line() */
18700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 rettv.v_type = VAR_NUMBER;
18702 f_line(argvars, &rettv);
18703 lnum = rettv.vval.v_number;
18704 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 }
18706 return lnum;
18707}
18708
18709/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018710 * Get the lnum from the first argument.
18711 * Also accepts "$", then "buf" is used.
18712 * Returns 0 on error.
18713 */
18714 static linenr_T
18715get_tv_lnum_buf(argvars, buf)
18716 typval_T *argvars;
18717 buf_T *buf;
18718{
18719 if (argvars[0].v_type == VAR_STRING
18720 && argvars[0].vval.v_string != NULL
18721 && argvars[0].vval.v_string[0] == '$'
18722 && buf != NULL)
18723 return buf->b_ml.ml_line_count;
18724 return get_tv_number_chk(&argvars[0], NULL);
18725}
18726
18727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 * Get the string value of a variable.
18729 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018730 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18731 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 * If the String variable has never been set, return an empty string.
18733 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018734 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18735 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 */
18737 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018739 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740{
18741 static char_u mybuf[NUMBUFLEN];
18742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744}
18745
18746 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018748 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018749 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018751 char_u *res = get_tv_string_buf_chk(varp, buf);
18752
18753 return res != NULL ? res : (char_u *)"";
18754}
18755
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018756 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018757get_tv_string_chk(varp)
18758 typval_T *varp;
18759{
18760 static char_u mybuf[NUMBUFLEN];
18761
18762 return get_tv_string_buf_chk(varp, mybuf);
18763}
18764
18765 static char_u *
18766get_tv_string_buf_chk(varp, buf)
18767 typval_T *varp;
18768 char_u *buf;
18769{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018770 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018772 case VAR_NUMBER:
18773 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18774 return buf;
18775 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018776 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018777 break;
18778 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018779 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018780 break;
18781 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018782 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018783 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018784#ifdef FEAT_FLOAT
18785 case VAR_FLOAT:
18786 EMSG(_("E806: using Float as a String"));
18787 break;
18788#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018789 case VAR_STRING:
18790 if (varp->vval.v_string != NULL)
18791 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018792 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018793 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018794 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018797 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798}
18799
18800/*
18801 * Find variable "name" in the list of variables.
18802 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018803 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018804 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018807 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018808find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018813 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814
Bram Moolenaara7043832005-01-21 11:56:39 +000018815 ht = find_var_ht(name, &varname);
18816 if (htp != NULL)
18817 *htp = ht;
18818 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018820 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821}
18822
18823/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018824 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018825 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018828find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018829 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018830 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018831 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018832{
Bram Moolenaar33570922005-01-25 22:26:29 +000018833 hashitem_T *hi;
18834
18835 if (*varname == NUL)
18836 {
18837 /* Must be something like "s:", otherwise "ht" would be NULL. */
18838 switch (varname[-2])
18839 {
18840 case 's': return &SCRIPT_SV(current_SID).sv_var;
18841 case 'g': return &globvars_var;
18842 case 'v': return &vimvars_var;
18843 case 'b': return &curbuf->b_bufvar;
18844 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018845#ifdef FEAT_WINDOWS
18846 case 't': return &curtab->tp_winvar;
18847#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018848 case 'l': return current_funccal == NULL
18849 ? NULL : &current_funccal->l_vars_var;
18850 case 'a': return current_funccal == NULL
18851 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 }
18853 return NULL;
18854 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018855
18856 hi = hash_find(ht, varname);
18857 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018858 {
18859 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018860 * worked find the variable again. Don't auto-load a script if it was
18861 * loaded already, otherwise it would be loaded every time when
18862 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018863 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018864 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018865 hi = hash_find(ht, varname);
18866 if (HASHITEM_EMPTY(hi))
18867 return NULL;
18868 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018870}
18871
18872/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018874 * Set "varname" to the start of name without ':'.
18875 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018876 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018877find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 char_u *name;
18879 char_u **varname;
18880{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018881 hashitem_T *hi;
18882
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 if (name[1] != ':')
18884 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018885 /* The name must not start with a colon or #. */
18886 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 return NULL;
18888 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018889
18890 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018891 hi = hash_find(&compat_hashtab, name);
18892 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018893 return &compat_hashtab;
18894
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018896 return &globvarht; /* global variable */
18897 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 }
18899 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018900 if (*name == 'g') /* global variable */
18901 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018902 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18903 */
18904 if (vim_strchr(name + 2, ':') != NULL
18905 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018906 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018908 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018911#ifdef FEAT_WINDOWS
18912 if (*name == 't') /* tab page variable */
18913 return &curtab->tp_vars.dv_hashtab;
18914#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 if (*name == 'v') /* v: variable */
18916 return &vimvarht;
18917 if (*name == 'a' && current_funccal != NULL) /* function argument */
18918 return &current_funccal->l_avars.dv_hashtab;
18919 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18920 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 if (*name == 's' /* script variable */
18922 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18923 return &SCRIPT_VARS(current_SID);
18924 return NULL;
18925}
18926
18927/*
18928 * Get the string value of a (global/local) variable.
18929 * Returns NULL when it doesn't exist.
18930 */
18931 char_u *
18932get_var_value(name)
18933 char_u *name;
18934{
Bram Moolenaar33570922005-01-25 22:26:29 +000018935 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936
Bram Moolenaara7043832005-01-21 11:56:39 +000018937 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 if (v == NULL)
18939 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018940 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941}
18942
18943/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 * sourcing this script and when executing functions defined in the script.
18946 */
18947 void
18948new_script_vars(id)
18949 scid_T id;
18950{
Bram Moolenaara7043832005-01-21 11:56:39 +000018951 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018952 hashtab_T *ht;
18953 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018954
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18956 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018957 /* Re-allocating ga_data means that an ht_array pointing to
18958 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018959 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018960 for (i = 1; i <= ga_scripts.ga_len; ++i)
18961 {
18962 ht = &SCRIPT_VARS(i);
18963 if (ht->ht_mask == HT_INIT_SIZE - 1)
18964 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018965 sv = &SCRIPT_SV(i);
18966 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018967 }
18968
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 while (ga_scripts.ga_len < id)
18970 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18972 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 }
18975 }
18976}
18977
18978/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18980 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 */
18982 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018983init_var_dict(dict, dict_var)
18984 dict_T *dict;
18985 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986{
Bram Moolenaar33570922005-01-25 22:26:29 +000018987 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018988 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 dict_var->di_tv.vval.v_dict = dict;
18990 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018991 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018992 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18993 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994}
18995
18996/*
18997 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018998 * Frees all allocated variables and the value they contain.
18999 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 */
19001 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019002vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 hashtab_T *ht;
19004{
19005 vars_clear_ext(ht, TRUE);
19006}
19007
19008/*
19009 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19010 */
19011 static void
19012vars_clear_ext(ht, free_val)
19013 hashtab_T *ht;
19014 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
Bram Moolenaara7043832005-01-21 11:56:39 +000019016 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 hashitem_T *hi;
19018 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019
Bram Moolenaar33570922005-01-25 22:26:29 +000019020 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019021 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019022 for (hi = ht->ht_array; todo > 0; ++hi)
19023 {
19024 if (!HASHITEM_EMPTY(hi))
19025 {
19026 --todo;
19027
Bram Moolenaar33570922005-01-25 22:26:29 +000019028 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019029 * ht_array might change then. hash_clear() takes care of it
19030 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019031 v = HI2DI(hi);
19032 if (free_val)
19033 clear_tv(&v->di_tv);
19034 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19035 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019036 }
19037 }
19038 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019039 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040}
19041
Bram Moolenaara7043832005-01-21 11:56:39 +000019042/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019043 * Delete a variable from hashtab "ht" at item "hi".
19044 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019047delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019048 hashtab_T *ht;
19049 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050{
Bram Moolenaar33570922005-01-25 22:26:29 +000019051 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019052
19053 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019054 clear_tv(&di->di_tv);
19055 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056}
19057
19058/*
19059 * List the value of one internal variable.
19060 */
19061 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019062list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019065 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019067 char_u *tofree;
19068 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019069 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019070
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019071 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019073 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019074 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075}
19076
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019078list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 char_u *prefix;
19080 char_u *name;
19081 int type;
19082 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019083 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084{
Bram Moolenaar31859182007-08-14 20:41:13 +000019085 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19086 msg_start();
19087 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 if (name != NULL) /* "a:" vars don't have a name stored */
19089 msg_puts(name);
19090 msg_putchar(' ');
19091 msg_advance(22);
19092 if (type == VAR_NUMBER)
19093 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019094 else if (type == VAR_FUNC)
19095 msg_putchar('*');
19096 else if (type == VAR_LIST)
19097 {
19098 msg_putchar('[');
19099 if (*string == '[')
19100 ++string;
19101 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019102 else if (type == VAR_DICT)
19103 {
19104 msg_putchar('{');
19105 if (*string == '{')
19106 ++string;
19107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 else
19109 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019110
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019112
19113 if (type == VAR_FUNC)
19114 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019115 if (*first)
19116 {
19117 msg_clr_eos();
19118 *first = FALSE;
19119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120}
19121
19122/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 * If the variable already exists, the value is updated.
19125 * Otherwise the variable is created.
19126 */
19127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019128set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019130 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019131 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132{
Bram Moolenaar33570922005-01-25 22:26:29 +000019133 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019135 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019136 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019138 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019139 {
19140 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19141 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19142 ? name[2] : name[0]))
19143 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019144 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019145 return;
19146 }
19147 if (function_exists(name))
19148 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019149 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019150 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019151 return;
19152 }
19153 }
19154
Bram Moolenaara7043832005-01-21 11:56:39 +000019155 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019156 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019157 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019158 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019159 return;
19160 }
19161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019162 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019163 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019165 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019166 if (var_check_ro(v->di_flags, name)
19167 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019168 return;
19169 if (v->di_tv.v_type != tv->v_type
19170 && !((v->di_tv.v_type == VAR_STRING
19171 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019173 || tv->v_type == VAR_NUMBER))
19174#ifdef FEAT_FLOAT
19175 && !((v->di_tv.v_type == VAR_NUMBER
19176 || v->di_tv.v_type == VAR_FLOAT)
19177 && (tv->v_type == VAR_NUMBER
19178 || tv->v_type == VAR_FLOAT))
19179#endif
19180 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019181 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019182 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019183 return;
19184 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019185
19186 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019187 * Handle setting internal v: variables separately: we don't change
19188 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 */
19190 if (ht == &vimvarht)
19191 {
19192 if (v->di_tv.v_type == VAR_STRING)
19193 {
19194 vim_free(v->di_tv.vval.v_string);
19195 if (copy || tv->v_type != VAR_STRING)
19196 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19197 else
19198 {
19199 /* Take over the string to avoid an extra alloc/free. */
19200 v->di_tv.vval.v_string = tv->vval.v_string;
19201 tv->vval.v_string = NULL;
19202 }
19203 }
19204 else if (v->di_tv.v_type != VAR_NUMBER)
19205 EMSG2(_(e_intern2), "set_var()");
19206 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019207 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019209 if (STRCMP(varname, "searchforward") == 0)
19210 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19211 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019212 return;
19213 }
19214
19215 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 }
19217 else /* add a new variable */
19218 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019219 /* Can't add "v:" variable. */
19220 if (ht == &vimvarht)
19221 {
19222 EMSG2(_(e_illvar), name);
19223 return;
19224 }
19225
Bram Moolenaar92124a32005-06-17 22:03:40 +000019226 /* Make sure the variable name is valid. */
19227 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019228 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19229 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019230 {
19231 EMSG2(_(e_illvar), varname);
19232 return;
19233 }
19234
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019235 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19236 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019237 if (v == NULL)
19238 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019240 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019242 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 return;
19244 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019245 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019247
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019248 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019249 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019250 else
19251 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019253 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256}
19257
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019258/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019259 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019260 * Also give an error message.
19261 */
19262 static int
19263var_check_ro(flags, name)
19264 int flags;
19265 char_u *name;
19266{
19267 if (flags & DI_FLAGS_RO)
19268 {
19269 EMSG2(_(e_readonlyvar), name);
19270 return TRUE;
19271 }
19272 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19273 {
19274 EMSG2(_(e_readonlysbx), name);
19275 return TRUE;
19276 }
19277 return FALSE;
19278}
19279
19280/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019281 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19282 * Also give an error message.
19283 */
19284 static int
19285var_check_fixed(flags, name)
19286 int flags;
19287 char_u *name;
19288{
19289 if (flags & DI_FLAGS_FIX)
19290 {
19291 EMSG2(_("E795: Cannot delete variable %s"), name);
19292 return TRUE;
19293 }
19294 return FALSE;
19295}
19296
19297/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019298 * Return TRUE if typeval "tv" is set to be locked (immutable).
19299 * Also give an error message, using "name".
19300 */
19301 static int
19302tv_check_lock(lock, name)
19303 int lock;
19304 char_u *name;
19305{
19306 if (lock & VAR_LOCKED)
19307 {
19308 EMSG2(_("E741: Value is locked: %s"),
19309 name == NULL ? (char_u *)_("Unknown") : name);
19310 return TRUE;
19311 }
19312 if (lock & VAR_FIXED)
19313 {
19314 EMSG2(_("E742: Cannot change value of %s"),
19315 name == NULL ? (char_u *)_("Unknown") : name);
19316 return TRUE;
19317 }
19318 return FALSE;
19319}
19320
19321/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019322 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019323 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019324 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019325 * It is OK for "from" and "to" to point to the same item. This is used to
19326 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019329copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019330 typval_T *from;
19331 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019333 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019334 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019335 switch (from->v_type)
19336 {
19337 case VAR_NUMBER:
19338 to->vval.v_number = from->vval.v_number;
19339 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019340#ifdef FEAT_FLOAT
19341 case VAR_FLOAT:
19342 to->vval.v_float = from->vval.v_float;
19343 break;
19344#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019345 case VAR_STRING:
19346 case VAR_FUNC:
19347 if (from->vval.v_string == NULL)
19348 to->vval.v_string = NULL;
19349 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019351 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019352 if (from->v_type == VAR_FUNC)
19353 func_ref(to->vval.v_string);
19354 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019355 break;
19356 case VAR_LIST:
19357 if (from->vval.v_list == NULL)
19358 to->vval.v_list = NULL;
19359 else
19360 {
19361 to->vval.v_list = from->vval.v_list;
19362 ++to->vval.v_list->lv_refcount;
19363 }
19364 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019365 case VAR_DICT:
19366 if (from->vval.v_dict == NULL)
19367 to->vval.v_dict = NULL;
19368 else
19369 {
19370 to->vval.v_dict = from->vval.v_dict;
19371 ++to->vval.v_dict->dv_refcount;
19372 }
19373 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019374 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019375 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019376 break;
19377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378}
19379
19380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019381 * Make a copy of an item.
19382 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019383 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19384 * reference to an already copied list/dict can be used.
19385 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019386 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019387 static int
19388item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 typval_T *from;
19390 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019391 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019392 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019393{
19394 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019395 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019396
Bram Moolenaar33570922005-01-25 22:26:29 +000019397 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019398 {
19399 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019400 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019401 }
19402 ++recurse;
19403
19404 switch (from->v_type)
19405 {
19406 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019407#ifdef FEAT_FLOAT
19408 case VAR_FLOAT:
19409#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019410 case VAR_STRING:
19411 case VAR_FUNC:
19412 copy_tv(from, to);
19413 break;
19414 case VAR_LIST:
19415 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019416 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019417 if (from->vval.v_list == NULL)
19418 to->vval.v_list = NULL;
19419 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19420 {
19421 /* use the copy made earlier */
19422 to->vval.v_list = from->vval.v_list->lv_copylist;
19423 ++to->vval.v_list->lv_refcount;
19424 }
19425 else
19426 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19427 if (to->vval.v_list == NULL)
19428 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019429 break;
19430 case VAR_DICT:
19431 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019432 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019433 if (from->vval.v_dict == NULL)
19434 to->vval.v_dict = NULL;
19435 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19436 {
19437 /* use the copy made earlier */
19438 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19439 ++to->vval.v_dict->dv_refcount;
19440 }
19441 else
19442 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19443 if (to->vval.v_dict == NULL)
19444 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019445 break;
19446 default:
19447 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019448 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019449 }
19450 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019451 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019452}
19453
19454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 * ":echo expr1 ..." print each argument separated with a space, add a
19456 * newline at the end.
19457 * ":echon expr1 ..." print each argument plain.
19458 */
19459 void
19460ex_echo(eap)
19461 exarg_T *eap;
19462{
19463 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019464 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019465 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 char_u *p;
19467 int needclr = TRUE;
19468 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019469 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470
19471 if (eap->skip)
19472 ++emsg_skip;
19473 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19474 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019475 /* If eval1() causes an error message the text from the command may
19476 * still need to be cleared. E.g., "echo 22,44". */
19477 need_clr_eos = needclr;
19478
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 {
19482 /*
19483 * Report the invalid expression unless the expression evaluation
19484 * has been cancelled due to an aborting error, an interrupt, or an
19485 * exception.
19486 */
19487 if (!aborting())
19488 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019489 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 break;
19491 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019492 need_clr_eos = FALSE;
19493
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 if (!eap->skip)
19495 {
19496 if (atstart)
19497 {
19498 atstart = FALSE;
19499 /* Call msg_start() after eval1(), evaluating the expression
19500 * may cause a message to appear. */
19501 if (eap->cmdidx == CMD_echo)
19502 msg_start();
19503 }
19504 else if (eap->cmdidx == CMD_echo)
19505 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019506 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019507 if (p != NULL)
19508 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019510 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019512 if (*p != TAB && needclr)
19513 {
19514 /* remove any text still there from the command */
19515 msg_clr_eos();
19516 needclr = FALSE;
19517 }
19518 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 }
19520 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019521 {
19522#ifdef FEAT_MBYTE
19523 if (has_mbyte)
19524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019525 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019526
19527 (void)msg_outtrans_len_attr(p, i, echo_attr);
19528 p += i - 1;
19529 }
19530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019532 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019535 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 arg = skipwhite(arg);
19539 }
19540 eap->nextcmd = check_nextcmd(arg);
19541
19542 if (eap->skip)
19543 --emsg_skip;
19544 else
19545 {
19546 /* remove text that may still be there from the command */
19547 if (needclr)
19548 msg_clr_eos();
19549 if (eap->cmdidx == CMD_echo)
19550 msg_end();
19551 }
19552}
19553
19554/*
19555 * ":echohl {name}".
19556 */
19557 void
19558ex_echohl(eap)
19559 exarg_T *eap;
19560{
19561 int id;
19562
19563 id = syn_name2id(eap->arg);
19564 if (id == 0)
19565 echo_attr = 0;
19566 else
19567 echo_attr = syn_id2attr(id);
19568}
19569
19570/*
19571 * ":execute expr1 ..." execute the result of an expression.
19572 * ":echomsg expr1 ..." Print a message
19573 * ":echoerr expr1 ..." Print an error
19574 * Each gets spaces around each argument and a newline at the end for
19575 * echo commands
19576 */
19577 void
19578ex_execute(eap)
19579 exarg_T *eap;
19580{
19581 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019582 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 int ret = OK;
19584 char_u *p;
19585 garray_T ga;
19586 int len;
19587 int save_did_emsg;
19588
19589 ga_init2(&ga, 1, 80);
19590
19591 if (eap->skip)
19592 ++emsg_skip;
19593 while (*arg != NUL && *arg != '|' && *arg != '\n')
19594 {
19595 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019596 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 {
19598 /*
19599 * Report the invalid expression unless the expression evaluation
19600 * has been cancelled due to an aborting error, an interrupt, or an
19601 * exception.
19602 */
19603 if (!aborting())
19604 EMSG2(_(e_invexpr2), p);
19605 ret = FAIL;
19606 break;
19607 }
19608
19609 if (!eap->skip)
19610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 len = (int)STRLEN(p);
19613 if (ga_grow(&ga, len + 2) == FAIL)
19614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 ret = FAIL;
19617 break;
19618 }
19619 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 ga.ga_len += len;
19623 }
19624
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019625 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 arg = skipwhite(arg);
19627 }
19628
19629 if (ret != FAIL && ga.ga_data != NULL)
19630 {
19631 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019634 out_flush();
19635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 else if (eap->cmdidx == CMD_echoerr)
19637 {
19638 /* We don't want to abort following commands, restore did_emsg. */
19639 save_did_emsg = did_emsg;
19640 EMSG((char_u *)ga.ga_data);
19641 if (!force_abort)
19642 did_emsg = save_did_emsg;
19643 }
19644 else if (eap->cmdidx == CMD_execute)
19645 do_cmdline((char_u *)ga.ga_data,
19646 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19647 }
19648
19649 ga_clear(&ga);
19650
19651 if (eap->skip)
19652 --emsg_skip;
19653
19654 eap->nextcmd = check_nextcmd(arg);
19655}
19656
19657/*
19658 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19659 * "arg" points to the "&" or '+' when called, to "option" when returning.
19660 * Returns NULL when no option name found. Otherwise pointer to the char
19661 * after the option name.
19662 */
19663 static char_u *
19664find_option_end(arg, opt_flags)
19665 char_u **arg;
19666 int *opt_flags;
19667{
19668 char_u *p = *arg;
19669
19670 ++p;
19671 if (*p == 'g' && p[1] == ':')
19672 {
19673 *opt_flags = OPT_GLOBAL;
19674 p += 2;
19675 }
19676 else if (*p == 'l' && p[1] == ':')
19677 {
19678 *opt_flags = OPT_LOCAL;
19679 p += 2;
19680 }
19681 else
19682 *opt_flags = 0;
19683
19684 if (!ASCII_ISALPHA(*p))
19685 return NULL;
19686 *arg = p;
19687
19688 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19689 p += 4; /* termcap option */
19690 else
19691 while (ASCII_ISALPHA(*p))
19692 ++p;
19693 return p;
19694}
19695
19696/*
19697 * ":function"
19698 */
19699 void
19700ex_function(eap)
19701 exarg_T *eap;
19702{
19703 char_u *theline;
19704 int j;
19705 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 char_u *name = NULL;
19708 char_u *p;
19709 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019710 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 garray_T newargs;
19712 garray_T newlines;
19713 int varargs = FALSE;
19714 int mustend = FALSE;
19715 int flags = 0;
19716 ufunc_T *fp;
19717 int indent;
19718 int nesting;
19719 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019720 dictitem_T *v;
19721 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019722 static int func_nr = 0; /* number for nameless function */
19723 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019724 hashtab_T *ht;
19725 int todo;
19726 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019727 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728
19729 /*
19730 * ":function" without argument: list functions.
19731 */
19732 if (ends_excmd(*eap->arg))
19733 {
19734 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019736 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019737 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019738 {
19739 if (!HASHITEM_EMPTY(hi))
19740 {
19741 --todo;
19742 fp = HI2UF(hi);
19743 if (!isdigit(*fp->uf_name))
19744 list_func_head(fp, FALSE);
19745 }
19746 }
19747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 eap->nextcmd = check_nextcmd(eap->arg);
19749 return;
19750 }
19751
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019752 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019753 * ":function /pat": list functions matching pattern.
19754 */
19755 if (*eap->arg == '/')
19756 {
19757 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19758 if (!eap->skip)
19759 {
19760 regmatch_T regmatch;
19761
19762 c = *p;
19763 *p = NUL;
19764 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19765 *p = c;
19766 if (regmatch.regprog != NULL)
19767 {
19768 regmatch.rm_ic = p_ic;
19769
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019770 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019771 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19772 {
19773 if (!HASHITEM_EMPTY(hi))
19774 {
19775 --todo;
19776 fp = HI2UF(hi);
19777 if (!isdigit(*fp->uf_name)
19778 && vim_regexec(&regmatch, fp->uf_name, 0))
19779 list_func_head(fp, FALSE);
19780 }
19781 }
19782 }
19783 }
19784 if (*p == '/')
19785 ++p;
19786 eap->nextcmd = check_nextcmd(p);
19787 return;
19788 }
19789
19790 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019791 * Get the function name. There are these situations:
19792 * func normal function name
19793 * "name" == func, "fudi.fd_dict" == NULL
19794 * dict.func new dictionary entry
19795 * "name" == NULL, "fudi.fd_dict" set,
19796 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19797 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019798 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019799 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19800 * dict.func existing dict entry that's not a Funcref
19801 * "name" == NULL, "fudi.fd_dict" set,
19802 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019805 name = trans_function_name(&p, eap->skip, 0, &fudi);
19806 paren = (vim_strchr(p, '(') != NULL);
19807 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 {
19809 /*
19810 * Return on an invalid expression in braces, unless the expression
19811 * evaluation has been cancelled due to an aborting error, an
19812 * interrupt, or an exception.
19813 */
19814 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019815 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019816 if (!eap->skip && fudi.fd_newkey != NULL)
19817 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019818 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 else
19822 eap->skip = TRUE;
19823 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019824
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 /* An error in a function call during evaluation of an expression in magic
19826 * braces should not cause the function not to be defined. */
19827 saved_did_emsg = did_emsg;
19828 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829
19830 /*
19831 * ":function func" with only function name: list function.
19832 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019833 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 {
19835 if (!ends_excmd(*skipwhite(p)))
19836 {
19837 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019838 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 }
19840 eap->nextcmd = check_nextcmd(p);
19841 if (eap->nextcmd != NULL)
19842 *p = NUL;
19843 if (!eap->skip && !got_int)
19844 {
19845 fp = find_func(name);
19846 if (fp != NULL)
19847 {
19848 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019849 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019851 if (FUNCLINE(fp, j) == NULL)
19852 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 msg_putchar('\n');
19854 msg_outnum((long)(j + 1));
19855 if (j < 9)
19856 msg_putchar(' ');
19857 if (j < 99)
19858 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019859 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 out_flush(); /* show a line at a time */
19861 ui_breakcheck();
19862 }
19863 if (!got_int)
19864 {
19865 msg_putchar('\n');
19866 msg_puts((char_u *)" endfunction");
19867 }
19868 }
19869 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019870 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019872 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 }
19874
19875 /*
19876 * ":function name(arg1, arg2)" Define function.
19877 */
19878 p = skipwhite(p);
19879 if (*p != '(')
19880 {
19881 if (!eap->skip)
19882 {
19883 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019884 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 }
19886 /* attempt to continue by skipping some text */
19887 if (vim_strchr(p, '(') != NULL)
19888 p = vim_strchr(p, '(');
19889 }
19890 p = skipwhite(p + 1);
19891
19892 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19893 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19894
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019895 if (!eap->skip)
19896 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019897 /* Check the name of the function. Unless it's a dictionary function
19898 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019899 if (name != NULL)
19900 arg = name;
19901 else
19902 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019903 if (arg != NULL && (fudi.fd_di == NULL
19904 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019905 {
19906 if (*arg == K_SPECIAL)
19907 j = 3;
19908 else
19909 j = 0;
19910 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19911 : eval_isnamec(arg[j])))
19912 ++j;
19913 if (arg[j] != NUL)
19914 emsg_funcname(_(e_invarg2), arg);
19915 }
19916 }
19917
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 /*
19919 * Isolate the arguments: "arg1, arg2, ...)"
19920 */
19921 while (*p != ')')
19922 {
19923 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19924 {
19925 varargs = TRUE;
19926 p += 3;
19927 mustend = TRUE;
19928 }
19929 else
19930 {
19931 arg = p;
19932 while (ASCII_ISALNUM(*p) || *p == '_')
19933 ++p;
19934 if (arg == p || isdigit(*arg)
19935 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19936 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19937 {
19938 if (!eap->skip)
19939 EMSG2(_("E125: Illegal argument: %s"), arg);
19940 break;
19941 }
19942 if (ga_grow(&newargs, 1) == FAIL)
19943 goto erret;
19944 c = *p;
19945 *p = NUL;
19946 arg = vim_strsave(arg);
19947 if (arg == NULL)
19948 goto erret;
19949 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19950 *p = c;
19951 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 if (*p == ',')
19953 ++p;
19954 else
19955 mustend = TRUE;
19956 }
19957 p = skipwhite(p);
19958 if (mustend && *p != ')')
19959 {
19960 if (!eap->skip)
19961 EMSG2(_(e_invarg2), eap->arg);
19962 break;
19963 }
19964 }
19965 ++p; /* skip the ')' */
19966
Bram Moolenaare9a41262005-01-15 22:18:47 +000019967 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 for (;;)
19969 {
19970 p = skipwhite(p);
19971 if (STRNCMP(p, "range", 5) == 0)
19972 {
19973 flags |= FC_RANGE;
19974 p += 5;
19975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019976 else if (STRNCMP(p, "dict", 4) == 0)
19977 {
19978 flags |= FC_DICT;
19979 p += 4;
19980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 else if (STRNCMP(p, "abort", 5) == 0)
19982 {
19983 flags |= FC_ABORT;
19984 p += 5;
19985 }
19986 else
19987 break;
19988 }
19989
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019990 /* When there is a line break use what follows for the function body.
19991 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19992 if (*p == '\n')
19993 line_arg = p + 1;
19994 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 EMSG(_(e_trailing));
19996
19997 /*
19998 * Read the body of the function, until ":endfunction" is found.
19999 */
20000 if (KeyTyped)
20001 {
20002 /* Check if the function already exists, don't let the user type the
20003 * whole function before telling him it doesn't work! For a script we
20004 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005 if (!eap->skip && !eap->forceit)
20006 {
20007 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20008 EMSG(_(e_funcdict));
20009 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020010 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020013 if (!eap->skip && did_emsg)
20014 goto erret;
20015
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 msg_putchar('\n'); /* don't overwrite the function name */
20017 cmdline_row = msg_row;
20018 }
20019
20020 indent = 2;
20021 nesting = 0;
20022 for (;;)
20023 {
20024 msg_scroll = TRUE;
20025 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020026 sourcing_lnum_off = sourcing_lnum;
20027
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020028 if (line_arg != NULL)
20029 {
20030 /* Use eap->arg, split up in parts by line breaks. */
20031 theline = line_arg;
20032 p = vim_strchr(theline, '\n');
20033 if (p == NULL)
20034 line_arg += STRLEN(line_arg);
20035 else
20036 {
20037 *p = NUL;
20038 line_arg = p + 1;
20039 }
20040 }
20041 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 theline = getcmdline(':', 0L, indent);
20043 else
20044 theline = eap->getline(':', eap->cookie, indent);
20045 if (KeyTyped)
20046 lines_left = Rows - 1;
20047 if (theline == NULL)
20048 {
20049 EMSG(_("E126: Missing :endfunction"));
20050 goto erret;
20051 }
20052
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020053 /* Detect line continuation: sourcing_lnum increased more than one. */
20054 if (sourcing_lnum > sourcing_lnum_off + 1)
20055 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20056 else
20057 sourcing_lnum_off = 0;
20058
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 if (skip_until != NULL)
20060 {
20061 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20062 * don't check for ":endfunc". */
20063 if (STRCMP(theline, skip_until) == 0)
20064 {
20065 vim_free(skip_until);
20066 skip_until = NULL;
20067 }
20068 }
20069 else
20070 {
20071 /* skip ':' and blanks*/
20072 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20073 ;
20074
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020075 /* Check for "endfunction". */
20076 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020078 if (line_arg == NULL)
20079 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 break;
20081 }
20082
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020083 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 * at "end". */
20085 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20086 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020087 else if (STRNCMP(p, "if", 2) == 0
20088 || STRNCMP(p, "wh", 2) == 0
20089 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 || STRNCMP(p, "try", 3) == 0)
20091 indent += 2;
20092
20093 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020094 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020096 if (*p == '!')
20097 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 p += eval_fname_script(p);
20099 if (ASCII_ISALPHA(*p))
20100 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020101 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 if (*skipwhite(p) == '(')
20103 {
20104 ++nesting;
20105 indent += 2;
20106 }
20107 }
20108 }
20109
20110 /* Check for ":append" or ":insert". */
20111 p = skip_range(p, NULL);
20112 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20113 || (p[0] == 'i'
20114 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20115 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20116 skip_until = vim_strsave((char_u *)".");
20117
20118 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20119 arg = skipwhite(skiptowhite(p));
20120 if (arg[0] == '<' && arg[1] =='<'
20121 && ((p[0] == 'p' && p[1] == 'y'
20122 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20123 || (p[0] == 'p' && p[1] == 'e'
20124 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20125 || (p[0] == 't' && p[1] == 'c'
20126 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20127 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20128 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020129 || (p[0] == 'm' && p[1] == 'z'
20130 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 ))
20132 {
20133 /* ":python <<" continues until a dot, like ":append" */
20134 p = skipwhite(arg + 2);
20135 if (*p == NUL)
20136 skip_until = vim_strsave((char_u *)".");
20137 else
20138 skip_until = vim_strsave(p);
20139 }
20140 }
20141
20142 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020143 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020144 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020145 if (line_arg == NULL)
20146 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020148 }
20149
20150 /* Copy the line to newly allocated memory. get_one_sourceline()
20151 * allocates 250 bytes per line, this saves 80% on average. The cost
20152 * is an extra alloc/free. */
20153 p = vim_strsave(theline);
20154 if (p != NULL)
20155 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020156 if (line_arg == NULL)
20157 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020158 theline = p;
20159 }
20160
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020161 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20162
20163 /* Add NULL lines for continuation lines, so that the line count is
20164 * equal to the index in the growarray. */
20165 while (sourcing_lnum_off-- > 0)
20166 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020167
20168 /* Check for end of eap->arg. */
20169 if (line_arg != NULL && *line_arg == NUL)
20170 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 }
20172
20173 /* Don't define the function when skipping commands or when an error was
20174 * detected. */
20175 if (eap->skip || did_emsg)
20176 goto erret;
20177
20178 /*
20179 * If there are no errors, add the function
20180 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020181 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020182 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020183 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020185 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020186 emsg_funcname("E707: Function name conflicts with variable: %s",
20187 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020188 goto erret;
20189 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020190
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020191 fp = find_func(name);
20192 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020194 if (!eap->forceit)
20195 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020196 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020197 goto erret;
20198 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020199 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020200 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020201 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020202 name);
20203 goto erret;
20204 }
20205 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020206 ga_clear_strings(&(fp->uf_args));
20207 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020208 vim_free(name);
20209 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 }
20212 else
20213 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020214 char numbuf[20];
20215
20216 fp = NULL;
20217 if (fudi.fd_newkey == NULL && !eap->forceit)
20218 {
20219 EMSG(_(e_funcdict));
20220 goto erret;
20221 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020222 if (fudi.fd_di == NULL)
20223 {
20224 /* Can't add a function to a locked dictionary */
20225 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20226 goto erret;
20227 }
20228 /* Can't change an existing function if it is locked */
20229 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20230 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020231
20232 /* Give the function a sequential number. Can only be used with a
20233 * Funcref! */
20234 vim_free(name);
20235 sprintf(numbuf, "%d", ++func_nr);
20236 name = vim_strsave((char_u *)numbuf);
20237 if (name == NULL)
20238 goto erret;
20239 }
20240
20241 if (fp == NULL)
20242 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020243 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020244 {
20245 int slen, plen;
20246 char_u *scriptname;
20247
20248 /* Check that the autoload name matches the script name. */
20249 j = FAIL;
20250 if (sourcing_name != NULL)
20251 {
20252 scriptname = autoload_name(name);
20253 if (scriptname != NULL)
20254 {
20255 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020256 plen = (int)STRLEN(p);
20257 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020258 if (slen > plen && fnamecmp(p,
20259 sourcing_name + slen - plen) == 0)
20260 j = OK;
20261 vim_free(scriptname);
20262 }
20263 }
20264 if (j == FAIL)
20265 {
20266 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20267 goto erret;
20268 }
20269 }
20270
20271 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 if (fp == NULL)
20273 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020274
20275 if (fudi.fd_dict != NULL)
20276 {
20277 if (fudi.fd_di == NULL)
20278 {
20279 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020280 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020281 if (fudi.fd_di == NULL)
20282 {
20283 vim_free(fp);
20284 goto erret;
20285 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020286 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20287 {
20288 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020289 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020290 goto erret;
20291 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020292 }
20293 else
20294 /* overwrite existing dict entry */
20295 clear_tv(&fudi.fd_di->di_tv);
20296 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020297 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020298 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020299 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020300
20301 /* behave like "dict" was used */
20302 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020303 }
20304
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020306 STRCPY(fp->uf_name, name);
20307 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020309 fp->uf_args = newargs;
20310 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020311#ifdef FEAT_PROFILE
20312 fp->uf_tml_count = NULL;
20313 fp->uf_tml_total = NULL;
20314 fp->uf_tml_self = NULL;
20315 fp->uf_profiling = FALSE;
20316 if (prof_def_func())
20317 func_do_profile(fp);
20318#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020319 fp->uf_varargs = varargs;
20320 fp->uf_flags = flags;
20321 fp->uf_calls = 0;
20322 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020323 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324
20325erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 ga_clear_strings(&newargs);
20327 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020328ret_free:
20329 vim_free(skip_until);
20330 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333}
20334
20335/*
20336 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020337 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020339 * flags:
20340 * TFN_INT: internal function name OK
20341 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 * Advances "pp" to just after the function name (if no error).
20343 */
20344 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020345trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 char_u **pp;
20347 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020348 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020351 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 char_u *start;
20353 char_u *end;
20354 int lead;
20355 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020357 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020358
20359 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020362
20363 /* Check for hard coded <SNR>: already translated function ID (from a user
20364 * command). */
20365 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20366 && (*pp)[2] == (int)KE_SNR)
20367 {
20368 *pp += 3;
20369 len = get_id_len(pp) + 3;
20370 return vim_strnsave(start, len);
20371 }
20372
20373 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20374 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020376 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020378
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020379 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20380 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020381 if (end == start)
20382 {
20383 if (!skip)
20384 EMSG(_("E129: Function name required"));
20385 goto theend;
20386 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020387 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020388 {
20389 /*
20390 * Report an invalid expression in braces, unless the expression
20391 * evaluation has been cancelled due to an aborting error, an
20392 * interrupt, or an exception.
20393 */
20394 if (!aborting())
20395 {
20396 if (end != NULL)
20397 EMSG2(_(e_invarg2), start);
20398 }
20399 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020400 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020401 goto theend;
20402 }
20403
20404 if (lv.ll_tv != NULL)
20405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020406 if (fdp != NULL)
20407 {
20408 fdp->fd_dict = lv.ll_dict;
20409 fdp->fd_newkey = lv.ll_newkey;
20410 lv.ll_newkey = NULL;
20411 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020412 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020413 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20414 {
20415 name = vim_strsave(lv.ll_tv->vval.v_string);
20416 *pp = end;
20417 }
20418 else
20419 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020420 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20421 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020422 EMSG(_(e_funcref));
20423 else
20424 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020425 name = NULL;
20426 }
20427 goto theend;
20428 }
20429
20430 if (lv.ll_name == NULL)
20431 {
20432 /* Error found, but continue after the function name. */
20433 *pp = end;
20434 goto theend;
20435 }
20436
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020437 /* Check if the name is a Funcref. If so, use the value. */
20438 if (lv.ll_exp_name != NULL)
20439 {
20440 len = (int)STRLEN(lv.ll_exp_name);
20441 name = deref_func_name(lv.ll_exp_name, &len);
20442 if (name == lv.ll_exp_name)
20443 name = NULL;
20444 }
20445 else
20446 {
20447 len = (int)(end - *pp);
20448 name = deref_func_name(*pp, &len);
20449 if (name == *pp)
20450 name = NULL;
20451 }
20452 if (name != NULL)
20453 {
20454 name = vim_strsave(name);
20455 *pp = end;
20456 goto theend;
20457 }
20458
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020459 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020460 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020461 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020462 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20463 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20464 {
20465 /* When there was "s:" already or the name expanded to get a
20466 * leading "s:" then remove it. */
20467 lv.ll_name += 2;
20468 len -= 2;
20469 lead = 2;
20470 }
20471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020472 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020473 {
20474 if (lead == 2) /* skip over "s:" */
20475 lv.ll_name += 2;
20476 len = (int)(end - lv.ll_name);
20477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020478
20479 /*
20480 * Copy the function name to allocated memory.
20481 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20482 * Accept <SNR>123_name() outside a script.
20483 */
20484 if (skip)
20485 lead = 0; /* do nothing */
20486 else if (lead > 0)
20487 {
20488 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020489 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20490 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020491 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020492 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020493 if (current_SID <= 0)
20494 {
20495 EMSG(_(e_usingsid));
20496 goto theend;
20497 }
20498 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20499 lead += (int)STRLEN(sid_buf);
20500 }
20501 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020502 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020503 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020504 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020505 goto theend;
20506 }
20507 name = alloc((unsigned)(len + lead + 1));
20508 if (name != NULL)
20509 {
20510 if (lead > 0)
20511 {
20512 name[0] = K_SPECIAL;
20513 name[1] = KS_EXTRA;
20514 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020515 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020516 STRCPY(name + 3, sid_buf);
20517 }
20518 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20519 name[len + lead] = NUL;
20520 }
20521 *pp = end;
20522
20523theend:
20524 clear_lval(&lv);
20525 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526}
20527
20528/*
20529 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20530 * Return 2 if "p" starts with "s:".
20531 * Return 0 otherwise.
20532 */
20533 static int
20534eval_fname_script(p)
20535 char_u *p;
20536{
20537 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20538 || STRNICMP(p + 1, "SNR>", 4) == 0))
20539 return 5;
20540 if (p[0] == 's' && p[1] == ':')
20541 return 2;
20542 return 0;
20543}
20544
20545/*
20546 * Return TRUE if "p" starts with "<SID>" or "s:".
20547 * Only works if eval_fname_script() returned non-zero for "p"!
20548 */
20549 static int
20550eval_fname_sid(p)
20551 char_u *p;
20552{
20553 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20554}
20555
20556/*
20557 * List the head of the function: "name(arg1, arg2)".
20558 */
20559 static void
20560list_func_head(fp, indent)
20561 ufunc_T *fp;
20562 int indent;
20563{
20564 int j;
20565
20566 msg_start();
20567 if (indent)
20568 MSG_PUTS(" ");
20569 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020570 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 {
20572 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020573 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 }
20575 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020576 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020578 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 {
20580 if (j)
20581 MSG_PUTS(", ");
20582 msg_puts(FUNCARG(fp, j));
20583 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020584 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 {
20586 if (j)
20587 MSG_PUTS(", ");
20588 MSG_PUTS("...");
20589 }
20590 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020591 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020592 if (p_verbose > 0)
20593 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594}
20595
20596/*
20597 * Find a function by name, return pointer to it in ufuncs.
20598 * Return NULL for unknown function.
20599 */
20600 static ufunc_T *
20601find_func(name)
20602 char_u *name;
20603{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020604 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020606 hi = hash_find(&func_hashtab, name);
20607 if (!HASHITEM_EMPTY(hi))
20608 return HI2UF(hi);
20609 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610}
20611
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020612#if defined(EXITFREE) || defined(PROTO)
20613 void
20614free_all_functions()
20615{
20616 hashitem_T *hi;
20617
20618 /* Need to start all over every time, because func_free() may change the
20619 * hash table. */
20620 while (func_hashtab.ht_used > 0)
20621 for (hi = func_hashtab.ht_array; ; ++hi)
20622 if (!HASHITEM_EMPTY(hi))
20623 {
20624 func_free(HI2UF(hi));
20625 break;
20626 }
20627}
20628#endif
20629
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020630/*
20631 * Return TRUE if a function "name" exists.
20632 */
20633 static int
20634function_exists(name)
20635 char_u *name;
20636{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020637 char_u *nm = name;
20638 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020639 int n = FALSE;
20640
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020641 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020642 nm = skipwhite(nm);
20643
20644 /* Only accept "funcname", "funcname ", "funcname (..." and
20645 * "funcname(...", not "funcname!...". */
20646 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020647 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020648 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020649 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020650 else
20651 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020652 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020653 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020654 return n;
20655}
20656
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020657/*
20658 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020659 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020660 */
20661 static int
20662builtin_function(name)
20663 char_u *name;
20664{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020665 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20666 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020667}
20668
Bram Moolenaar05159a02005-02-26 23:04:13 +000020669#if defined(FEAT_PROFILE) || defined(PROTO)
20670/*
20671 * Start profiling function "fp".
20672 */
20673 static void
20674func_do_profile(fp)
20675 ufunc_T *fp;
20676{
20677 fp->uf_tm_count = 0;
20678 profile_zero(&fp->uf_tm_self);
20679 profile_zero(&fp->uf_tm_total);
20680 if (fp->uf_tml_count == NULL)
20681 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20682 (sizeof(int) * fp->uf_lines.ga_len));
20683 if (fp->uf_tml_total == NULL)
20684 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20685 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20686 if (fp->uf_tml_self == NULL)
20687 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20688 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20689 fp->uf_tml_idx = -1;
20690 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20691 || fp->uf_tml_self == NULL)
20692 return; /* out of memory */
20693
20694 fp->uf_profiling = TRUE;
20695}
20696
20697/*
20698 * Dump the profiling results for all functions in file "fd".
20699 */
20700 void
20701func_dump_profile(fd)
20702 FILE *fd;
20703{
20704 hashitem_T *hi;
20705 int todo;
20706 ufunc_T *fp;
20707 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020708 ufunc_T **sorttab;
20709 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020711 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020712 if (todo == 0)
20713 return; /* nothing to dump */
20714
Bram Moolenaar73830342005-02-28 22:48:19 +000020715 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20716
Bram Moolenaar05159a02005-02-26 23:04:13 +000020717 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20718 {
20719 if (!HASHITEM_EMPTY(hi))
20720 {
20721 --todo;
20722 fp = HI2UF(hi);
20723 if (fp->uf_profiling)
20724 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020725 if (sorttab != NULL)
20726 sorttab[st_len++] = fp;
20727
Bram Moolenaar05159a02005-02-26 23:04:13 +000020728 if (fp->uf_name[0] == K_SPECIAL)
20729 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20730 else
20731 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20732 if (fp->uf_tm_count == 1)
20733 fprintf(fd, "Called 1 time\n");
20734 else
20735 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20736 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20737 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20738 fprintf(fd, "\n");
20739 fprintf(fd, "count total (s) self (s)\n");
20740
20741 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20742 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020743 if (FUNCLINE(fp, i) == NULL)
20744 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020745 prof_func_line(fd, fp->uf_tml_count[i],
20746 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020747 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20748 }
20749 fprintf(fd, "\n");
20750 }
20751 }
20752 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020753
20754 if (sorttab != NULL && st_len > 0)
20755 {
20756 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20757 prof_total_cmp);
20758 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20759 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20760 prof_self_cmp);
20761 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20762 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020763
20764 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020765}
Bram Moolenaar73830342005-02-28 22:48:19 +000020766
20767 static void
20768prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20769 FILE *fd;
20770 ufunc_T **sorttab;
20771 int st_len;
20772 char *title;
20773 int prefer_self; /* when equal print only self time */
20774{
20775 int i;
20776 ufunc_T *fp;
20777
20778 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20779 fprintf(fd, "count total (s) self (s) function\n");
20780 for (i = 0; i < 20 && i < st_len; ++i)
20781 {
20782 fp = sorttab[i];
20783 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20784 prefer_self);
20785 if (fp->uf_name[0] == K_SPECIAL)
20786 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20787 else
20788 fprintf(fd, " %s()\n", fp->uf_name);
20789 }
20790 fprintf(fd, "\n");
20791}
20792
20793/*
20794 * Print the count and times for one function or function line.
20795 */
20796 static void
20797prof_func_line(fd, count, total, self, prefer_self)
20798 FILE *fd;
20799 int count;
20800 proftime_T *total;
20801 proftime_T *self;
20802 int prefer_self; /* when equal print only self time */
20803{
20804 if (count > 0)
20805 {
20806 fprintf(fd, "%5d ", count);
20807 if (prefer_self && profile_equal(total, self))
20808 fprintf(fd, " ");
20809 else
20810 fprintf(fd, "%s ", profile_msg(total));
20811 if (!prefer_self && profile_equal(total, self))
20812 fprintf(fd, " ");
20813 else
20814 fprintf(fd, "%s ", profile_msg(self));
20815 }
20816 else
20817 fprintf(fd, " ");
20818}
20819
20820/*
20821 * Compare function for total time sorting.
20822 */
20823 static int
20824#ifdef __BORLANDC__
20825_RTLENTRYF
20826#endif
20827prof_total_cmp(s1, s2)
20828 const void *s1;
20829 const void *s2;
20830{
20831 ufunc_T *p1, *p2;
20832
20833 p1 = *(ufunc_T **)s1;
20834 p2 = *(ufunc_T **)s2;
20835 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20836}
20837
20838/*
20839 * Compare function for self time sorting.
20840 */
20841 static int
20842#ifdef __BORLANDC__
20843_RTLENTRYF
20844#endif
20845prof_self_cmp(s1, s2)
20846 const void *s1;
20847 const void *s2;
20848{
20849 ufunc_T *p1, *p2;
20850
20851 p1 = *(ufunc_T **)s1;
20852 p2 = *(ufunc_T **)s2;
20853 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20854}
20855
Bram Moolenaar05159a02005-02-26 23:04:13 +000020856#endif
20857
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020858/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020859 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020860 * Return TRUE if a package was loaded.
20861 */
20862 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020863script_autoload(name, reload)
20864 char_u *name;
20865 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020866{
20867 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020868 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020869 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020870 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020871
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020872 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020873 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020874 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020875 return FALSE;
20876
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020877 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020878
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020879 /* Find the name in the list of previously loaded package names. Skip
20880 * "autoload/", it's always the same. */
20881 for (i = 0; i < ga_loaded.ga_len; ++i)
20882 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20883 break;
20884 if (!reload && i < ga_loaded.ga_len)
20885 ret = FALSE; /* was loaded already */
20886 else
20887 {
20888 /* Remember the name if it wasn't loaded already. */
20889 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20890 {
20891 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20892 tofree = NULL;
20893 }
20894
20895 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020896 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020897 ret = TRUE;
20898 }
20899
20900 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020901 return ret;
20902}
20903
20904/*
20905 * Return the autoload script name for a function or variable name.
20906 * Returns NULL when out of memory.
20907 */
20908 static char_u *
20909autoload_name(name)
20910 char_u *name;
20911{
20912 char_u *p;
20913 char_u *scriptname;
20914
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020915 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020916 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20917 if (scriptname == NULL)
20918 return FALSE;
20919 STRCPY(scriptname, "autoload/");
20920 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020921 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020922 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020923 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020924 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020925 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020926}
20927
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20929
20930/*
20931 * Function given to ExpandGeneric() to obtain the list of user defined
20932 * function names.
20933 */
20934 char_u *
20935get_user_func_name(xp, idx)
20936 expand_T *xp;
20937 int idx;
20938{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020939 static long_u done;
20940 static hashitem_T *hi;
20941 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942
20943 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020945 done = 0;
20946 hi = func_hashtab.ht_array;
20947 }
20948 if (done < func_hashtab.ht_used)
20949 {
20950 if (done++ > 0)
20951 ++hi;
20952 while (HASHITEM_EMPTY(hi))
20953 ++hi;
20954 fp = HI2UF(hi);
20955
20956 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20957 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958
20959 cat_func_name(IObuff, fp);
20960 if (xp->xp_context != EXPAND_USER_FUNC)
20961 {
20962 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020963 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 STRCAT(IObuff, ")");
20965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 return IObuff;
20967 }
20968 return NULL;
20969}
20970
20971#endif /* FEAT_CMDL_COMPL */
20972
20973/*
20974 * Copy the function name of "fp" to buffer "buf".
20975 * "buf" must be able to hold the function name plus three bytes.
20976 * Takes care of script-local function names.
20977 */
20978 static void
20979cat_func_name(buf, fp)
20980 char_u *buf;
20981 ufunc_T *fp;
20982{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020983 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 {
20985 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020986 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 }
20988 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020989 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990}
20991
20992/*
20993 * ":delfunction {name}"
20994 */
20995 void
20996ex_delfunction(eap)
20997 exarg_T *eap;
20998{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020999 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 char_u *p;
21001 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003
21004 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021005 name = trans_function_name(&p, eap->skip, 0, &fudi);
21006 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021008 {
21009 if (fudi.fd_dict != NULL && !eap->skip)
21010 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 if (!ends_excmd(*skipwhite(p)))
21014 {
21015 vim_free(name);
21016 EMSG(_(e_trailing));
21017 return;
21018 }
21019 eap->nextcmd = check_nextcmd(p);
21020 if (eap->nextcmd != NULL)
21021 *p = NUL;
21022
21023 if (!eap->skip)
21024 fp = find_func(name);
21025 vim_free(name);
21026
21027 if (!eap->skip)
21028 {
21029 if (fp == NULL)
21030 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021031 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 return;
21033 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021034 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 {
21036 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21037 return;
21038 }
21039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021040 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021042 /* Delete the dict item that refers to the function, it will
21043 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021044 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021046 else
21047 func_free(fp);
21048 }
21049}
21050
21051/*
21052 * Free a function and remove it from the list of functions.
21053 */
21054 static void
21055func_free(fp)
21056 ufunc_T *fp;
21057{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021058 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021059
21060 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021061 ga_clear_strings(&(fp->uf_args));
21062 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021063#ifdef FEAT_PROFILE
21064 vim_free(fp->uf_tml_count);
21065 vim_free(fp->uf_tml_total);
21066 vim_free(fp->uf_tml_self);
21067#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021069 /* remove the function from the function hashtable */
21070 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21071 if (HASHITEM_EMPTY(hi))
21072 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021073 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021074 hash_remove(&func_hashtab, hi);
21075
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021076 vim_free(fp);
21077}
21078
21079/*
21080 * Unreference a Function: decrement the reference count and free it when it
21081 * becomes zero. Only for numbered functions.
21082 */
21083 static void
21084func_unref(name)
21085 char_u *name;
21086{
21087 ufunc_T *fp;
21088
21089 if (name != NULL && isdigit(*name))
21090 {
21091 fp = find_func(name);
21092 if (fp == NULL)
21093 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021094 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021095 {
21096 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021097 * when "uf_calls" becomes zero. */
21098 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021099 func_free(fp);
21100 }
21101 }
21102}
21103
21104/*
21105 * Count a reference to a Function.
21106 */
21107 static void
21108func_ref(name)
21109 char_u *name;
21110{
21111 ufunc_T *fp;
21112
21113 if (name != NULL && isdigit(*name))
21114 {
21115 fp = find_func(name);
21116 if (fp == NULL)
21117 EMSG2(_(e_intern2), "func_ref()");
21118 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021119 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 }
21121}
21122
21123/*
21124 * Call a user function.
21125 */
21126 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021127call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 ufunc_T *fp; /* pointer to function */
21129 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021130 typval_T *argvars; /* arguments */
21131 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 linenr_T firstline; /* first line of range */
21133 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021134 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135{
Bram Moolenaar33570922005-01-25 22:26:29 +000021136 char_u *save_sourcing_name;
21137 linenr_T save_sourcing_lnum;
21138 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021139 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021140 int save_did_emsg;
21141 static int depth = 0;
21142 dictitem_T *v;
21143 int fixvar_idx = 0; /* index in fixvar[] */
21144 int i;
21145 int ai;
21146 char_u numbuf[NUMBUFLEN];
21147 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021148#ifdef FEAT_PROFILE
21149 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021150 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152
21153 /* If depth of calling is getting too high, don't execute the function */
21154 if (depth >= p_mfd)
21155 {
21156 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 rettv->v_type = VAR_NUMBER;
21158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 return;
21160 }
21161 ++depth;
21162
21163 line_breakcheck(); /* check for CTRL-C hit */
21164
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021165 fc = (funccall_T *)alloc(sizeof(funccall_T));
21166 fc->caller = current_funccal;
21167 current_funccal = fc;
21168 fc->func = fp;
21169 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021171 fc->linenr = 0;
21172 fc->returned = FALSE;
21173 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021175 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21176 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177
Bram Moolenaar33570922005-01-25 22:26:29 +000021178 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021179 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021180 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21181 * each argument variable and saves a lot of time.
21182 */
21183 /*
21184 * Init l: variables.
21185 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021186 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021187 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021188 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021189 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21190 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021191 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021192 name = v->di_key;
21193 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021194 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021195 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021196 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021197 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 v->di_tv.vval.v_dict = selfdict;
21199 ++selfdict->dv_refcount;
21200 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021201
Bram Moolenaar33570922005-01-25 22:26:29 +000021202 /*
21203 * Init a: variables.
21204 * Set a:0 to "argcount".
21205 * Set a:000 to a list with room for the "..." arguments.
21206 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021207 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21208 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021209 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021210 /* Use "name" to avoid a warning from some compiler that checks the
21211 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021212 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021213 name = v->di_key;
21214 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021215 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021216 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021217 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021218 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021219 v->di_tv.vval.v_list = &fc->l_varlist;
21220 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21221 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21222 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021223
21224 /*
21225 * Set a:firstline to "firstline" and a:lastline to "lastline".
21226 * Set a:name to named arguments.
21227 * Set a:N to the "..." arguments.
21228 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021229 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021230 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021231 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021232 (varnumber_T)lastline);
21233 for (i = 0; i < argcount; ++i)
21234 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021235 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021236 if (ai < 0)
21237 /* named argument a:name */
21238 name = FUNCARG(fp, i);
21239 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021240 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021241 /* "..." argument a:1, a:2, etc. */
21242 sprintf((char *)numbuf, "%d", ai + 1);
21243 name = numbuf;
21244 }
21245 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21246 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021247 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021248 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21249 }
21250 else
21251 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021252 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21253 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021254 if (v == NULL)
21255 break;
21256 v->di_flags = DI_FLAGS_RO;
21257 }
21258 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021259 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021260
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021261 /* Note: the values are copied directly to avoid alloc/free.
21262 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021263 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021264 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021265
21266 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21267 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021268 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21269 fc->l_listitems[ai].li_tv = argvars[i];
21270 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021271 }
21272 }
21273
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 /* Don't redraw while executing the function. */
21275 ++RedrawingDisabled;
21276 save_sourcing_name = sourcing_name;
21277 save_sourcing_lnum = sourcing_lnum;
21278 sourcing_lnum = 1;
21279 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021280 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 if (sourcing_name != NULL)
21282 {
21283 if (save_sourcing_name != NULL
21284 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21285 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21286 else
21287 STRCPY(sourcing_name, "function ");
21288 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21289
21290 if (p_verbose >= 12)
21291 {
21292 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021293 verbose_enter_scroll();
21294
Bram Moolenaar555b2802005-05-19 21:08:39 +000021295 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 if (p_verbose >= 14)
21297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021299 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021300 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021301 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302
21303 msg_puts((char_u *)"(");
21304 for (i = 0; i < argcount; ++i)
21305 {
21306 if (i > 0)
21307 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021308 if (argvars[i].v_type == VAR_NUMBER)
21309 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 else
21311 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021312 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21313 if (s != NULL)
21314 {
21315 trunc_string(s, buf, MSG_BUF_CLEN);
21316 msg_puts(buf);
21317 vim_free(tofree);
21318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 }
21320 }
21321 msg_puts((char_u *)")");
21322 }
21323 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021324
21325 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 --no_wait_return;
21327 }
21328 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021329#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021330 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021331 {
21332 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21333 func_do_profile(fp);
21334 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021335 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021336 {
21337 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021338 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021339 profile_zero(&fp->uf_tm_children);
21340 }
21341 script_prof_save(&wait_start);
21342 }
21343#endif
21344
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021346 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 save_did_emsg = did_emsg;
21348 did_emsg = FALSE;
21349
21350 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021351 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21353
21354 --RedrawingDisabled;
21355
21356 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021357 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021359 clear_tv(rettv);
21360 rettv->v_type = VAR_NUMBER;
21361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 }
21363
Bram Moolenaar05159a02005-02-26 23:04:13 +000021364#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021365 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021366 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021367 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021368 profile_end(&call_start);
21369 profile_sub_wait(&wait_start, &call_start);
21370 profile_add(&fp->uf_tm_total, &call_start);
21371 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021372 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021373 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021374 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21375 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021376 }
21377 }
21378#endif
21379
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 /* when being verbose, mention the return value */
21381 if (p_verbose >= 12)
21382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021384 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021387 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021388 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021389 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021390 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021391 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021393 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021394 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021395 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021396 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021397
Bram Moolenaar555b2802005-05-19 21:08:39 +000021398 /* The value may be very long. Skip the middle part, so that we
21399 * have some idea how it starts and ends. smsg() would always
21400 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021401 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021402 if (s != NULL)
21403 {
21404 trunc_string(s, buf, MSG_BUF_CLEN);
21405 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21406 vim_free(tofree);
21407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 }
21409 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021410
21411 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 --no_wait_return;
21413 }
21414
21415 vim_free(sourcing_name);
21416 sourcing_name = save_sourcing_name;
21417 sourcing_lnum = save_sourcing_lnum;
21418 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021419#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021420 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021421 script_prof_restore(&wait_start);
21422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423
21424 if (p_verbose >= 12 && sourcing_name != NULL)
21425 {
21426 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021427 verbose_enter_scroll();
21428
Bram Moolenaar555b2802005-05-19 21:08:39 +000021429 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021431
21432 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 --no_wait_return;
21434 }
21435
21436 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021437 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021439
21440 /* if the a:000 list and the a: dict are not referenced we can free the
21441 * funccall_T and what's in it. */
21442 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21443 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21444 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21445 {
21446 free_funccal(fc, FALSE);
21447 }
21448 else
21449 {
21450 hashitem_T *hi;
21451 listitem_T *li;
21452 int todo;
21453
21454 /* "fc" is still in use. This can happen when returning "a:000" or
21455 * assigning "l:" to a global variable.
21456 * Link "fc" in the list for garbage collection later. */
21457 fc->caller = previous_funccal;
21458 previous_funccal = fc;
21459
21460 /* Make a copy of the a: variables, since we didn't do that above. */
21461 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21462 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21463 {
21464 if (!HASHITEM_EMPTY(hi))
21465 {
21466 --todo;
21467 v = HI2DI(hi);
21468 copy_tv(&v->di_tv, &v->di_tv);
21469 }
21470 }
21471
21472 /* Make a copy of the a:000 items, since we didn't do that above. */
21473 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21474 copy_tv(&li->li_tv, &li->li_tv);
21475 }
21476}
21477
21478/*
21479 * Return TRUE if items in "fc" do not have "copyID". That means they are not
21480 * referenced from anywyere.
21481 */
21482 static int
21483can_free_funccal(fc, copyID)
21484 funccall_T *fc;
21485 int copyID;
21486{
21487 return (fc->l_varlist.lv_copyID != copyID
21488 && fc->l_vars.dv_copyID != copyID
21489 && fc->l_avars.dv_copyID != copyID);
21490}
21491
21492/*
21493 * Free "fc" and what it contains.
21494 */
21495 static void
21496free_funccal(fc, free_val)
21497 funccall_T *fc;
21498 int free_val; /* a: vars were allocated */
21499{
21500 listitem_T *li;
21501
21502 /* The a: variables typevals may not have been allocated, only free the
21503 * allocated variables. */
21504 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21505
21506 /* free all l: variables */
21507 vars_clear(&fc->l_vars.dv_hashtab);
21508
21509 /* Free the a:000 variables if they were allocated. */
21510 if (free_val)
21511 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21512 clear_tv(&li->li_tv);
21513
21514 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515}
21516
21517/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021518 * Add a number variable "name" to dict "dp" with value "nr".
21519 */
21520 static void
21521add_nr_var(dp, v, name, nr)
21522 dict_T *dp;
21523 dictitem_T *v;
21524 char *name;
21525 varnumber_T nr;
21526{
21527 STRCPY(v->di_key, name);
21528 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21529 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21530 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021531 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 v->di_tv.vval.v_number = nr;
21533}
21534
21535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 * ":return [expr]"
21537 */
21538 void
21539ex_return(eap)
21540 exarg_T *eap;
21541{
21542 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 int returning = FALSE;
21545
21546 if (current_funccal == NULL)
21547 {
21548 EMSG(_("E133: :return not inside a function"));
21549 return;
21550 }
21551
21552 if (eap->skip)
21553 ++emsg_skip;
21554
21555 eap->nextcmd = NULL;
21556 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021557 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 {
21559 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021560 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 }
21564 /* It's safer to return also on error. */
21565 else if (!eap->skip)
21566 {
21567 /*
21568 * Return unless the expression evaluation has been cancelled due to an
21569 * aborting error, an interrupt, or an exception.
21570 */
21571 if (!aborting())
21572 returning = do_return(eap, FALSE, TRUE, NULL);
21573 }
21574
21575 /* When skipping or the return gets pending, advance to the next command
21576 * in this line (!returning). Otherwise, ignore the rest of the line.
21577 * Following lines will be ignored by get_func_line(). */
21578 if (returning)
21579 eap->nextcmd = NULL;
21580 else if (eap->nextcmd == NULL) /* no argument */
21581 eap->nextcmd = check_nextcmd(arg);
21582
21583 if (eap->skip)
21584 --emsg_skip;
21585}
21586
21587/*
21588 * Return from a function. Possibly makes the return pending. Also called
21589 * for a pending return at the ":endtry" or after returning from an extra
21590 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021591 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021592 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 * FALSE when the return gets pending.
21594 */
21595 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021596do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 exarg_T *eap;
21598 int reanimate;
21599 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021600 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601{
21602 int idx;
21603 struct condstack *cstack = eap->cstack;
21604
21605 if (reanimate)
21606 /* Undo the return. */
21607 current_funccal->returned = FALSE;
21608
21609 /*
21610 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21611 * not in its finally clause (which then is to be executed next) is found.
21612 * In this case, make the ":return" pending for execution at the ":endtry".
21613 * Otherwise, return normally.
21614 */
21615 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21616 if (idx >= 0)
21617 {
21618 cstack->cs_pending[idx] = CSTP_RETURN;
21619
21620 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021621 /* A pending return again gets pending. "rettv" points to an
21622 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021624 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 else
21626 {
21627 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021628 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021630 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 {
21634 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021635 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 else
21638 EMSG(_(e_outofmem));
21639 }
21640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021641 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642
21643 if (reanimate)
21644 {
21645 /* The pending return value could be overwritten by a ":return"
21646 * without argument in a finally clause; reset the default
21647 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021648 current_funccal->rettv->v_type = VAR_NUMBER;
21649 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 }
21651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021652 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 }
21654 else
21655 {
21656 current_funccal->returned = TRUE;
21657
21658 /* If the return is carried out now, store the return value. For
21659 * a return immediately after reanimation, the value is already
21660 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021661 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021663 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021666 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 }
21668 }
21669
21670 return idx < 0;
21671}
21672
21673/*
21674 * Free the variable with a pending return value.
21675 */
21676 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021677discard_pending_return(rettv)
21678 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679{
Bram Moolenaar33570922005-01-25 22:26:29 +000021680 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681}
21682
21683/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021684 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 * is an allocated string. Used by report_pending() for verbose messages.
21686 */
21687 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021688get_return_cmd(rettv)
21689 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021691 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021692 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021693 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021695 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021696 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021697 if (s == NULL)
21698 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021699
21700 STRCPY(IObuff, ":return ");
21701 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21702 if (STRLEN(s) + 8 >= IOSIZE)
21703 STRCPY(IObuff + IOSIZE - 4, "...");
21704 vim_free(tofree);
21705 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706}
21707
21708/*
21709 * Get next function line.
21710 * Called by do_cmdline() to get the next line.
21711 * Returns allocated string, or NULL for end of function.
21712 */
21713/* ARGSUSED */
21714 char_u *
21715get_func_line(c, cookie, indent)
21716 int c; /* not used */
21717 void *cookie;
21718 int indent; /* not used */
21719{
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021721 ufunc_T *fp = fcp->func;
21722 char_u *retval;
21723 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724
21725 /* If breakpoints have been added/deleted need to check for it. */
21726 if (fcp->dbg_tick != debug_tick)
21727 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021728 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 sourcing_lnum);
21730 fcp->dbg_tick = debug_tick;
21731 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021732#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021733 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021734 func_line_end(cookie);
21735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736
Bram Moolenaar05159a02005-02-26 23:04:13 +000021737 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021738 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21739 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 retval = NULL;
21741 else
21742 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021743 /* Skip NULL lines (continuation lines). */
21744 while (fcp->linenr < gap->ga_len
21745 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21746 ++fcp->linenr;
21747 if (fcp->linenr >= gap->ga_len)
21748 retval = NULL;
21749 else
21750 {
21751 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21752 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021753#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021754 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021755 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021756#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 }
21759
21760 /* Did we encounter a breakpoint? */
21761 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21762 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021763 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021765 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 sourcing_lnum);
21767 fcp->dbg_tick = debug_tick;
21768 }
21769
21770 return retval;
21771}
21772
Bram Moolenaar05159a02005-02-26 23:04:13 +000021773#if defined(FEAT_PROFILE) || defined(PROTO)
21774/*
21775 * Called when starting to read a function line.
21776 * "sourcing_lnum" must be correct!
21777 * When skipping lines it may not actually be executed, but we won't find out
21778 * until later and we need to store the time now.
21779 */
21780 void
21781func_line_start(cookie)
21782 void *cookie;
21783{
21784 funccall_T *fcp = (funccall_T *)cookie;
21785 ufunc_T *fp = fcp->func;
21786
21787 if (fp->uf_profiling && sourcing_lnum >= 1
21788 && sourcing_lnum <= fp->uf_lines.ga_len)
21789 {
21790 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021791 /* Skip continuation lines. */
21792 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21793 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021794 fp->uf_tml_execed = FALSE;
21795 profile_start(&fp->uf_tml_start);
21796 profile_zero(&fp->uf_tml_children);
21797 profile_get_wait(&fp->uf_tml_wait);
21798 }
21799}
21800
21801/*
21802 * Called when actually executing a function line.
21803 */
21804 void
21805func_line_exec(cookie)
21806 void *cookie;
21807{
21808 funccall_T *fcp = (funccall_T *)cookie;
21809 ufunc_T *fp = fcp->func;
21810
21811 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21812 fp->uf_tml_execed = TRUE;
21813}
21814
21815/*
21816 * Called when done with a function line.
21817 */
21818 void
21819func_line_end(cookie)
21820 void *cookie;
21821{
21822 funccall_T *fcp = (funccall_T *)cookie;
21823 ufunc_T *fp = fcp->func;
21824
21825 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21826 {
21827 if (fp->uf_tml_execed)
21828 {
21829 ++fp->uf_tml_count[fp->uf_tml_idx];
21830 profile_end(&fp->uf_tml_start);
21831 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021832 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021833 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21834 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021835 }
21836 fp->uf_tml_idx = -1;
21837 }
21838}
21839#endif
21840
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841/*
21842 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021843 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844 */
21845 int
21846func_has_ended(cookie)
21847 void *cookie;
21848{
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850
21851 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21852 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021853 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 || fcp->returned);
21855}
21856
21857/*
21858 * return TRUE if cookie indicates a function which "abort"s on errors.
21859 */
21860 int
21861func_has_abort(cookie)
21862 void *cookie;
21863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021864 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865}
21866
21867#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21868typedef enum
21869{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021870 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21871 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21872 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873} var_flavour_T;
21874
21875static var_flavour_T var_flavour __ARGS((char_u *varname));
21876
21877 static var_flavour_T
21878var_flavour(varname)
21879 char_u *varname;
21880{
21881 char_u *p = varname;
21882
21883 if (ASCII_ISUPPER(*p))
21884 {
21885 while (*(++p))
21886 if (ASCII_ISLOWER(*p))
21887 return VAR_FLAVOUR_SESSION;
21888 return VAR_FLAVOUR_VIMINFO;
21889 }
21890 else
21891 return VAR_FLAVOUR_DEFAULT;
21892}
21893#endif
21894
21895#if defined(FEAT_VIMINFO) || defined(PROTO)
21896/*
21897 * Restore global vars that start with a capital from the viminfo file
21898 */
21899 int
21900read_viminfo_varlist(virp, writing)
21901 vir_T *virp;
21902 int writing;
21903{
21904 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021905 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907
21908 if (!writing && (find_viminfo_parameter('!') != NULL))
21909 {
21910 tab = vim_strchr(virp->vir_line + 1, '\t');
21911 if (tab != NULL)
21912 {
21913 *tab++ = '\0'; /* isolate the variable name */
21914 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021915 type = VAR_STRING;
21916#ifdef FEAT_FLOAT
21917 else if (*tab == 'F')
21918 type = VAR_FLOAT;
21919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920
21921 tab = vim_strchr(tab, '\t');
21922 if (tab != NULL)
21923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021924 tv.v_type = type;
21925 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021926 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021928#ifdef FEAT_FLOAT
21929 else if (type == VAR_FLOAT)
21930 (void)string2float(tab + 1, &tv.vval.v_float);
21931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021933 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021934 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021935 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021936 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 }
21938 }
21939 }
21940
21941 return viminfo_readline(virp);
21942}
21943
21944/*
21945 * Write global vars that start with a capital to the viminfo file
21946 */
21947 void
21948write_viminfo_varlist(fp)
21949 FILE *fp;
21950{
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 hashitem_T *hi;
21952 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021953 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021954 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021955 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021956 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021957 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958
21959 if (find_viminfo_parameter('!') == NULL)
21960 return;
21961
21962 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021963
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021964 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021965 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021967 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021969 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 this_var = HI2DI(hi);
21971 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021972 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021973 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021974 {
21975 case VAR_STRING: s = "STR"; break;
21976 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021977#ifdef FEAT_FLOAT
21978 case VAR_FLOAT: s = "FLO"; break;
21979#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021980 default: continue;
21981 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021983 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021984 if (p != NULL)
21985 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021986 vim_free(tofree);
21987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 }
21989 }
21990}
21991#endif
21992
21993#if defined(FEAT_SESSION) || defined(PROTO)
21994 int
21995store_session_globals(fd)
21996 FILE *fd;
21997{
Bram Moolenaar33570922005-01-25 22:26:29 +000021998 hashitem_T *hi;
21999 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022000 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 char_u *p, *t;
22002
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022003 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022006 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022008 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 this_var = HI2DI(hi);
22010 if ((this_var->di_tv.v_type == VAR_NUMBER
22011 || this_var->di_tv.v_type == VAR_STRING)
22012 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022013 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022014 /* Escape special characters with a backslash. Turn a LF and
22015 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022016 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022017 (char_u *)"\\\"\n\r");
22018 if (p == NULL) /* out of memory */
22019 break;
22020 for (t = p; *t != NUL; ++t)
22021 if (*t == '\n')
22022 *t = 'n';
22023 else if (*t == '\r')
22024 *t = 'r';
22025 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 this_var->di_key,
22027 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22028 : ' ',
22029 p,
22030 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22031 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022032 || put_eol(fd) == FAIL)
22033 {
22034 vim_free(p);
22035 return FAIL;
22036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 vim_free(p);
22038 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022039#ifdef FEAT_FLOAT
22040 else if (this_var->di_tv.v_type == VAR_FLOAT
22041 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22042 {
22043 float_T f = this_var->di_tv.vval.v_float;
22044 int sign = ' ';
22045
22046 if (f < 0)
22047 {
22048 f = -f;
22049 sign = '-';
22050 }
22051 if ((fprintf(fd, "let %s = %c&%f",
22052 this_var->di_key, sign, f) < 0)
22053 || put_eol(fd) == FAIL)
22054 return FAIL;
22055 }
22056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 }
22058 }
22059 return OK;
22060}
22061#endif
22062
Bram Moolenaar661b1822005-07-28 22:36:45 +000022063/*
22064 * Display script name where an item was last set.
22065 * Should only be invoked when 'verbose' is non-zero.
22066 */
22067 void
22068last_set_msg(scriptID)
22069 scid_T scriptID;
22070{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022071 char_u *p;
22072
Bram Moolenaar661b1822005-07-28 22:36:45 +000022073 if (scriptID != 0)
22074 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022075 p = home_replace_save(NULL, get_scriptname(scriptID));
22076 if (p != NULL)
22077 {
22078 verbose_enter();
22079 MSG_PUTS(_("\n\tLast set from "));
22080 MSG_PUTS(p);
22081 vim_free(p);
22082 verbose_leave();
22083 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022084 }
22085}
22086
Bram Moolenaard812df62008-11-09 12:46:09 +000022087/*
22088 * List v:oldfiles in a nice way.
22089 */
22090/*ARGSUSED*/
22091 void
22092ex_oldfiles(eap)
22093 exarg_T *eap;
22094{
22095 list_T *l = vimvars[VV_OLDFILES].vv_list;
22096 listitem_T *li;
22097 int nr = 0;
22098
22099 if (l == NULL)
22100 msg((char_u *)_("No old files"));
22101 else
22102 {
22103 msg_start();
22104 msg_scroll = TRUE;
22105 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22106 {
22107 msg_outnum((long)++nr);
22108 MSG_PUTS(": ");
22109 msg_outtrans(get_tv_string(&li->li_tv));
22110 msg_putchar('\n');
22111 out_flush(); /* output one line at a time */
22112 ui_breakcheck();
22113 }
22114 /* Assume "got_int" was set to truncate the listing. */
22115 got_int = FALSE;
22116
22117#ifdef FEAT_BROWSE_CMD
22118 if (cmdmod.browse)
22119 {
22120 quit_more = FALSE;
22121 nr = prompt_for_number(FALSE);
22122 msg_starthere();
22123 if (nr > 0)
22124 {
22125 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22126 (long)nr);
22127
22128 if (p != NULL)
22129 {
22130 p = expand_env_save(p);
22131 eap->arg = p;
22132 eap->cmdidx = CMD_edit;
22133 cmdmod.browse = FALSE;
22134 do_exedit(eap, NULL);
22135 vim_free(p);
22136 }
22137 }
22138 }
22139#endif
22140 }
22141}
22142
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143#endif /* FEAT_EVAL */
22144
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147
22148#ifdef WIN3264
22149/*
22150 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22151 */
22152static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22153static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22154static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22155
22156/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022157 * Get the short path (8.3) for the filename in "fnamep".
22158 * Only works for a valid file name.
22159 * When the path gets longer "fnamep" is changed and the allocated buffer
22160 * is put in "bufp".
22161 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22162 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 */
22164 static int
22165get_short_pathname(fnamep, bufp, fnamelen)
22166 char_u **fnamep;
22167 char_u **bufp;
22168 int *fnamelen;
22169{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022170 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 char_u *newbuf;
22172
22173 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 l = GetShortPathName(*fnamep, *fnamep, len);
22175 if (l > len - 1)
22176 {
22177 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022178 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 newbuf = vim_strnsave(*fnamep, l);
22180 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182
22183 vim_free(*bufp);
22184 *fnamep = *bufp = newbuf;
22185
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022186 /* Really should always succeed, as the buffer is big enough. */
22187 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 }
22189
22190 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022191 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192}
22193
22194/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022195 * Get the short path (8.3) for the filename in "fname". The converted
22196 * path is returned in "bufp".
22197 *
22198 * Some of the directories specified in "fname" may not exist. This function
22199 * will shorten the existing directories at the beginning of the path and then
22200 * append the remaining non-existing path.
22201 *
22202 * fname - Pointer to the filename to shorten. On return, contains the
22203 * pointer to the shortened pathname
22204 * bufp - Pointer to an allocated buffer for the filename.
22205 * fnamelen - Length of the filename pointed to by fname
22206 *
22207 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 */
22209 static int
22210shortpath_for_invalid_fname(fname, bufp, fnamelen)
22211 char_u **fname;
22212 char_u **bufp;
22213 int *fnamelen;
22214{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022215 char_u *short_fname, *save_fname, *pbuf_unused;
22216 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022218 int old_len, len;
22219 int new_len, sfx_len;
22220 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221
22222 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022223 old_len = *fnamelen;
22224 save_fname = vim_strnsave(*fname, old_len);
22225 pbuf_unused = NULL;
22226 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022228 endp = save_fname + old_len - 1; /* Find the end of the copy */
22229 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022231 /*
22232 * Try shortening the supplied path till it succeeds by removing one
22233 * directory at a time from the tail of the path.
22234 */
22235 len = 0;
22236 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022238 /* go back one path-separator */
22239 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22240 --endp;
22241 if (endp <= save_fname)
22242 break; /* processed the complete path */
22243
22244 /*
22245 * Replace the path separator with a NUL and try to shorten the
22246 * resulting path.
22247 */
22248 ch = *endp;
22249 *endp = 0;
22250 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022251 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022252 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22253 {
22254 retval = FAIL;
22255 goto theend;
22256 }
22257 *endp = ch; /* preserve the string */
22258
22259 if (len > 0)
22260 break; /* successfully shortened the path */
22261
22262 /* failed to shorten the path. Skip the path separator */
22263 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 }
22265
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022266 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022268 /*
22269 * Succeeded in shortening the path. Now concatenate the shortened
22270 * path with the remaining path at the tail.
22271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022273 /* Compute the length of the new path. */
22274 sfx_len = (int)(save_endp - endp) + 1;
22275 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022277 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022279 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022281 /* There is not enough space in the currently allocated string,
22282 * copy it to a buffer big enough. */
22283 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022285 {
22286 retval = FAIL;
22287 goto theend;
22288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290 else
22291 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022292 /* Transfer short_fname to the main buffer (it's big enough),
22293 * unless get_short_pathname() did its work in-place. */
22294 *fname = *bufp = save_fname;
22295 if (short_fname != save_fname)
22296 vim_strncpy(save_fname, short_fname, len);
22297 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022299
22300 /* concat the not-shortened part of the path */
22301 vim_strncpy(*fname + len, endp, sfx_len);
22302 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022304
22305theend:
22306 vim_free(pbuf_unused);
22307 vim_free(save_fname);
22308
22309 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310}
22311
22312/*
22313 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022314 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 */
22316 static int
22317shortpath_for_partial(fnamep, bufp, fnamelen)
22318 char_u **fnamep;
22319 char_u **bufp;
22320 int *fnamelen;
22321{
22322 int sepcount, len, tflen;
22323 char_u *p;
22324 char_u *pbuf, *tfname;
22325 int hasTilde;
22326
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022327 /* Count up the path separators from the RHS.. so we know which part
22328 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022330 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 if (vim_ispathsep(*p))
22332 ++sepcount;
22333
22334 /* Need full path first (use expand_env() to remove a "~/") */
22335 hasTilde = (**fnamep == '~');
22336 if (hasTilde)
22337 pbuf = tfname = expand_env_save(*fnamep);
22338 else
22339 pbuf = tfname = FullName_save(*fnamep, FALSE);
22340
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022341 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022343 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345
22346 if (len == 0)
22347 {
22348 /* Don't have a valid filename, so shorten the rest of the
22349 * path if we can. This CAN give us invalid 8.3 filenames, but
22350 * there's not a lot of point in guessing what it might be.
22351 */
22352 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022353 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 }
22356
22357 /* Count the paths backward to find the beginning of the desired string. */
22358 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022359 {
22360#ifdef FEAT_MBYTE
22361 if (has_mbyte)
22362 p -= mb_head_off(tfname, p);
22363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 if (vim_ispathsep(*p))
22365 {
22366 if (sepcount == 0 || (hasTilde && sepcount == 1))
22367 break;
22368 else
22369 sepcount --;
22370 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 if (hasTilde)
22373 {
22374 --p;
22375 if (p >= tfname)
22376 *p = '~';
22377 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 }
22380 else
22381 ++p;
22382
22383 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22384 vim_free(*bufp);
22385 *fnamelen = (int)STRLEN(p);
22386 *bufp = pbuf;
22387 *fnamep = p;
22388
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022389 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390}
22391#endif /* WIN3264 */
22392
22393/*
22394 * Adjust a filename, according to a string of modifiers.
22395 * *fnamep must be NUL terminated when called. When returning, the length is
22396 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022397 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 * When there is an error, *fnamep is set to NULL.
22399 */
22400 int
22401modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22402 char_u *src; /* string with modifiers */
22403 int *usedlen; /* characters after src that are used */
22404 char_u **fnamep; /* file name so far */
22405 char_u **bufp; /* buffer for allocated file name or NULL */
22406 int *fnamelen; /* length of fnamep */
22407{
22408 int valid = 0;
22409 char_u *tail;
22410 char_u *s, *p, *pbuf;
22411 char_u dirname[MAXPATHL];
22412 int c;
22413 int has_fullname = 0;
22414#ifdef WIN3264
22415 int has_shortname = 0;
22416#endif
22417
22418repeat:
22419 /* ":p" - full path/file_name */
22420 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22421 {
22422 has_fullname = 1;
22423
22424 valid |= VALID_PATH;
22425 *usedlen += 2;
22426
22427 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22428 if ((*fnamep)[0] == '~'
22429#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22430 && ((*fnamep)[1] == '/'
22431# ifdef BACKSLASH_IN_FILENAME
22432 || (*fnamep)[1] == '\\'
22433# endif
22434 || (*fnamep)[1] == NUL)
22435
22436#endif
22437 )
22438 {
22439 *fnamep = expand_env_save(*fnamep);
22440 vim_free(*bufp); /* free any allocated file name */
22441 *bufp = *fnamep;
22442 if (*fnamep == NULL)
22443 return -1;
22444 }
22445
22446 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022447 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 {
22449 if (vim_ispathsep(*p)
22450 && p[1] == '.'
22451 && (p[2] == NUL
22452 || vim_ispathsep(p[2])
22453 || (p[2] == '.'
22454 && (p[3] == NUL || vim_ispathsep(p[3])))))
22455 break;
22456 }
22457
22458 /* FullName_save() is slow, don't use it when not needed. */
22459 if (*p != NUL || !vim_isAbsName(*fnamep))
22460 {
22461 *fnamep = FullName_save(*fnamep, *p != NUL);
22462 vim_free(*bufp); /* free any allocated file name */
22463 *bufp = *fnamep;
22464 if (*fnamep == NULL)
22465 return -1;
22466 }
22467
22468 /* Append a path separator to a directory. */
22469 if (mch_isdir(*fnamep))
22470 {
22471 /* Make room for one or two extra characters. */
22472 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22473 vim_free(*bufp); /* free any allocated file name */
22474 *bufp = *fnamep;
22475 if (*fnamep == NULL)
22476 return -1;
22477 add_pathsep(*fnamep);
22478 }
22479 }
22480
22481 /* ":." - path relative to the current directory */
22482 /* ":~" - path relative to the home directory */
22483 /* ":8" - shortname path - postponed till after */
22484 while (src[*usedlen] == ':'
22485 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22486 {
22487 *usedlen += 2;
22488 if (c == '8')
22489 {
22490#ifdef WIN3264
22491 has_shortname = 1; /* Postpone this. */
22492#endif
22493 continue;
22494 }
22495 pbuf = NULL;
22496 /* Need full path first (use expand_env() to remove a "~/") */
22497 if (!has_fullname)
22498 {
22499 if (c == '.' && **fnamep == '~')
22500 p = pbuf = expand_env_save(*fnamep);
22501 else
22502 p = pbuf = FullName_save(*fnamep, FALSE);
22503 }
22504 else
22505 p = *fnamep;
22506
22507 has_fullname = 0;
22508
22509 if (p != NULL)
22510 {
22511 if (c == '.')
22512 {
22513 mch_dirname(dirname, MAXPATHL);
22514 s = shorten_fname(p, dirname);
22515 if (s != NULL)
22516 {
22517 *fnamep = s;
22518 if (pbuf != NULL)
22519 {
22520 vim_free(*bufp); /* free any allocated file name */
22521 *bufp = pbuf;
22522 pbuf = NULL;
22523 }
22524 }
22525 }
22526 else
22527 {
22528 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22529 /* Only replace it when it starts with '~' */
22530 if (*dirname == '~')
22531 {
22532 s = vim_strsave(dirname);
22533 if (s != NULL)
22534 {
22535 *fnamep = s;
22536 vim_free(*bufp);
22537 *bufp = s;
22538 }
22539 }
22540 }
22541 vim_free(pbuf);
22542 }
22543 }
22544
22545 tail = gettail(*fnamep);
22546 *fnamelen = (int)STRLEN(*fnamep);
22547
22548 /* ":h" - head, remove "/file_name", can be repeated */
22549 /* Don't remove the first "/" or "c:\" */
22550 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22551 {
22552 valid |= VALID_HEAD;
22553 *usedlen += 2;
22554 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022555 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022556 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 *fnamelen = (int)(tail - *fnamep);
22558#ifdef VMS
22559 if (*fnamelen > 0)
22560 *fnamelen += 1; /* the path separator is part of the path */
22561#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022562 if (*fnamelen == 0)
22563 {
22564 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22565 p = vim_strsave((char_u *)".");
22566 if (p == NULL)
22567 return -1;
22568 vim_free(*bufp);
22569 *bufp = *fnamep = tail = p;
22570 *fnamelen = 1;
22571 }
22572 else
22573 {
22574 while (tail > s && !after_pathsep(s, tail))
22575 mb_ptr_back(*fnamep, tail);
22576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 }
22578
22579 /* ":8" - shortname */
22580 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22581 {
22582 *usedlen += 2;
22583#ifdef WIN3264
22584 has_shortname = 1;
22585#endif
22586 }
22587
22588#ifdef WIN3264
22589 /* Check shortname after we have done 'heads' and before we do 'tails'
22590 */
22591 if (has_shortname)
22592 {
22593 pbuf = NULL;
22594 /* Copy the string if it is shortened by :h */
22595 if (*fnamelen < (int)STRLEN(*fnamep))
22596 {
22597 p = vim_strnsave(*fnamep, *fnamelen);
22598 if (p == 0)
22599 return -1;
22600 vim_free(*bufp);
22601 *bufp = *fnamep = p;
22602 }
22603
22604 /* Split into two implementations - makes it easier. First is where
22605 * there isn't a full name already, second is where there is.
22606 */
22607 if (!has_fullname && !vim_isAbsName(*fnamep))
22608 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022609 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 return -1;
22611 }
22612 else
22613 {
22614 int l;
22615
22616 /* Simple case, already have the full-name
22617 * Nearly always shorter, so try first time. */
22618 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022619 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 return -1;
22621
22622 if (l == 0)
22623 {
22624 /* Couldn't find the filename.. search the paths.
22625 */
22626 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022627 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 return -1;
22629 }
22630 *fnamelen = l;
22631 }
22632 }
22633#endif /* WIN3264 */
22634
22635 /* ":t" - tail, just the basename */
22636 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22637 {
22638 *usedlen += 2;
22639 *fnamelen -= (int)(tail - *fnamep);
22640 *fnamep = tail;
22641 }
22642
22643 /* ":e" - extension, can be repeated */
22644 /* ":r" - root, without extension, can be repeated */
22645 while (src[*usedlen] == ':'
22646 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22647 {
22648 /* find a '.' in the tail:
22649 * - for second :e: before the current fname
22650 * - otherwise: The last '.'
22651 */
22652 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22653 s = *fnamep - 2;
22654 else
22655 s = *fnamep + *fnamelen - 1;
22656 for ( ; s > tail; --s)
22657 if (s[0] == '.')
22658 break;
22659 if (src[*usedlen + 1] == 'e') /* :e */
22660 {
22661 if (s > tail)
22662 {
22663 *fnamelen += (int)(*fnamep - (s + 1));
22664 *fnamep = s + 1;
22665#ifdef VMS
22666 /* cut version from the extension */
22667 s = *fnamep + *fnamelen - 1;
22668 for ( ; s > *fnamep; --s)
22669 if (s[0] == ';')
22670 break;
22671 if (s > *fnamep)
22672 *fnamelen = s - *fnamep;
22673#endif
22674 }
22675 else if (*fnamep <= tail)
22676 *fnamelen = 0;
22677 }
22678 else /* :r */
22679 {
22680 if (s > tail) /* remove one extension */
22681 *fnamelen = (int)(s - *fnamep);
22682 }
22683 *usedlen += 2;
22684 }
22685
22686 /* ":s?pat?foo?" - substitute */
22687 /* ":gs?pat?foo?" - global substitute */
22688 if (src[*usedlen] == ':'
22689 && (src[*usedlen + 1] == 's'
22690 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22691 {
22692 char_u *str;
22693 char_u *pat;
22694 char_u *sub;
22695 int sep;
22696 char_u *flags;
22697 int didit = FALSE;
22698
22699 flags = (char_u *)"";
22700 s = src + *usedlen + 2;
22701 if (src[*usedlen + 1] == 'g')
22702 {
22703 flags = (char_u *)"g";
22704 ++s;
22705 }
22706
22707 sep = *s++;
22708 if (sep)
22709 {
22710 /* find end of pattern */
22711 p = vim_strchr(s, sep);
22712 if (p != NULL)
22713 {
22714 pat = vim_strnsave(s, (int)(p - s));
22715 if (pat != NULL)
22716 {
22717 s = p + 1;
22718 /* find end of substitution */
22719 p = vim_strchr(s, sep);
22720 if (p != NULL)
22721 {
22722 sub = vim_strnsave(s, (int)(p - s));
22723 str = vim_strnsave(*fnamep, *fnamelen);
22724 if (sub != NULL && str != NULL)
22725 {
22726 *usedlen = (int)(p + 1 - src);
22727 s = do_string_sub(str, pat, sub, flags);
22728 if (s != NULL)
22729 {
22730 *fnamep = s;
22731 *fnamelen = (int)STRLEN(s);
22732 vim_free(*bufp);
22733 *bufp = s;
22734 didit = TRUE;
22735 }
22736 }
22737 vim_free(sub);
22738 vim_free(str);
22739 }
22740 vim_free(pat);
22741 }
22742 }
22743 /* after using ":s", repeat all the modifiers */
22744 if (didit)
22745 goto repeat;
22746 }
22747 }
22748
22749 return valid;
22750}
22751
22752/*
22753 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22754 * "flags" can be "g" to do a global substitute.
22755 * Returns an allocated string, NULL for error.
22756 */
22757 char_u *
22758do_string_sub(str, pat, sub, flags)
22759 char_u *str;
22760 char_u *pat;
22761 char_u *sub;
22762 char_u *flags;
22763{
22764 int sublen;
22765 regmatch_T regmatch;
22766 int i;
22767 int do_all;
22768 char_u *tail;
22769 garray_T ga;
22770 char_u *ret;
22771 char_u *save_cpo;
22772
22773 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22774 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022775 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776
22777 ga_init2(&ga, 1, 200);
22778
22779 do_all = (flags[0] == 'g');
22780
22781 regmatch.rm_ic = p_ic;
22782 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22783 if (regmatch.regprog != NULL)
22784 {
22785 tail = str;
22786 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22787 {
22788 /*
22789 * Get some space for a temporary buffer to do the substitution
22790 * into. It will contain:
22791 * - The text up to where the match is.
22792 * - The substituted text.
22793 * - The text after the match.
22794 */
22795 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22796 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22797 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22798 {
22799 ga_clear(&ga);
22800 break;
22801 }
22802
22803 /* copy the text up to where the match is */
22804 i = (int)(regmatch.startp[0] - tail);
22805 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22806 /* add the substituted text */
22807 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22808 + ga.ga_len + i, TRUE, TRUE, FALSE);
22809 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 /* avoid getting stuck on a match with an empty string */
22811 if (tail == regmatch.endp[0])
22812 {
22813 if (*tail == NUL)
22814 break;
22815 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22816 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 }
22818 else
22819 {
22820 tail = regmatch.endp[0];
22821 if (*tail == NUL)
22822 break;
22823 }
22824 if (!do_all)
22825 break;
22826 }
22827
22828 if (ga.ga_data != NULL)
22829 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22830
22831 vim_free(regmatch.regprog);
22832 }
22833
22834 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22835 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022836 if (p_cpo == empty_option)
22837 p_cpo = save_cpo;
22838 else
22839 /* Darn, evaluating {sub} expression changed the value. */
22840 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841
22842 return ret;
22843}
22844
22845#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */