blob: 89a77f80e3382a0015f184f0f304c8cb293c0b2f [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 Moolenaar798b30b2009-04-22 10:56:16 +00001288#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001289 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001292 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 retval = NULL;
1294 else
1295 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001296 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001297 {
1298 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001299 if (tv.vval.v_list != NULL)
1300 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001301 ga_append(&ga, NUL);
1302 retval = (char_u *)ga.ga_data;
1303 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001304#ifdef FEAT_FLOAT
1305 else if (convert && tv.v_type == VAR_FLOAT)
1306 {
1307 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1308 retval = vim_strsave(numbuf);
1309 }
1310#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001311 else
1312 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001320 * Call eval_to_string() without using current local variables and using
1321 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 */
1323 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001324eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 char_u *arg;
1326 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001327 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 char_u *retval;
1330 void *save_funccalp;
1331
1332 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001333 if (use_sandbox)
1334 ++sandbox;
1335 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001336 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001337 if (use_sandbox)
1338 --sandbox;
1339 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 restore_funccal(save_funccalp);
1341 return retval;
1342}
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344/*
1345 * Top level evaluation function, returning a number.
1346 * Evaluates "expr" silently.
1347 * Returns -1 for an error.
1348 */
1349 int
1350eval_to_number(expr)
1351 char_u *expr;
1352{
Bram Moolenaar33570922005-01-25 22:26:29 +00001353 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001355 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 ++emsg_off;
1358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 retval = -1;
1361 else
1362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001363 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366 --emsg_off;
1367
1368 return retval;
1369}
1370
Bram Moolenaara40058a2005-07-11 22:42:07 +00001371/*
1372 * Prepare v: variable "idx" to be used.
1373 * Save the current typeval in "save_tv".
1374 * When not used yet add the variable to the v: hashtable.
1375 */
1376 static void
1377prepare_vimvar(idx, save_tv)
1378 int idx;
1379 typval_T *save_tv;
1380{
1381 *save_tv = vimvars[idx].vv_tv;
1382 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1383 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1384}
1385
1386/*
1387 * Restore v: variable "idx" to typeval "save_tv".
1388 * When no longer defined, remove the variable from the v: hashtable.
1389 */
1390 static void
1391restore_vimvar(idx, save_tv)
1392 int idx;
1393 typval_T *save_tv;
1394{
1395 hashitem_T *hi;
1396
Bram Moolenaara40058a2005-07-11 22:42:07 +00001397 vimvars[idx].vv_tv = *save_tv;
1398 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1399 {
1400 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1401 if (HASHITEM_EMPTY(hi))
1402 EMSG2(_(e_intern2), "restore_vimvar()");
1403 else
1404 hash_remove(&vimvarht, hi);
1405 }
1406}
1407
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001408#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001409/*
1410 * Evaluate an expression to a list with suggestions.
1411 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001412 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001413 */
1414 list_T *
1415eval_spell_expr(badword, expr)
1416 char_u *badword;
1417 char_u *expr;
1418{
1419 typval_T save_val;
1420 typval_T rettv;
1421 list_T *list = NULL;
1422 char_u *p = skipwhite(expr);
1423
1424 /* Set "v:val" to the bad word. */
1425 prepare_vimvar(VV_VAL, &save_val);
1426 vimvars[VV_VAL].vv_type = VAR_STRING;
1427 vimvars[VV_VAL].vv_str = badword;
1428 if (p_verbose == 0)
1429 ++emsg_off;
1430
1431 if (eval1(&p, &rettv, TRUE) == OK)
1432 {
1433 if (rettv.v_type != VAR_LIST)
1434 clear_tv(&rettv);
1435 else
1436 list = rettv.vval.v_list;
1437 }
1438
1439 if (p_verbose == 0)
1440 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001441 restore_vimvar(VV_VAL, &save_val);
1442
1443 return list;
1444}
1445
1446/*
1447 * "list" is supposed to contain two items: a word and a number. Return the
1448 * word in "pp" and the number as the return value.
1449 * Return -1 if anything isn't right.
1450 * Used to get the good word and score from the eval_spell_expr() result.
1451 */
1452 int
1453get_spellword(list, pp)
1454 list_T *list;
1455 char_u **pp;
1456{
1457 listitem_T *li;
1458
1459 li = list->lv_first;
1460 if (li == NULL)
1461 return -1;
1462 *pp = get_tv_string(&li->li_tv);
1463
1464 li = li->li_next;
1465 if (li == NULL)
1466 return -1;
1467 return get_tv_number(&li->li_tv);
1468}
1469#endif
1470
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001471/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001472 * Top level evaluation function.
1473 * Returns an allocated typval_T with the result.
1474 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001475 */
1476 typval_T *
1477eval_expr(arg, nextcmd)
1478 char_u *arg;
1479 char_u **nextcmd;
1480{
1481 typval_T *tv;
1482
1483 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001484 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001485 {
1486 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001487 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001488 }
1489
1490 return tv;
1491}
1492
1493
Bram Moolenaar4f688582007-07-24 12:34:30 +00001494#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1495 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001498 * Uses argv[argc] for the function arguments. Only Number and String
1499 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001502 static int
1503call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 char_u *func;
1505 int argc;
1506 char_u **argv;
1507 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
Bram Moolenaar33570922005-01-25 22:26:29 +00001510 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 long n;
1512 int len;
1513 int i;
1514 int doesrange;
1515 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001516 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001518 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001520 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
1522 for (i = 0; i < argc; i++)
1523 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001524 /* Pass a NULL or empty argument as an empty string */
1525 if (argv[i] == NULL || *argv[i] == NUL)
1526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001527 argvars[i].v_type = VAR_STRING;
1528 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001529 continue;
1530 }
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 /* Recognize a number argument, the others must be strings. */
1533 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1534 if (len != 0 && len == (int)STRLEN(argv[i]))
1535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001536 argvars[i].v_type = VAR_NUMBER;
1537 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539 else
1540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001541 argvars[i].v_type = VAR_STRING;
1542 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544 }
1545
1546 if (safe)
1547 {
1548 save_funccalp = save_funccal();
1549 ++sandbox;
1550 }
1551
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001552 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1553 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (safe)
1557 {
1558 --sandbox;
1559 restore_funccal(save_funccalp);
1560 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 vim_free(argvars);
1562
1563 if (ret == FAIL)
1564 clear_tv(rettv);
1565
1566 return ret;
1567}
1568
Bram Moolenaar4f688582007-07-24 12:34:30 +00001569# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001571 * Call vimL function "func" and return the result as a string.
1572 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 * Uses argv[argc] for the function arguments.
1574 */
1575 void *
1576call_func_retstr(func, argc, argv, safe)
1577 char_u *func;
1578 int argc;
1579 char_u **argv;
1580 int safe; /* use the sandbox */
1581{
1582 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001583 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584
1585 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1586 return NULL;
1587
1588 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 return retval;
1591}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001592# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593
Bram Moolenaar4f688582007-07-24 12:34:30 +00001594# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001596 * Call vimL function "func" and return the result as a number.
1597 * Returns -1 when calling the function fails.
1598 * Uses argv[argc] for the function arguments.
1599 */
1600 long
1601call_func_retnr(func, argc, argv, safe)
1602 char_u *func;
1603 int argc;
1604 char_u **argv;
1605 int safe; /* use the sandbox */
1606{
1607 typval_T rettv;
1608 long retval;
1609
1610 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1611 return -1;
1612
1613 retval = get_tv_number_chk(&rettv, NULL);
1614 clear_tv(&rettv);
1615 return retval;
1616}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001617# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001618
1619/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001620 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001622 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 */
1624 void *
1625call_func_retlist(func, argc, argv, safe)
1626 char_u *func;
1627 int argc;
1628 char_u **argv;
1629 int safe; /* use the sandbox */
1630{
1631 typval_T rettv;
1632
1633 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1634 return NULL;
1635
1636 if (rettv.v_type != VAR_LIST)
1637 {
1638 clear_tv(&rettv);
1639 return NULL;
1640 }
1641
1642 return rettv.vval.v_list;
1643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645
Bram Moolenaar4f688582007-07-24 12:34:30 +00001646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647/*
1648 * Save the current function call pointer, and set it to NULL.
1649 * Used when executing autocommands and for ":source".
1650 */
1651 void *
1652save_funccal()
1653{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001654 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 current_funccal = NULL;
1657 return (void *)fc;
1658}
1659
1660 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001661restore_funccal(vfc)
1662 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001664 funccall_T *fc = (funccall_T *)vfc;
1665
1666 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
Bram Moolenaar05159a02005-02-26 23:04:13 +00001669#if defined(FEAT_PROFILE) || defined(PROTO)
1670/*
1671 * Prepare profiling for entering a child or something else that is not
1672 * counted for the script/function itself.
1673 * Should always be called in pair with prof_child_exit().
1674 */
1675 void
1676prof_child_enter(tm)
1677 proftime_T *tm; /* place to store waittime */
1678{
1679 funccall_T *fc = current_funccal;
1680
1681 if (fc != NULL && fc->func->uf_profiling)
1682 profile_start(&fc->prof_child);
1683 script_prof_save(tm);
1684}
1685
1686/*
1687 * Take care of time spent in a child.
1688 * Should always be called after prof_child_enter().
1689 */
1690 void
1691prof_child_exit(tm)
1692 proftime_T *tm; /* where waittime was stored */
1693{
1694 funccall_T *fc = current_funccal;
1695
1696 if (fc != NULL && fc->func->uf_profiling)
1697 {
1698 profile_end(&fc->prof_child);
1699 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1700 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1701 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1702 }
1703 script_prof_restore(tm);
1704}
1705#endif
1706
1707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#ifdef FEAT_FOLDING
1709/*
1710 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1711 * it in "*cp". Doesn't give error messages.
1712 */
1713 int
1714eval_foldexpr(arg, cp)
1715 char_u *arg;
1716 int *cp;
1717{
Bram Moolenaar33570922005-01-25 22:26:29 +00001718 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 int retval;
1720 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001721 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1722 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001725 if (use_sandbox)
1726 ++sandbox;
1727 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 retval = 0;
1731 else
1732 {
1733 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 if (tv.v_type == VAR_NUMBER)
1735 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001736 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 retval = 0;
1738 else
1739 {
1740 /* If the result is a string, check if there is a non-digit before
1741 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (!VIM_ISDIGIT(*s) && *s != '-')
1744 *cp = *s++;
1745 retval = atol((char *)s);
1746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001750 if (use_sandbox)
1751 --sandbox;
1752 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 return retval;
1755}
1756#endif
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 * ":let" list all variable values
1760 * ":let var1 var2" list variable values
1761 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 * ":let var += expr" assignment command.
1763 * ":let var -= expr" assignment command.
1764 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
1767 void
1768ex_let(eap)
1769 exarg_T *eap;
1770{
1771 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 int var_count = 0;
1776 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001778 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001779 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaardb552d602006-03-23 22:59:57 +00001781 argend = skip_var_list(arg, &var_count, &semicolon);
1782 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001784 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1785 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001786 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001789 /*
1790 * ":let" without "=": list variables
1791 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 if (*arg == '[')
1793 EMSG(_(e_invarg));
1794 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001796 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001800 list_glob_vars(&first);
1801 list_buf_vars(&first);
1802 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001803#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001804 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001805#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001806 list_script_vars(&first);
1807 list_func_vars(&first);
1808 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 eap->nextcmd = check_nextcmd(arg);
1811 }
1812 else
1813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001814 op[0] = '=';
1815 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001816 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 {
1818 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1819 op[0] = expr[-1]; /* +=, -= or .= */
1820 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (eap->skip)
1824 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (eap->skip)
1827 {
1828 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 --emsg_skip;
1831 }
1832 else if (i != FAIL)
1833 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 }
1839}
1840
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841/*
1842 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1843 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 * When "nextchars" is not NULL it points to a string with characters that
1845 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1846 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 * Returns OK or FAIL;
1848 */
1849 static int
1850ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1851 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 int copy; /* copy values from "tv", don't move */
1854 int semicolon; /* from skip_var_list() */
1855 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001856 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857{
1858 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001859 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 listitem_T *item;
1862 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863
1864 if (*arg != '[')
1865 {
1866 /*
1867 * ":let var = expr" or ":for var in list"
1868 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 return FAIL;
1871 return OK;
1872 }
1873
1874 /*
1875 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1876 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001877 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 {
1879 EMSG(_(e_listreq));
1880 return FAIL;
1881 }
1882
1883 i = list_len(l);
1884 if (semicolon == 0 && var_count < i)
1885 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001886 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 return FAIL;
1888 }
1889 if (var_count - semicolon > i)
1890 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001891 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 return FAIL;
1893 }
1894
1895 item = l->lv_first;
1896 while (*arg != ']')
1897 {
1898 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 item = item->li_next;
1901 if (arg == NULL)
1902 return FAIL;
1903
1904 arg = skipwhite(arg);
1905 if (*arg == ';')
1906 {
1907 /* Put the rest of the list (may be empty) in the var after ';'.
1908 * Create a new list for this. */
1909 l = list_alloc();
1910 if (l == NULL)
1911 return FAIL;
1912 while (item != NULL)
1913 {
1914 list_append_tv(l, &item->li_tv);
1915 item = item->li_next;
1916 }
1917
1918 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001919 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 ltv.vval.v_list = l;
1921 l->lv_refcount = 1;
1922
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1924 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 clear_tv(&ltv);
1926 if (arg == NULL)
1927 return FAIL;
1928 break;
1929 }
1930 else if (*arg != ',' && *arg != ']')
1931 {
1932 EMSG2(_(e_intern2), "ex_let_vars()");
1933 return FAIL;
1934 }
1935 }
1936
1937 return OK;
1938}
1939
1940/*
1941 * Skip over assignable variable "var" or list of variables "[var, var]".
1942 * Used for ":let varvar = expr" and ":for varvar in expr".
1943 * For "[var, var]" increment "*var_count" for each variable.
1944 * for "[var, var; var]" set "semicolon".
1945 * Return NULL for an error.
1946 */
1947 static char_u *
1948skip_var_list(arg, var_count, semicolon)
1949 char_u *arg;
1950 int *var_count;
1951 int *semicolon;
1952{
1953 char_u *p, *s;
1954
1955 if (*arg == '[')
1956 {
1957 /* "[var, var]": find the matching ']'. */
1958 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001959 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 {
1961 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1962 s = skip_var_one(p);
1963 if (s == p)
1964 {
1965 EMSG2(_(e_invarg2), p);
1966 return NULL;
1967 }
1968 ++*var_count;
1969
1970 p = skipwhite(s);
1971 if (*p == ']')
1972 break;
1973 else if (*p == ';')
1974 {
1975 if (*semicolon == 1)
1976 {
1977 EMSG(_("Double ; in list of variables"));
1978 return NULL;
1979 }
1980 *semicolon = 1;
1981 }
1982 else if (*p != ',')
1983 {
1984 EMSG2(_(e_invarg2), p);
1985 return NULL;
1986 }
1987 }
1988 return p + 1;
1989 }
1990 else
1991 return skip_var_one(arg);
1992}
1993
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001995 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001996 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001997 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 static char_u *
1999skip_var_one(arg)
2000 char_u *arg;
2001{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002002 if (*arg == '@' && arg[1] != NUL)
2003 return arg + 2;
2004 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2005 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006}
2007
Bram Moolenaara7043832005-01-21 11:56:39 +00002008/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002009 * List variables for hashtab "ht" with prefix "prefix".
2010 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002011 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002014 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002015 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002016 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002017 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002018{
Bram Moolenaar33570922005-01-25 22:26:29 +00002019 hashitem_T *hi;
2020 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002021 int todo;
2022
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002023 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002024 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2025 {
2026 if (!HASHITEM_EMPTY(hi))
2027 {
2028 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002029 di = HI2DI(hi);
2030 if (empty || di->di_tv.v_type != VAR_STRING
2031 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002032 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002033 }
2034 }
2035}
2036
2037/*
2038 * List global variables.
2039 */
2040 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002041list_glob_vars(first)
2042 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002043{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002044 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002045}
2046
2047/*
2048 * List buffer variables.
2049 */
2050 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051list_buf_vars(first)
2052 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002053{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054 char_u numbuf[NUMBUFLEN];
2055
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002056 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2057 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058
2059 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002060 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2061 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002062}
2063
2064/*
2065 * List window variables.
2066 */
2067 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002068list_win_vars(first)
2069 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002070{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2072 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002073}
2074
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002075#ifdef FEAT_WINDOWS
2076/*
2077 * List tab page variables.
2078 */
2079 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002080list_tab_vars(first)
2081 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002082{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2084 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002085}
2086#endif
2087
Bram Moolenaara7043832005-01-21 11:56:39 +00002088/*
2089 * List Vim variables.
2090 */
2091 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092list_vim_vars(first)
2093 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096}
2097
2098/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002099 * List script-local variables, if there is a script.
2100 */
2101 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102list_script_vars(first)
2103 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002104{
2105 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2107 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002108}
2109
2110/*
2111 * List function variables, if there is a function.
2112 */
2113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_func_vars(first)
2115 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002116{
2117 if (current_funccal != NULL)
2118 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002120}
2121
2122/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 * List variables in "arg".
2124 */
2125 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 exarg_T *eap;
2128 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130{
2131 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002134 char_u *name_start;
2135 char_u *arg_subsc;
2136 char_u *tofree;
2137 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138
2139 while (!ends_excmd(*arg) && !got_int)
2140 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002141 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002143 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2145 {
2146 emsg_severe = TRUE;
2147 EMSG(_(e_trailing));
2148 break;
2149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002153 /* get_name_len() takes care of expanding curly braces */
2154 name_start = name = arg;
2155 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2156 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 /* This is mainly to keep test 49 working: when expanding
2159 * curly braces fails overrule the exception error message. */
2160 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 emsg_severe = TRUE;
2163 EMSG2(_(e_invarg2), arg);
2164 break;
2165 }
2166 error = TRUE;
2167 }
2168 else
2169 {
2170 if (tofree != NULL)
2171 name = tofree;
2172 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 else
2175 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176 /* handle d.key, l[idx], f(expr) */
2177 arg_subsc = arg;
2178 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002183 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 case 'g': list_glob_vars(first); break;
2187 case 'b': list_buf_vars(first); break;
2188 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002191#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 case 'v': list_vim_vars(first); break;
2193 case 's': list_script_vars(first); break;
2194 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 default:
2196 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 }
2199 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 {
2201 char_u numbuf[NUMBUFLEN];
2202 char_u *tf;
2203 int c;
2204 char_u *s;
2205
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002206 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 c = *arg;
2208 *arg = NUL;
2209 list_one_var_a((char_u *)"",
2210 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 tv.v_type,
2212 s == NULL ? (char_u *)"" : s,
2213 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 *arg = c;
2215 vim_free(tf);
2216 }
2217 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 }
2220 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221
2222 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224
2225 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 }
2227
2228 return arg;
2229}
2230
2231/*
2232 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2233 * Returns a pointer to the char just after the var name.
2234 * Returns NULL if there is an error.
2235 */
2236 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002237ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002239 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002241 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243{
2244 int c1;
2245 char_u *name;
2246 char_u *p;
2247 char_u *arg_end = NULL;
2248 int len;
2249 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251
2252 /*
2253 * ":let $VAR = expr": Set environment variable.
2254 */
2255 if (*arg == '$')
2256 {
2257 /* Find the end of the name. */
2258 ++arg;
2259 name = arg;
2260 len = get_env_len(&arg);
2261 if (len == 0)
2262 EMSG2(_(e_invarg2), name - 1);
2263 else
2264 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265 if (op != NULL && (*op == '+' || *op == '-'))
2266 EMSG2(_(e_letwrong), op);
2267 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002268 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 EMSG(_(e_letunexp));
2270 else
2271 {
2272 c1 = name[len];
2273 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 p = get_tv_string_chk(tv);
2275 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 {
2277 int mustfree = FALSE;
2278 char_u *s = vim_getenv(name, &mustfree);
2279
2280 if (s != NULL)
2281 {
2282 p = tofree = concat_str(s, p);
2283 if (mustfree)
2284 vim_free(s);
2285 }
2286 }
2287 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002290 if (STRICMP(name, "HOME") == 0)
2291 init_homedir();
2292 else if (didset_vim && STRICMP(name, "VIM") == 0)
2293 didset_vim = FALSE;
2294 else if (didset_vimruntime
2295 && STRICMP(name, "VIMRUNTIME") == 0)
2296 didset_vimruntime = FALSE;
2297 arg_end = arg;
2298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
2302 }
2303 }
2304
2305 /*
2306 * ":let &option = expr": Set option value.
2307 * ":let &l:option = expr": Set local option value.
2308 * ":let &g:option = expr": Set global option value.
2309 */
2310 else if (*arg == '&')
2311 {
2312 /* Find the end of the name. */
2313 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002314 if (p == NULL || (endchars != NULL
2315 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 EMSG(_(e_letunexp));
2317 else
2318 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 long n;
2320 int opt_type;
2321 long numval;
2322 char_u *stringval = NULL;
2323 char_u *s;
2324
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 c1 = *p;
2326 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327
2328 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002329 s = get_tv_string_chk(tv); /* != NULL if number or string */
2330 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 {
2332 opt_type = get_option_value(arg, &numval,
2333 &stringval, opt_flags);
2334 if ((opt_type == 1 && *op == '.')
2335 || (opt_type == 0 && *op != '.'))
2336 EMSG2(_(e_letwrong), op);
2337 else
2338 {
2339 if (opt_type == 1) /* number */
2340 {
2341 if (*op == '+')
2342 n = numval + n;
2343 else
2344 n = numval - n;
2345 }
2346 else if (opt_type == 0 && stringval != NULL) /* string */
2347 {
2348 s = concat_str(stringval, s);
2349 vim_free(stringval);
2350 stringval = s;
2351 }
2352 }
2353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (s != NULL)
2355 {
2356 set_option_value(arg, n, s, opt_flags);
2357 arg_end = p;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363
2364 /*
2365 * ":let @r = expr": Set register contents.
2366 */
2367 else if (*arg == '@')
2368 {
2369 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 if (op != NULL && (*op == '+' || *op == '-'))
2371 EMSG2(_(e_letwrong), op);
2372 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
2375 else
2376 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002377 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 char_u *s;
2379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002380 p = get_tv_string_chk(tv);
2381 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002383 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 if (s != NULL)
2385 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002386 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 vim_free(s);
2388 }
2389 }
2390 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 arg_end = arg + 1;
2394 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002395 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 }
2397 }
2398
2399 /*
2400 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002403 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002405 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002407 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2411 EMSG(_(e_letunexp));
2412 else
2413 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 arg_end = p;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420
2421 else
2422 EMSG2(_(e_invarg2), arg);
2423
2424 return arg_end;
2425}
2426
2427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2429 */
2430 static int
2431check_changedtick(arg)
2432 char_u *arg;
2433{
2434 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2435 {
2436 EMSG2(_(e_readonlyvar), arg);
2437 return TRUE;
2438 }
2439 return FALSE;
2440}
2441
2442/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 * Get an lval: variable, Dict item or List item that can be assigned a value
2444 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2445 * "name.key", "name.key[expr]" etc.
2446 * Indexing only works if "name" is an existing List or Dictionary.
2447 * "name" points to the start of the name.
2448 * If "rettv" is not NULL it points to the value to be assigned.
2449 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2450 * wrong; must end in space or cmd separator.
2451 *
2452 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002453 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 */
2456 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002459 typval_T *rettv;
2460 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 int unlet;
2462 int skip;
2463 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002464 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 char_u *expr_start, *expr_end;
2468 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 dictitem_T *v;
2470 typval_T var1;
2471 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002479 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480
2481 if (skip)
2482 {
2483 /* When skipping just find the end of the name. */
2484 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 }
2487
2488 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002489 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 if (expr_start != NULL)
2491 {
2492 /* Don't expand the name when we already know there is an error. */
2493 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2494 && *p != '[' && *p != '.')
2495 {
2496 EMSG(_(e_trailing));
2497 return NULL;
2498 }
2499
2500 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2501 if (lp->ll_exp_name == NULL)
2502 {
2503 /* Report an invalid expression in braces, unless the
2504 * expression evaluation has been cancelled due to an
2505 * aborting error, an interrupt, or an exception. */
2506 if (!aborting() && !quiet)
2507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002508 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 EMSG2(_(e_invarg2), name);
2510 return NULL;
2511 }
2512 }
2513 lp->ll_name = lp->ll_exp_name;
2514 }
2515 else
2516 lp->ll_name = name;
2517
2518 /* Without [idx] or .key we are done. */
2519 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2520 return p;
2521
2522 cc = *p;
2523 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002524 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (v == NULL && !quiet)
2526 EMSG2(_(e_undefvar), lp->ll_name);
2527 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 if (v == NULL)
2529 return NULL;
2530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 /*
2532 * Loop until no more [idx] or .key is following.
2533 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2538 && !(lp->ll_tv->v_type == VAR_DICT
2539 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (!quiet)
2542 EMSG(_("E689: Can only index a List or Dictionary"));
2543 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 if (!quiet)
2548 EMSG(_("E708: [:] must come last"));
2549 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 len = -1;
2553 if (*p == '.')
2554 {
2555 key = p + 1;
2556 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2557 ;
2558 if (len == 0)
2559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (!quiet)
2561 EMSG(_(e_emptykey));
2562 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 }
2564 p = key + len;
2565 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002566 else
2567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002569 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 if (*p == ':')
2571 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002572 else
2573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 empty1 = FALSE;
2575 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 if (get_tv_string_chk(&var1) == NULL)
2578 {
2579 /* not a number or string */
2580 clear_tv(&var1);
2581 return NULL;
2582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 }
2584
2585 /* Optionally get the second index [ :expr]. */
2586 if (*p == ':')
2587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 if (!empty1)
2593 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (rettv != NULL && (rettv->v_type != VAR_LIST
2597 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!quiet)
2600 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (!empty1)
2602 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
2605 p = skipwhite(p + 1);
2606 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 else
2609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 if (!empty1)
2614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002617 if (get_tv_string_chk(&var2) == NULL)
2618 {
2619 /* not a number or string */
2620 if (!empty1)
2621 clear_tv(&var1);
2622 clear_tv(&var2);
2623 return NULL;
2624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (!empty1)
2636 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641
2642 /* Skip to past ']'. */
2643 ++p;
2644 }
2645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
2648 if (len == -1)
2649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 if (*key == NUL)
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
2655 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 }
2659 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 lp->ll_list = NULL;
2661 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002665 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (len == -1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 }
2674 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (len == -1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 p = NULL;
2682 break;
2683 }
2684 if (len == -1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
2688 else
2689 {
2690 /*
2691 * Get the number and item for the only or first index of the List.
2692 */
2693 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 else
2696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var1);
2699 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 lp->ll_list = lp->ll_tv->vval.v_list;
2702 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2703 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002705 if (lp->ll_n1 < 0)
2706 {
2707 lp->ll_n1 = 0;
2708 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2709 }
2710 }
2711 if (lp->ll_li == NULL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717
2718 /*
2719 * May need to find the item or absolute index for the second
2720 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 * When no index given: "lp->ll_empty2" is TRUE.
2722 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
2735
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2737 if (lp->ll_n1 < 0)
2738 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2739 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return p;
2748}
2749
2750/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 */
2753 static void
2754clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756{
2757 vim_free(lp->ll_exp_name);
2758 vim_free(lp->ll_newkey);
2759}
2760
2761/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002762 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 */
2766 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002768 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773{
2774 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002775 listitem_T *ri;
2776 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777
2778 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 cc = *endp;
2783 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 if (op != NULL && *op != '=')
2785 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002786 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002788 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002789 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002790 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002791 {
2792 if (tv_op(&tv, rettv, op) == OK)
2793 set_var(lp->ll_name, &tv, FALSE);
2794 clear_tv(&tv);
2795 }
2796 }
2797 else
2798 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002802 else if (tv_check_lock(lp->ll_newkey == NULL
2803 ? lp->ll_tv->v_lock
2804 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2805 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 else if (lp->ll_range)
2807 {
2808 /*
2809 * Assign the List values to the list items.
2810 */
2811 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002812 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 if (op != NULL && *op != '=')
2814 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2815 else
2816 {
2817 clear_tv(&lp->ll_li->li_tv);
2818 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2819 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 ri = ri->li_next;
2821 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2822 break;
2823 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002826 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 ri = NULL;
2829 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002830 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002831 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_li = lp->ll_li->li_next;
2833 ++lp->ll_n1;
2834 }
2835 if (ri != NULL)
2836 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 else if (lp->ll_empty2
2838 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 : lp->ll_n1 != lp->ll_n2)
2840 EMSG(_("E711: List value has not enough items"));
2841 }
2842 else
2843 {
2844 /*
2845 * Assign to a List or Dictionary item.
2846 */
2847 if (lp->ll_newkey != NULL)
2848 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 if (op != NULL && *op != '=')
2850 {
2851 EMSG2(_(e_letwrong), op);
2852 return;
2853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002856 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (di == NULL)
2858 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002859 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2860 {
2861 vim_free(di);
2862 return;
2863 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002866 else if (op != NULL && *op != '=')
2867 {
2868 tv_op(lp->ll_tv, rettv, op);
2869 return;
2870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 /*
2875 * Assign the value to the variable or list item.
2876 */
2877 if (copy)
2878 copy_tv(rettv, lp->ll_tv);
2879 else
2880 {
2881 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002882 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886}
2887
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2890 * Returns OK or FAIL.
2891 */
2892 static int
2893tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *tv1;
2895 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
2897{
2898 long n;
2899 char_u numbuf[NUMBUFLEN];
2900 char_u *s;
2901
2902 /* Can't do anything with a Funcref or a Dict on the right. */
2903 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2904 {
2905 switch (tv1->v_type)
2906 {
2907 case VAR_DICT:
2908 case VAR_FUNC:
2909 break;
2910
2911 case VAR_LIST:
2912 if (*op != '+' || tv2->v_type != VAR_LIST)
2913 break;
2914 /* List += List */
2915 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2916 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2917 return OK;
2918
2919 case VAR_NUMBER:
2920 case VAR_STRING:
2921 if (tv2->v_type == VAR_LIST)
2922 break;
2923 if (*op == '+' || *op == '-')
2924 {
2925 /* nr += nr or nr -= nr*/
2926 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002927#ifdef FEAT_FLOAT
2928 if (tv2->v_type == VAR_FLOAT)
2929 {
2930 float_T f = n;
2931
2932 if (*op == '+')
2933 f += tv2->vval.v_float;
2934 else
2935 f -= tv2->vval.v_float;
2936 clear_tv(tv1);
2937 tv1->v_type = VAR_FLOAT;
2938 tv1->vval.v_float = f;
2939 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002941#endif
2942 {
2943 if (*op == '+')
2944 n += get_tv_number(tv2);
2945 else
2946 n -= get_tv_number(tv2);
2947 clear_tv(tv1);
2948 tv1->v_type = VAR_NUMBER;
2949 tv1->vval.v_number = n;
2950 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 }
2952 else
2953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002954 if (tv2->v_type == VAR_FLOAT)
2955 break;
2956
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 /* str .= str */
2958 s = get_tv_string(tv1);
2959 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2960 clear_tv(tv1);
2961 tv1->v_type = VAR_STRING;
2962 tv1->vval.v_string = s;
2963 }
2964 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965
2966#ifdef FEAT_FLOAT
2967 case VAR_FLOAT:
2968 {
2969 float_T f;
2970
2971 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2972 && tv2->v_type != VAR_NUMBER
2973 && tv2->v_type != VAR_STRING))
2974 break;
2975 if (tv2->v_type == VAR_FLOAT)
2976 f = tv2->vval.v_float;
2977 else
2978 f = get_tv_number(tv2);
2979 if (*op == '+')
2980 tv1->vval.v_float += f;
2981 else
2982 tv1->vval.v_float -= f;
2983 }
2984 return OK;
2985#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 }
2987 }
2988
2989 EMSG2(_(e_letwrong), op);
2990 return FAIL;
2991}
2992
2993/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 * Add a watcher to a list.
2995 */
2996 static void
2997list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002998 list_T *l;
2999 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000{
3001 lw->lw_next = l->lv_watch;
3002 l->lv_watch = lw;
3003}
3004
3005/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 * No warning when it isn't found...
3008 */
3009 static void
3010list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 list_T *l;
3012 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013{
Bram Moolenaar33570922005-01-25 22:26:29 +00003014 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015
3016 lwp = &l->lv_watch;
3017 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3018 {
3019 if (lw == lwrem)
3020 {
3021 *lwp = lw->lw_next;
3022 break;
3023 }
3024 lwp = &lw->lw_next;
3025 }
3026}
3027
3028/*
3029 * Just before removing an item from a list: advance watchers to the next
3030 * item.
3031 */
3032 static void
3033list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003034 list_T *l;
3035 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003036{
Bram Moolenaar33570922005-01-25 22:26:29 +00003037 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003038
3039 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3040 if (lw->lw_item == item)
3041 lw->lw_item = item->li_next;
3042}
3043
3044/*
3045 * Evaluate the expression used in a ":for var in expr" command.
3046 * "arg" points to "var".
3047 * Set "*errp" to TRUE for an error, FALSE otherwise;
3048 * Return a pointer that holds the info. Null when there is an error.
3049 */
3050 void *
3051eval_for_line(arg, errp, nextcmdp, skip)
3052 char_u *arg;
3053 int *errp;
3054 char_u **nextcmdp;
3055 int skip;
3056{
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 typval_T tv;
3060 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061
3062 *errp = TRUE; /* default: there is an error */
3063
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065 if (fi == NULL)
3066 return NULL;
3067
3068 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3069 if (expr == NULL)
3070 return fi;
3071
3072 expr = skipwhite(expr);
3073 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3074 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003075 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076 return fi;
3077 }
3078
3079 if (skip)
3080 ++emsg_skip;
3081 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3082 {
3083 *errp = FALSE;
3084 if (!skip)
3085 {
3086 l = tv.vval.v_list;
3087 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003088 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003090 clear_tv(&tv);
3091 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092 else
3093 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003094 /* No need to increment the refcount, it's already set for the
3095 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 fi->fi_list = l;
3097 list_add_watch(l, &fi->fi_lw);
3098 fi->fi_lw.lw_item = l->lv_first;
3099 }
3100 }
3101 }
3102 if (skip)
3103 --emsg_skip;
3104
3105 return fi;
3106}
3107
3108/*
3109 * Use the first item in a ":for" list. Advance to the next.
3110 * Assign the values to the variable (list). "arg" points to the first one.
3111 * Return TRUE when a valid item was found, FALSE when at end of list or
3112 * something wrong.
3113 */
3114 int
3115next_for_item(fi_void, arg)
3116 void *fi_void;
3117 char_u *arg;
3118{
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122
3123 item = fi->fi_lw.lw_item;
3124 if (item == NULL)
3125 result = FALSE;
3126 else
3127 {
3128 fi->fi_lw.lw_item = item->li_next;
3129 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3130 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3131 }
3132 return result;
3133}
3134
3135/*
3136 * Free the structure used to store info used by ":for".
3137 */
3138 void
3139free_for_info(fi_void)
3140 void *fi_void;
3141{
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003144 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003145 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147 list_unref(fi->fi_list);
3148 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149 vim_free(fi);
3150}
3151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3153
3154 void
3155set_context_for_expression(xp, arg, cmdidx)
3156 expand_T *xp;
3157 char_u *arg;
3158 cmdidx_T cmdidx;
3159{
3160 int got_eq = FALSE;
3161 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 if (cmdidx == CMD_let)
3165 {
3166 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003167 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 {
3169 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003170 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 {
3172 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 if (vim_iswhite(*p))
3175 break;
3176 }
3177 return;
3178 }
3179 }
3180 else
3181 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3182 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 while ((xp->xp_pattern = vim_strpbrk(arg,
3184 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3185 {
3186 c = *xp->xp_pattern;
3187 if (c == '&')
3188 {
3189 c = xp->xp_pattern[1];
3190 if (c == '&')
3191 {
3192 ++xp->xp_pattern;
3193 xp->xp_context = cmdidx != CMD_let || got_eq
3194 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3195 }
3196 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003199 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3200 xp->xp_pattern += 2;
3201
3202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 else if (c == '$')
3205 {
3206 /* environment variable */
3207 xp->xp_context = EXPAND_ENV_VARS;
3208 }
3209 else if (c == '=')
3210 {
3211 got_eq = TRUE;
3212 xp->xp_context = EXPAND_EXPRESSION;
3213 }
3214 else if (c == '<'
3215 && xp->xp_context == EXPAND_FUNCTIONS
3216 && vim_strchr(xp->xp_pattern, '(') == NULL)
3217 {
3218 /* Function name can start with "<SNR>" */
3219 break;
3220 }
3221 else if (cmdidx != CMD_let || got_eq)
3222 {
3223 if (c == '"') /* string */
3224 {
3225 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3226 if (c == '\\' && xp->xp_pattern[1] != NUL)
3227 ++xp->xp_pattern;
3228 xp->xp_context = EXPAND_NOTHING;
3229 }
3230 else if (c == '\'') /* literal string */
3231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3234 /* skip */ ;
3235 xp->xp_context = EXPAND_NOTHING;
3236 }
3237 else if (c == '|')
3238 {
3239 if (xp->xp_pattern[1] == '|')
3240 {
3241 ++xp->xp_pattern;
3242 xp->xp_context = EXPAND_EXPRESSION;
3243 }
3244 else
3245 xp->xp_context = EXPAND_COMMANDS;
3246 }
3247 else
3248 xp->xp_context = EXPAND_EXPRESSION;
3249 }
3250 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 /* Doesn't look like something valid, expand as an expression
3252 * anyway. */
3253 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 arg = xp->xp_pattern;
3255 if (*arg != NUL)
3256 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3257 /* skip */ ;
3258 }
3259 xp->xp_pattern = arg;
3260}
3261
3262#endif /* FEAT_CMDL_COMPL */
3263
3264/*
3265 * ":1,25call func(arg1, arg2)" function call.
3266 */
3267 void
3268ex_call(eap)
3269 exarg_T *eap;
3270{
3271 char_u *arg = eap->arg;
3272 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003274 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 linenr_T lnum;
3278 int doesrange;
3279 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003282 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003283 if (fudi.fd_newkey != NULL)
3284 {
3285 /* Still need to give an error message for missing key. */
3286 EMSG2(_(e_dictkey), fudi.fd_newkey);
3287 vim_free(fudi.fd_newkey);
3288 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003289 if (tofree == NULL)
3290 return;
3291
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003292 /* Increase refcount on dictionary, it could get deleted when evaluating
3293 * the arguments. */
3294 if (fudi.fd_dict != NULL)
3295 ++fudi.fd_dict->dv_refcount;
3296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003297 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003298 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003299 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar532c7802005-01-27 14:44:31 +00003301 /* Skip white space to allow ":call func ()". Not good, but required for
3302 * backward compatibility. */
3303 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003304 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 if (*startarg != '(')
3307 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003308 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 goto end;
3310 }
3311
3312 /*
3313 * When skipping, evaluate the function once, to find the end of the
3314 * arguments.
3315 * When the function takes a range, this is discovered after the first
3316 * call, and the loop is broken.
3317 */
3318 if (eap->skip)
3319 {
3320 ++emsg_skip;
3321 lnum = eap->line2; /* do it once, also with an invalid range */
3322 }
3323 else
3324 lnum = eap->line1;
3325 for ( ; lnum <= eap->line2; ++lnum)
3326 {
3327 if (!eap->skip && eap->addr_count > 0)
3328 {
3329 curwin->w_cursor.lnum = lnum;
3330 curwin->w_cursor.col = 0;
3331 }
3332 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003333 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003334 eap->line1, eap->line2, &doesrange,
3335 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 failed = TRUE;
3338 break;
3339 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003340
3341 /* Handle a function returning a Funcref, Dictionary or List. */
3342 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3343 {
3344 failed = TRUE;
3345 break;
3346 }
3347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (doesrange || eap->skip)
3350 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003353 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003355 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (aborting())
3357 break;
3358 }
3359 if (eap->skip)
3360 --emsg_skip;
3361
3362 if (!failed)
3363 {
3364 /* Check for trailing illegal characters and a following command. */
3365 if (!ends_excmd(*arg))
3366 {
3367 emsg_severe = TRUE;
3368 EMSG(_(e_trailing));
3369 }
3370 else
3371 eap->nextcmd = check_nextcmd(arg);
3372 }
3373
3374end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003375 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003376 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377}
3378
3379/*
3380 * ":unlet[!] var1 ... " command.
3381 */
3382 void
3383ex_unlet(eap)
3384 exarg_T *eap;
3385{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003386 ex_unletlock(eap, eap->arg, 0);
3387}
3388
3389/*
3390 * ":lockvar" and ":unlockvar" commands
3391 */
3392 void
3393ex_lockvar(eap)
3394 exarg_T *eap;
3395{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003397 int deep = 2;
3398
3399 if (eap->forceit)
3400 deep = -1;
3401 else if (vim_isdigit(*arg))
3402 {
3403 deep = getdigits(&arg);
3404 arg = skipwhite(arg);
3405 }
3406
3407 ex_unletlock(eap, arg, deep);
3408}
3409
3410/*
3411 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3412 */
3413 static void
3414ex_unletlock(eap, argstart, deep)
3415 exarg_T *eap;
3416 char_u *argstart;
3417 int deep;
3418{
3419 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003422 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
3424 do
3425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003426 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003427 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3428 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003429 if (lv.ll_name == NULL)
3430 error = TRUE; /* error but continue parsing */
3431 if (name_end == NULL || (!vim_iswhite(*name_end)
3432 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003434 if (name_end != NULL)
3435 {
3436 emsg_severe = TRUE;
3437 EMSG(_(e_trailing));
3438 }
3439 if (!(eap->skip || error))
3440 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
3442 }
3443
3444 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003445 {
3446 if (eap->cmdidx == CMD_unlet)
3447 {
3448 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3449 error = TRUE;
3450 }
3451 else
3452 {
3453 if (do_lock_var(&lv, name_end, deep,
3454 eap->cmdidx == CMD_lockvar) == FAIL)
3455 error = TRUE;
3456 }
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 if (!eap->skip)
3460 clear_lval(&lv);
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 arg = skipwhite(name_end);
3463 } while (!ends_excmd(*arg));
3464
3465 eap->nextcmd = check_nextcmd(arg);
3466}
3467
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003469do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003471 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 int forceit;
3473{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 int ret = OK;
3475 int cc;
3476
3477 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 cc = *name_end;
3480 *name_end = NUL;
3481
3482 /* Normal name or expanded name. */
3483 if (check_changedtick(lp->ll_name))
3484 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003485 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003486 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003488 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003489 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3490 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 else if (lp->ll_range)
3492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003493 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003494
3495 /* Delete a range of List items. */
3496 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3497 {
3498 li = lp->ll_li->li_next;
3499 listitem_remove(lp->ll_list, lp->ll_li);
3500 lp->ll_li = li;
3501 ++lp->ll_n1;
3502 }
3503 }
3504 else
3505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003506 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003507 /* unlet a List item. */
3508 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003509 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003510 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003511 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003512 }
3513
3514 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515}
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517/*
3518 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 */
3521 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003522do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
Bram Moolenaar33570922005-01-25 22:26:29 +00003526 hashtab_T *ht;
3527 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003528 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003529 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaar33570922005-01-25 22:26:29 +00003531 ht = find_var_ht(name, &varname);
3532 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003534 hi = hash_find(ht, varname);
3535 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003537 di = HI2DI(hi);
3538 if (var_check_fixed(di->di_flags, name)
3539 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003540 return FAIL;
3541 delete_var(ht, hi);
3542 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 if (forceit)
3546 return OK;
3547 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return FAIL;
3549}
3550
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003551/*
3552 * Lock or unlock variable indicated by "lp".
3553 * "deep" is the levels to go (-1 for unlimited);
3554 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3555 */
3556 static int
3557do_lock_var(lp, name_end, deep, lock)
3558 lval_T *lp;
3559 char_u *name_end;
3560 int deep;
3561 int lock;
3562{
3563 int ret = OK;
3564 int cc;
3565 dictitem_T *di;
3566
3567 if (deep == 0) /* nothing to do */
3568 return OK;
3569
3570 if (lp->ll_tv == NULL)
3571 {
3572 cc = *name_end;
3573 *name_end = NUL;
3574
3575 /* Normal name or expanded name. */
3576 if (check_changedtick(lp->ll_name))
3577 ret = FAIL;
3578 else
3579 {
3580 di = find_var(lp->ll_name, NULL);
3581 if (di == NULL)
3582 ret = FAIL;
3583 else
3584 {
3585 if (lock)
3586 di->di_flags |= DI_FLAGS_LOCK;
3587 else
3588 di->di_flags &= ~DI_FLAGS_LOCK;
3589 item_lock(&di->di_tv, deep, lock);
3590 }
3591 }
3592 *name_end = cc;
3593 }
3594 else if (lp->ll_range)
3595 {
3596 listitem_T *li = lp->ll_li;
3597
3598 /* (un)lock a range of List items. */
3599 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3600 {
3601 item_lock(&li->li_tv, deep, lock);
3602 li = li->li_next;
3603 ++lp->ll_n1;
3604 }
3605 }
3606 else if (lp->ll_list != NULL)
3607 /* (un)lock a List item. */
3608 item_lock(&lp->ll_li->li_tv, deep, lock);
3609 else
3610 /* un(lock) a Dictionary item. */
3611 item_lock(&lp->ll_di->di_tv, deep, lock);
3612
3613 return ret;
3614}
3615
3616/*
3617 * Lock or unlock an item. "deep" is nr of levels to go.
3618 */
3619 static void
3620item_lock(tv, deep, lock)
3621 typval_T *tv;
3622 int deep;
3623 int lock;
3624{
3625 static int recurse = 0;
3626 list_T *l;
3627 listitem_T *li;
3628 dict_T *d;
3629 hashitem_T *hi;
3630 int todo;
3631
3632 if (recurse >= DICT_MAXNEST)
3633 {
3634 EMSG(_("E743: variable nested too deep for (un)lock"));
3635 return;
3636 }
3637 if (deep == 0)
3638 return;
3639 ++recurse;
3640
3641 /* lock/unlock the item itself */
3642 if (lock)
3643 tv->v_lock |= VAR_LOCKED;
3644 else
3645 tv->v_lock &= ~VAR_LOCKED;
3646
3647 switch (tv->v_type)
3648 {
3649 case VAR_LIST:
3650 if ((l = tv->vval.v_list) != NULL)
3651 {
3652 if (lock)
3653 l->lv_lock |= VAR_LOCKED;
3654 else
3655 l->lv_lock &= ~VAR_LOCKED;
3656 if (deep < 0 || deep > 1)
3657 /* recursive: lock/unlock the items the List contains */
3658 for (li = l->lv_first; li != NULL; li = li->li_next)
3659 item_lock(&li->li_tv, deep - 1, lock);
3660 }
3661 break;
3662 case VAR_DICT:
3663 if ((d = tv->vval.v_dict) != NULL)
3664 {
3665 if (lock)
3666 d->dv_lock |= VAR_LOCKED;
3667 else
3668 d->dv_lock &= ~VAR_LOCKED;
3669 if (deep < 0 || deep > 1)
3670 {
3671 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003672 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3674 {
3675 if (!HASHITEM_EMPTY(hi))
3676 {
3677 --todo;
3678 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3679 }
3680 }
3681 }
3682 }
3683 }
3684 --recurse;
3685}
3686
Bram Moolenaara40058a2005-07-11 22:42:07 +00003687/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003688 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3689 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003690 */
3691 static int
3692tv_islocked(tv)
3693 typval_T *tv;
3694{
3695 return (tv->v_lock & VAR_LOCKED)
3696 || (tv->v_type == VAR_LIST
3697 && tv->vval.v_list != NULL
3698 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3699 || (tv->v_type == VAR_DICT
3700 && tv->vval.v_dict != NULL
3701 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3702}
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3705/*
3706 * Delete all "menutrans_" variables.
3707 */
3708 void
3709del_menutrans_vars()
3710{
Bram Moolenaar33570922005-01-25 22:26:29 +00003711 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003712 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003715 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003717 {
3718 if (!HASHITEM_EMPTY(hi))
3719 {
3720 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3722 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003723 }
3724 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726}
3727#endif
3728
3729#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3730
3731/*
3732 * Local string buffer for the next two functions to store a variable name
3733 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3734 * get_user_var_name().
3735 */
3736
3737static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3738
3739static char_u *varnamebuf = NULL;
3740static int varnamebuflen = 0;
3741
3742/*
3743 * Function to concatenate a prefix and a variable name.
3744 */
3745 static char_u *
3746cat_prefix_varname(prefix, name)
3747 int prefix;
3748 char_u *name;
3749{
3750 int len;
3751
3752 len = (int)STRLEN(name) + 3;
3753 if (len > varnamebuflen)
3754 {
3755 vim_free(varnamebuf);
3756 len += 10; /* some additional space */
3757 varnamebuf = alloc(len);
3758 if (varnamebuf == NULL)
3759 {
3760 varnamebuflen = 0;
3761 return NULL;
3762 }
3763 varnamebuflen = len;
3764 }
3765 *varnamebuf = prefix;
3766 varnamebuf[1] = ':';
3767 STRCPY(varnamebuf + 2, name);
3768 return varnamebuf;
3769}
3770
3771/*
3772 * Function given to ExpandGeneric() to obtain the list of user defined
3773 * (global/buffer/window/built-in) variable names.
3774 */
3775/*ARGSUSED*/
3776 char_u *
3777get_user_var_name(xp, idx)
3778 expand_T *xp;
3779 int idx;
3780{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003781 static long_u gdone;
3782 static long_u bdone;
3783 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003784#ifdef FEAT_WINDOWS
3785 static long_u tdone;
3786#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003787 static int vidx;
3788 static hashitem_T *hi;
3789 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003792 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003793 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003794#ifdef FEAT_WINDOWS
3795 tdone = 0;
3796#endif
3797 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003798
3799 /* Global variables */
3800 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003802 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003803 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003804 else
3805 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003806 while (HASHITEM_EMPTY(hi))
3807 ++hi;
3808 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3809 return cat_prefix_varname('g', hi->hi_key);
3810 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003812
3813 /* b: variables */
3814 ht = &curbuf->b_vars.dv_hashtab;
3815 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003817 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003818 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003819 else
3820 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003821 while (HASHITEM_EMPTY(hi))
3822 ++hi;
3823 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003825 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003827 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return (char_u *)"b:changedtick";
3829 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003830
3831 /* w: variables */
3832 ht = &curwin->w_vars.dv_hashtab;
3833 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003835 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003837 else
3838 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 while (HASHITEM_EMPTY(hi))
3840 ++hi;
3841 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003843
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003844#ifdef FEAT_WINDOWS
3845 /* t: variables */
3846 ht = &curtab->tp_vars.dv_hashtab;
3847 if (tdone < ht->ht_used)
3848 {
3849 if (tdone++ == 0)
3850 hi = ht->ht_array;
3851 else
3852 ++hi;
3853 while (HASHITEM_EMPTY(hi))
3854 ++hi;
3855 return cat_prefix_varname('t', hi->hi_key);
3856 }
3857#endif
3858
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 /* v: variables */
3860 if (vidx < VV_LEN)
3861 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 vim_free(varnamebuf);
3864 varnamebuf = NULL;
3865 varnamebuflen = 0;
3866 return NULL;
3867}
3868
3869#endif /* FEAT_CMDL_COMPL */
3870
3871/*
3872 * types for expressions.
3873 */
3874typedef enum
3875{
3876 TYPE_UNKNOWN = 0
3877 , TYPE_EQUAL /* == */
3878 , TYPE_NEQUAL /* != */
3879 , TYPE_GREATER /* > */
3880 , TYPE_GEQUAL /* >= */
3881 , TYPE_SMALLER /* < */
3882 , TYPE_SEQUAL /* <= */
3883 , TYPE_MATCH /* =~ */
3884 , TYPE_NOMATCH /* !~ */
3885} exptype_T;
3886
3887/*
3888 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3891 */
3892
3893/*
3894 * Handle zero level expression.
3895 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003896 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003897 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 * Return OK or FAIL.
3899 */
3900 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 char_u **nextcmd;
3905 int evaluate;
3906{
3907 int ret;
3908 char_u *p;
3909
3910 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (ret == FAIL || !ends_excmd(*p))
3913 {
3914 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 /*
3917 * Report the invalid expression unless the expression evaluation has
3918 * been cancelled due to an aborting error, an interrupt, or an
3919 * exception.
3920 */
3921 if (!aborting())
3922 EMSG2(_(e_invexpr2), arg);
3923 ret = FAIL;
3924 }
3925 if (nextcmd != NULL)
3926 *nextcmd = check_nextcmd(p);
3927
3928 return ret;
3929}
3930
3931/*
3932 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003933 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 *
3935 * "arg" must point to the first non-white of the expression.
3936 * "arg" is advanced to the next non-white after the recognized expression.
3937 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003938 * Note: "rettv.v_lock" is not set.
3939 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 * Return OK or FAIL.
3941 */
3942 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 int evaluate;
3947{
3948 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951 /*
3952 * Get the first variable.
3953 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return FAIL;
3956
3957 if ((*arg)[0] == '?')
3958 {
3959 result = FALSE;
3960 if (evaluate)
3961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003962 int error = FALSE;
3963
3964 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003967 if (error)
3968 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970
3971 /*
3972 * Get the second variable.
3973 */
3974 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 return FAIL;
3977
3978 /*
3979 * Check for the ":".
3980 */
3981 if ((*arg)[0] != ':')
3982 {
3983 EMSG(_("E109: Missing ':' after '?'"));
3984 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003985 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 return FAIL;
3987 }
3988
3989 /*
3990 * Get the third variable.
3991 */
3992 *arg = skipwhite(*arg + 1);
3993 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3994 {
3995 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return FAIL;
3998 }
3999 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002
4003 return OK;
4004}
4005
4006/*
4007 * Handle first level expression:
4008 * expr2 || expr2 || expr2 logical OR
4009 *
4010 * "arg" must point to the first non-white of the expression.
4011 * "arg" is advanced to the next non-white after the recognized expression.
4012 *
4013 * Return OK or FAIL.
4014 */
4015 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 int evaluate;
4020{
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 long result;
4023 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004024 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
4026 /*
4027 * Get the first variable.
4028 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return FAIL;
4031
4032 /*
4033 * Repeat until there is no following "||".
4034 */
4035 first = TRUE;
4036 result = FALSE;
4037 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4038 {
4039 if (evaluate && first)
4040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004041 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 if (error)
4045 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 first = FALSE;
4047 }
4048
4049 /*
4050 * Get the second variable.
4051 */
4052 *arg = skipwhite(*arg + 2);
4053 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4054 return FAIL;
4055
4056 /*
4057 * Compute the result.
4058 */
4059 if (evaluate && !result)
4060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004061 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004064 if (error)
4065 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 if (evaluate)
4068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 rettv->v_type = VAR_NUMBER;
4070 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072 }
4073
4074 return OK;
4075}
4076
4077/*
4078 * Handle second level expression:
4079 * expr3 && expr3 && expr3 logical AND
4080 *
4081 * "arg" must point to the first non-white of the expression.
4082 * "arg" is advanced to the next non-white after the recognized expression.
4083 *
4084 * Return OK or FAIL.
4085 */
4086 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 int evaluate;
4091{
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 long result;
4094 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097 /*
4098 * Get the first variable.
4099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return FAIL;
4102
4103 /*
4104 * Repeat until there is no following "&&".
4105 */
4106 first = TRUE;
4107 result = TRUE;
4108 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4109 {
4110 if (evaluate && first)
4111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 if (error)
4116 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 first = FALSE;
4118 }
4119
4120 /*
4121 * Get the second variable.
4122 */
4123 *arg = skipwhite(*arg + 2);
4124 if (eval4(arg, &var2, evaluate && result) == FAIL)
4125 return FAIL;
4126
4127 /*
4128 * Compute the result.
4129 */
4130 if (evaluate && result)
4131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004132 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 if (error)
4136 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 if (evaluate)
4139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 rettv->v_type = VAR_NUMBER;
4141 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 }
4144
4145 return OK;
4146}
4147
4148/*
4149 * Handle third level expression:
4150 * var1 == var2
4151 * var1 =~ var2
4152 * var1 != var2
4153 * var1 !~ var2
4154 * var1 > var2
4155 * var1 >= var2
4156 * var1 < var2
4157 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004158 * var1 is var2
4159 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
4164 * Return OK or FAIL.
4165 */
4166 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 int evaluate;
4171{
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 char_u *p;
4174 int i;
4175 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004176 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 int len = 2;
4178 long n1, n2;
4179 char_u *s1, *s2;
4180 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4181 regmatch_T regmatch;
4182 int ic;
4183 char_u *save_cpo;
4184
4185 /*
4186 * Get the first variable.
4187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return FAIL;
4190
4191 p = *arg;
4192 switch (p[0])
4193 {
4194 case '=': if (p[1] == '=')
4195 type = TYPE_EQUAL;
4196 else if (p[1] == '~')
4197 type = TYPE_MATCH;
4198 break;
4199 case '!': if (p[1] == '=')
4200 type = TYPE_NEQUAL;
4201 else if (p[1] == '~')
4202 type = TYPE_NOMATCH;
4203 break;
4204 case '>': if (p[1] != '=')
4205 {
4206 type = TYPE_GREATER;
4207 len = 1;
4208 }
4209 else
4210 type = TYPE_GEQUAL;
4211 break;
4212 case '<': if (p[1] != '=')
4213 {
4214 type = TYPE_SMALLER;
4215 len = 1;
4216 }
4217 else
4218 type = TYPE_SEQUAL;
4219 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 case 'i': if (p[1] == 's')
4221 {
4222 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4223 len = 5;
4224 if (!vim_isIDc(p[len]))
4225 {
4226 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4227 type_is = TRUE;
4228 }
4229 }
4230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232
4233 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004234 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 */
4236 if (type != TYPE_UNKNOWN)
4237 {
4238 /* extra question mark appended: ignore case */
4239 if (p[len] == '?')
4240 {
4241 ic = TRUE;
4242 ++len;
4243 }
4244 /* extra '#' appended: match case */
4245 else if (p[len] == '#')
4246 {
4247 ic = FALSE;
4248 ++len;
4249 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004250 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 else
4252 ic = p_ic;
4253
4254 /*
4255 * Get the second variable.
4256 */
4257 *arg = skipwhite(p + len);
4258 if (eval5(arg, &var2, evaluate) == FAIL)
4259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 return FAIL;
4262 }
4263
4264 if (evaluate)
4265 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004266 if (type_is && rettv->v_type != var2.v_type)
4267 {
4268 /* For "is" a different type always means FALSE, for "notis"
4269 * it means TRUE. */
4270 n1 = (type == TYPE_NEQUAL);
4271 }
4272 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4273 {
4274 if (type_is)
4275 {
4276 n1 = (rettv->v_type == var2.v_type
4277 && rettv->vval.v_list == var2.vval.v_list);
4278 if (type == TYPE_NEQUAL)
4279 n1 = !n1;
4280 }
4281 else if (rettv->v_type != var2.v_type
4282 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4283 {
4284 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004285 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004286 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004287 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004288 clear_tv(rettv);
4289 clear_tv(&var2);
4290 return FAIL;
4291 }
4292 else
4293 {
4294 /* Compare two Lists for being equal or unequal. */
4295 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4296 if (type == TYPE_NEQUAL)
4297 n1 = !n1;
4298 }
4299 }
4300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004301 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4302 {
4303 if (type_is)
4304 {
4305 n1 = (rettv->v_type == var2.v_type
4306 && rettv->vval.v_dict == var2.vval.v_dict);
4307 if (type == TYPE_NEQUAL)
4308 n1 = !n1;
4309 }
4310 else if (rettv->v_type != var2.v_type
4311 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4312 {
4313 if (rettv->v_type != var2.v_type)
4314 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4315 else
4316 EMSG(_("E736: Invalid operation for Dictionary"));
4317 clear_tv(rettv);
4318 clear_tv(&var2);
4319 return FAIL;
4320 }
4321 else
4322 {
4323 /* Compare two Dictionaries for being equal or unequal. */
4324 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4325 if (type == TYPE_NEQUAL)
4326 n1 = !n1;
4327 }
4328 }
4329
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004330 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4331 {
4332 if (rettv->v_type != var2.v_type
4333 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4334 {
4335 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004336 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004337 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004338 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 clear_tv(rettv);
4340 clear_tv(&var2);
4341 return FAIL;
4342 }
4343 else
4344 {
4345 /* Compare two Funcrefs for being equal or unequal. */
4346 if (rettv->vval.v_string == NULL
4347 || var2.vval.v_string == NULL)
4348 n1 = FALSE;
4349 else
4350 n1 = STRCMP(rettv->vval.v_string,
4351 var2.vval.v_string) == 0;
4352 if (type == TYPE_NEQUAL)
4353 n1 = !n1;
4354 }
4355 }
4356
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357#ifdef FEAT_FLOAT
4358 /*
4359 * If one of the two variables is a float, compare as a float.
4360 * When using "=~" or "!~", always compare as string.
4361 */
4362 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4363 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4364 {
4365 float_T f1, f2;
4366
4367 if (rettv->v_type == VAR_FLOAT)
4368 f1 = rettv->vval.v_float;
4369 else
4370 f1 = get_tv_number(rettv);
4371 if (var2.v_type == VAR_FLOAT)
4372 f2 = var2.vval.v_float;
4373 else
4374 f2 = get_tv_number(&var2);
4375 n1 = FALSE;
4376 switch (type)
4377 {
4378 case TYPE_EQUAL: n1 = (f1 == f2); break;
4379 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4380 case TYPE_GREATER: n1 = (f1 > f2); break;
4381 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4382 case TYPE_SMALLER: n1 = (f1 < f2); break;
4383 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4384 case TYPE_UNKNOWN:
4385 case TYPE_MATCH:
4386 case TYPE_NOMATCH: break; /* avoid gcc warning */
4387 }
4388 }
4389#endif
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 /*
4392 * If one of the two variables is a number, compare as a number.
4393 * When using "=~" or "!~", always compare as string.
4394 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 n1 = get_tv_number(rettv);
4399 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 switch (type)
4401 {
4402 case TYPE_EQUAL: n1 = (n1 == n2); break;
4403 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4404 case TYPE_GREATER: n1 = (n1 > n2); break;
4405 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4406 case TYPE_SMALLER: n1 = (n1 < n2); break;
4407 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4408 case TYPE_UNKNOWN:
4409 case TYPE_MATCH:
4410 case TYPE_NOMATCH: break; /* avoid gcc warning */
4411 }
4412 }
4413 else
4414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 s1 = get_tv_string_buf(rettv, buf1);
4416 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4418 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4419 else
4420 i = 0;
4421 n1 = FALSE;
4422 switch (type)
4423 {
4424 case TYPE_EQUAL: n1 = (i == 0); break;
4425 case TYPE_NEQUAL: n1 = (i != 0); break;
4426 case TYPE_GREATER: n1 = (i > 0); break;
4427 case TYPE_GEQUAL: n1 = (i >= 0); break;
4428 case TYPE_SMALLER: n1 = (i < 0); break;
4429 case TYPE_SEQUAL: n1 = (i <= 0); break;
4430
4431 case TYPE_MATCH:
4432 case TYPE_NOMATCH:
4433 /* avoid 'l' flag in 'cpoptions' */
4434 save_cpo = p_cpo;
4435 p_cpo = (char_u *)"";
4436 regmatch.regprog = vim_regcomp(s2,
4437 RE_MAGIC + RE_STRING);
4438 regmatch.rm_ic = ic;
4439 if (regmatch.regprog != NULL)
4440 {
4441 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4442 vim_free(regmatch.regprog);
4443 if (type == TYPE_NOMATCH)
4444 n1 = !n1;
4445 }
4446 p_cpo = save_cpo;
4447 break;
4448
4449 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4450 }
4451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 clear_tv(rettv);
4453 clear_tv(&var2);
4454 rettv->v_type = VAR_NUMBER;
4455 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457 }
4458
4459 return OK;
4460}
4461
4462/*
4463 * Handle fourth level expression:
4464 * + number addition
4465 * - number subtraction
4466 * . string concatenation
4467 *
4468 * "arg" must point to the first non-white of the expression.
4469 * "arg" is advanced to the next non-white after the recognized expression.
4470 *
4471 * Return OK or FAIL.
4472 */
4473 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 int evaluate;
4478{
Bram Moolenaar33570922005-01-25 22:26:29 +00004479 typval_T var2;
4480 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 int op;
4482 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004483#ifdef FEAT_FLOAT
4484 float_T f1 = 0, f2 = 0;
4485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u *s1, *s2;
4487 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4488 char_u *p;
4489
4490 /*
4491 * Get the first variable.
4492 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004493 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return FAIL;
4495
4496 /*
4497 * Repeat computing, until no '+', '-' or '.' is following.
4498 */
4499 for (;;)
4500 {
4501 op = **arg;
4502 if (op != '+' && op != '-' && op != '.')
4503 break;
4504
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004505 if ((op != '+' || rettv->v_type != VAR_LIST)
4506#ifdef FEAT_FLOAT
4507 && (op == '.' || rettv->v_type != VAR_FLOAT)
4508#endif
4509 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004510 {
4511 /* For "list + ...", an illegal use of the first operand as
4512 * a number cannot be determined before evaluating the 2nd
4513 * operand: if this is also a list, all is ok.
4514 * For "something . ...", "something - ..." or "non-list + ...",
4515 * we know that the first operand needs to be a string or number
4516 * without evaluating the 2nd operand. So check before to avoid
4517 * side effects after an error. */
4518 if (evaluate && get_tv_string_chk(rettv) == NULL)
4519 {
4520 clear_tv(rettv);
4521 return FAIL;
4522 }
4523 }
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 /*
4526 * Get the second variable.
4527 */
4528 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004529 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 return FAIL;
4533 }
4534
4535 if (evaluate)
4536 {
4537 /*
4538 * Compute the result.
4539 */
4540 if (op == '.')
4541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4543 s2 = get_tv_string_buf_chk(&var2, buf2);
4544 if (s2 == NULL) /* type error ? */
4545 {
4546 clear_tv(rettv);
4547 clear_tv(&var2);
4548 return FAIL;
4549 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004550 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004551 clear_tv(rettv);
4552 rettv->v_type = VAR_STRING;
4553 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004555 else if (op == '+' && rettv->v_type == VAR_LIST
4556 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 {
4558 /* concatenate Lists */
4559 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4560 &var3) == FAIL)
4561 {
4562 clear_tv(rettv);
4563 clear_tv(&var2);
4564 return FAIL;
4565 }
4566 clear_tv(rettv);
4567 *rettv = var3;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
4570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004571 int error = FALSE;
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004575 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004576 f1 = rettv->vval.v_float;
4577 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004578 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004579 else
4580#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004581 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004582 n1 = get_tv_number_chk(rettv, &error);
4583 if (error)
4584 {
4585 /* This can only happen for "list + non-list". For
4586 * "non-list + ..." or "something - ...", we returned
4587 * before evaluating the 2nd operand. */
4588 clear_tv(rettv);
4589 return FAIL;
4590 }
4591#ifdef FEAT_FLOAT
4592 if (var2.v_type == VAR_FLOAT)
4593 f1 = n1;
4594#endif
4595 }
4596#ifdef FEAT_FLOAT
4597 if (var2.v_type == VAR_FLOAT)
4598 {
4599 f2 = var2.vval.v_float;
4600 n2 = 0;
4601 }
4602 else
4603#endif
4604 {
4605 n2 = get_tv_number_chk(&var2, &error);
4606 if (error)
4607 {
4608 clear_tv(rettv);
4609 clear_tv(&var2);
4610 return FAIL;
4611 }
4612#ifdef FEAT_FLOAT
4613 if (rettv->v_type == VAR_FLOAT)
4614 f2 = n2;
4615#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618
4619#ifdef FEAT_FLOAT
4620 /* If there is a float on either side the result is a float. */
4621 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4622 {
4623 if (op == '+')
4624 f1 = f1 + f2;
4625 else
4626 f1 = f1 - f2;
4627 rettv->v_type = VAR_FLOAT;
4628 rettv->vval.v_float = f1;
4629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631#endif
4632 {
4633 if (op == '+')
4634 n1 = n1 + n2;
4635 else
4636 n1 = n1 - n2;
4637 rettv->v_type = VAR_NUMBER;
4638 rettv->vval.v_number = n1;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 }
4644 return OK;
4645}
4646
4647/*
4648 * Handle fifth level expression:
4649 * * number multiplication
4650 * / number division
4651 * % number modulo
4652 *
4653 * "arg" must point to the first non-white of the expression.
4654 * "arg" is advanced to the next non-white after the recognized expression.
4655 *
4656 * Return OK or FAIL.
4657 */
4658 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004659eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004663 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
Bram Moolenaar33570922005-01-25 22:26:29 +00004665 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 int op;
4667 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004668#ifdef FEAT_FLOAT
4669 int use_float = FALSE;
4670 float_T f1 = 0, f2;
4671#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004672 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 /*
4675 * Get the first variable.
4676 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004677 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679
4680 /*
4681 * Repeat computing, until no '*', '/' or '%' is following.
4682 */
4683 for (;;)
4684 {
4685 op = **arg;
4686 if (op != '*' && op != '/' && op != '%')
4687 break;
4688
4689 if (evaluate)
4690 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#ifdef FEAT_FLOAT
4692 if (rettv->v_type == VAR_FLOAT)
4693 {
4694 f1 = rettv->vval.v_float;
4695 use_float = TRUE;
4696 n1 = 0;
4697 }
4698 else
4699#endif
4700 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 if (error)
4703 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 else
4706 n1 = 0;
4707
4708 /*
4709 * Get the second variable.
4710 */
4711 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 return FAIL;
4714
4715 if (evaluate)
4716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717#ifdef FEAT_FLOAT
4718 if (var2.v_type == VAR_FLOAT)
4719 {
4720 if (!use_float)
4721 {
4722 f1 = n1;
4723 use_float = TRUE;
4724 }
4725 f2 = var2.vval.v_float;
4726 n2 = 0;
4727 }
4728 else
4729#endif
4730 {
4731 n2 = get_tv_number_chk(&var2, &error);
4732 clear_tv(&var2);
4733 if (error)
4734 return FAIL;
4735#ifdef FEAT_FLOAT
4736 if (use_float)
4737 f2 = n2;
4738#endif
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 /*
4742 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745#ifdef FEAT_FLOAT
4746 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748 if (op == '*')
4749 f1 = f1 * f2;
4750 else if (op == '/')
4751 {
4752 /* We rely on the floating point library to handle divide
4753 * by zero to result in "inf" and not a crash. */
4754 f1 = f1 / f2;
4755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004758 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759 return FAIL;
4760 }
4761 rettv->v_type = VAR_FLOAT;
4762 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767 if (op == '*')
4768 n1 = n1 * n2;
4769 else if (op == '/')
4770 {
4771 if (n2 == 0) /* give an error message? */
4772 {
4773 if (n1 == 0)
4774 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4775 else if (n1 < 0)
4776 n1 = -0x7fffffffL;
4777 else
4778 n1 = 0x7fffffffL;
4779 }
4780 else
4781 n1 = n1 / n2;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784 {
4785 if (n2 == 0) /* give an error message? */
4786 n1 = 0;
4787 else
4788 n1 = n1 % n2;
4789 }
4790 rettv->v_type = VAR_NUMBER;
4791 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794 }
4795
4796 return OK;
4797}
4798
4799/*
4800 * Handle sixth level expression:
4801 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004802 * "string" string constant
4803 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 * &option-name option value
4805 * @r register contents
4806 * identifier variable value
4807 * function() function call
4808 * $VAR environment variable
4809 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004810 * [expr, expr] List
4811 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 *
4813 * Also handle:
4814 * ! in front logical NOT
4815 * - in front unary minus
4816 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817 * trailing [] subscript in String or List
4818 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 *
4820 * "arg" must point to the first non-white of the expression.
4821 * "arg" is advanced to the next non-white after the recognized expression.
4822 *
4823 * Return OK or FAIL.
4824 */
4825 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004830 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 long n;
4833 int len;
4834 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 char_u *start_leader, *end_leader;
4836 int ret = OK;
4837 char_u *alias;
4838
4839 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004843 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 /*
4846 * Skip '!' and '-' characters. They are handled later.
4847 */
4848 start_leader = *arg;
4849 while (**arg == '!' || **arg == '-' || **arg == '+')
4850 *arg = skipwhite(*arg + 1);
4851 end_leader = *arg;
4852
4853 switch (**arg)
4854 {
4855 /*
4856 * Number constant.
4857 */
4858 case '0':
4859 case '1':
4860 case '2':
4861 case '3':
4862 case '4':
4863 case '5':
4864 case '6':
4865 case '7':
4866 case '8':
4867 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 {
4869#ifdef FEAT_FLOAT
4870 char_u *p = skipdigits(*arg + 1);
4871 int get_float = FALSE;
4872
4873 /* We accept a float when the format matches
4874 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004875 * strict to avoid backwards compatibility problems.
4876 * Don't look for a float after the "." operator, so that
4877 * ":let vers = 1.2.3" doesn't fail. */
4878 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 get_float = TRUE;
4881 p = skipdigits(p + 2);
4882 if (*p == 'e' || *p == 'E')
4883 {
4884 ++p;
4885 if (*p == '-' || *p == '+')
4886 ++p;
4887 if (!vim_isdigit(*p))
4888 get_float = FALSE;
4889 else
4890 p = skipdigits(p + 1);
4891 }
4892 if (ASCII_ISALPHA(*p) || *p == '.')
4893 get_float = FALSE;
4894 }
4895 if (get_float)
4896 {
4897 float_T f;
4898
4899 *arg += string2float(*arg, &f);
4900 if (evaluate)
4901 {
4902 rettv->v_type = VAR_FLOAT;
4903 rettv->vval.v_float = f;
4904 }
4905 }
4906 else
4907#endif
4908 {
4909 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4910 *arg += len;
4911 if (evaluate)
4912 {
4913 rettv->v_type = VAR_NUMBER;
4914 rettv->vval.v_number = n;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
4920 /*
4921 * String constant: "string".
4922 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925
4926 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 break;
4931
4932 /*
4933 * List: [expr, expr]
4934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004935 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937
4938 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939 * Dictionary: {key: val, key: val}
4940 */
4941 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4942 break;
4943
4944 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004945 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004947 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 break;
4949
4950 /*
4951 * Environment variable: $VAR.
4952 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 break;
4955
4956 /*
4957 * Register contents: @r.
4958 */
4959 case '@': ++*arg;
4960 if (evaluate)
4961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004962 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004963 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 if (**arg != NUL)
4966 ++*arg;
4967 break;
4968
4969 /*
4970 * nested expression: (expression).
4971 */
4972 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (**arg == ')')
4975 ++*arg;
4976 else if (ret == OK)
4977 {
4978 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 ret = FAIL;
4981 }
4982 break;
4983
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
4986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987
4988 if (ret == NOTDONE)
4989 {
4990 /*
4991 * Must be a variable or function name.
4992 * Can also be a curly-braces kind of name: {expr}.
4993 */
4994 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 if (alias != NULL)
4997 s = alias;
4998
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004999 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 ret = FAIL;
5001 else
5002 {
5003 if (**arg == '(') /* recursive! */
5004 {
5005 /* If "s" is the name of a variable of type VAR_FUNC
5006 * use its contents. */
5007 s = deref_func_name(s, &len);
5008
5009 /* Invoke the function. */
5010 ret = get_func_tv(s, len, rettv, arg,
5011 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005012 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 /* Stop the expression evaluation when immediately
5014 * aborting on error, or when an interrupt occurred or
5015 * an exception was thrown but not caught. */
5016 if (aborting())
5017 {
5018 if (ret == OK)
5019 clear_tv(rettv);
5020 ret = FAIL;
5021 }
5022 }
5023 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005024 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005025 else
5026 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 }
5028
5029 if (alias != NULL)
5030 vim_free(alias);
5031 }
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *arg = skipwhite(*arg);
5034
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005035 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5036 * expr(expr). */
5037 if (ret == OK)
5038 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040 /*
5041 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5042 */
5043 if (ret == OK && evaluate && end_leader > start_leader)
5044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005045 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046 int val = 0;
5047#ifdef FEAT_FLOAT
5048 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005049
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005050 if (rettv->v_type == VAR_FLOAT)
5051 f = rettv->vval.v_float;
5052 else
5053#endif
5054 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005055 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005057 clear_tv(rettv);
5058 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005060 else
5061 {
5062 while (end_leader > start_leader)
5063 {
5064 --end_leader;
5065 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 {
5067#ifdef FEAT_FLOAT
5068 if (rettv->v_type == VAR_FLOAT)
5069 f = !f;
5070 else
5071#endif
5072 val = !val;
5073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005074 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 {
5076#ifdef FEAT_FLOAT
5077 if (rettv->v_type == VAR_FLOAT)
5078 f = -f;
5079 else
5080#endif
5081 val = -val;
5082 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005083 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005084#ifdef FEAT_FLOAT
5085 if (rettv->v_type == VAR_FLOAT)
5086 {
5087 clear_tv(rettv);
5088 rettv->vval.v_float = f;
5089 }
5090 else
5091#endif
5092 {
5093 clear_tv(rettv);
5094 rettv->v_type = VAR_NUMBER;
5095 rettv->vval.v_number = val;
5096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099
5100 return ret;
5101}
5102
5103/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005104 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5105 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005106 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5107 */
5108 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005109eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005111 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005113 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114{
5115 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005116 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 long len = -1;
5119 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 if (rettv->v_type == VAR_FUNC
5124#ifdef FEAT_FLOAT
5125 || rettv->v_type == VAR_FLOAT
5126#endif
5127 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005129 if (verbose)
5130 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 return FAIL;
5132 }
5133
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 /*
5137 * dict.name
5138 */
5139 key = *arg + 1;
5140 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5141 ;
5142 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 }
5146 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 /*
5149 * something[idx]
5150 *
5151 * Get the (first) variable from inside the [].
5152 */
5153 *arg = skipwhite(*arg + 1);
5154 if (**arg == ':')
5155 empty1 = TRUE;
5156 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5157 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005158 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5159 {
5160 /* not a number or string */
5161 clear_tv(&var1);
5162 return FAIL;
5163 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164
5165 /*
5166 * Get the second variable from inside the [:].
5167 */
5168 if (**arg == ':')
5169 {
5170 range = TRUE;
5171 *arg = skipwhite(*arg + 1);
5172 if (**arg == ']')
5173 empty2 = TRUE;
5174 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176 if (!empty1)
5177 clear_tv(&var1);
5178 return FAIL;
5179 }
5180 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5181 {
5182 /* not a number or string */
5183 if (!empty1)
5184 clear_tv(&var1);
5185 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 return FAIL;
5187 }
5188 }
5189
5190 /* Check for the ']'. */
5191 if (**arg != ']')
5192 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005193 if (verbose)
5194 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 clear_tv(&var1);
5196 if (range)
5197 clear_tv(&var2);
5198 return FAIL;
5199 }
5200 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201 }
5202
5203 if (evaluate)
5204 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 n1 = 0;
5206 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 n1 = get_tv_number(&var1);
5209 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 }
5211 if (range)
5212 {
5213 if (empty2)
5214 n2 = -1;
5215 else
5216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 n2 = get_tv_number(&var2);
5218 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 }
5220 }
5221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223 {
5224 case VAR_NUMBER:
5225 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 len = (long)STRLEN(s);
5228 if (range)
5229 {
5230 /* The resulting variable is a substring. If the indexes
5231 * are out of range the result is empty. */
5232 if (n1 < 0)
5233 {
5234 n1 = len + n1;
5235 if (n1 < 0)
5236 n1 = 0;
5237 }
5238 if (n2 < 0)
5239 n2 = len + n2;
5240 else if (n2 >= len)
5241 n2 = len;
5242 if (n1 >= len || n2 < 0 || n1 > n2)
5243 s = NULL;
5244 else
5245 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5246 }
5247 else
5248 {
5249 /* The resulting variable is a string of a single
5250 * character. If the index is too big or negative the
5251 * result is empty. */
5252 if (n1 >= len || n1 < 0)
5253 s = NULL;
5254 else
5255 s = vim_strnsave(s + n1, 1);
5256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005257 clear_tv(rettv);
5258 rettv->v_type = VAR_STRING;
5259 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 break;
5261
5262 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005263 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 if (n1 < 0)
5265 n1 = len + n1;
5266 if (!empty1 && (n1 < 0 || n1 >= len))
5267 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005268 /* For a range we allow invalid values and return an empty
5269 * list. A list index out of range is an error. */
5270 if (!range)
5271 {
5272 if (verbose)
5273 EMSGN(_(e_listidx), n1);
5274 return FAIL;
5275 }
5276 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 }
5278 if (range)
5279 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 list_T *l;
5281 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282
5283 if (n2 < 0)
5284 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005285 else if (n2 >= len)
5286 n2 = len - 1;
5287 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005288 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 l = list_alloc();
5290 if (l == NULL)
5291 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005292 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 n1 <= n2; ++n1)
5294 {
5295 if (list_append_tv(l, &item->li_tv) == FAIL)
5296 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005297 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 return FAIL;
5299 }
5300 item = item->li_next;
5301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005302 clear_tv(rettv);
5303 rettv->v_type = VAR_LIST;
5304 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005305 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 }
5307 else
5308 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005309 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005310 clear_tv(rettv);
5311 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 }
5313 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314
5315 case VAR_DICT:
5316 if (range)
5317 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005318 if (verbose)
5319 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 if (len == -1)
5321 clear_tv(&var1);
5322 return FAIL;
5323 }
5324 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005325 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326
5327 if (len == -1)
5328 {
5329 key = get_tv_string(&var1);
5330 if (*key == NUL)
5331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005332 if (verbose)
5333 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 clear_tv(&var1);
5335 return FAIL;
5336 }
5337 }
5338
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005339 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005342 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 if (len == -1)
5344 clear_tv(&var1);
5345 if (item == NULL)
5346 return FAIL;
5347
5348 copy_tv(&item->di_tv, &var1);
5349 clear_tv(rettv);
5350 *rettv = var1;
5351 }
5352 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 }
5354 }
5355
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 return OK;
5357}
5358
5359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 * Get an option value.
5361 * "arg" points to the '&' or '+' before the option name.
5362 * "arg" is advanced to character after the option name.
5363 * Return OK or FAIL.
5364 */
5365 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005366get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005368 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 int evaluate;
5370{
5371 char_u *option_end;
5372 long numval;
5373 char_u *stringval;
5374 int opt_type;
5375 int c;
5376 int working = (**arg == '+'); /* has("+option") */
5377 int ret = OK;
5378 int opt_flags;
5379
5380 /*
5381 * Isolate the option name and find its value.
5382 */
5383 option_end = find_option_end(arg, &opt_flags);
5384 if (option_end == NULL)
5385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 EMSG2(_("E112: Option name missing: %s"), *arg);
5388 return FAIL;
5389 }
5390
5391 if (!evaluate)
5392 {
5393 *arg = option_end;
5394 return OK;
5395 }
5396
5397 c = *option_end;
5398 *option_end = NUL;
5399 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402 if (opt_type == -3) /* invalid name */
5403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005404 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 EMSG2(_("E113: Unknown option: %s"), *arg);
5406 ret = FAIL;
5407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 if (opt_type == -2) /* hidden string option */
5411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 rettv->v_type = VAR_STRING;
5413 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 }
5415 else if (opt_type == -1) /* hidden number option */
5416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 rettv->v_type = VAR_NUMBER;
5418 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420 else if (opt_type == 1) /* number option */
5421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005422 rettv->v_type = VAR_NUMBER;
5423 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425 else /* string option */
5426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005427 rettv->v_type = VAR_STRING;
5428 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
5430 }
5431 else if (working && (opt_type == -2 || opt_type == -1))
5432 ret = FAIL;
5433
5434 *option_end = c; /* put back for error messages */
5435 *arg = option_end;
5436
5437 return ret;
5438}
5439
5440/*
5441 * Allocate a variable for a string constant.
5442 * Return OK or FAIL.
5443 */
5444 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005445get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 int evaluate;
5449{
5450 char_u *p;
5451 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 int extra = 0;
5453
5454 /*
5455 * Find the end of the string, skipping backslashed characters.
5456 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005457 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
5459 if (*p == '\\' && p[1] != NUL)
5460 {
5461 ++p;
5462 /* A "\<x>" form occupies at least 4 characters, and produces up
5463 * to 6 characters: reserve space for 2 extra */
5464 if (*p == '<')
5465 extra += 2;
5466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468
5469 if (*p != '"')
5470 {
5471 EMSG2(_("E114: Missing quote: %s"), *arg);
5472 return FAIL;
5473 }
5474
5475 /* If only parsing, set *arg and return here */
5476 if (!evaluate)
5477 {
5478 *arg = p + 1;
5479 return OK;
5480 }
5481
5482 /*
5483 * Copy the string into allocated memory, handling backslashed
5484 * characters.
5485 */
5486 name = alloc((unsigned)(p - *arg + extra));
5487 if (name == NULL)
5488 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 rettv->v_type = VAR_STRING;
5490 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 {
5494 if (*p == '\\')
5495 {
5496 switch (*++p)
5497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 case 'b': *name++ = BS; ++p; break;
5499 case 'e': *name++ = ESC; ++p; break;
5500 case 'f': *name++ = FF; ++p; break;
5501 case 'n': *name++ = NL; ++p; break;
5502 case 'r': *name++ = CAR; ++p; break;
5503 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 case 'X': /* hex: "\x1", "\x12" */
5506 case 'x':
5507 case 'u': /* Unicode: "\u0023" */
5508 case 'U':
5509 if (vim_isxdigit(p[1]))
5510 {
5511 int n, nr;
5512 int c = toupper(*p);
5513
5514 if (c == 'X')
5515 n = 2;
5516 else
5517 n = 4;
5518 nr = 0;
5519 while (--n >= 0 && vim_isxdigit(p[1]))
5520 {
5521 ++p;
5522 nr = (nr << 4) + hex2nr(*p);
5523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#ifdef FEAT_MBYTE
5526 /* For "\u" store the number according to
5527 * 'encoding'. */
5528 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005529 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 else
5531#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 break;
5535
5536 /* octal: "\1", "\12", "\123" */
5537 case '0':
5538 case '1':
5539 case '2':
5540 case '3':
5541 case '4':
5542 case '5':
5543 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 case '7': *name = *p++ - '0';
5545 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547 *name = (*name << 3) + *p++ - '0';
5548 if (*p >= '0' && *p <= '7')
5549 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 break;
5553
5554 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (extra != 0)
5557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 break;
5560 }
5561 /* FALLTHROUGH */
5562
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 break;
5565 }
5566 }
5567 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 *arg = p + 1;
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 return OK;
5575}
5576
5577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 * Return OK or FAIL.
5580 */
5581 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 int evaluate;
5586{
5587 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005588 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005589 int reduce = 0;
5590
5591 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005593 */
5594 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005599 break;
5600 ++reduce;
5601 ++p;
5602 }
5603 }
5604
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005606 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 return FAIL;
5609 }
5610
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 if (!evaluate)
5613 {
5614 *arg = p + 1;
5615 return OK;
5616 }
5617
5618 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005620 */
5621 str = alloc((unsigned)((p - *arg) - reduce));
5622 if (str == NULL)
5623 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 rettv->v_type = VAR_STRING;
5625 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005632 break;
5633 ++p;
5634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005638 *arg = p + 1;
5639
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 return OK;
5641}
5642
5643/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 * Allocate a variable for a List and fill it from "*arg".
5645 * Return OK or FAIL.
5646 */
5647 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005650 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 int evaluate;
5652{
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 list_T *l = NULL;
5654 typval_T tv;
5655 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656
5657 if (evaluate)
5658 {
5659 l = list_alloc();
5660 if (l == NULL)
5661 return FAIL;
5662 }
5663
5664 *arg = skipwhite(*arg + 1);
5665 while (**arg != ']' && **arg != NUL)
5666 {
5667 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5668 goto failret;
5669 if (evaluate)
5670 {
5671 item = listitem_alloc();
5672 if (item != NULL)
5673 {
5674 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005675 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676 list_append(l, item);
5677 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005678 else
5679 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 }
5681
5682 if (**arg == ']')
5683 break;
5684 if (**arg != ',')
5685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 goto failret;
5688 }
5689 *arg = skipwhite(*arg + 1);
5690 }
5691
5692 if (**arg != ']')
5693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695failret:
5696 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005697 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 return FAIL;
5699 }
5700
5701 *arg = skipwhite(*arg + 1);
5702 if (evaluate)
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 rettv->v_type = VAR_LIST;
5705 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 ++l->lv_refcount;
5707 }
5708
5709 return OK;
5710}
5711
5712/*
5713 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005714 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005716 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717list_alloc()
5718{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005719 list_T *l;
5720
5721 l = (list_T *)alloc_clear(sizeof(list_T));
5722 if (l != NULL)
5723 {
5724 /* Prepend the list to the list of lists for garbage collection. */
5725 if (first_list != NULL)
5726 first_list->lv_used_prev = l;
5727 l->lv_used_prev = NULL;
5728 l->lv_used_next = first_list;
5729 first_list = l;
5730 }
5731 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732}
5733
5734/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005735 * Allocate an empty list for a return value.
5736 * Returns OK or FAIL.
5737 */
5738 static int
5739rettv_list_alloc(rettv)
5740 typval_T *rettv;
5741{
5742 list_T *l = list_alloc();
5743
5744 if (l == NULL)
5745 return FAIL;
5746
5747 rettv->vval.v_list = l;
5748 rettv->v_type = VAR_LIST;
5749 ++l->lv_refcount;
5750 return OK;
5751}
5752
5753/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 * Unreference a list: decrement the reference count and free it when it
5755 * becomes zero.
5756 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005757 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005759 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005761 if (l != NULL && --l->lv_refcount <= 0)
5762 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763}
5764
5765/*
5766 * Free a list, including all items it points to.
5767 * Ignores the reference count.
5768 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005769 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005770list_free(l, recurse)
5771 list_T *l;
5772 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773{
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005776 /* Remove the list from the list of lists for garbage collection. */
5777 if (l->lv_used_prev == NULL)
5778 first_list = l->lv_used_next;
5779 else
5780 l->lv_used_prev->lv_used_next = l->lv_used_next;
5781 if (l->lv_used_next != NULL)
5782 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5783
Bram Moolenaard9fba312005-06-26 22:34:35 +00005784 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005786 /* Remove the item before deleting it. */
5787 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005788 if (recurse || (item->li_tv.v_type != VAR_LIST
5789 && item->li_tv.v_type != VAR_DICT))
5790 clear_tv(&item->li_tv);
5791 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 }
5793 vim_free(l);
5794}
5795
5796/*
5797 * Allocate a list item.
5798 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005799 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800listitem_alloc()
5801{
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803}
5804
5805/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005806 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 */
5808 static void
5809listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005812 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 vim_free(item);
5814}
5815
5816/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005817 * Remove a list item from a List and free it. Also clears the value.
5818 */
5819 static void
5820listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 list_T *l;
5822 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005823{
5824 list_remove(l, item, item);
5825 listitem_free(item);
5826}
5827
5828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 * Get the number of items in a list.
5830 */
5831 static long
5832list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 if (l == NULL)
5836 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005837 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838}
5839
5840/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005841 * Return TRUE when two lists have exactly the same values.
5842 */
5843 static int
5844list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 list_T *l1;
5846 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005847 int ic; /* ignore case for strings */
5848{
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005850
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005851 if (l1 == NULL || l2 == NULL)
5852 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005853 if (l1 == l2)
5854 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005855 if (list_len(l1) != list_len(l2))
5856 return FALSE;
5857
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858 for (item1 = l1->lv_first, item2 = l2->lv_first;
5859 item1 != NULL && item2 != NULL;
5860 item1 = item1->li_next, item2 = item2->li_next)
5861 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5862 return FALSE;
5863 return item1 == NULL && item2 == NULL;
5864}
5865
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005866#if defined(FEAT_PYTHON) || defined(PROTO)
5867/*
5868 * Return the dictitem that an entry in a hashtable points to.
5869 */
5870 dictitem_T *
5871dict_lookup(hi)
5872 hashitem_T *hi;
5873{
5874 return HI2DI(hi);
5875}
5876#endif
5877
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005878/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005879 * Return TRUE when two dictionaries have exactly the same key/values.
5880 */
5881 static int
5882dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 dict_T *d1;
5884 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005885 int ic; /* ignore case for strings */
5886{
Bram Moolenaar33570922005-01-25 22:26:29 +00005887 hashitem_T *hi;
5888 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005889 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005890
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005891 if (d1 == NULL || d2 == NULL)
5892 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005893 if (d1 == d2)
5894 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005895 if (dict_len(d1) != dict_len(d2))
5896 return FALSE;
5897
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005898 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005901 if (!HASHITEM_EMPTY(hi))
5902 {
5903 item2 = dict_find(d2, hi->hi_key, -1);
5904 if (item2 == NULL)
5905 return FALSE;
5906 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5907 return FALSE;
5908 --todo;
5909 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005910 }
5911 return TRUE;
5912}
5913
5914/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005915 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005916 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005917 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005918 */
5919 static int
5920tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 typval_T *tv1;
5922 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923 int ic; /* ignore case */
5924{
5925 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005926 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005927 static int recursive = 0; /* cach recursive loops */
5928 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005930 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005931 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005932 /* Catch lists and dicts that have an endless loop by limiting
5933 * recursiveness to 1000. We guess they are equal then. */
5934 if (recursive >= 1000)
5935 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005936
5937 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005938 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005939 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005940 ++recursive;
5941 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5942 --recursive;
5943 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005944
5945 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005946 ++recursive;
5947 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5948 --recursive;
5949 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005950
5951 case VAR_FUNC:
5952 return (tv1->vval.v_string != NULL
5953 && tv2->vval.v_string != NULL
5954 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5955
5956 case VAR_NUMBER:
5957 return tv1->vval.v_number == tv2->vval.v_number;
5958
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005959#ifdef FEAT_FLOAT
5960 case VAR_FLOAT:
5961 return tv1->vval.v_float == tv2->vval.v_float;
5962#endif
5963
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005964 case VAR_STRING:
5965 s1 = get_tv_string_buf(tv1, buf1);
5966 s2 = get_tv_string_buf(tv2, buf2);
5967 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005969
5970 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 return TRUE;
5972}
5973
5974/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 * Locate item with index "n" in list "l" and return it.
5976 * A negative index is counted from the end; -1 is the last item.
5977 * Returns NULL when "n" is out of range.
5978 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 long n;
5983{
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 long idx;
5986
5987 if (l == NULL)
5988 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005989
5990 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005992 n = l->lv_len + n;
5993
5994 /* Check for index out of range. */
5995 if (n < 0 || n >= l->lv_len)
5996 return NULL;
5997
5998 /* When there is a cached index may start search from there. */
5999 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006001 if (n < l->lv_idx / 2)
6002 {
6003 /* closest to the start of the list */
6004 item = l->lv_first;
6005 idx = 0;
6006 }
6007 else if (n > (l->lv_idx + l->lv_len) / 2)
6008 {
6009 /* closest to the end of the list */
6010 item = l->lv_last;
6011 idx = l->lv_len - 1;
6012 }
6013 else
6014 {
6015 /* closest to the cached index */
6016 item = l->lv_idx_item;
6017 idx = l->lv_idx;
6018 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 }
6020 else
6021 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006022 if (n < l->lv_len / 2)
6023 {
6024 /* closest to the start of the list */
6025 item = l->lv_first;
6026 idx = 0;
6027 }
6028 else
6029 {
6030 /* closest to the end of the list */
6031 item = l->lv_last;
6032 idx = l->lv_len - 1;
6033 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006035
6036 while (n > idx)
6037 {
6038 /* search forward */
6039 item = item->li_next;
6040 ++idx;
6041 }
6042 while (n < idx)
6043 {
6044 /* search backward */
6045 item = item->li_prev;
6046 --idx;
6047 }
6048
6049 /* cache the used index */
6050 l->lv_idx = idx;
6051 l->lv_idx_item = item;
6052
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 return item;
6054}
6055
6056/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006057 * Get list item "l[idx]" as a number.
6058 */
6059 static long
6060list_find_nr(l, idx, errorp)
6061 list_T *l;
6062 long idx;
6063 int *errorp; /* set to TRUE when something wrong */
6064{
6065 listitem_T *li;
6066
6067 li = list_find(l, idx);
6068 if (li == NULL)
6069 {
6070 if (errorp != NULL)
6071 *errorp = TRUE;
6072 return -1L;
6073 }
6074 return get_tv_number_chk(&li->li_tv, errorp);
6075}
6076
6077/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006078 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6079 */
6080 char_u *
6081list_find_str(l, idx)
6082 list_T *l;
6083 long idx;
6084{
6085 listitem_T *li;
6086
6087 li = list_find(l, idx - 1);
6088 if (li == NULL)
6089 {
6090 EMSGN(_(e_listidx), idx);
6091 return NULL;
6092 }
6093 return get_tv_string(&li->li_tv);
6094}
6095
6096/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006097 * Locate "item" list "l" and return its index.
6098 * Returns -1 when "item" is not in the list.
6099 */
6100 static long
6101list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 list_T *l;
6103 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006104{
6105 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006107
6108 if (l == NULL)
6109 return -1;
6110 idx = 0;
6111 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6112 ++idx;
6113 if (li == NULL)
6114 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006115 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006116}
6117
6118/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119 * Append item "item" to the end of list "l".
6120 */
6121 static void
6122list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 list_T *l;
6124 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125{
6126 if (l->lv_last == NULL)
6127 {
6128 /* empty list */
6129 l->lv_first = item;
6130 l->lv_last = item;
6131 item->li_prev = NULL;
6132 }
6133 else
6134 {
6135 l->lv_last->li_next = item;
6136 item->li_prev = l->lv_last;
6137 l->lv_last = item;
6138 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006139 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 item->li_next = NULL;
6141}
6142
6143/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 */
6147 static int
6148list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 list_T *l;
6150 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006152 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153
Bram Moolenaar05159a02005-02-26 23:04:13 +00006154 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006156 copy_tv(tv, &li->li_tv);
6157 list_append(l, li);
6158 return OK;
6159}
6160
6161/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006162 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006163 * Return FAIL when out of memory.
6164 */
6165 int
6166list_append_dict(list, dict)
6167 list_T *list;
6168 dict_T *dict;
6169{
6170 listitem_T *li = listitem_alloc();
6171
6172 if (li == NULL)
6173 return FAIL;
6174 li->li_tv.v_type = VAR_DICT;
6175 li->li_tv.v_lock = 0;
6176 li->li_tv.vval.v_dict = dict;
6177 list_append(list, li);
6178 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 return OK;
6180}
6181
6182/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006183 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006184 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006185 * Returns FAIL when out of memory.
6186 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006187 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006188list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006189 list_T *l;
6190 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006191 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006192{
6193 listitem_T *li = listitem_alloc();
6194
6195 if (li == NULL)
6196 return FAIL;
6197 list_append(l, li);
6198 li->li_tv.v_type = VAR_STRING;
6199 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006200 if (str == NULL)
6201 li->li_tv.vval.v_string = NULL;
6202 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006203 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006204 return FAIL;
6205 return OK;
6206}
6207
6208/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006209 * Append "n" to list "l".
6210 * Returns FAIL when out of memory.
6211 */
6212 static int
6213list_append_number(l, n)
6214 list_T *l;
6215 varnumber_T n;
6216{
6217 listitem_T *li;
6218
6219 li = listitem_alloc();
6220 if (li == NULL)
6221 return FAIL;
6222 li->li_tv.v_type = VAR_NUMBER;
6223 li->li_tv.v_lock = 0;
6224 li->li_tv.vval.v_number = n;
6225 list_append(l, li);
6226 return OK;
6227}
6228
6229/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006231 * If "item" is NULL append at the end.
6232 * Return FAIL when out of memory.
6233 */
6234 static int
6235list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 list_T *l;
6237 typval_T *tv;
6238 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241
6242 if (ni == NULL)
6243 return FAIL;
6244 copy_tv(tv, &ni->li_tv);
6245 if (item == NULL)
6246 /* Append new item at end of list. */
6247 list_append(l, ni);
6248 else
6249 {
6250 /* Insert new item before existing item. */
6251 ni->li_prev = item->li_prev;
6252 ni->li_next = item;
6253 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006254 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006255 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006256 ++l->lv_idx;
6257 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006259 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006261 l->lv_idx_item = NULL;
6262 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006263 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265 }
6266 return OK;
6267}
6268
6269/*
6270 * Extend "l1" with "l2".
6271 * If "bef" is NULL append at the end, otherwise insert before this item.
6272 * Returns FAIL when out of memory.
6273 */
6274 static int
6275list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 list_T *l1;
6277 list_T *l2;
6278 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006279{
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006281 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006282
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006283 /* We also quit the loop when we have inserted the original item count of
6284 * the list, avoid a hang when we extend a list with itself. */
6285 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6287 return FAIL;
6288 return OK;
6289}
6290
6291/*
6292 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6293 * Return FAIL when out of memory.
6294 */
6295 static int
6296list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 list_T *l1;
6298 list_T *l2;
6299 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006300{
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006302
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006303 if (l1 == NULL || l2 == NULL)
6304 return FAIL;
6305
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006307 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 if (l == NULL)
6309 return FAIL;
6310 tv->v_type = VAR_LIST;
6311 tv->vval.v_list = l;
6312
6313 /* append all items from the second list */
6314 return list_extend(l, l2, NULL);
6315}
6316
6317/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006318 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006320 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 * Returns NULL when out of memory.
6322 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006324list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006327 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328{
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 list_T *copy;
6330 listitem_T *item;
6331 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332
6333 if (orig == NULL)
6334 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335
6336 copy = list_alloc();
6337 if (copy != NULL)
6338 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006339 if (copyID != 0)
6340 {
6341 /* Do this before adding the items, because one of the items may
6342 * refer back to this list. */
6343 orig->lv_copyID = copyID;
6344 orig->lv_copylist = copy;
6345 }
6346 for (item = orig->lv_first; item != NULL && !got_int;
6347 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 {
6349 ni = listitem_alloc();
6350 if (ni == NULL)
6351 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006353 {
6354 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6355 {
6356 vim_free(ni);
6357 break;
6358 }
6359 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006361 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 list_append(copy, ni);
6363 }
6364 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006365 if (item != NULL)
6366 {
6367 list_unref(copy);
6368 copy = NULL;
6369 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 }
6371
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 return copy;
6373}
6374
6375/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006376 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006377 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006380list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 list_T *l;
6382 listitem_T *item;
6383 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006384{
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006387 /* notify watchers */
6388 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006391 list_fix_watch(l, ip);
6392 if (ip == item2)
6393 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395
6396 if (item2->li_next == NULL)
6397 l->lv_last = item->li_prev;
6398 else
6399 item2->li_next->li_prev = item->li_prev;
6400 if (item->li_prev == NULL)
6401 l->lv_first = item2->li_next;
6402 else
6403 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006404 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405}
6406
6407/*
6408 * Return an allocated string with the string representation of a list.
6409 * May return NULL.
6410 */
6411 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006412list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006414 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415{
6416 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417
6418 if (tv->vval.v_list == NULL)
6419 return NULL;
6420 ga_init2(&ga, (int)sizeof(char), 80);
6421 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006422 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006423 {
6424 vim_free(ga.ga_data);
6425 return NULL;
6426 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 ga_append(&ga, ']');
6428 ga_append(&ga, NUL);
6429 return (char_u *)ga.ga_data;
6430}
6431
6432/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006433 * Join list "l" into a string in "*gap", using separator "sep".
6434 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006436 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006437 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006438list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006439 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006441 char_u *sep;
6442 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006443 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006444{
6445 int first = TRUE;
6446 char_u *tofree;
6447 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006449 char_u *s;
6450
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006452 {
6453 if (first)
6454 first = FALSE;
6455 else
6456 ga_concat(gap, sep);
6457
6458 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006459 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006460 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006461 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006462 if (s != NULL)
6463 ga_concat(gap, s);
6464 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006465 if (s == NULL)
6466 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006467 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006469}
6470
6471/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006472 * Garbage collection for lists and dictionaries.
6473 *
6474 * We use reference counts to be able to free most items right away when they
6475 * are no longer used. But for composite items it's possible that it becomes
6476 * unused while the reference count is > 0: When there is a recursive
6477 * reference. Example:
6478 * :let l = [1, 2, 3]
6479 * :let d = {9: l}
6480 * :let l[1] = d
6481 *
6482 * Since this is quite unusual we handle this with garbage collection: every
6483 * once in a while find out which lists and dicts are not referenced from any
6484 * variable.
6485 *
6486 * Here is a good reference text about garbage collection (refers to Python
6487 * but it applies to all reference-counting mechanisms):
6488 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006489 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006490
6491/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006492 * Do garbage collection for lists and dicts.
6493 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006494 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006495 int
6496garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006497{
6498 dict_T *dd;
6499 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006500 int copyID = ++current_copyID;
6501 buf_T *buf;
6502 win_T *wp;
6503 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006504 funccall_T *fc, **pfc;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006505 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006506#ifdef FEAT_WINDOWS
6507 tabpage_T *tp;
6508#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006509
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006510 /* Only do this once. */
6511 want_garbage_collect = FALSE;
6512 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006513 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006514
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006515 /*
6516 * 1. Go through all accessible variables and mark all lists and dicts
6517 * with copyID.
6518 */
6519 /* script-local variables */
6520 for (i = 1; i <= ga_scripts.ga_len; ++i)
6521 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6522
6523 /* buffer-local variables */
6524 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6525 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6526
6527 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006528 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006529 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6530
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006531#ifdef FEAT_WINDOWS
6532 /* tabpage-local variables */
6533 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6534 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6535#endif
6536
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006537 /* global variables */
6538 set_ref_in_ht(&globvarht, copyID);
6539
6540 /* function-local variables */
6541 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6542 {
6543 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6544 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6545 }
6546
Bram Moolenaard812df62008-11-09 12:46:09 +00006547 /* v: vars */
6548 set_ref_in_ht(&vimvarht, copyID);
6549
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006550 /*
6551 * 2. Go through the list of dicts and free items without the copyID.
6552 */
6553 for (dd = first_dict; dd != NULL; )
6554 if (dd->dv_copyID != copyID)
6555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006556 /* Free the Dictionary and ordinary items it contains, but don't
6557 * recurse into Lists and Dictionaries, they will be in the list
6558 * of dicts or list of lists. */
6559 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006560 did_free = TRUE;
6561
6562 /* restart, next dict may also have been freed */
6563 dd = first_dict;
6564 }
6565 else
6566 dd = dd->dv_used_next;
6567
6568 /*
6569 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006570 * But don't free a list that has a watcher (used in a for loop), these
6571 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006572 */
6573 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006574 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006575 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006576 /* Free the List and ordinary items it contains, but don't recurse
6577 * into Lists and Dictionaries, they will be in the list of dicts
6578 * or list of lists. */
6579 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006580 did_free = TRUE;
6581
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006582 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006583 ll = first_list;
6584 }
6585 else
6586 ll = ll->lv_used_next;
6587
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006588 /* check if any funccal can be freed now */
6589 for (pfc = &previous_funccal; *pfc != NULL; )
6590 {
6591 if (can_free_funccal(*pfc, copyID))
6592 {
6593 fc = *pfc;
6594 *pfc = fc->caller;
6595 free_funccal(fc, TRUE);
6596 did_free = TRUE;
6597 }
6598 else
6599 pfc = &(*pfc)->caller;
6600 }
6601
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006602 return did_free;
6603}
6604
6605/*
6606 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6607 */
6608 static void
6609set_ref_in_ht(ht, copyID)
6610 hashtab_T *ht;
6611 int copyID;
6612{
6613 int todo;
6614 hashitem_T *hi;
6615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006616 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 for (hi = ht->ht_array; todo > 0; ++hi)
6618 if (!HASHITEM_EMPTY(hi))
6619 {
6620 --todo;
6621 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6622 }
6623}
6624
6625/*
6626 * Mark all lists and dicts referenced through list "l" with "copyID".
6627 */
6628 static void
6629set_ref_in_list(l, copyID)
6630 list_T *l;
6631 int copyID;
6632{
6633 listitem_T *li;
6634
6635 for (li = l->lv_first; li != NULL; li = li->li_next)
6636 set_ref_in_item(&li->li_tv, copyID);
6637}
6638
6639/*
6640 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6641 */
6642 static void
6643set_ref_in_item(tv, copyID)
6644 typval_T *tv;
6645 int copyID;
6646{
6647 dict_T *dd;
6648 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006649
6650 switch (tv->v_type)
6651 {
6652 case VAR_DICT:
6653 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006654 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006655 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006656 /* Didn't see this dict yet. */
6657 dd->dv_copyID = copyID;
6658 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006659 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006660 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006661
6662 case VAR_LIST:
6663 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006664 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006665 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006666 /* Didn't see this list yet. */
6667 ll->lv_copyID = copyID;
6668 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006669 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006670 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006671 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006672 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006673}
6674
6675/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 * Allocate an empty header for a dictionary.
6677 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006678 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679dict_alloc()
6680{
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006682
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006684 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006685 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006686 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006687 if (first_dict != NULL)
6688 first_dict->dv_used_prev = d;
6689 d->dv_used_next = first_dict;
6690 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006691 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006692
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006694 d->dv_lock = 0;
6695 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006696 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006697 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006698 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699}
6700
6701/*
6702 * Unreference a Dictionary: decrement the reference count and free it when it
6703 * becomes zero.
6704 */
6705 static void
6706dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006707 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006708{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006709 if (d != NULL && --d->dv_refcount <= 0)
6710 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711}
6712
6713/*
6714 * Free a Dictionary, including all items it contains.
6715 * Ignores the reference count.
6716 */
6717 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006718dict_free(d, recurse)
6719 dict_T *d;
6720 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006722 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006725
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006726 /* Remove the dict from the list of dicts for garbage collection. */
6727 if (d->dv_used_prev == NULL)
6728 first_dict = d->dv_used_next;
6729 else
6730 d->dv_used_prev->dv_used_next = d->dv_used_next;
6731 if (d->dv_used_next != NULL)
6732 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6733
6734 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006736 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006737 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006739 if (!HASHITEM_EMPTY(hi))
6740 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 /* Remove the item before deleting it, just in case there is
6742 * something recursive causing trouble. */
6743 di = HI2DI(hi);
6744 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006745 if (recurse || (di->di_tv.v_type != VAR_LIST
6746 && di->di_tv.v_type != VAR_DICT))
6747 clear_tv(&di->di_tv);
6748 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006749 --todo;
6750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006752 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 vim_free(d);
6754}
6755
6756/*
6757 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006758 * The "key" is copied to the new item.
6759 * Note that the value of the item "di_tv" still needs to be initialized!
6760 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006762 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006763dictitem_alloc(key)
6764 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006765{
Bram Moolenaar33570922005-01-25 22:26:29 +00006766 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006767
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006768 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006769 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006772 di->di_flags = 0;
6773 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006774 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775}
6776
6777/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778 * Make a copy of a Dictionary item.
6779 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006783{
Bram Moolenaar33570922005-01-25 22:26:29 +00006784 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006785
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006786 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6787 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006788 if (di != NULL)
6789 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006790 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006791 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006792 copy_tv(&org->di_tv, &di->di_tv);
6793 }
6794 return di;
6795}
6796
6797/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006798 * Remove item "item" from Dictionary "dict" and free it.
6799 */
6800 static void
6801dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006802 dict_T *dict;
6803 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006804{
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006806
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006808 if (HASHITEM_EMPTY(hi))
6809 EMSG2(_(e_intern2), "dictitem_remove()");
6810 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006811 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006812 dictitem_free(item);
6813}
6814
6815/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006816 * Free a dict item. Also clears the value.
6817 */
6818 static void
6819dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006820 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006821{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822 clear_tv(&item->di_tv);
6823 vim_free(item);
6824}
6825
6826/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006827 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6828 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006829 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006830 * Returns NULL when out of memory.
6831 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006833dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006836 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837{
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 dict_T *copy;
6839 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006840 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006842
6843 if (orig == NULL)
6844 return NULL;
6845
6846 copy = dict_alloc();
6847 if (copy != NULL)
6848 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006849 if (copyID != 0)
6850 {
6851 orig->dv_copyID = copyID;
6852 orig->dv_copydict = copy;
6853 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006854 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006855 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006858 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006859 --todo;
6860
6861 di = dictitem_alloc(hi->hi_key);
6862 if (di == NULL)
6863 break;
6864 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006865 {
6866 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6867 copyID) == FAIL)
6868 {
6869 vim_free(di);
6870 break;
6871 }
6872 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006873 else
6874 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6875 if (dict_add(copy, di) == FAIL)
6876 {
6877 dictitem_free(di);
6878 break;
6879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006881 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006884 if (todo > 0)
6885 {
6886 dict_unref(copy);
6887 copy = NULL;
6888 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 }
6890
6891 return copy;
6892}
6893
6894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006896 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006897 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006899dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 dict_T *d;
6901 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006902{
Bram Moolenaar33570922005-01-25 22:26:29 +00006903 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904}
6905
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006907 * Add a number or string entry to dictionary "d".
6908 * When "str" is NULL use number "nr", otherwise use "str".
6909 * Returns FAIL when out of memory and when key already exists.
6910 */
6911 int
6912dict_add_nr_str(d, key, nr, str)
6913 dict_T *d;
6914 char *key;
6915 long nr;
6916 char_u *str;
6917{
6918 dictitem_T *item;
6919
6920 item = dictitem_alloc((char_u *)key);
6921 if (item == NULL)
6922 return FAIL;
6923 item->di_tv.v_lock = 0;
6924 if (str == NULL)
6925 {
6926 item->di_tv.v_type = VAR_NUMBER;
6927 item->di_tv.vval.v_number = nr;
6928 }
6929 else
6930 {
6931 item->di_tv.v_type = VAR_STRING;
6932 item->di_tv.vval.v_string = vim_strsave(str);
6933 }
6934 if (dict_add(d, item) == FAIL)
6935 {
6936 dictitem_free(item);
6937 return FAIL;
6938 }
6939 return OK;
6940}
6941
6942/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006943 * Get the number of items in a Dictionary.
6944 */
6945 static long
6946dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006948{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006949 if (d == NULL)
6950 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006951 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952}
6953
6954/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006955 * Find item "key[len]" in Dictionary "d".
6956 * If "len" is negative use strlen(key).
6957 * Returns NULL when not found.
6958 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006960dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962 char_u *key;
6963 int len;
6964{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965#define AKEYLEN 200
6966 char_u buf[AKEYLEN];
6967 char_u *akey;
6968 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 if (len < 0)
6972 akey = key;
6973 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 tofree = akey = vim_strnsave(key, len);
6976 if (akey == NULL)
6977 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006978 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006979 else
6980 {
6981 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006982 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 akey = buf;
6984 }
6985
Bram Moolenaar33570922005-01-25 22:26:29 +00006986 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006987 vim_free(tofree);
6988 if (HASHITEM_EMPTY(hi))
6989 return NULL;
6990 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006991}
6992
6993/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006994 * Get a string item from a dictionary.
6995 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006996 * Returns NULL if the entry doesn't exist or out of memory.
6997 */
6998 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006999get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007000 dict_T *d;
7001 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007002 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007003{
7004 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007005 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007006
7007 di = dict_find(d, key, -1);
7008 if (di == NULL)
7009 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007010 s = get_tv_string(&di->di_tv);
7011 if (save && s != NULL)
7012 s = vim_strsave(s);
7013 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007014}
7015
7016/*
7017 * Get a number item from a dictionary.
7018 * Returns 0 if the entry doesn't exist or out of memory.
7019 */
7020 long
7021get_dict_number(d, key)
7022 dict_T *d;
7023 char_u *key;
7024{
7025 dictitem_T *di;
7026
7027 di = dict_find(d, key, -1);
7028 if (di == NULL)
7029 return 0;
7030 return get_tv_number(&di->di_tv);
7031}
7032
7033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034 * Return an allocated string with the string representation of a Dictionary.
7035 * May return NULL.
7036 */
7037 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007038dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007040 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041{
7042 garray_T ga;
7043 int first = TRUE;
7044 char_u *tofree;
7045 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007049 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 return NULL;
7053 ga_init2(&ga, (int)sizeof(char), 80);
7054 ga_append(&ga, '{');
7055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007056 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007057 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 --todo;
7062
7063 if (first)
7064 first = FALSE;
7065 else
7066 ga_concat(&ga, (char_u *)", ");
7067
7068 tofree = string_quote(hi->hi_key, FALSE);
7069 if (tofree != NULL)
7070 {
7071 ga_concat(&ga, tofree);
7072 vim_free(tofree);
7073 }
7074 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007075 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076 if (s != NULL)
7077 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007079 if (s == NULL)
7080 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007082 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007083 if (todo > 0)
7084 {
7085 vim_free(ga.ga_data);
7086 return NULL;
7087 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088
7089 ga_append(&ga, '}');
7090 ga_append(&ga, NUL);
7091 return (char_u *)ga.ga_data;
7092}
7093
7094/*
7095 * Allocate a variable for a Dictionary and fill it from "*arg".
7096 * Return OK or FAIL. Returns NOTDONE for {expr}.
7097 */
7098 static int
7099get_dict_tv(arg, rettv, evaluate)
7100 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 int evaluate;
7103{
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dict_T *d = NULL;
7105 typval_T tvkey;
7106 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007107 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111
7112 /*
7113 * First check if it's not a curly-braces thing: {expr}.
7114 * Must do this without evaluating, otherwise a function may be called
7115 * twice. Unfortunately this means we need to call eval1() twice for the
7116 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 if (*start != '}')
7120 {
7121 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7122 return FAIL;
7123 if (*start == '}')
7124 return NOTDONE;
7125 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007126
7127 if (evaluate)
7128 {
7129 d = dict_alloc();
7130 if (d == NULL)
7131 return FAIL;
7132 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 tvkey.v_type = VAR_UNKNOWN;
7134 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135
7136 *arg = skipwhite(*arg + 1);
7137 while (**arg != '}' && **arg != NUL)
7138 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007139 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007140 goto failret;
7141 if (**arg != ':')
7142 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007143 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007144 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 goto failret;
7146 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007147 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007149 key = get_tv_string_buf_chk(&tvkey, buf);
7150 if (key == NULL || *key == NUL)
7151 {
7152 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7153 if (key != NULL)
7154 EMSG(_(e_emptykey));
7155 clear_tv(&tvkey);
7156 goto failret;
7157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159
7160 *arg = skipwhite(*arg + 1);
7161 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7162 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007163 if (evaluate)
7164 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 goto failret;
7166 }
7167 if (evaluate)
7168 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 if (item != NULL)
7171 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007172 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 clear_tv(&tv);
7175 goto failret;
7176 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 item = dictitem_alloc(key);
7178 clear_tv(&tvkey);
7179 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007182 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 if (dict_add(d, item) == FAIL)
7184 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 }
7186 }
7187
7188 if (**arg == '}')
7189 break;
7190 if (**arg != ',')
7191 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007192 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 goto failret;
7194 }
7195 *arg = skipwhite(*arg + 1);
7196 }
7197
7198 if (**arg != '}')
7199 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007200 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201failret:
7202 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007203 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 return FAIL;
7205 }
7206
7207 *arg = skipwhite(*arg + 1);
7208 if (evaluate)
7209 {
7210 rettv->v_type = VAR_DICT;
7211 rettv->vval.v_dict = d;
7212 ++d->dv_refcount;
7213 }
7214
7215 return OK;
7216}
7217
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007219 * Return a string with the string representation of a variable.
7220 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007221 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007222 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007223 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007224 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007225 */
7226 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007227echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007229 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007230 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007231 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007232{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233 static int recurse = 0;
7234 char_u *r = NULL;
7235
Bram Moolenaar33570922005-01-25 22:26:29 +00007236 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007238 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007239 *tofree = NULL;
7240 return NULL;
7241 }
7242 ++recurse;
7243
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 switch (tv->v_type)
7245 {
7246 case VAR_FUNC:
7247 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007248 r = tv->vval.v_string;
7249 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007250
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007251 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007252 if (tv->vval.v_list == NULL)
7253 {
7254 *tofree = NULL;
7255 r = NULL;
7256 }
7257 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7258 {
7259 *tofree = NULL;
7260 r = (char_u *)"[...]";
7261 }
7262 else
7263 {
7264 tv->vval.v_list->lv_copyID = copyID;
7265 *tofree = list2string(tv, copyID);
7266 r = *tofree;
7267 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007268 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007269
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007271 if (tv->vval.v_dict == NULL)
7272 {
7273 *tofree = NULL;
7274 r = NULL;
7275 }
7276 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7277 {
7278 *tofree = NULL;
7279 r = (char_u *)"{...}";
7280 }
7281 else
7282 {
7283 tv->vval.v_dict->dv_copyID = copyID;
7284 *tofree = dict2string(tv, copyID);
7285 r = *tofree;
7286 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007289 case VAR_STRING:
7290 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 *tofree = NULL;
7292 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007293 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007295#ifdef FEAT_FLOAT
7296 case VAR_FLOAT:
7297 *tofree = NULL;
7298 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7299 r = numbuf;
7300 break;
7301#endif
7302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007303 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007304 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007306 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307
7308 --recurse;
7309 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007310}
7311
7312/*
7313 * Return a string with the string representation of a variable.
7314 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7315 * "numbuf" is used for a number.
7316 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007317 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007318 */
7319 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007320tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007322 char_u **tofree;
7323 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007324 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007325{
7326 switch (tv->v_type)
7327 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007328 case VAR_FUNC:
7329 *tofree = string_quote(tv->vval.v_string, TRUE);
7330 return *tofree;
7331 case VAR_STRING:
7332 *tofree = string_quote(tv->vval.v_string, FALSE);
7333 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007334#ifdef FEAT_FLOAT
7335 case VAR_FLOAT:
7336 *tofree = NULL;
7337 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7338 return numbuf;
7339#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007341 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007344 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007347 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007348}
7349
7350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 * Return string "str" in ' quotes, doubling ' characters.
7352 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007354 */
7355 static char_u *
7356string_quote(str, function)
7357 char_u *str;
7358 int function;
7359{
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007361 char_u *p, *r, *s;
7362
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 len = (function ? 13 : 3);
7364 if (str != NULL)
7365 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 for (p = str; *p != NUL; mb_ptr_adv(p))
7368 if (*p == '\'')
7369 ++len;
7370 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007371 s = r = alloc(len);
7372 if (r != NULL)
7373 {
7374 if (function)
7375 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007377 r += 10;
7378 }
7379 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 if (str != NULL)
7382 for (p = str; *p != NUL; )
7383 {
7384 if (*p == '\'')
7385 *r++ = '\'';
7386 MB_COPY_CHAR(p, r);
7387 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007389 if (function)
7390 *r++ = ')';
7391 *r++ = NUL;
7392 }
7393 return s;
7394}
7395
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007396#ifdef FEAT_FLOAT
7397/*
7398 * Convert the string "text" to a floating point number.
7399 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7400 * this always uses a decimal point.
7401 * Returns the length of the text that was consumed.
7402 */
7403 static int
7404string2float(text, value)
7405 char_u *text;
7406 float_T *value; /* result stored here */
7407{
7408 char *s = (char *)text;
7409 float_T f;
7410
7411 f = strtod(s, &s);
7412 *value = f;
7413 return (int)((char_u *)s - text);
7414}
7415#endif
7416
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 * Get the value of an environment variable.
7419 * "arg" is pointing to the '$'. It is advanced to after the name.
7420 * If the environment variable was not set, silently assume it is empty.
7421 * Always return OK.
7422 */
7423 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 int evaluate;
7428{
7429 char_u *string = NULL;
7430 int len;
7431 int cc;
7432 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007433 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 ++*arg;
7436 name = *arg;
7437 len = get_env_len(arg);
7438 if (evaluate)
7439 {
7440 if (len != 0)
7441 {
7442 cc = name[len];
7443 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007444 /* first try vim_getenv(), fast for normal environment vars */
7445 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007447 {
7448 if (!mustfree)
7449 string = vim_strsave(string);
7450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 else
7452 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007453 if (mustfree)
7454 vim_free(string);
7455
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 /* next try expanding things like $VIM and ${HOME} */
7457 string = expand_env_save(name - 1);
7458 if (string != NULL && *string == '$')
7459 {
7460 vim_free(string);
7461 string = NULL;
7462 }
7463 }
7464 name[len] = cc;
7465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007466 rettv->v_type = VAR_STRING;
7467 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469
7470 return OK;
7471}
7472
7473/*
7474 * Array with names and number of arguments of all internal functions
7475 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7476 */
7477static struct fst
7478{
7479 char *f_name; /* function name */
7480 char f_min_argc; /* minimal number of arguments */
7481 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007483 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484} functions[] =
7485{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007486#ifdef FEAT_FLOAT
7487 {"abs", 1, 1, f_abs},
7488#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007489 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {"append", 2, 2, f_append},
7491 {"argc", 0, 0, f_argc},
7492 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007493 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007494#ifdef FEAT_FLOAT
7495 {"atan", 1, 1, f_atan},
7496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007498 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 {"bufexists", 1, 1, f_bufexists},
7500 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7501 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7502 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7503 {"buflisted", 1, 1, f_buflisted},
7504 {"bufloaded", 1, 1, f_bufloaded},
7505 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007506 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {"bufwinnr", 1, 1, f_bufwinnr},
7508 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007509 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007511#ifdef FEAT_FLOAT
7512 {"ceil", 1, 1, f_ceil},
7513#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007514 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 {"char2nr", 1, 1, f_char2nr},
7516 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007517 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007519#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007520 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007521 {"complete_add", 1, 1, f_complete_add},
7522 {"complete_check", 0, 0, f_complete_check},
7523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007525 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007526#ifdef FEAT_FLOAT
7527 {"cos", 1, 1, f_cos},
7528#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007529 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007531 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007532 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 {"delete", 1, 1, f_delete},
7534 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007535 {"diff_filler", 1, 1, f_diff_filler},
7536 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007537 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007539 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {"eventhandler", 0, 0, f_eventhandler},
7541 {"executable", 1, 1, f_executable},
7542 {"exists", 1, 1, f_exists},
7543 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007544 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007545 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7547 {"filereadable", 1, 1, f_filereadable},
7548 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007549 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007550 {"finddir", 1, 3, f_finddir},
7551 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007552#ifdef FEAT_FLOAT
7553 {"float2nr", 1, 1, f_float2nr},
7554 {"floor", 1, 1, f_floor},
7555#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007556 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {"fnamemodify", 2, 2, f_fnamemodify},
7558 {"foldclosed", 1, 1, f_foldclosed},
7559 {"foldclosedend", 1, 1, f_foldclosedend},
7560 {"foldlevel", 1, 1, f_foldlevel},
7561 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007562 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007564 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007565 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007566 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007567 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {"getbufvar", 2, 2, f_getbufvar},
7569 {"getchar", 0, 1, f_getchar},
7570 {"getcharmod", 0, 0, f_getcharmod},
7571 {"getcmdline", 0, 0, f_getcmdline},
7572 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007573 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007575 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007576 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {"getfsize", 1, 1, f_getfsize},
7578 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007579 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007580 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007581 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007582 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007583 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007584 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007586 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007588 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {"getwinposx", 0, 0, f_getwinposx},
7590 {"getwinposy", 0, 0, f_getwinposy},
7591 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007592 {"glob", 1, 2, f_glob},
7593 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007595 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007596 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007597 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7599 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7600 {"histadd", 2, 2, f_histadd},
7601 {"histdel", 1, 2, f_histdel},
7602 {"histget", 1, 2, f_histget},
7603 {"histnr", 1, 1, f_histnr},
7604 {"hlID", 1, 1, f_hlID},
7605 {"hlexists", 1, 1, f_hlexists},
7606 {"hostname", 0, 0, f_hostname},
7607 {"iconv", 3, 3, f_iconv},
7608 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007609 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007610 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007612 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {"inputrestore", 0, 0, f_inputrestore},
7614 {"inputsave", 0, 0, f_inputsave},
7615 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007618 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007620 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007623 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {"libcall", 3, 3, f_libcall},
7625 {"libcallnr", 3, 3, f_libcallnr},
7626 {"line", 1, 1, f_line},
7627 {"line2byte", 1, 1, f_line2byte},
7628 {"lispindent", 1, 1, f_lispindent},
7629 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007630#ifdef FEAT_FLOAT
7631 {"log10", 1, 1, f_log10},
7632#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007633 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007634 {"maparg", 1, 3, f_maparg},
7635 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007636 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007637 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007638 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007639 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007640 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007641 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007642 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007643 {"max", 1, 1, f_max},
7644 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007645#ifdef vim_mkdir
7646 {"mkdir", 1, 3, f_mkdir},
7647#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007648 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {"nextnonblank", 1, 1, f_nextnonblank},
7650 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007651 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007652#ifdef FEAT_FLOAT
7653 {"pow", 2, 2, f_pow},
7654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007656 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007657 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007659 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007660 {"reltime", 0, 2, f_reltime},
7661 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {"remote_expr", 2, 3, f_remote_expr},
7663 {"remote_foreground", 1, 1, f_remote_foreground},
7664 {"remote_peek", 1, 2, f_remote_peek},
7665 {"remote_read", 1, 1, f_remote_read},
7666 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007667 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007669 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007671 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 {"round", 1, 1, f_round},
7674#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007675 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007676 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007677 {"searchpair", 3, 7, f_searchpair},
7678 {"searchpairpos", 3, 7, f_searchpairpos},
7679 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {"server2client", 2, 2, f_server2client},
7681 {"serverlist", 0, 0, f_serverlist},
7682 {"setbufvar", 3, 3, f_setbufvar},
7683 {"setcmdpos", 1, 1, f_setcmdpos},
7684 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007685 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007686 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007687 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007688 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007690 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007692 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007694#ifdef FEAT_FLOAT
7695 {"sin", 1, 1, f_sin},
7696#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007697 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007698 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007699 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007700 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007701 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007702#ifdef FEAT_FLOAT
7703 {"sqrt", 1, 1, f_sqrt},
7704 {"str2float", 1, 1, f_str2float},
7705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007706 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707#ifdef HAVE_STRFTIME
7708 {"strftime", 1, 2, f_strftime},
7709#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007711 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"strlen", 1, 1, f_strlen},
7713 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007714 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 {"strtrans", 1, 1, f_strtrans},
7716 {"submatch", 1, 1, f_submatch},
7717 {"substitute", 4, 4, f_substitute},
7718 {"synID", 3, 3, f_synID},
7719 {"synIDattr", 2, 3, f_synIDattr},
7720 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007721 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007722 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007723 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007724 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007725 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007726 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007727 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007729 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"tolower", 1, 1, f_tolower},
7731 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007732 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007733#ifdef FEAT_FLOAT
7734 {"trunc", 1, 1, f_trunc},
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {"virtcol", 1, 1, f_virtcol},
7739 {"visualmode", 0, 1, f_visualmode},
7740 {"winbufnr", 1, 1, f_winbufnr},
7741 {"wincol", 0, 0, f_wincol},
7742 {"winheight", 1, 1, f_winheight},
7743 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007744 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007746 {"winrestview", 1, 1, f_winrestview},
7747 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007749 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750};
7751
7752#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7753
7754/*
7755 * Function given to ExpandGeneric() to obtain the list of internal
7756 * or user defined function names.
7757 */
7758 char_u *
7759get_function_name(xp, idx)
7760 expand_T *xp;
7761 int idx;
7762{
7763 static int intidx = -1;
7764 char_u *name;
7765
7766 if (idx == 0)
7767 intidx = -1;
7768 if (intidx < 0)
7769 {
7770 name = get_user_func_name(xp, idx);
7771 if (name != NULL)
7772 return name;
7773 }
7774 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7775 {
7776 STRCPY(IObuff, functions[intidx].f_name);
7777 STRCAT(IObuff, "(");
7778 if (functions[intidx].f_max_argc == 0)
7779 STRCAT(IObuff, ")");
7780 return IObuff;
7781 }
7782
7783 return NULL;
7784}
7785
7786/*
7787 * Function given to ExpandGeneric() to obtain the list of internal or
7788 * user defined variable or function names.
7789 */
7790/*ARGSUSED*/
7791 char_u *
7792get_expr_name(xp, idx)
7793 expand_T *xp;
7794 int idx;
7795{
7796 static int intidx = -1;
7797 char_u *name;
7798
7799 if (idx == 0)
7800 intidx = -1;
7801 if (intidx < 0)
7802 {
7803 name = get_function_name(xp, idx);
7804 if (name != NULL)
7805 return name;
7806 }
7807 return get_user_var_name(xp, ++intidx);
7808}
7809
7810#endif /* FEAT_CMDL_COMPL */
7811
7812/*
7813 * Find internal function in table above.
7814 * Return index, or -1 if not found
7815 */
7816 static int
7817find_internal_func(name)
7818 char_u *name; /* name of the function */
7819{
7820 int first = 0;
7821 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7822 int cmp;
7823 int x;
7824
7825 /*
7826 * Find the function name in the table. Binary search.
7827 */
7828 while (first <= last)
7829 {
7830 x = first + ((unsigned)(last - first) >> 1);
7831 cmp = STRCMP(name, functions[x].f_name);
7832 if (cmp < 0)
7833 last = x - 1;
7834 else if (cmp > 0)
7835 first = x + 1;
7836 else
7837 return x;
7838 }
7839 return -1;
7840}
7841
7842/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7844 * name it contains, otherwise return "name".
7845 */
7846 static char_u *
7847deref_func_name(name, lenp)
7848 char_u *name;
7849 int *lenp;
7850{
Bram Moolenaar33570922005-01-25 22:26:29 +00007851 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 int cc;
7853
7854 cc = name[*lenp];
7855 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007856 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007861 {
7862 *lenp = 0;
7863 return (char_u *)""; /* just in case */
7864 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007865 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 }
7868
7869 return name;
7870}
7871
7872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 * Allocate a variable for the result of a function.
7874 * Return OK or FAIL.
7875 */
7876 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7878 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 char_u *name; /* name of the function */
7880 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 char_u **arg; /* argument, pointing to the '(' */
7883 linenr_T firstline; /* first line of range */
7884 linenr_T lastline; /* last line of range */
7885 int *doesrange; /* return: function handled range */
7886 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007887 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 char_u *argp;
7890 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007891 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 int argcount = 0; /* number of arguments found */
7893
7894 /*
7895 * Get the arguments.
7896 */
7897 argp = *arg;
7898 while (argcount < MAX_FUNC_ARGS)
7899 {
7900 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7901 if (*argp == ')' || *argp == ',' || *argp == NUL)
7902 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7904 {
7905 ret = FAIL;
7906 break;
7907 }
7908 ++argcount;
7909 if (*argp != ',')
7910 break;
7911 }
7912 if (*argp == ')')
7913 ++argp;
7914 else
7915 ret = FAIL;
7916
7917 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007919 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 {
7922 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007923 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007925 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
7928 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
7931 *arg = skipwhite(argp);
7932 return ret;
7933}
7934
7935
7936/*
7937 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007938 * Return OK when the function can't be called, FAIL otherwise.
7939 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 */
7941 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 char_u *name; /* name of the function */
7945 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007948 typval_T *argvars; /* vars for arguments, must have "argcount"
7949 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 linenr_T firstline; /* first line of range */
7951 linenr_T lastline; /* last line of range */
7952 int *doesrange; /* return: function handled range */
7953 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957#define ERROR_UNKNOWN 0
7958#define ERROR_TOOMANY 1
7959#define ERROR_TOOFEW 2
7960#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961#define ERROR_DICT 4
7962#define ERROR_NONE 5
7963#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 int error = ERROR_NONE;
7965 int i;
7966 int llen;
7967 ufunc_T *fp;
7968 int cc;
7969#define FLEN_FIXED 40
7970 char_u fname_buf[FLEN_FIXED + 1];
7971 char_u *fname;
7972
7973 /*
7974 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7975 * Change <SNR>123_name() to K_SNR 123_name().
7976 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7977 */
7978 cc = name[len];
7979 name[len] = NUL;
7980 llen = eval_fname_script(name);
7981 if (llen > 0)
7982 {
7983 fname_buf[0] = K_SPECIAL;
7984 fname_buf[1] = KS_EXTRA;
7985 fname_buf[2] = (int)KE_SNR;
7986 i = 3;
7987 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7988 {
7989 if (current_SID <= 0)
7990 error = ERROR_SCRIPT;
7991 else
7992 {
7993 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7994 i = (int)STRLEN(fname_buf);
7995 }
7996 }
7997 if (i + STRLEN(name + llen) < FLEN_FIXED)
7998 {
7999 STRCPY(fname_buf + i, name + llen);
8000 fname = fname_buf;
8001 }
8002 else
8003 {
8004 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8005 if (fname == NULL)
8006 error = ERROR_OTHER;
8007 else
8008 {
8009 mch_memmove(fname, fname_buf, (size_t)i);
8010 STRCPY(fname + i, name + llen);
8011 }
8012 }
8013 }
8014 else
8015 fname = name;
8016
8017 *doesrange = FALSE;
8018
8019
8020 /* execute the function if no errors detected and executing */
8021 if (evaluate && error == ERROR_NONE)
8022 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008023 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8024 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 error = ERROR_UNKNOWN;
8026
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008027 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {
8029 /*
8030 * User defined function.
8031 */
8032 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008033
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008035 /* Trigger FuncUndefined event, may load the function. */
8036 if (fp == NULL
8037 && apply_autocmds(EVENT_FUNCUNDEFINED,
8038 fname, fname, TRUE, NULL)
8039 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008041 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 fp = find_func(fname);
8043 }
8044#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008045 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008046 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008047 {
8048 /* loaded a package, search for the function again */
8049 fp = find_func(fname);
8050 }
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 if (fp != NULL)
8053 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008054 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008056 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008058 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008060 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008061 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 else
8063 {
8064 /*
8065 * Call the user function.
8066 * Save and restore search patterns, script variables and
8067 * redo buffer.
8068 */
8069 save_search_patterns();
8070 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008071 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008073 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008074 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8075 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8076 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008077 /* Function was unreferenced while being used, free it
8078 * now. */
8079 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 restoreRedobuff();
8081 restore_search_patterns();
8082 error = ERROR_NONE;
8083 }
8084 }
8085 }
8086 else
8087 {
8088 /*
8089 * Find the function name in the table, call its implementation.
8090 */
8091 i = find_internal_func(fname);
8092 if (i >= 0)
8093 {
8094 if (argcount < functions[i].f_min_argc)
8095 error = ERROR_TOOFEW;
8096 else if (argcount > functions[i].f_max_argc)
8097 error = ERROR_TOOMANY;
8098 else
8099 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008100 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 error = ERROR_NONE;
8103 }
8104 }
8105 }
8106 /*
8107 * The function call (or "FuncUndefined" autocommand sequence) might
8108 * have been aborted by an error, an interrupt, or an explicitly thrown
8109 * exception that has not been caught so far. This situation can be
8110 * tested for by calling aborting(). For an error in an internal
8111 * function or for the "E132" error in call_user_func(), however, the
8112 * throw point at which the "force_abort" flag (temporarily reset by
8113 * emsg()) is normally updated has not been reached yet. We need to
8114 * update that flag first to make aborting() reliable.
8115 */
8116 update_force_abort();
8117 }
8118 if (error == ERROR_NONE)
8119 ret = OK;
8120
8121 /*
8122 * Report an error unless the argument evaluation or function call has been
8123 * cancelled due to an aborting error, an interrupt, or an exception.
8124 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008125 if (!aborting())
8126 {
8127 switch (error)
8128 {
8129 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008130 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008131 break;
8132 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008133 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008134 break;
8135 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008136 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008137 name);
8138 break;
8139 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008140 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008141 name);
8142 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008144 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 name);
8146 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008147 }
8148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
8150 name[len] = cc;
8151 if (fname != name && fname != fname_buf)
8152 vim_free(fname);
8153
8154 return ret;
8155}
8156
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157/*
8158 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008159 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008160 */
8161 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008162emsg_funcname(ermsg, name)
8163 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008164 char_u *name;
8165{
8166 char_u *p;
8167
8168 if (*name == K_SPECIAL)
8169 p = concat_str((char_u *)"<SNR>", name + 3);
8170 else
8171 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008172 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008173 if (p != name)
8174 vim_free(p);
8175}
8176
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008177/*
8178 * Return TRUE for a non-zero Number and a non-empty String.
8179 */
8180 static int
8181non_zero_arg(argvars)
8182 typval_T *argvars;
8183{
8184 return ((argvars[0].v_type == VAR_NUMBER
8185 && argvars[0].vval.v_number != 0)
8186 || (argvars[0].v_type == VAR_STRING
8187 && argvars[0].vval.v_string != NULL
8188 && *argvars[0].vval.v_string != NUL));
8189}
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191/*********************************************
8192 * Implementation of the built-in functions
8193 */
8194
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008195#ifdef FEAT_FLOAT
8196/*
8197 * "abs(expr)" function
8198 */
8199 static void
8200f_abs(argvars, rettv)
8201 typval_T *argvars;
8202 typval_T *rettv;
8203{
8204 if (argvars[0].v_type == VAR_FLOAT)
8205 {
8206 rettv->v_type = VAR_FLOAT;
8207 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8208 }
8209 else
8210 {
8211 varnumber_T n;
8212 int error = FALSE;
8213
8214 n = get_tv_number_chk(&argvars[0], &error);
8215 if (error)
8216 rettv->vval.v_number = -1;
8217 else if (n > 0)
8218 rettv->vval.v_number = n;
8219 else
8220 rettv->vval.v_number = -n;
8221 }
8222}
8223#endif
8224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008226 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 */
8228 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008229f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 typval_T *argvars;
8231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232{
Bram Moolenaar33570922005-01-25 22:26:29 +00008233 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008238 if ((l = argvars[0].vval.v_list) != NULL
8239 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8240 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008242 }
8243 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008244 EMSG(_(e_listreq));
8245}
8246
8247/*
8248 * "append(lnum, string/list)" function
8249 */
8250 static void
8251f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008252 typval_T *argvars;
8253 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008254{
8255 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008256 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 list_T *l = NULL;
8258 listitem_T *li = NULL;
8259 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008260 long added = 0;
8261
Bram Moolenaar0d660222005-01-07 21:51:51 +00008262 lnum = get_tv_lnum(argvars);
8263 if (lnum >= 0
8264 && lnum <= curbuf->b_ml.ml_line_count
8265 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008267 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008269 l = argvars[1].vval.v_list;
8270 if (l == NULL)
8271 return;
8272 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008274 for (;;)
8275 {
8276 if (l == NULL)
8277 tv = &argvars[1]; /* append a string */
8278 else if (li == NULL)
8279 break; /* end of list */
8280 else
8281 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008282 line = get_tv_string_chk(tv);
8283 if (line == NULL) /* type error */
8284 {
8285 rettv->vval.v_number = 1; /* Failed */
8286 break;
8287 }
8288 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008289 ++added;
8290 if (l == NULL)
8291 break;
8292 li = li->li_next;
8293 }
8294
8295 appended_lines_mark(lnum, added);
8296 if (curwin->w_cursor.lnum > lnum)
8297 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008299 else
8300 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
8302
8303/*
8304 * "argc()" function
8305 */
8306/* ARGSUSED */
8307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 typval_T *argvars;
8310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
8315/*
8316 * "argidx()" function
8317 */
8318/* ARGSUSED */
8319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 typval_T *argvars;
8322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008324 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325}
8326
8327/*
8328 * "argv(nr)" function
8329 */
8330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 typval_T *argvars;
8333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 int idx;
8336
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008337 if (argvars[0].v_type != VAR_UNKNOWN)
8338 {
8339 idx = get_tv_number_chk(&argvars[0], NULL);
8340 if (idx >= 0 && idx < ARGCOUNT)
8341 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8342 else
8343 rettv->vval.v_string = NULL;
8344 rettv->v_type = VAR_STRING;
8345 }
8346 else if (rettv_list_alloc(rettv) == OK)
8347 for (idx = 0; idx < ARGCOUNT; ++idx)
8348 list_append_string(rettv->vval.v_list,
8349 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350}
8351
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008352#ifdef FEAT_FLOAT
8353static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8354
8355/*
8356 * Get the float value of "argvars[0]" into "f".
8357 * Returns FAIL when the argument is not a Number or Float.
8358 */
8359 static int
8360get_float_arg(argvars, f)
8361 typval_T *argvars;
8362 float_T *f;
8363{
8364 if (argvars[0].v_type == VAR_FLOAT)
8365 {
8366 *f = argvars[0].vval.v_float;
8367 return OK;
8368 }
8369 if (argvars[0].v_type == VAR_NUMBER)
8370 {
8371 *f = (float_T)argvars[0].vval.v_number;
8372 return OK;
8373 }
8374 EMSG(_("E808: Number or Float required"));
8375 return FAIL;
8376}
8377
8378/*
8379 * "atan()" function
8380 */
8381 static void
8382f_atan(argvars, rettv)
8383 typval_T *argvars;
8384 typval_T *rettv;
8385{
8386 float_T f;
8387
8388 rettv->v_type = VAR_FLOAT;
8389 if (get_float_arg(argvars, &f) == OK)
8390 rettv->vval.v_float = atan(f);
8391 else
8392 rettv->vval.v_float = 0.0;
8393}
8394#endif
8395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396/*
8397 * "browse(save, title, initdir, default)" function
8398 */
8399/* ARGSUSED */
8400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *argvars;
8403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405#ifdef FEAT_BROWSE
8406 int save;
8407 char_u *title;
8408 char_u *initdir;
8409 char_u *defname;
8410 char_u buf[NUMBUFLEN];
8411 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 save = get_tv_number_chk(&argvars[0], &error);
8415 title = get_tv_string_chk(&argvars[1]);
8416 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8417 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008419 if (error || title == NULL || initdir == NULL || defname == NULL)
8420 rettv->vval.v_string = NULL;
8421 else
8422 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008423 do_browse(save ? BROWSE_SAVE : 0,
8424 title, defname, NULL, initdir, NULL, curbuf);
8425#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008429}
8430
8431/*
8432 * "browsedir(title, initdir)" function
8433 */
8434/* ARGSUSED */
8435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008436f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 typval_T *argvars;
8438 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008439{
8440#ifdef FEAT_BROWSE
8441 char_u *title;
8442 char_u *initdir;
8443 char_u buf[NUMBUFLEN];
8444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445 title = get_tv_string_chk(&argvars[0]);
8446 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 if (title == NULL || initdir == NULL)
8449 rettv->vval.v_string = NULL;
8450 else
8451 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008452 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
Bram Moolenaar33570922005-01-25 22:26:29 +00008459static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461/*
8462 * Find a buffer by number or exact name.
8463 */
8464 static buf_T *
8465find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008470 if (avar->v_type == VAR_NUMBER)
8471 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008472 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008474 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008475 if (buf == NULL)
8476 {
8477 /* No full path name match, try a match with a URL or a "nofile"
8478 * buffer, these don't use the full path. */
8479 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8480 if (buf->b_fname != NULL
8481 && (path_with_url(buf->b_fname)
8482#ifdef FEAT_QUICKFIX
8483 || bt_nofile(buf)
8484#endif
8485 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008487 break;
8488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490 return buf;
8491}
8492
8493/*
8494 * "bufexists(expr)" function
8495 */
8496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 typval_T *argvars;
8499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502}
8503
8504/*
8505 * "buflisted(expr)" function
8506 */
8507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008509 typval_T *argvars;
8510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 buf_T *buf;
8513
8514 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516}
8517
8518/*
8519 * "bufloaded(expr)" function
8520 */
8521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 typval_T *argvars;
8524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
8526 buf_T *buf;
8527
8528 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530}
8531
Bram Moolenaar33570922005-01-25 22:26:29 +00008532static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008533
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534/*
8535 * Get buffer by number or pattern.
8536 */
8537 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008538get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008539 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 int save_magic;
8543 char_u *save_cpo;
8544 buf_T *buf;
8545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 if (tv->v_type == VAR_NUMBER)
8547 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008548 if (tv->v_type != VAR_STRING)
8549 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (name == NULL || *name == NUL)
8551 return curbuf;
8552 if (name[0] == '$' && name[1] == NUL)
8553 return lastbuf;
8554
8555 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8556 save_magic = p_magic;
8557 p_magic = TRUE;
8558 save_cpo = p_cpo;
8559 p_cpo = (char_u *)"";
8560
8561 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8562 TRUE, FALSE));
8563
8564 p_magic = save_magic;
8565 p_cpo = save_cpo;
8566
8567 /* If not found, try expanding the name, like done for bufexists(). */
8568 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
8571 return buf;
8572}
8573
8574/*
8575 * "bufname(expr)" function
8576 */
8577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 typval_T *argvars;
8580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 buf_T *buf;
8583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008584 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 buf = get_buf_tv(&argvars[0]);
8587 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 --emsg_off;
8593}
8594
8595/*
8596 * "bufnr(expr)" function
8597 */
8598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008604 int error = FALSE;
8605 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008607 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008610 --emsg_off;
8611
8612 /* If the buffer isn't found and the second argument is not zero create a
8613 * new buffer. */
8614 if (buf == NULL
8615 && argvars[1].v_type != VAR_UNKNOWN
8616 && get_tv_number_chk(&argvars[1], &error) != 0
8617 && !error
8618 && (name = get_tv_string_chk(&argvars[0])) != NULL
8619 && !error)
8620 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626}
8627
8628/*
8629 * "bufwinnr(nr)" function
8630 */
8631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 typval_T *argvars;
8634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636#ifdef FEAT_WINDOWS
8637 win_T *wp;
8638 int winnr = 0;
8639#endif
8640 buf_T *buf;
8641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008642 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#ifdef FEAT_WINDOWS
8646 for (wp = firstwin; wp; wp = wp->w_next)
8647 {
8648 ++winnr;
8649 if (wp->w_buffer == buf)
8650 break;
8651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655#endif
8656 --emsg_off;
8657}
8658
8659/*
8660 * "byte2line(byte)" function
8661 */
8662/*ARGSUSED*/
8663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
8668#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670#else
8671 long boff = 0;
8672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 (linenr_T)0, &boff);
8679#endif
8680}
8681
8682/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008683 * "byteidx()" function
8684 */
8685/*ARGSUSED*/
8686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008688 typval_T *argvars;
8689 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008690{
8691#ifdef FEAT_MBYTE
8692 char_u *t;
8693#endif
8694 char_u *str;
8695 long idx;
8696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008697 str = get_tv_string_chk(&argvars[0]);
8698 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008701 return;
8702
8703#ifdef FEAT_MBYTE
8704 t = str;
8705 for ( ; idx > 0; idx--)
8706 {
8707 if (*t == NUL) /* EOL reached */
8708 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008709 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008710 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008711 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008712#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008713 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008714 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008715#endif
8716}
8717
8718/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008719 * "call(func, arglist)" function
8720 */
8721 static void
8722f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008725{
8726 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008727 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008728 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008730 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008732
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008733 if (argvars[1].v_type != VAR_LIST)
8734 {
8735 EMSG(_(e_listreq));
8736 return;
8737 }
8738 if (argvars[1].vval.v_list == NULL)
8739 return;
8740
8741 if (argvars[0].v_type == VAR_FUNC)
8742 func = argvars[0].vval.v_string;
8743 else
8744 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 if (*func == NUL)
8746 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008747
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 if (argvars[2].v_type != VAR_UNKNOWN)
8749 {
8750 if (argvars[2].v_type != VAR_DICT)
8751 {
8752 EMSG(_(e_dictreq));
8753 return;
8754 }
8755 selfdict = argvars[2].vval.v_dict;
8756 }
8757
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008758 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8759 item = item->li_next)
8760 {
8761 if (argc == MAX_FUNC_ARGS)
8762 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008763 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008764 break;
8765 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008766 /* Make a copy of each argument. This is needed to be able to set
8767 * v_lock to VAR_FIXED in the copy without changing the original list.
8768 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008769 copy_tv(&item->li_tv, &argv[argc++]);
8770 }
8771
8772 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008773 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008774 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8775 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008776
8777 /* Free the arguments. */
8778 while (argc > 0)
8779 clear_tv(&argv[--argc]);
8780}
8781
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008782#ifdef FEAT_FLOAT
8783/*
8784 * "ceil({float})" function
8785 */
8786 static void
8787f_ceil(argvars, rettv)
8788 typval_T *argvars;
8789 typval_T *rettv;
8790{
8791 float_T f;
8792
8793 rettv->v_type = VAR_FLOAT;
8794 if (get_float_arg(argvars, &f) == OK)
8795 rettv->vval.v_float = ceil(f);
8796 else
8797 rettv->vval.v_float = 0.0;
8798}
8799#endif
8800
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008801/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008802 * "changenr()" function
8803 */
8804/*ARGSUSED*/
8805 static void
8806f_changenr(argvars, rettv)
8807 typval_T *argvars;
8808 typval_T *rettv;
8809{
8810 rettv->vval.v_number = curbuf->b_u_seq_cur;
8811}
8812
8813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 * "char2nr(string)" function
8815 */
8816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 typval_T *argvars;
8819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821#ifdef FEAT_MBYTE
8822 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 else
8825#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
8829/*
8830 * "cindent(lnum)" function
8831 */
8832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 typval_T *argvars;
8835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836{
8837#ifdef FEAT_CINDENT
8838 pos_T pos;
8839 linenr_T lnum;
8840
8841 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8844 {
8845 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 curwin->w_cursor = pos;
8848 }
8849 else
8850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852}
8853
8854/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008855 * "clearmatches()" function
8856 */
8857/*ARGSUSED*/
8858 static void
8859f_clearmatches(argvars, rettv)
8860 typval_T *argvars;
8861 typval_T *rettv;
8862{
8863#ifdef FEAT_SEARCH_EXTRA
8864 clear_matches(curwin);
8865#endif
8866}
8867
8868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 * "col(string)" function
8870 */
8871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 typval_T *argvars;
8874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
8876 colnr_T col = 0;
8877 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008878 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008880 fp = var2fpos(&argvars[0], FALSE, &fnum);
8881 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 {
8883 if (fp->col == MAXCOL)
8884 {
8885 /* '> can be MAXCOL, get the length of the line then */
8886 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008887 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 else
8889 col = MAXCOL;
8890 }
8891 else
8892 {
8893 col = fp->col + 1;
8894#ifdef FEAT_VIRTUALEDIT
8895 /* col(".") when the cursor is on the NUL at the end of the line
8896 * because of "coladd" can be seen as an extra column. */
8897 if (virtual_active() && fp == &curwin->w_cursor)
8898 {
8899 char_u *p = ml_get_cursor();
8900
8901 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8902 curwin->w_virtcol - curwin->w_cursor.coladd))
8903 {
8904# ifdef FEAT_MBYTE
8905 int l;
8906
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008907 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 col += l;
8909# else
8910 if (*p != NUL && p[1] == NUL)
8911 ++col;
8912# endif
8913 }
8914 }
8915#endif
8916 }
8917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920
Bram Moolenaar572cb562005-08-05 21:35:02 +00008921#if defined(FEAT_INS_EXPAND)
8922/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008923 * "complete()" function
8924 */
8925/*ARGSUSED*/
8926 static void
8927f_complete(argvars, rettv)
8928 typval_T *argvars;
8929 typval_T *rettv;
8930{
8931 int startcol;
8932
8933 if ((State & INSERT) == 0)
8934 {
8935 EMSG(_("E785: complete() can only be used in Insert mode"));
8936 return;
8937 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008938
8939 /* Check for undo allowed here, because if something was already inserted
8940 * the line was already saved for undo and this check isn't done. */
8941 if (!undo_allowed())
8942 return;
8943
Bram Moolenaarade00832006-03-10 21:46:58 +00008944 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8945 {
8946 EMSG(_(e_invarg));
8947 return;
8948 }
8949
8950 startcol = get_tv_number_chk(&argvars[0], NULL);
8951 if (startcol <= 0)
8952 return;
8953
8954 set_completion(startcol - 1, argvars[1].vval.v_list);
8955}
8956
8957/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008958 * "complete_add()" function
8959 */
8960/*ARGSUSED*/
8961 static void
8962f_complete_add(argvars, rettv)
8963 typval_T *argvars;
8964 typval_T *rettv;
8965{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008966 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008967}
8968
8969/*
8970 * "complete_check()" function
8971 */
8972/*ARGSUSED*/
8973 static void
8974f_complete_check(argvars, rettv)
8975 typval_T *argvars;
8976 typval_T *rettv;
8977{
8978 int saved = RedrawingDisabled;
8979
8980 RedrawingDisabled = 0;
8981 ins_compl_check_keys(0);
8982 rettv->vval.v_number = compl_interrupted;
8983 RedrawingDisabled = saved;
8984}
8985#endif
8986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987/*
8988 * "confirm(message, buttons[, default [, type]])" function
8989 */
8990/*ARGSUSED*/
8991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008993 typval_T *argvars;
8994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8997 char_u *message;
8998 char_u *buttons = NULL;
8999 char_u buf[NUMBUFLEN];
9000 char_u buf2[NUMBUFLEN];
9001 int def = 1;
9002 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009003 char_u *typestr;
9004 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 message = get_tv_string_chk(&argvars[0]);
9007 if (message == NULL)
9008 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009009 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9012 if (buttons == NULL)
9013 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009017 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9020 if (typestr == NULL)
9021 error = TRUE;
9022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 switch (TOUPPER_ASC(*typestr))
9025 {
9026 case 'E': type = VIM_ERROR; break;
9027 case 'Q': type = VIM_QUESTION; break;
9028 case 'I': type = VIM_INFO; break;
9029 case 'W': type = VIM_WARNING; break;
9030 case 'G': type = VIM_GENERIC; break;
9031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 }
9033 }
9034 }
9035 }
9036
9037 if (buttons == NULL || *buttons == NUL)
9038 buttons = (char_u *)_("&Ok");
9039
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009040 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043#endif
9044}
9045
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009046/*
9047 * "copy()" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009053{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009054 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009055}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009057#ifdef FEAT_FLOAT
9058/*
9059 * "cos()" function
9060 */
9061 static void
9062f_cos(argvars, rettv)
9063 typval_T *argvars;
9064 typval_T *rettv;
9065{
9066 float_T f;
9067
9068 rettv->v_type = VAR_FLOAT;
9069 if (get_float_arg(argvars, &f) == OK)
9070 rettv->vval.v_float = cos(f);
9071 else
9072 rettv->vval.v_float = 0.0;
9073}
9074#endif
9075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009077 * "count()" function
9078 */
9079 static void
9080f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 typval_T *argvars;
9082 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009083{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009084 long n = 0;
9085 int ic = FALSE;
9086
Bram Moolenaare9a41262005-01-15 22:18:47 +00009087 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009088 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 listitem_T *li;
9090 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009091 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009092
Bram Moolenaare9a41262005-01-15 22:18:47 +00009093 if ((l = argvars[0].vval.v_list) != NULL)
9094 {
9095 li = l->lv_first;
9096 if (argvars[2].v_type != VAR_UNKNOWN)
9097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 int error = FALSE;
9099
9100 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009101 if (argvars[3].v_type != VAR_UNKNOWN)
9102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009103 idx = get_tv_number_chk(&argvars[3], &error);
9104 if (!error)
9105 {
9106 li = list_find(l, idx);
9107 if (li == NULL)
9108 EMSGN(_(e_listidx), idx);
9109 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009110 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009111 if (error)
9112 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009113 }
9114
9115 for ( ; li != NULL; li = li->li_next)
9116 if (tv_equal(&li->li_tv, &argvars[1], ic))
9117 ++n;
9118 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009120 else if (argvars[0].v_type == VAR_DICT)
9121 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 int todo;
9123 dict_T *d;
9124 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009126 if ((d = argvars[0].vval.v_dict) != NULL)
9127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 int error = FALSE;
9129
Bram Moolenaare9a41262005-01-15 22:18:47 +00009130 if (argvars[2].v_type != VAR_UNKNOWN)
9131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 if (argvars[3].v_type != VAR_UNKNOWN)
9134 EMSG(_(e_invarg));
9135 }
9136
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009137 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009138 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009139 {
9140 if (!HASHITEM_EMPTY(hi))
9141 {
9142 --todo;
9143 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9144 ++n;
9145 }
9146 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 }
9148 }
9149 else
9150 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009151 rettv->vval.v_number = n;
9152}
9153
9154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9156 *
9157 * Checks the existence of a cscope connection.
9158 */
9159/*ARGSUSED*/
9160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 typval_T *argvars;
9163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165#ifdef FEAT_CSCOPE
9166 int num = 0;
9167 char_u *dbpath = NULL;
9168 char_u *prepend = NULL;
9169 char_u buf[NUMBUFLEN];
9170
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009171 if (argvars[0].v_type != VAR_UNKNOWN
9172 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 num = (int)get_tv_number(&argvars[0]);
9175 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
9179
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181#endif
9182}
9183
9184/*
9185 * "cursor(lnum, col)" function
9186 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009187 * Moves the cursor to the specified line and column.
9188 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 */
9190/*ARGSUSED*/
9191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009193 typval_T *argvars;
9194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009197#ifdef FEAT_VIRTUALEDIT
9198 long coladd = 0;
9199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009201 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009202 if (argvars[1].v_type == VAR_UNKNOWN)
9203 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009204 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009205
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009206 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009207 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009208 line = pos.lnum;
9209 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009210#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009211 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009212#endif
9213 }
9214 else
9215 {
9216 line = get_tv_lnum(argvars);
9217 col = get_tv_number_chk(&argvars[1], NULL);
9218#ifdef FEAT_VIRTUALEDIT
9219 if (argvars[2].v_type != VAR_UNKNOWN)
9220 coladd = get_tv_number_chk(&argvars[2], NULL);
9221#endif
9222 }
9223 if (line < 0 || col < 0
9224#ifdef FEAT_VIRTUALEDIT
9225 || coladd < 0
9226#endif
9227 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 if (line > 0)
9230 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 if (col > 0)
9232 curwin->w_cursor.col = col - 1;
9233#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009234 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235#endif
9236
9237 /* Make sure the cursor is in a valid position. */
9238 check_cursor();
9239#ifdef FEAT_MBYTE
9240 /* Correct cursor for multi-byte character. */
9241 if (has_mbyte)
9242 mb_adjust_cursor();
9243#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009244
9245 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009246 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247}
9248
9249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 */
9252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 typval_T *argvars;
9255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009257 int noref = 0;
9258
9259 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009261 if (noref < 0 || noref > 1)
9262 EMSG(_(e_invarg));
9263 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009264 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265}
9266
9267/*
9268 * "delete()" function
9269 */
9270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009271f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *argvars;
9273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279}
9280
9281/*
9282 * "did_filetype()" function
9283 */
9284/*ARGSUSED*/
9285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009287 typval_T *argvars;
9288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
9290#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292#endif
9293}
9294
9295/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009296 * "diff_filler()" function
9297 */
9298/*ARGSUSED*/
9299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009301 typval_T *argvars;
9302 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009303{
9304#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009306#endif
9307}
9308
9309/*
9310 * "diff_hlID()" function
9311 */
9312/*ARGSUSED*/
9313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T *argvars;
9316 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009317{
9318#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009320 static linenr_T prev_lnum = 0;
9321 static int changedtick = 0;
9322 static int fnum = 0;
9323 static int change_start = 0;
9324 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009325 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009326 int filler_lines;
9327 int col;
9328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 if (lnum < 0) /* ignore type error in {lnum} arg */
9330 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009331 if (lnum != prev_lnum
9332 || changedtick != curbuf->b_changedtick
9333 || fnum != curbuf->b_fnum)
9334 {
9335 /* New line, buffer, change: need to get the values. */
9336 filler_lines = diff_check(curwin, lnum);
9337 if (filler_lines < 0)
9338 {
9339 if (filler_lines == -1)
9340 {
9341 change_start = MAXCOL;
9342 change_end = -1;
9343 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9344 hlID = HLF_ADD; /* added line */
9345 else
9346 hlID = HLF_CHD; /* changed line */
9347 }
9348 else
9349 hlID = HLF_ADD; /* added line */
9350 }
9351 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009352 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009353 prev_lnum = lnum;
9354 changedtick = curbuf->b_changedtick;
9355 fnum = curbuf->b_fnum;
9356 }
9357
9358 if (hlID == HLF_CHD || hlID == HLF_TXD)
9359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009361 if (col >= change_start && col <= change_end)
9362 hlID = HLF_TXD; /* changed text */
9363 else
9364 hlID = HLF_CHD; /* changed line */
9365 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009366 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009367#endif
9368}
9369
9370/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009371 * "empty({expr})" function
9372 */
9373 static void
9374f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009375 typval_T *argvars;
9376 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009377{
9378 int n;
9379
9380 switch (argvars[0].v_type)
9381 {
9382 case VAR_STRING:
9383 case VAR_FUNC:
9384 n = argvars[0].vval.v_string == NULL
9385 || *argvars[0].vval.v_string == NUL;
9386 break;
9387 case VAR_NUMBER:
9388 n = argvars[0].vval.v_number == 0;
9389 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009390#ifdef FEAT_FLOAT
9391 case VAR_FLOAT:
9392 n = argvars[0].vval.v_float == 0.0;
9393 break;
9394#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009395 case VAR_LIST:
9396 n = argvars[0].vval.v_list == NULL
9397 || argvars[0].vval.v_list->lv_first == NULL;
9398 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009399 case VAR_DICT:
9400 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009401 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009403 default:
9404 EMSG2(_(e_intern2), "f_empty()");
9405 n = 0;
9406 }
9407
9408 rettv->vval.v_number = n;
9409}
9410
9411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 * "escape({string}, {chars})" function
9413 */
9414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419 char_u buf[NUMBUFLEN];
9420
Bram Moolenaar758711c2005-02-02 23:11:38 +00009421 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9422 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425
9426/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009427 * "eval()" function
9428 */
9429/*ARGSUSED*/
9430 static void
9431f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 typval_T *argvars;
9433 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009434{
9435 char_u *s;
9436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 s = get_tv_string_chk(&argvars[0]);
9438 if (s != NULL)
9439 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9442 {
9443 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009444 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009446 else if (*s != NUL)
9447 EMSG(_(e_trailing));
9448}
9449
9450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 * "eventhandler()" function
9452 */
9453/*ARGSUSED*/
9454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009456 typval_T *argvars;
9457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462/*
9463 * "executable()" function
9464 */
9465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471}
9472
9473/*
9474 * "exists()" function
9475 */
9476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009478 typval_T *argvars;
9479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
9481 char_u *p;
9482 char_u *name;
9483 int n = FALSE;
9484 int len = 0;
9485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 if (*p == '$') /* environment variable */
9488 {
9489 /* first try "normal" environment variables (fast) */
9490 if (mch_getenv(p + 1) != NULL)
9491 n = TRUE;
9492 else
9493 {
9494 /* try expanding things like $VIM and ${HOME} */
9495 p = expand_env_save(p);
9496 if (p != NULL && *p != '$')
9497 n = TRUE;
9498 vim_free(p);
9499 }
9500 }
9501 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009504 if (*skipwhite(p) != NUL)
9505 n = FALSE; /* trailing garbage */
9506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 else if (*p == '*') /* internal or user defined function */
9508 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009509 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 }
9511 else if (*p == ':')
9512 {
9513 n = cmd_exists(p + 1);
9514 }
9515 else if (*p == '#')
9516 {
9517#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009518 if (p[1] == '#')
9519 n = autocmd_supported(p + 2);
9520 else
9521 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522#endif
9523 }
9524 else /* internal variable */
9525 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009526 char_u *tofree;
9527 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009529 /* get_name_len() takes care of expanding curly braces */
9530 name = p;
9531 len = get_name_len(&p, &tofree, TRUE, FALSE);
9532 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009534 if (tofree != NULL)
9535 name = tofree;
9536 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9537 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009539 /* handle d.key, l[idx], f(expr) */
9540 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9541 if (n)
9542 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 }
9544 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009545 if (*p != NUL)
9546 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009548 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 }
9550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552}
9553
9554/*
9555 * "expand()" function
9556 */
9557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009559 typval_T *argvars;
9560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 char_u *s;
9563 int len;
9564 char_u *errormsg;
9565 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9566 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->v_type = VAR_STRING;
9570 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 if (*s == '%' || *s == '#' || *s == '<')
9572 {
9573 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009574 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 --emsg_off;
9576 }
9577 else
9578 {
9579 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009580 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 if (argvars[1].v_type != VAR_UNKNOWN
9582 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 if (!error)
9585 {
9586 ExpandInit(&xpc);
9587 xpc.xp_context = EXPAND_FILES;
9588 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 }
9590 else
9591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 }
9593}
9594
9595/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009597 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009598 */
9599 static void
9600f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009601 typval_T *argvars;
9602 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009603{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009605 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 list_T *l1, *l2;
9607 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009610
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 l1 = argvars[0].vval.v_list;
9612 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009613 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9614 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 {
9616 if (argvars[2].v_type != VAR_UNKNOWN)
9617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 before = get_tv_number_chk(&argvars[2], &error);
9619 if (error)
9620 return; /* type error; errmsg already given */
9621
Bram Moolenaar758711c2005-02-02 23:11:38 +00009622 if (before == l1->lv_len)
9623 item = NULL;
9624 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009626 item = list_find(l1, before);
9627 if (item == NULL)
9628 {
9629 EMSGN(_(e_listidx), before);
9630 return;
9631 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 }
9633 }
9634 else
9635 item = NULL;
9636 list_extend(l1, l2, item);
9637
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 copy_tv(&argvars[0], rettv);
9639 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 dict_T *d1, *d2;
9644 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 char_u *action;
9646 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009647 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009648 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649
9650 d1 = argvars[0].vval.v_dict;
9651 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009652 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9653 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 {
9655 /* Check the third argument. */
9656 if (argvars[2].v_type != VAR_UNKNOWN)
9657 {
9658 static char *(av[]) = {"keep", "force", "error"};
9659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 action = get_tv_string_chk(&argvars[2]);
9661 if (action == NULL)
9662 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663 for (i = 0; i < 3; ++i)
9664 if (STRCMP(action, av[i]) == 0)
9665 break;
9666 if (i == 3)
9667 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009668 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009669 return;
9670 }
9671 }
9672 else
9673 action = (char_u *)"force";
9674
9675 /* Go over all entries in the second dict and add them to the
9676 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009677 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009678 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009680 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009682 --todo;
9683 di1 = dict_find(d1, hi2->hi_key, -1);
9684 if (di1 == NULL)
9685 {
9686 di1 = dictitem_copy(HI2DI(hi2));
9687 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9688 dictitem_free(di1);
9689 }
9690 else if (*action == 'e')
9691 {
9692 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9693 break;
9694 }
9695 else if (*action == 'f')
9696 {
9697 clear_tv(&di1->di_tv);
9698 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 }
9701 }
9702
Bram Moolenaare9a41262005-01-15 22:18:47 +00009703 copy_tv(&argvars[0], rettv);
9704 }
9705 }
9706 else
9707 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009708}
9709
9710/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009711 * "feedkeys()" function
9712 */
9713/*ARGSUSED*/
9714 static void
9715f_feedkeys(argvars, rettv)
9716 typval_T *argvars;
9717 typval_T *rettv;
9718{
9719 int remap = TRUE;
9720 char_u *keys, *flags;
9721 char_u nbuf[NUMBUFLEN];
9722 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009723 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009724
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009725 /* This is not allowed in the sandbox. If the commands would still be
9726 * executed in the sandbox it would be OK, but it probably happens later,
9727 * when "sandbox" is no longer set. */
9728 if (check_secure())
9729 return;
9730
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009731 keys = get_tv_string(&argvars[0]);
9732 if (*keys != NUL)
9733 {
9734 if (argvars[1].v_type != VAR_UNKNOWN)
9735 {
9736 flags = get_tv_string_buf(&argvars[1], nbuf);
9737 for ( ; *flags != NUL; ++flags)
9738 {
9739 switch (*flags)
9740 {
9741 case 'n': remap = FALSE; break;
9742 case 'm': remap = TRUE; break;
9743 case 't': typed = TRUE; break;
9744 }
9745 }
9746 }
9747
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009748 /* Need to escape K_SPECIAL and CSI before putting the string in the
9749 * typeahead buffer. */
9750 keys_esc = vim_strsave_escape_csi(keys);
9751 if (keys_esc != NULL)
9752 {
9753 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009754 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009755 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009756 if (vgetc_busy)
9757 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009758 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009759 }
9760}
9761
9762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 * "filereadable()" function
9764 */
9765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009767 typval_T *argvars;
9768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009770 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 char_u *p;
9772 int n;
9773
Bram Moolenaarc236c162008-07-13 17:41:49 +00009774#ifndef O_NONBLOCK
9775# define O_NONBLOCK 0
9776#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009778 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9779 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
9781 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009782 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 }
9784 else
9785 n = FALSE;
9786
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009791 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 * rights to write into.
9793 */
9794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 typval_T *argvars;
9797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009799 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800}
9801
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009802static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009803
9804 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009805findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009808 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009809{
9810#ifdef FEAT_SEARCHPATH
9811 char_u *fname;
9812 char_u *fresult = NULL;
9813 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9814 char_u *p;
9815 char_u pathbuf[NUMBUFLEN];
9816 int count = 1;
9817 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009818 int error = FALSE;
9819#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009820
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009821 rettv->vval.v_string = NULL;
9822 rettv->v_type = VAR_STRING;
9823
9824#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009827 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9830 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009831 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 else
9833 {
9834 if (*p != NUL)
9835 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009838 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009840 }
9841
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009842 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9843 error = TRUE;
9844
9845 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 do
9848 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009849 if (rettv->v_type == VAR_STRING)
9850 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 fresult = find_file_in_path_option(first ? fname : NULL,
9852 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009853 0, first, path,
9854 find_what,
9855 curbuf->b_ffname,
9856 find_what == FINDFILE_DIR
9857 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009859
9860 if (fresult != NULL && rettv->v_type == VAR_LIST)
9861 list_append_string(rettv->vval.v_list, fresult, -1);
9862
9863 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009865
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009866 if (rettv->v_type == VAR_STRING)
9867 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009868#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009869}
9870
Bram Moolenaar33570922005-01-25 22:26:29 +00009871static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9872static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009873
9874/*
9875 * Implementation of map() and filter().
9876 */
9877 static void
9878filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009881 int map;
9882{
9883 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009885 listitem_T *li, *nli;
9886 list_T *l = NULL;
9887 dictitem_T *di;
9888 hashtab_T *ht;
9889 hashitem_T *hi;
9890 dict_T *d = NULL;
9891 typval_T save_val;
9892 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009893 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009894 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009895 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009896 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009897
Bram Moolenaare9a41262005-01-15 22:18:47 +00009898 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009899 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009900 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009901 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 return;
9903 }
9904 else if (argvars[0].v_type == VAR_DICT)
9905 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009906 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009907 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 return;
9909 }
9910 else
9911 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009912 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 return;
9914 }
9915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009916 expr = get_tv_string_buf_chk(&argvars[1], buf);
9917 /* On type errors, the preceding call has already displayed an error
9918 * message. Avoid a misleading error message for an empty string that
9919 * was not passed as argument. */
9920 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 prepare_vimvar(VV_VAL, &save_val);
9923 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009924
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009925 /* We reset "did_emsg" to be able to detect whether an error
9926 * occurred during evaluation of the expression. */
9927 save_did_emsg = did_emsg;
9928 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009932 prepare_vimvar(VV_KEY, &save_key);
9933 vimvars[VV_KEY].vv_type = VAR_STRING;
9934
9935 ht = &d->dv_hashtab;
9936 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009937 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009940 if (!HASHITEM_EMPTY(hi))
9941 {
9942 --todo;
9943 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009944 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 break;
9946 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009947 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009948 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 break;
9950 if (!map && rem)
9951 dictitem_remove(d, di);
9952 clear_tv(&vimvars[VV_KEY].vv_tv);
9953 }
9954 }
9955 hash_unlock(ht);
9956
9957 restore_vimvar(VV_KEY, &save_key);
9958 }
9959 else
9960 {
9961 for (li = l->lv_first; li != NULL; li = nli)
9962 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009963 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009964 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009966 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009967 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009968 break;
9969 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009971 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009972 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009975
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009976 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009978
9979 copy_tv(&argvars[0], rettv);
9980}
9981
9982 static int
9983filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985 char_u *expr;
9986 int map;
9987 int *remp;
9988{
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009991 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009992
Bram Moolenaar33570922005-01-25 22:26:29 +00009993 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994 s = expr;
9995 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009996 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 if (*s != NUL) /* check for trailing chars after expr */
9998 {
9999 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010000 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010001 }
10002 if (map)
10003 {
10004 /* map(): replace the list item value */
10005 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010006 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007 *tv = rettv;
10008 }
10009 else
10010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 int error = FALSE;
10012
Bram Moolenaare9a41262005-01-15 22:18:47 +000010013 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010015 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016 /* On type error, nothing has been removed; return FAIL to stop the
10017 * loop. The error message was given by get_tv_number_chk(). */
10018 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010019 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010020 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010021 retval = OK;
10022theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010024 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010025}
10026
10027/*
10028 * "filter()" function
10029 */
10030 static void
10031f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010032 typval_T *argvars;
10033 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010034{
10035 filter_map(argvars, rettv, FALSE);
10036}
10037
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010038/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010039 * "finddir({fname}[, {path}[, {count}]])" function
10040 */
10041 static void
10042f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010043 typval_T *argvars;
10044 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010045{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010046 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010047}
10048
10049/*
10050 * "findfile({fname}[, {path}[, {count}]])" function
10051 */
10052 static void
10053f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010054 typval_T *argvars;
10055 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010056{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010057 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010058}
10059
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010060#ifdef FEAT_FLOAT
10061/*
10062 * "float2nr({float})" function
10063 */
10064 static void
10065f_float2nr(argvars, rettv)
10066 typval_T *argvars;
10067 typval_T *rettv;
10068{
10069 float_T f;
10070
10071 if (get_float_arg(argvars, &f) == OK)
10072 {
10073 if (f < -0x7fffffff)
10074 rettv->vval.v_number = -0x7fffffff;
10075 else if (f > 0x7fffffff)
10076 rettv->vval.v_number = 0x7fffffff;
10077 else
10078 rettv->vval.v_number = (varnumber_T)f;
10079 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010080}
10081
10082/*
10083 * "floor({float})" function
10084 */
10085 static void
10086f_floor(argvars, rettv)
10087 typval_T *argvars;
10088 typval_T *rettv;
10089{
10090 float_T f;
10091
10092 rettv->v_type = VAR_FLOAT;
10093 if (get_float_arg(argvars, &f) == OK)
10094 rettv->vval.v_float = floor(f);
10095 else
10096 rettv->vval.v_float = 0.0;
10097}
10098#endif
10099
Bram Moolenaar0d660222005-01-07 21:51:51 +000010100/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010101 * "fnameescape({string})" function
10102 */
10103 static void
10104f_fnameescape(argvars, rettv)
10105 typval_T *argvars;
10106 typval_T *rettv;
10107{
10108 rettv->vval.v_string = vim_strsave_fnameescape(
10109 get_tv_string(&argvars[0]), FALSE);
10110 rettv->v_type = VAR_STRING;
10111}
10112
10113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 * "fnamemodify({fname}, {mods})" function
10115 */
10116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010118 typval_T *argvars;
10119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121 char_u *fname;
10122 char_u *mods;
10123 int usedlen = 0;
10124 int len;
10125 char_u *fbuf = NULL;
10126 char_u buf[NUMBUFLEN];
10127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 fname = get_tv_string_chk(&argvars[0]);
10129 mods = get_tv_string_buf_chk(&argvars[1], buf);
10130 if (fname == NULL || mods == NULL)
10131 fname = NULL;
10132 else
10133 {
10134 len = (int)STRLEN(fname);
10135 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010138 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 vim_free(fbuf);
10144}
10145
Bram Moolenaar33570922005-01-25 22:26:29 +000010146static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147
10148/*
10149 * "foldclosed()" function
10150 */
10151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 int end;
10156{
10157#ifdef FEAT_FOLDING
10158 linenr_T lnum;
10159 linenr_T first, last;
10160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10163 {
10164 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10165 {
10166 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 return;
10171 }
10172 }
10173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175}
10176
10177/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010178 * "foldclosed()" function
10179 */
10180 static void
10181f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 typval_T *argvars;
10183 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010184{
10185 foldclosed_both(argvars, rettv, FALSE);
10186}
10187
10188/*
10189 * "foldclosedend()" function
10190 */
10191 static void
10192f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 typval_T *argvars;
10194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010195{
10196 foldclosed_both(argvars, rettv, TRUE);
10197}
10198
10199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 * "foldlevel()" function
10201 */
10202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010203f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010204 typval_T *argvars;
10205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206{
10207#ifdef FEAT_FOLDING
10208 linenr_T lnum;
10209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214}
10215
10216/*
10217 * "foldtext()" function
10218 */
10219/*ARGSUSED*/
10220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 typval_T *argvars;
10223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225#ifdef FEAT_FOLDING
10226 linenr_T lnum;
10227 char_u *s;
10228 char_u *r;
10229 int len;
10230 char *txt;
10231#endif
10232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 rettv->v_type = VAR_STRING;
10234 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10237 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10238 <= curbuf->b_ml.ml_line_count
10239 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 {
10241 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010242 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10243 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 {
10245 if (!linewhite(lnum))
10246 break;
10247 ++lnum;
10248 }
10249
10250 /* Find interesting text in this line. */
10251 s = skipwhite(ml_get(lnum));
10252 /* skip C comment-start */
10253 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010256 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010258 {
10259 s = skipwhite(ml_get(lnum + 1));
10260 if (*s == '*')
10261 s = skipwhite(s + 1);
10262 }
10263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 txt = _("+-%s%3ld lines: ");
10265 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 + 20 /* for %3ld */
10268 + STRLEN(s))); /* concatenated */
10269 if (r != NULL)
10270 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10272 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10273 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 len = (int)STRLEN(r);
10275 STRCAT(r, s);
10276 /* remove 'foldmarker' and 'commentstring' */
10277 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 }
10280 }
10281#endif
10282}
10283
10284/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010285 * "foldtextresult(lnum)" function
10286 */
10287/*ARGSUSED*/
10288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010290 typval_T *argvars;
10291 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010292{
10293#ifdef FEAT_FOLDING
10294 linenr_T lnum;
10295 char_u *text;
10296 char_u buf[51];
10297 foldinfo_T foldinfo;
10298 int fold_count;
10299#endif
10300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301 rettv->v_type = VAR_STRING;
10302 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010303#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 /* treat illegal types and illegal string values for {lnum} the same */
10306 if (lnum < 0)
10307 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010308 fold_count = foldedCount(curwin, lnum, &foldinfo);
10309 if (fold_count > 0)
10310 {
10311 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10312 &foldinfo, buf);
10313 if (text == buf)
10314 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010316 }
10317#endif
10318}
10319
10320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 * "foreground()" function
10322 */
10323/*ARGSUSED*/
10324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010326 typval_T *argvars;
10327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329#ifdef FEAT_GUI
10330 if (gui.in_use)
10331 gui_mch_set_foreground();
10332#else
10333# ifdef WIN32
10334 win32_set_foreground();
10335# endif
10336#endif
10337}
10338
10339/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 * "function()" function
10341 */
10342/*ARGSUSED*/
10343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010345 typval_T *argvars;
10346 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010347{
10348 char_u *s;
10349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010351 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010352 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010353 /* Don't check an autoload name for existence here. */
10354 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010355 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010356 else
10357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 rettv->vval.v_string = vim_strsave(s);
10359 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010360 }
10361}
10362
10363/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010364 * "garbagecollect()" function
10365 */
10366/*ARGSUSED*/
10367 static void
10368f_garbagecollect(argvars, rettv)
10369 typval_T *argvars;
10370 typval_T *rettv;
10371{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010372 /* This is postponed until we are back at the toplevel, because we may be
10373 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10374 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010375
10376 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10377 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010378}
10379
10380/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010381 * "get()" function
10382 */
10383 static void
10384f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 typval_T *argvars;
10386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010387{
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 listitem_T *li;
10389 list_T *l;
10390 dictitem_T *di;
10391 dict_T *d;
10392 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010393
Bram Moolenaare9a41262005-01-15 22:18:47 +000010394 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010395 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010396 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398 int error = FALSE;
10399
10400 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10401 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010402 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010403 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 else if (argvars[0].v_type == VAR_DICT)
10406 {
10407 if ((d = argvars[0].vval.v_dict) != NULL)
10408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010409 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010410 if (di != NULL)
10411 tv = &di->di_tv;
10412 }
10413 }
10414 else
10415 EMSG2(_(e_listdictarg), "get()");
10416
10417 if (tv == NULL)
10418 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010419 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 copy_tv(&argvars[2], rettv);
10421 }
10422 else
10423 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010424}
10425
Bram Moolenaar342337a2005-07-21 21:11:17 +000010426static 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 +000010427
10428/*
10429 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010430 * Return a range (from start to end) of lines in rettv from the specified
10431 * buffer.
10432 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010433 */
10434 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010435get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010436 buf_T *buf;
10437 linenr_T start;
10438 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010439 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010440 typval_T *rettv;
10441{
10442 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010443
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010444 if (retlist && rettv_list_alloc(rettv) == FAIL)
10445 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010446
10447 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10448 return;
10449
10450 if (!retlist)
10451 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010452 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10453 p = ml_get_buf(buf, start, FALSE);
10454 else
10455 p = (char_u *)"";
10456
10457 rettv->v_type = VAR_STRING;
10458 rettv->vval.v_string = vim_strsave(p);
10459 }
10460 else
10461 {
10462 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010463 return;
10464
10465 if (start < 1)
10466 start = 1;
10467 if (end > buf->b_ml.ml_line_count)
10468 end = buf->b_ml.ml_line_count;
10469 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010470 if (list_append_string(rettv->vval.v_list,
10471 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010472 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010473 }
10474}
10475
10476/*
10477 * "getbufline()" function
10478 */
10479 static void
10480f_getbufline(argvars, rettv)
10481 typval_T *argvars;
10482 typval_T *rettv;
10483{
10484 linenr_T lnum;
10485 linenr_T end;
10486 buf_T *buf;
10487
10488 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10489 ++emsg_off;
10490 buf = get_buf_tv(&argvars[0]);
10491 --emsg_off;
10492
Bram Moolenaar661b1822005-07-28 22:36:45 +000010493 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010494 if (argvars[2].v_type == VAR_UNKNOWN)
10495 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010496 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010497 end = get_tv_lnum_buf(&argvars[2], buf);
10498
Bram Moolenaar342337a2005-07-21 21:11:17 +000010499 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010500}
10501
Bram Moolenaar0d660222005-01-07 21:51:51 +000010502/*
10503 * "getbufvar()" function
10504 */
10505 static void
10506f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010509{
10510 buf_T *buf;
10511 buf_T *save_curbuf;
10512 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010513 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10516 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010517 ++emsg_off;
10518 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010519
10520 rettv->v_type = VAR_STRING;
10521 rettv->vval.v_string = NULL;
10522
10523 if (buf != NULL && varname != NULL)
10524 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010525 /* set curbuf to be our buf, temporarily */
10526 save_curbuf = curbuf;
10527 curbuf = buf;
10528
Bram Moolenaar0d660222005-01-07 21:51:51 +000010529 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010530 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010531 else
10532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 if (*varname == NUL)
10534 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10535 * scope prefix before the NUL byte is required by
10536 * find_var_in_ht(). */
10537 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010538 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010539 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010540 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010541 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010542 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010543
10544 /* restore previous notion of curbuf */
10545 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010546 }
10547
10548 --emsg_off;
10549}
10550
10551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 * "getchar()" function
10553 */
10554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 typval_T *argvars;
10557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
10559 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010562 /* Position the cursor. Needed after a message that ends in a space. */
10563 windgoto(msg_row, msg_col);
10564
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 ++no_mapping;
10566 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010567 for (;;)
10568 {
10569 if (argvars[0].v_type == VAR_UNKNOWN)
10570 /* getchar(): blocking wait. */
10571 n = safe_vgetc();
10572 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10573 /* getchar(1): only check if char avail */
10574 n = vpeekc();
10575 else if (error || vpeekc() == NUL)
10576 /* illegal argument or getchar(0) and no char avail: return zero */
10577 n = 0;
10578 else
10579 /* getchar(0) and char avail: return char */
10580 n = safe_vgetc();
10581 if (n == K_IGNORE)
10582 continue;
10583 break;
10584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 --no_mapping;
10586 --allow_keys;
10587
Bram Moolenaar219b8702006-11-01 14:32:36 +000010588 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10589 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10590 vimvars[VV_MOUSE_COL].vv_nr = 0;
10591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 if (IS_SPECIAL(n) || mod_mask != 0)
10594 {
10595 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10596 int i = 0;
10597
10598 /* Turn a special key into three bytes, plus modifier. */
10599 if (mod_mask != 0)
10600 {
10601 temp[i++] = K_SPECIAL;
10602 temp[i++] = KS_MODIFIER;
10603 temp[i++] = mod_mask;
10604 }
10605 if (IS_SPECIAL(n))
10606 {
10607 temp[i++] = K_SPECIAL;
10608 temp[i++] = K_SECOND(n);
10609 temp[i++] = K_THIRD(n);
10610 }
10611#ifdef FEAT_MBYTE
10612 else if (has_mbyte)
10613 i += (*mb_char2bytes)(n, temp + i);
10614#endif
10615 else
10616 temp[i++] = n;
10617 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 rettv->v_type = VAR_STRING;
10619 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010620
10621#ifdef FEAT_MOUSE
10622 if (n == K_LEFTMOUSE
10623 || n == K_LEFTMOUSE_NM
10624 || n == K_LEFTDRAG
10625 || n == K_LEFTRELEASE
10626 || n == K_LEFTRELEASE_NM
10627 || n == K_MIDDLEMOUSE
10628 || n == K_MIDDLEDRAG
10629 || n == K_MIDDLERELEASE
10630 || n == K_RIGHTMOUSE
10631 || n == K_RIGHTDRAG
10632 || n == K_RIGHTRELEASE
10633 || n == K_X1MOUSE
10634 || n == K_X1DRAG
10635 || n == K_X1RELEASE
10636 || n == K_X2MOUSE
10637 || n == K_X2DRAG
10638 || n == K_X2RELEASE
10639 || n == K_MOUSEDOWN
10640 || n == K_MOUSEUP)
10641 {
10642 int row = mouse_row;
10643 int col = mouse_col;
10644 win_T *win;
10645 linenr_T lnum;
10646# ifdef FEAT_WINDOWS
10647 win_T *wp;
10648# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010649 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010650
10651 if (row >= 0 && col >= 0)
10652 {
10653 /* Find the window at the mouse coordinates and compute the
10654 * text position. */
10655 win = mouse_find_win(&row, &col);
10656 (void)mouse_comp_pos(win, &row, &col, &lnum);
10657# ifdef FEAT_WINDOWS
10658 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010659 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010660# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010661 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010662 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10663 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10664 }
10665 }
10666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 }
10668}
10669
10670/*
10671 * "getcharmod()" function
10672 */
10673/*ARGSUSED*/
10674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
10681
10682/*
10683 * "getcmdline()" function
10684 */
10685/*ARGSUSED*/
10686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010688 typval_T *argvars;
10689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->v_type = VAR_STRING;
10692 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693}
10694
10695/*
10696 * "getcmdpos()" function
10697 */
10698/*ARGSUSED*/
10699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 typval_T *argvars;
10702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010708 * "getcmdtype()" function
10709 */
10710/*ARGSUSED*/
10711 static void
10712f_getcmdtype(argvars, rettv)
10713 typval_T *argvars;
10714 typval_T *rettv;
10715{
10716 rettv->v_type = VAR_STRING;
10717 rettv->vval.v_string = alloc(2);
10718 if (rettv->vval.v_string != NULL)
10719 {
10720 rettv->vval.v_string[0] = get_cmdline_type();
10721 rettv->vval.v_string[1] = NUL;
10722 }
10723}
10724
10725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 * "getcwd()" function
10727 */
10728/*ARGSUSED*/
10729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *argvars;
10732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
10734 char_u cwd[MAXPATHL];
10735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 else
10740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010743 if (rettv->vval.v_string != NULL)
10744 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745#endif
10746 }
10747}
10748
10749/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010750 * "getfontname()" function
10751 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010752/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010755 typval_T *argvars;
10756 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->v_type = VAR_STRING;
10759 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010760#ifdef FEAT_GUI
10761 if (gui.in_use)
10762 {
10763 GuiFont font;
10764 char_u *name = NULL;
10765
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010766 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010767 {
10768 /* Get the "Normal" font. Either the name saved by
10769 * hl_set_font_name() or from the font ID. */
10770 font = gui.norm_font;
10771 name = hl_get_font_name();
10772 }
10773 else
10774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010776 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10777 return;
10778 font = gui_mch_get_font(name, FALSE);
10779 if (font == NOFONT)
10780 return; /* Invalid font name, return empty string. */
10781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010783 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010784 gui_mch_free_font(font);
10785 }
10786#endif
10787}
10788
10789/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010790 * "getfperm({fname})" function
10791 */
10792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010794 typval_T *argvars;
10795 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010796{
10797 char_u *fname;
10798 struct stat st;
10799 char_u *perm = NULL;
10800 char_u flags[] = "rwx";
10801 int i;
10802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010806 if (mch_stat((char *)fname, &st) >= 0)
10807 {
10808 perm = vim_strsave((char_u *)"---------");
10809 if (perm != NULL)
10810 {
10811 for (i = 0; i < 9; i++)
10812 {
10813 if (st.st_mode & (1 << (8 - i)))
10814 perm[i] = flags[i % 3];
10815 }
10816 }
10817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010819}
10820
10821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 * "getfsize({fname})" function
10823 */
10824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *argvars;
10827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828{
10829 char_u *fname;
10830 struct stat st;
10831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835
10836 if (mch_stat((char *)fname, &st) >= 0)
10837 {
10838 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010843
10844 /* non-perfect check for overflow */
10845 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10846 rettv->vval.v_number = -2;
10847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 }
10849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851}
10852
10853/*
10854 * "getftime({fname})" function
10855 */
10856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010858 typval_T *argvars;
10859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860{
10861 char_u *fname;
10862 struct stat st;
10863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865
10866 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870}
10871
10872/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010873 * "getftype({fname})" function
10874 */
10875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010879{
10880 char_u *fname;
10881 struct stat st;
10882 char_u *type = NULL;
10883 char *t;
10884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010888 if (mch_lstat((char *)fname, &st) >= 0)
10889 {
10890#ifdef S_ISREG
10891 if (S_ISREG(st.st_mode))
10892 t = "file";
10893 else if (S_ISDIR(st.st_mode))
10894 t = "dir";
10895# ifdef S_ISLNK
10896 else if (S_ISLNK(st.st_mode))
10897 t = "link";
10898# endif
10899# ifdef S_ISBLK
10900 else if (S_ISBLK(st.st_mode))
10901 t = "bdev";
10902# endif
10903# ifdef S_ISCHR
10904 else if (S_ISCHR(st.st_mode))
10905 t = "cdev";
10906# endif
10907# ifdef S_ISFIFO
10908 else if (S_ISFIFO(st.st_mode))
10909 t = "fifo";
10910# endif
10911# ifdef S_ISSOCK
10912 else if (S_ISSOCK(st.st_mode))
10913 t = "fifo";
10914# endif
10915 else
10916 t = "other";
10917#else
10918# ifdef S_IFMT
10919 switch (st.st_mode & S_IFMT)
10920 {
10921 case S_IFREG: t = "file"; break;
10922 case S_IFDIR: t = "dir"; break;
10923# ifdef S_IFLNK
10924 case S_IFLNK: t = "link"; break;
10925# endif
10926# ifdef S_IFBLK
10927 case S_IFBLK: t = "bdev"; break;
10928# endif
10929# ifdef S_IFCHR
10930 case S_IFCHR: t = "cdev"; break;
10931# endif
10932# ifdef S_IFIFO
10933 case S_IFIFO: t = "fifo"; break;
10934# endif
10935# ifdef S_IFSOCK
10936 case S_IFSOCK: t = "socket"; break;
10937# endif
10938 default: t = "other";
10939 }
10940# else
10941 if (mch_isdir(fname))
10942 t = "dir";
10943 else
10944 t = "file";
10945# endif
10946#endif
10947 type = vim_strsave((char_u *)t);
10948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010950}
10951
10952/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010953 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010954 */
10955 static void
10956f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959{
10960 linenr_T lnum;
10961 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010962 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010963
10964 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010965 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010966 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010967 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010968 retlist = FALSE;
10969 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010970 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010971 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010972 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010973 retlist = TRUE;
10974 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010975
Bram Moolenaar342337a2005-07-21 21:11:17 +000010976 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010977}
10978
10979/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010980 * "getmatches()" function
10981 */
10982/*ARGSUSED*/
10983 static void
10984f_getmatches(argvars, rettv)
10985 typval_T *argvars;
10986 typval_T *rettv;
10987{
10988#ifdef FEAT_SEARCH_EXTRA
10989 dict_T *dict;
10990 matchitem_T *cur = curwin->w_match_head;
10991
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010992 if (rettv_list_alloc(rettv) == OK)
10993 {
10994 while (cur != NULL)
10995 {
10996 dict = dict_alloc();
10997 if (dict == NULL)
10998 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010999 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11000 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11001 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11002 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11003 list_append_dict(rettv->vval.v_list, dict);
11004 cur = cur->next;
11005 }
11006 }
11007#endif
11008}
11009
11010/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011011 * "getpid()" function
11012 */
11013/*ARGSUSED*/
11014 static void
11015f_getpid(argvars, rettv)
11016 typval_T *argvars;
11017 typval_T *rettv;
11018{
11019 rettv->vval.v_number = mch_get_pid();
11020}
11021
11022/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011023 * "getpos(string)" function
11024 */
11025 static void
11026f_getpos(argvars, rettv)
11027 typval_T *argvars;
11028 typval_T *rettv;
11029{
11030 pos_T *fp;
11031 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011032 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011033
11034 if (rettv_list_alloc(rettv) == OK)
11035 {
11036 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011037 fp = var2fpos(&argvars[0], TRUE, &fnum);
11038 if (fnum != -1)
11039 list_append_number(l, (varnumber_T)fnum);
11040 else
11041 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011042 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11043 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011044 list_append_number(l, (fp != NULL)
11045 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011046 : (varnumber_T)0);
11047 list_append_number(l,
11048#ifdef FEAT_VIRTUALEDIT
11049 (fp != NULL) ? (varnumber_T)fp->coladd :
11050#endif
11051 (varnumber_T)0);
11052 }
11053 else
11054 rettv->vval.v_number = FALSE;
11055}
11056
11057/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011058 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011059 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000011060/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000011061 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011062f_getqflist(argvars, rettv)
11063 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011064 typval_T *rettv;
11065{
11066#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011067 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011068#endif
11069
Bram Moolenaar2641f772005-03-25 21:58:17 +000011070#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011071 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011072 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011073 wp = NULL;
11074 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11075 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011076 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011077 if (wp == NULL)
11078 return;
11079 }
11080
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011081 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011082 }
11083#endif
11084}
11085
11086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 * "getreg()" function
11088 */
11089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011091 typval_T *argvars;
11092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093{
11094 char_u *strregname;
11095 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011096 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011099 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 strregname = get_tv_string_chk(&argvars[0]);
11102 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011103 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011104 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 regname = (strregname == NULL ? '"' : *strregname);
11109 if (regname == 0)
11110 regname = '"';
11111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 rettv->vval.v_string = error ? NULL :
11114 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115}
11116
11117/*
11118 * "getregtype()" function
11119 */
11120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 typval_T *argvars;
11123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124{
11125 char_u *strregname;
11126 int regname;
11127 char_u buf[NUMBUFLEN + 2];
11128 long reglen = 0;
11129
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011130 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 {
11132 strregname = get_tv_string_chk(&argvars[0]);
11133 if (strregname == NULL) /* type error; errmsg already given */
11134 {
11135 rettv->v_type = VAR_STRING;
11136 rettv->vval.v_string = NULL;
11137 return;
11138 }
11139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 else
11141 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143
11144 regname = (strregname == NULL ? '"' : *strregname);
11145 if (regname == 0)
11146 regname = '"';
11147
11148 buf[0] = NUL;
11149 buf[1] = NUL;
11150 switch (get_reg_type(regname, &reglen))
11151 {
11152 case MLINE: buf[0] = 'V'; break;
11153 case MCHAR: buf[0] = 'v'; break;
11154#ifdef FEAT_VISUAL
11155 case MBLOCK:
11156 buf[0] = Ctrl_V;
11157 sprintf((char *)buf + 1, "%ld", reglen + 1);
11158 break;
11159#endif
11160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 rettv->v_type = VAR_STRING;
11162 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164
11165/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011166 * "gettabwinvar()" function
11167 */
11168 static void
11169f_gettabwinvar(argvars, rettv)
11170 typval_T *argvars;
11171 typval_T *rettv;
11172{
11173 getwinvar(argvars, rettv, 1);
11174}
11175
11176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 * "getwinposx()" function
11178 */
11179/*ARGSUSED*/
11180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011182 typval_T *argvars;
11183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186#ifdef FEAT_GUI
11187 if (gui.in_use)
11188 {
11189 int x, y;
11190
11191 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 }
11194#endif
11195}
11196
11197/*
11198 * "getwinposy()" function
11199 */
11200/*ARGSUSED*/
11201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 typval_T *argvars;
11204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207#ifdef FEAT_GUI
11208 if (gui.in_use)
11209 {
11210 int x, y;
11211
11212 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 }
11215#endif
11216}
11217
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011218/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011219 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011220 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011221 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011222find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011223 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011224 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011225{
11226#ifdef FEAT_WINDOWS
11227 win_T *wp;
11228#endif
11229 int nr;
11230
11231 nr = get_tv_number_chk(vp, NULL);
11232
11233#ifdef FEAT_WINDOWS
11234 if (nr < 0)
11235 return NULL;
11236 if (nr == 0)
11237 return curwin;
11238
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011239 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11240 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011241 if (--nr <= 0)
11242 break;
11243 return wp;
11244#else
11245 if (nr == 0 || nr == 1)
11246 return curwin;
11247 return NULL;
11248#endif
11249}
11250
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251/*
11252 * "getwinvar()" function
11253 */
11254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011256 typval_T *argvars;
11257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011259 getwinvar(argvars, rettv, 0);
11260}
11261
11262/*
11263 * getwinvar() and gettabwinvar()
11264 */
11265 static void
11266getwinvar(argvars, rettv, off)
11267 typval_T *argvars;
11268 typval_T *rettv;
11269 int off; /* 1 for gettabwinvar() */
11270{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 win_T *win, *oldcurwin;
11272 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011274 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011276#ifdef FEAT_WINDOWS
11277 if (off == 1)
11278 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11279 else
11280 tp = curtab;
11281#endif
11282 win = find_win_by_nr(&argvars[off], tp);
11283 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->v_type = VAR_STRING;
11287 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288
11289 if (win != NULL && varname != NULL)
11290 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011291 /* Set curwin to be our win, temporarily. Also set curbuf, so
11292 * that we can get buffer-local options. */
11293 oldcurwin = curwin;
11294 curwin = win;
11295 curbuf = win->w_buffer;
11296
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 else
11300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 if (*varname == NUL)
11302 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11303 * scope prefix before the NUL byte is required by
11304 * find_var_in_ht(). */
11305 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011307 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011311
11312 /* restore previous notion of curwin */
11313 curwin = oldcurwin;
11314 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 }
11316
11317 --emsg_off;
11318}
11319
11320/*
11321 * "glob()" function
11322 */
11323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011325 typval_T *argvars;
11326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011328 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011330 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011332 /* When the optional second argument is non-zero, don't remove matches
11333 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11334 if (argvars[1].v_type != VAR_UNKNOWN
11335 && get_tv_number_chk(&argvars[1], &error))
11336 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011338 if (!error)
11339 {
11340 ExpandInit(&xpc);
11341 xpc.xp_context = EXPAND_FILES;
11342 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11343 NULL, flags, WILD_ALL);
11344 }
11345 else
11346 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347}
11348
11349/*
11350 * "globpath()" function
11351 */
11352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011354 typval_T *argvars;
11355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011357 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011360 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011362 /* When the optional second argument is non-zero, don't remove matches
11363 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11364 if (argvars[2].v_type != VAR_UNKNOWN
11365 && get_tv_number_chk(&argvars[2], &error))
11366 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011368 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 rettv->vval.v_string = NULL;
11370 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011371 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11372 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373}
11374
11375/*
11376 * "has()" function
11377 */
11378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382{
11383 int i;
11384 char_u *name;
11385 int n = FALSE;
11386 static char *(has_list[]) =
11387 {
11388#ifdef AMIGA
11389 "amiga",
11390# ifdef FEAT_ARP
11391 "arp",
11392# endif
11393#endif
11394#ifdef __BEOS__
11395 "beos",
11396#endif
11397#ifdef MSDOS
11398# ifdef DJGPP
11399 "dos32",
11400# else
11401 "dos16",
11402# endif
11403#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011404#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 "mac",
11406#endif
11407#if defined(MACOS_X_UNIX)
11408 "macunix",
11409#endif
11410#ifdef OS2
11411 "os2",
11412#endif
11413#ifdef __QNX__
11414 "qnx",
11415#endif
11416#ifdef RISCOS
11417 "riscos",
11418#endif
11419#ifdef UNIX
11420 "unix",
11421#endif
11422#ifdef VMS
11423 "vms",
11424#endif
11425#ifdef WIN16
11426 "win16",
11427#endif
11428#ifdef WIN32
11429 "win32",
11430#endif
11431#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11432 "win32unix",
11433#endif
11434#ifdef WIN64
11435 "win64",
11436#endif
11437#ifdef EBCDIC
11438 "ebcdic",
11439#endif
11440#ifndef CASE_INSENSITIVE_FILENAME
11441 "fname_case",
11442#endif
11443#ifdef FEAT_ARABIC
11444 "arabic",
11445#endif
11446#ifdef FEAT_AUTOCMD
11447 "autocmd",
11448#endif
11449#ifdef FEAT_BEVAL
11450 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011451# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11452 "balloon_multiline",
11453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454#endif
11455#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11456 "builtin_terms",
11457# ifdef ALL_BUILTIN_TCAPS
11458 "all_builtin_terms",
11459# endif
11460#endif
11461#ifdef FEAT_BYTEOFF
11462 "byte_offset",
11463#endif
11464#ifdef FEAT_CINDENT
11465 "cindent",
11466#endif
11467#ifdef FEAT_CLIENTSERVER
11468 "clientserver",
11469#endif
11470#ifdef FEAT_CLIPBOARD
11471 "clipboard",
11472#endif
11473#ifdef FEAT_CMDL_COMPL
11474 "cmdline_compl",
11475#endif
11476#ifdef FEAT_CMDHIST
11477 "cmdline_hist",
11478#endif
11479#ifdef FEAT_COMMENTS
11480 "comments",
11481#endif
11482#ifdef FEAT_CRYPT
11483 "cryptv",
11484#endif
11485#ifdef FEAT_CSCOPE
11486 "cscope",
11487#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011488#ifdef CURSOR_SHAPE
11489 "cursorshape",
11490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491#ifdef DEBUG
11492 "debug",
11493#endif
11494#ifdef FEAT_CON_DIALOG
11495 "dialog_con",
11496#endif
11497#ifdef FEAT_GUI_DIALOG
11498 "dialog_gui",
11499#endif
11500#ifdef FEAT_DIFF
11501 "diff",
11502#endif
11503#ifdef FEAT_DIGRAPHS
11504 "digraphs",
11505#endif
11506#ifdef FEAT_DND
11507 "dnd",
11508#endif
11509#ifdef FEAT_EMACS_TAGS
11510 "emacs_tags",
11511#endif
11512 "eval", /* always present, of course! */
11513#ifdef FEAT_EX_EXTRA
11514 "ex_extra",
11515#endif
11516#ifdef FEAT_SEARCH_EXTRA
11517 "extra_search",
11518#endif
11519#ifdef FEAT_FKMAP
11520 "farsi",
11521#endif
11522#ifdef FEAT_SEARCHPATH
11523 "file_in_path",
11524#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011525#if defined(UNIX) && !defined(USE_SYSTEM)
11526 "filterpipe",
11527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528#ifdef FEAT_FIND_ID
11529 "find_in_path",
11530#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011531#ifdef FEAT_FLOAT
11532 "float",
11533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534#ifdef FEAT_FOLDING
11535 "folding",
11536#endif
11537#ifdef FEAT_FOOTER
11538 "footer",
11539#endif
11540#if !defined(USE_SYSTEM) && defined(UNIX)
11541 "fork",
11542#endif
11543#ifdef FEAT_GETTEXT
11544 "gettext",
11545#endif
11546#ifdef FEAT_GUI
11547 "gui",
11548#endif
11549#ifdef FEAT_GUI_ATHENA
11550# ifdef FEAT_GUI_NEXTAW
11551 "gui_neXtaw",
11552# else
11553 "gui_athena",
11554# endif
11555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556#ifdef FEAT_GUI_GTK
11557 "gui_gtk",
11558# ifdef HAVE_GTK2
11559 "gui_gtk2",
11560# endif
11561#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011562#ifdef FEAT_GUI_GNOME
11563 "gui_gnome",
11564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565#ifdef FEAT_GUI_MAC
11566 "gui_mac",
11567#endif
11568#ifdef FEAT_GUI_MOTIF
11569 "gui_motif",
11570#endif
11571#ifdef FEAT_GUI_PHOTON
11572 "gui_photon",
11573#endif
11574#ifdef FEAT_GUI_W16
11575 "gui_win16",
11576#endif
11577#ifdef FEAT_GUI_W32
11578 "gui_win32",
11579#endif
11580#ifdef FEAT_HANGULIN
11581 "hangul_input",
11582#endif
11583#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11584 "iconv",
11585#endif
11586#ifdef FEAT_INS_EXPAND
11587 "insert_expand",
11588#endif
11589#ifdef FEAT_JUMPLIST
11590 "jumplist",
11591#endif
11592#ifdef FEAT_KEYMAP
11593 "keymap",
11594#endif
11595#ifdef FEAT_LANGMAP
11596 "langmap",
11597#endif
11598#ifdef FEAT_LIBCALL
11599 "libcall",
11600#endif
11601#ifdef FEAT_LINEBREAK
11602 "linebreak",
11603#endif
11604#ifdef FEAT_LISP
11605 "lispindent",
11606#endif
11607#ifdef FEAT_LISTCMDS
11608 "listcmds",
11609#endif
11610#ifdef FEAT_LOCALMAP
11611 "localmap",
11612#endif
11613#ifdef FEAT_MENU
11614 "menu",
11615#endif
11616#ifdef FEAT_SESSION
11617 "mksession",
11618#endif
11619#ifdef FEAT_MODIFY_FNAME
11620 "modify_fname",
11621#endif
11622#ifdef FEAT_MOUSE
11623 "mouse",
11624#endif
11625#ifdef FEAT_MOUSESHAPE
11626 "mouseshape",
11627#endif
11628#if defined(UNIX) || defined(VMS)
11629# ifdef FEAT_MOUSE_DEC
11630 "mouse_dec",
11631# endif
11632# ifdef FEAT_MOUSE_GPM
11633 "mouse_gpm",
11634# endif
11635# ifdef FEAT_MOUSE_JSB
11636 "mouse_jsbterm",
11637# endif
11638# ifdef FEAT_MOUSE_NET
11639 "mouse_netterm",
11640# endif
11641# ifdef FEAT_MOUSE_PTERM
11642 "mouse_pterm",
11643# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011644# ifdef FEAT_SYSMOUSE
11645 "mouse_sysmouse",
11646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647# ifdef FEAT_MOUSE_XTERM
11648 "mouse_xterm",
11649# endif
11650#endif
11651#ifdef FEAT_MBYTE
11652 "multi_byte",
11653#endif
11654#ifdef FEAT_MBYTE_IME
11655 "multi_byte_ime",
11656#endif
11657#ifdef FEAT_MULTI_LANG
11658 "multi_lang",
11659#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011660#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011661#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011662 "mzscheme",
11663#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665#ifdef FEAT_OLE
11666 "ole",
11667#endif
11668#ifdef FEAT_OSFILETYPE
11669 "osfiletype",
11670#endif
11671#ifdef FEAT_PATH_EXTRA
11672 "path_extra",
11673#endif
11674#ifdef FEAT_PERL
11675#ifndef DYNAMIC_PERL
11676 "perl",
11677#endif
11678#endif
11679#ifdef FEAT_PYTHON
11680#ifndef DYNAMIC_PYTHON
11681 "python",
11682#endif
11683#endif
11684#ifdef FEAT_POSTSCRIPT
11685 "postscript",
11686#endif
11687#ifdef FEAT_PRINTER
11688 "printer",
11689#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011690#ifdef FEAT_PROFILE
11691 "profile",
11692#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011693#ifdef FEAT_RELTIME
11694 "reltime",
11695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696#ifdef FEAT_QUICKFIX
11697 "quickfix",
11698#endif
11699#ifdef FEAT_RIGHTLEFT
11700 "rightleft",
11701#endif
11702#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11703 "ruby",
11704#endif
11705#ifdef FEAT_SCROLLBIND
11706 "scrollbind",
11707#endif
11708#ifdef FEAT_CMDL_INFO
11709 "showcmd",
11710 "cmdline_info",
11711#endif
11712#ifdef FEAT_SIGNS
11713 "signs",
11714#endif
11715#ifdef FEAT_SMARTINDENT
11716 "smartindent",
11717#endif
11718#ifdef FEAT_SNIFF
11719 "sniff",
11720#endif
11721#ifdef FEAT_STL_OPT
11722 "statusline",
11723#endif
11724#ifdef FEAT_SUN_WORKSHOP
11725 "sun_workshop",
11726#endif
11727#ifdef FEAT_NETBEANS_INTG
11728 "netbeans_intg",
11729#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011730#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011731 "spell",
11732#endif
11733#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 "syntax",
11735#endif
11736#if defined(USE_SYSTEM) || !defined(UNIX)
11737 "system",
11738#endif
11739#ifdef FEAT_TAG_BINS
11740 "tag_binary",
11741#endif
11742#ifdef FEAT_TAG_OLDSTATIC
11743 "tag_old_static",
11744#endif
11745#ifdef FEAT_TAG_ANYWHITE
11746 "tag_any_white",
11747#endif
11748#ifdef FEAT_TCL
11749# ifndef DYNAMIC_TCL
11750 "tcl",
11751# endif
11752#endif
11753#ifdef TERMINFO
11754 "terminfo",
11755#endif
11756#ifdef FEAT_TERMRESPONSE
11757 "termresponse",
11758#endif
11759#ifdef FEAT_TEXTOBJ
11760 "textobjects",
11761#endif
11762#ifdef HAVE_TGETENT
11763 "tgetent",
11764#endif
11765#ifdef FEAT_TITLE
11766 "title",
11767#endif
11768#ifdef FEAT_TOOLBAR
11769 "toolbar",
11770#endif
11771#ifdef FEAT_USR_CMDS
11772 "user-commands", /* was accidentally included in 5.4 */
11773 "user_commands",
11774#endif
11775#ifdef FEAT_VIMINFO
11776 "viminfo",
11777#endif
11778#ifdef FEAT_VERTSPLIT
11779 "vertsplit",
11780#endif
11781#ifdef FEAT_VIRTUALEDIT
11782 "virtualedit",
11783#endif
11784#ifdef FEAT_VISUAL
11785 "visual",
11786#endif
11787#ifdef FEAT_VISUALEXTRA
11788 "visualextra",
11789#endif
11790#ifdef FEAT_VREPLACE
11791 "vreplace",
11792#endif
11793#ifdef FEAT_WILDIGN
11794 "wildignore",
11795#endif
11796#ifdef FEAT_WILDMENU
11797 "wildmenu",
11798#endif
11799#ifdef FEAT_WINDOWS
11800 "windows",
11801#endif
11802#ifdef FEAT_WAK
11803 "winaltkeys",
11804#endif
11805#ifdef FEAT_WRITEBACKUP
11806 "writebackup",
11807#endif
11808#ifdef FEAT_XIM
11809 "xim",
11810#endif
11811#ifdef FEAT_XFONTSET
11812 "xfontset",
11813#endif
11814#ifdef USE_XSMP
11815 "xsmp",
11816#endif
11817#ifdef USE_XSMP_INTERACT
11818 "xsmp_interact",
11819#endif
11820#ifdef FEAT_XCLIPBOARD
11821 "xterm_clipboard",
11822#endif
11823#ifdef FEAT_XTERM_SAVE
11824 "xterm_save",
11825#endif
11826#if defined(UNIX) && defined(FEAT_X11)
11827 "X11",
11828#endif
11829 NULL
11830 };
11831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 for (i = 0; has_list[i] != NULL; ++i)
11834 if (STRICMP(name, has_list[i]) == 0)
11835 {
11836 n = TRUE;
11837 break;
11838 }
11839
11840 if (n == FALSE)
11841 {
11842 if (STRNICMP(name, "patch", 5) == 0)
11843 n = has_patch(atoi((char *)name + 5));
11844 else if (STRICMP(name, "vim_starting") == 0)
11845 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011846#ifdef FEAT_MBYTE
11847 else if (STRICMP(name, "multi_byte_encoding") == 0)
11848 n = has_mbyte;
11849#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011850#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11851 else if (STRICMP(name, "balloon_multiline") == 0)
11852 n = multiline_balloon_available();
11853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854#ifdef DYNAMIC_TCL
11855 else if (STRICMP(name, "tcl") == 0)
11856 n = tcl_enabled(FALSE);
11857#endif
11858#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11859 else if (STRICMP(name, "iconv") == 0)
11860 n = iconv_enabled(FALSE);
11861#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011862#ifdef DYNAMIC_MZSCHEME
11863 else if (STRICMP(name, "mzscheme") == 0)
11864 n = mzscheme_enabled(FALSE);
11865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866#ifdef DYNAMIC_RUBY
11867 else if (STRICMP(name, "ruby") == 0)
11868 n = ruby_enabled(FALSE);
11869#endif
11870#ifdef DYNAMIC_PYTHON
11871 else if (STRICMP(name, "python") == 0)
11872 n = python_enabled(FALSE);
11873#endif
11874#ifdef DYNAMIC_PERL
11875 else if (STRICMP(name, "perl") == 0)
11876 n = perl_enabled(FALSE);
11877#endif
11878#ifdef FEAT_GUI
11879 else if (STRICMP(name, "gui_running") == 0)
11880 n = (gui.in_use || gui.starting);
11881# ifdef FEAT_GUI_W32
11882 else if (STRICMP(name, "gui_win32s") == 0)
11883 n = gui_is_win32s();
11884# endif
11885# ifdef FEAT_BROWSE
11886 else if (STRICMP(name, "browse") == 0)
11887 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11888# endif
11889#endif
11890#ifdef FEAT_SYN_HL
11891 else if (STRICMP(name, "syntax_items") == 0)
11892 n = syntax_present(curbuf);
11893#endif
11894#if defined(WIN3264)
11895 else if (STRICMP(name, "win95") == 0)
11896 n = mch_windows95();
11897#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011898#ifdef FEAT_NETBEANS_INTG
11899 else if (STRICMP(name, "netbeans_enabled") == 0)
11900 n = usingNetbeans;
11901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 }
11903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905}
11906
11907/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011908 * "has_key()" function
11909 */
11910 static void
11911f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 typval_T *argvars;
11913 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011914{
Bram Moolenaare9a41262005-01-15 22:18:47 +000011915 if (argvars[0].v_type != VAR_DICT)
11916 {
11917 EMSG(_(e_dictreq));
11918 return;
11919 }
11920 if (argvars[0].vval.v_dict == NULL)
11921 return;
11922
11923 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011924 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011925}
11926
11927/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011928 * "haslocaldir()" function
11929 */
11930/*ARGSUSED*/
11931 static void
11932f_haslocaldir(argvars, rettv)
11933 typval_T *argvars;
11934 typval_T *rettv;
11935{
11936 rettv->vval.v_number = (curwin->w_localdir != NULL);
11937}
11938
11939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 * "hasmapto()" function
11941 */
11942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 typval_T *argvars;
11945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946{
11947 char_u *name;
11948 char_u *mode;
11949 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011950 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011953 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 mode = (char_u *)"nvo";
11955 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011958 if (argvars[2].v_type != VAR_UNKNOWN)
11959 abbr = get_tv_number(&argvars[2]);
11960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
Bram Moolenaar2c932302006-03-18 21:42:09 +000011962 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966}
11967
11968/*
11969 * "histadd()" function
11970 */
11971/*ARGSUSED*/
11972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011974 typval_T *argvars;
11975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976{
11977#ifdef FEAT_CMDHIST
11978 int histype;
11979 char_u *str;
11980 char_u buf[NUMBUFLEN];
11981#endif
11982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 if (check_restricted() || check_secure())
11985 return;
11986#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11988 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 if (histype >= 0)
11990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 if (*str != NUL)
11993 {
11994 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 return;
11997 }
11998 }
11999#endif
12000}
12001
12002/*
12003 * "histdel()" function
12004 */
12005/*ARGSUSED*/
12006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
12011#ifdef FEAT_CMDHIST
12012 int n;
12013 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012014 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012016 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12017 if (str == NULL)
12018 n = 0;
12019 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012022 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 else
12027 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 get_tv_string_buf(&argvars[1], buf));
12030 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031#endif
12032}
12033
12034/*
12035 * "histget()" function
12036 */
12037/*ARGSUSED*/
12038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012040 typval_T *argvars;
12041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042{
12043#ifdef FEAT_CMDHIST
12044 int type;
12045 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12049 if (str == NULL)
12050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 {
12053 type = get_histtype(str);
12054 if (argvars[1].v_type == VAR_UNKNOWN)
12055 idx = get_history_idx(type);
12056 else
12057 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12058 /* -1 on type error */
12059 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065}
12066
12067/*
12068 * "histnr()" function
12069 */
12070/*ARGSUSED*/
12071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 typval_T *argvars;
12074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076 int i;
12077
12078#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012079 char_u *history = get_tv_string_chk(&argvars[0]);
12080
12081 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 if (i >= HIST_CMD && i < HIST_COUNT)
12083 i = get_history_idx(i);
12084 else
12085#endif
12086 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088}
12089
12090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 * "highlightID(name)" function
12092 */
12093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012095 typval_T *argvars;
12096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012098 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099}
12100
12101/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012102 * "highlight_exists()" function
12103 */
12104 static void
12105f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108{
12109 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12110}
12111
12112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 * "hostname()" function
12114 */
12115/*ARGSUSED*/
12116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012118 typval_T *argvars;
12119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120{
12121 char_u hostname[256];
12122
12123 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012124 rettv->v_type = VAR_STRING;
12125 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126}
12127
12128/*
12129 * iconv() function
12130 */
12131/*ARGSUSED*/
12132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 typval_T *argvars;
12135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136{
12137#ifdef FEAT_MBYTE
12138 char_u buf1[NUMBUFLEN];
12139 char_u buf2[NUMBUFLEN];
12140 char_u *from, *to, *str;
12141 vimconv_T vimconv;
12142#endif
12143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 rettv->v_type = VAR_STRING;
12145 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146
12147#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 str = get_tv_string(&argvars[0]);
12149 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12150 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 vimconv.vc_type = CONV_NONE;
12152 convert_setup(&vimconv, from, to);
12153
12154 /* If the encodings are equal, no conversion needed. */
12155 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
12160 convert_setup(&vimconv, NULL, NULL);
12161 vim_free(from);
12162 vim_free(to);
12163#endif
12164}
12165
12166/*
12167 * "indent()" function
12168 */
12169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012171 typval_T *argvars;
12172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173{
12174 linenr_T lnum;
12175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181}
12182
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012183/*
12184 * "index()" function
12185 */
12186 static void
12187f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012188 typval_T *argvars;
12189 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012190{
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 list_T *l;
12192 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012193 long idx = 0;
12194 int ic = FALSE;
12195
12196 rettv->vval.v_number = -1;
12197 if (argvars[0].v_type != VAR_LIST)
12198 {
12199 EMSG(_(e_listreq));
12200 return;
12201 }
12202 l = argvars[0].vval.v_list;
12203 if (l != NULL)
12204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012205 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012208 int error = FALSE;
12209
Bram Moolenaar758711c2005-02-02 23:11:38 +000012210 /* Start at specified item. Use the cached index that list_find()
12211 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012212 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012213 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012214 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012215 ic = get_tv_number_chk(&argvars[3], &error);
12216 if (error)
12217 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012218 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012219
Bram Moolenaar758711c2005-02-02 23:11:38 +000012220 for ( ; item != NULL; item = item->li_next, ++idx)
12221 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012222 {
12223 rettv->vval.v_number = idx;
12224 break;
12225 }
12226 }
12227}
12228
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229static int inputsecret_flag = 0;
12230
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012231static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12232
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012234 * This function is used by f_input() and f_inputdialog() functions. The third
12235 * argument to f_input() specifies the type of completion to use at the
12236 * prompt. The third argument to f_inputdialog() specifies the value to return
12237 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 */
12239 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012240get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012241 typval_T *argvars;
12242 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012243 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012245 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 char_u *p = NULL;
12247 int c;
12248 char_u buf[NUMBUFLEN];
12249 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012250 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012251 int xp_type = EXPAND_NOTHING;
12252 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256
12257#ifdef NO_CONSOLE_INPUT
12258 /* While starting up, there is no place to enter text. */
12259 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261#endif
12262
12263 cmd_silent = FALSE; /* Want to see the prompt. */
12264 if (prompt != NULL)
12265 {
12266 /* Only the part of the message after the last NL is considered as
12267 * prompt for the command line */
12268 p = vim_strrchr(prompt, '\n');
12269 if (p == NULL)
12270 p = prompt;
12271 else
12272 {
12273 ++p;
12274 c = *p;
12275 *p = NUL;
12276 msg_start();
12277 msg_clr_eos();
12278 msg_puts_attr(prompt, echo_attr);
12279 msg_didout = FALSE;
12280 msg_starthere();
12281 *p = c;
12282 }
12283 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 if (argvars[1].v_type != VAR_UNKNOWN)
12286 {
12287 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12288 if (defstr != NULL)
12289 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012291 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012292 {
12293 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012294 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012295 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012296
Bram Moolenaar4463f292005-09-25 22:20:24 +000012297 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012298
Bram Moolenaar4463f292005-09-25 22:20:24 +000012299 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12300 if (xp_name == NULL)
12301 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012303 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012304
Bram Moolenaar4463f292005-09-25 22:20:24 +000012305 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12306 &xp_arg) == FAIL)
12307 return;
12308 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012309 }
12310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012311 if (defstr != NULL)
12312 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012313 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12314 xp_type, xp_arg);
12315
12316 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012318 /* since the user typed this, no need to wait for return */
12319 need_wait_return = FALSE;
12320 msg_didout = FALSE;
12321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 cmd_silent = cmd_silent_save;
12323}
12324
12325/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012326 * "input()" function
12327 * Also handles inputsecret() when inputsecret is set.
12328 */
12329 static void
12330f_input(argvars, rettv)
12331 typval_T *argvars;
12332 typval_T *rettv;
12333{
12334 get_user_input(argvars, rettv, FALSE);
12335}
12336
12337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 * "inputdialog()" function
12339 */
12340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012342 typval_T *argvars;
12343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344{
12345#if defined(FEAT_GUI_TEXTDIALOG)
12346 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12347 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12348 {
12349 char_u *message;
12350 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012351 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012353 message = get_tv_string_chk(&argvars[0]);
12354 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012355 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012356 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 else
12358 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012359 if (message != NULL && defstr != NULL
12360 && do_dialog(VIM_QUESTION, NULL, message,
12361 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 else
12364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012365 if (message != NULL && defstr != NULL
12366 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012367 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->vval.v_string = vim_strsave(
12369 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 }
12375 else
12376#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012377 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378}
12379
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012380/*
12381 * "inputlist()" function
12382 */
12383 static void
12384f_inputlist(argvars, rettv)
12385 typval_T *argvars;
12386 typval_T *rettv;
12387{
12388 listitem_T *li;
12389 int selected;
12390 int mouse_used;
12391
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012392#ifdef NO_CONSOLE_INPUT
12393 /* While starting up, there is no place to enter text. */
12394 if (no_console_input())
12395 return;
12396#endif
12397 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12398 {
12399 EMSG2(_(e_listarg), "inputlist()");
12400 return;
12401 }
12402
12403 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012404 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012405 lines_left = Rows; /* avoid more prompt */
12406 msg_scroll = TRUE;
12407 msg_clr_eos();
12408
12409 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12410 {
12411 msg_puts(get_tv_string(&li->li_tv));
12412 msg_putchar('\n');
12413 }
12414
12415 /* Ask for choice. */
12416 selected = prompt_for_number(&mouse_used);
12417 if (mouse_used)
12418 selected -= lines_left;
12419
12420 rettv->vval.v_number = selected;
12421}
12422
12423
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12425
12426/*
12427 * "inputrestore()" function
12428 */
12429/*ARGSUSED*/
12430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434{
12435 if (ga_userinput.ga_len > 0)
12436 {
12437 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12439 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012440 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 }
12442 else if (p_verbose > 1)
12443 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012444 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 }
12447}
12448
12449/*
12450 * "inputsave()" function
12451 */
12452/*ARGSUSED*/
12453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012455 typval_T *argvars;
12456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012458 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 if (ga_grow(&ga_userinput, 1) == OK)
12460 {
12461 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12462 + ga_userinput.ga_len);
12463 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012464 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 }
12466 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468}
12469
12470/*
12471 * "inputsecret()" function
12472 */
12473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012475 typval_T *argvars;
12476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477{
12478 ++cmdline_star;
12479 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 --cmdline_star;
12482 --inputsecret_flag;
12483}
12484
12485/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012486 * "insert()" function
12487 */
12488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 typval_T *argvars;
12491 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012492{
12493 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012494 listitem_T *item;
12495 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012496 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012497
12498 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012500 else if ((l = argvars[0].vval.v_list) != NULL
12501 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012502 {
12503 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 before = get_tv_number_chk(&argvars[2], &error);
12505 if (error)
12506 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012507
Bram Moolenaar758711c2005-02-02 23:11:38 +000012508 if (before == l->lv_len)
12509 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012510 else
12511 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012512 item = list_find(l, before);
12513 if (item == NULL)
12514 {
12515 EMSGN(_(e_listidx), before);
12516 l = NULL;
12517 }
12518 }
12519 if (l != NULL)
12520 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012521 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012522 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012523 }
12524 }
12525}
12526
12527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 * "isdirectory()" function
12529 */
12530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012532 typval_T *argvars;
12533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536}
12537
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012538/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012539 * "islocked()" function
12540 */
12541 static void
12542f_islocked(argvars, rettv)
12543 typval_T *argvars;
12544 typval_T *rettv;
12545{
12546 lval_T lv;
12547 char_u *end;
12548 dictitem_T *di;
12549
12550 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012551 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12552 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012553 if (end != NULL && lv.ll_name != NULL)
12554 {
12555 if (*end != NUL)
12556 EMSG(_(e_trailing));
12557 else
12558 {
12559 if (lv.ll_tv == NULL)
12560 {
12561 if (check_changedtick(lv.ll_name))
12562 rettv->vval.v_number = 1; /* always locked */
12563 else
12564 {
12565 di = find_var(lv.ll_name, NULL);
12566 if (di != NULL)
12567 {
12568 /* Consider a variable locked when:
12569 * 1. the variable itself is locked
12570 * 2. the value of the variable is locked.
12571 * 3. the List or Dict value is locked.
12572 */
12573 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12574 || tv_islocked(&di->di_tv));
12575 }
12576 }
12577 }
12578 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012579 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012580 else if (lv.ll_newkey != NULL)
12581 EMSG2(_(e_dictkey), lv.ll_newkey);
12582 else if (lv.ll_list != NULL)
12583 /* List item. */
12584 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12585 else
12586 /* Dictionary item. */
12587 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12588 }
12589 }
12590
12591 clear_lval(&lv);
12592}
12593
Bram Moolenaar33570922005-01-25 22:26:29 +000012594static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012595
12596/*
12597 * Turn a dict into a list:
12598 * "what" == 0: list of keys
12599 * "what" == 1: list of values
12600 * "what" == 2: list of items
12601 */
12602 static void
12603dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012604 typval_T *argvars;
12605 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012606 int what;
12607{
Bram Moolenaar33570922005-01-25 22:26:29 +000012608 list_T *l2;
12609 dictitem_T *di;
12610 hashitem_T *hi;
12611 listitem_T *li;
12612 listitem_T *li2;
12613 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012614 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012615
Bram Moolenaar8c711452005-01-14 21:53:12 +000012616 if (argvars[0].v_type != VAR_DICT)
12617 {
12618 EMSG(_(e_dictreq));
12619 return;
12620 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012621 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012622 return;
12623
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012624 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012625 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012626
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012627 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012630 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012631 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012632 --todo;
12633 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012634
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012635 li = listitem_alloc();
12636 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012637 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012638 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012639
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012640 if (what == 0)
12641 {
12642 /* keys() */
12643 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012644 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012645 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12646 }
12647 else if (what == 1)
12648 {
12649 /* values() */
12650 copy_tv(&di->di_tv, &li->li_tv);
12651 }
12652 else
12653 {
12654 /* items() */
12655 l2 = list_alloc();
12656 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012657 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012658 li->li_tv.vval.v_list = l2;
12659 if (l2 == NULL)
12660 break;
12661 ++l2->lv_refcount;
12662
12663 li2 = listitem_alloc();
12664 if (li2 == NULL)
12665 break;
12666 list_append(l2, li2);
12667 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012668 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012669 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12670
12671 li2 = listitem_alloc();
12672 if (li2 == NULL)
12673 break;
12674 list_append(l2, li2);
12675 copy_tv(&di->di_tv, &li2->li_tv);
12676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012677 }
12678 }
12679}
12680
12681/*
12682 * "items(dict)" function
12683 */
12684 static void
12685f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 typval_T *argvars;
12687 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012688{
12689 dict_list(argvars, rettv, 2);
12690}
12691
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012693 * "join()" function
12694 */
12695 static void
12696f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *argvars;
12698 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012699{
12700 garray_T ga;
12701 char_u *sep;
12702
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012703 if (argvars[0].v_type != VAR_LIST)
12704 {
12705 EMSG(_(e_listreq));
12706 return;
12707 }
12708 if (argvars[0].vval.v_list == NULL)
12709 return;
12710 if (argvars[1].v_type == VAR_UNKNOWN)
12711 sep = (char_u *)" ";
12712 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012714
12715 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716
12717 if (sep != NULL)
12718 {
12719 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012720 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 ga_append(&ga, NUL);
12722 rettv->vval.v_string = (char_u *)ga.ga_data;
12723 }
12724 else
12725 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012726}
12727
12728/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012729 * "keys()" function
12730 */
12731 static void
12732f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *argvars;
12734 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012735{
12736 dict_list(argvars, rettv, 0);
12737}
12738
12739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 * "last_buffer_nr()" function.
12741 */
12742/*ARGSUSED*/
12743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 typval_T *argvars;
12746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747{
12748 int n = 0;
12749 buf_T *buf;
12750
12751 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12752 if (n < buf->b_fnum)
12753 n = buf->b_fnum;
12754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012756}
12757
12758/*
12759 * "len()" function
12760 */
12761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012765{
12766 switch (argvars[0].v_type)
12767 {
12768 case VAR_STRING:
12769 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = (varnumber_T)STRLEN(
12771 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012772 break;
12773 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012775 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012776 case VAR_DICT:
12777 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12778 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012779 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012780 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012781 break;
12782 }
12783}
12784
Bram Moolenaar33570922005-01-25 22:26:29 +000012785static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012786
12787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *argvars;
12790 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012791 int type;
12792{
12793#ifdef FEAT_LIBCALL
12794 char_u *string_in;
12795 char_u **string_result;
12796 int nr_result;
12797#endif
12798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012800 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802
12803 if (check_restricted() || check_secure())
12804 return;
12805
12806#ifdef FEAT_LIBCALL
12807 /* The first two args must be strings, otherwise its meaningless */
12808 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12809 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012810 string_in = NULL;
12811 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012812 string_in = argvars[2].vval.v_string;
12813 if (type == VAR_NUMBER)
12814 string_result = NULL;
12815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012817 if (mch_libcall(argvars[0].vval.v_string,
12818 argvars[1].vval.v_string,
12819 string_in,
12820 argvars[2].vval.v_number,
12821 string_result,
12822 &nr_result) == OK
12823 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825 }
12826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827}
12828
12829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012830 * "libcall()" function
12831 */
12832 static void
12833f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 typval_T *argvars;
12835 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012836{
12837 libcall_common(argvars, rettv, VAR_STRING);
12838}
12839
12840/*
12841 * "libcallnr()" function
12842 */
12843 static void
12844f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012845 typval_T *argvars;
12846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847{
12848 libcall_common(argvars, rettv, VAR_NUMBER);
12849}
12850
12851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 * "line(string)" function
12853 */
12854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 typval_T *argvars;
12857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
12859 linenr_T lnum = 0;
12860 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012861 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012863 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 if (fp != NULL)
12865 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867}
12868
12869/*
12870 * "line2byte(lnum)" function
12871 */
12872/*ARGSUSED*/
12873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012875 typval_T *argvars;
12876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877{
12878#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880#else
12881 linenr_T lnum;
12882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12888 if (rettv->vval.v_number >= 0)
12889 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890#endif
12891}
12892
12893/*
12894 * "lispindent(lnum)" function
12895 */
12896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012898 typval_T *argvars;
12899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900{
12901#ifdef FEAT_LISP
12902 pos_T pos;
12903 linenr_T lnum;
12904
12905 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12908 {
12909 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 curwin->w_cursor = pos;
12912 }
12913 else
12914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916}
12917
12918/*
12919 * "localtime()" function
12920 */
12921/*ARGSUSED*/
12922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 typval_T *argvars;
12925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928}
12929
Bram Moolenaar33570922005-01-25 22:26:29 +000012930static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931
12932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012934 typval_T *argvars;
12935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 int exact;
12937{
12938 char_u *keys;
12939 char_u *which;
12940 char_u buf[NUMBUFLEN];
12941 char_u *keys_buf = NULL;
12942 char_u *rhs;
12943 int mode;
12944 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012945 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
12947 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->v_type = VAR_STRING;
12949 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 if (*keys == NUL)
12953 return;
12954
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012955 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012957 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012958 if (argvars[2].v_type != VAR_UNKNOWN)
12959 abbr = get_tv_number(&argvars[2]);
12960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 else
12962 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012963 if (which == NULL)
12964 return;
12965
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 mode = get_map_mode(&which, 0);
12967
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012968 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012969 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970 vim_free(keys_buf);
12971 if (rhs != NULL)
12972 {
12973 ga_init(&ga);
12974 ga.ga_itemsize = 1;
12975 ga.ga_growsize = 40;
12976
12977 while (*rhs != NUL)
12978 ga_concat(&ga, str2special(&rhs, FALSE));
12979
12980 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 }
12983}
12984
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012985#ifdef FEAT_FLOAT
12986/*
12987 * "log10()" function
12988 */
12989 static void
12990f_log10(argvars, rettv)
12991 typval_T *argvars;
12992 typval_T *rettv;
12993{
12994 float_T f;
12995
12996 rettv->v_type = VAR_FLOAT;
12997 if (get_float_arg(argvars, &f) == OK)
12998 rettv->vval.v_float = log10(f);
12999 else
13000 rettv->vval.v_float = 0.0;
13001}
13002#endif
13003
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013005 * "map()" function
13006 */
13007 static void
13008f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013009 typval_T *argvars;
13010 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013011{
13012 filter_map(argvars, rettv, TRUE);
13013}
13014
13015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013016 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 */
13018 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013019f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013020 typval_T *argvars;
13021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024}
13025
13026/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013027 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 */
13029 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013030f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013031 typval_T *argvars;
13032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013034 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035}
13036
Bram Moolenaar33570922005-01-25 22:26:29 +000013037static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038
13039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 typval_T *argvars;
13042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 int type;
13044{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013045 char_u *str = NULL;
13046 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 char_u *pat;
13048 regmatch_T regmatch;
13049 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013050 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 char_u *save_cpo;
13052 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013053 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013054 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013055 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 list_T *l = NULL;
13057 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013058 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013059 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060
13061 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13062 save_cpo = p_cpo;
13063 p_cpo = (char_u *)"";
13064
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013065 rettv->vval.v_number = -1;
13066 if (type == 3)
13067 {
13068 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013069 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013070 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013071 }
13072 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 rettv->v_type = VAR_STRING;
13075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013078 if (argvars[0].v_type == VAR_LIST)
13079 {
13080 if ((l = argvars[0].vval.v_list) == NULL)
13081 goto theend;
13082 li = l->lv_first;
13083 }
13084 else
13085 expr = str = get_tv_string(&argvars[0]);
13086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13088 if (pat == NULL)
13089 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 int error = FALSE;
13094
13095 start = get_tv_number_chk(&argvars[2], &error);
13096 if (error)
13097 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013098 if (l != NULL)
13099 {
13100 li = list_find(l, start);
13101 if (li == NULL)
13102 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013103 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013104 }
13105 else
13106 {
13107 if (start < 0)
13108 start = 0;
13109 if (start > (long)STRLEN(str))
13110 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013111 /* When "count" argument is there ignore matches before "start",
13112 * otherwise skip part of the string. Differs when pattern is "^"
13113 * or "\<". */
13114 if (argvars[3].v_type != VAR_UNKNOWN)
13115 startcol = start;
13116 else
13117 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013118 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013119
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013120 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013121 nth = get_tv_number_chk(&argvars[3], &error);
13122 if (error)
13123 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 }
13125
13126 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13127 if (regmatch.regprog != NULL)
13128 {
13129 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013130
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013131 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013132 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013133 if (l != NULL)
13134 {
13135 if (li == NULL)
13136 {
13137 match = FALSE;
13138 break;
13139 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013140 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013141 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013142 if (str == NULL)
13143 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013144 }
13145
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013146 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013147
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013148 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013149 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013150 if (l == NULL && !match)
13151 break;
13152
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013153 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013154 if (l != NULL)
13155 {
13156 li = li->li_next;
13157 ++idx;
13158 }
13159 else
13160 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013161#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013162 startcol = (colnr_T)(regmatch.startp[0]
13163 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013164#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013165 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013166#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013167 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013168 }
13169
13170 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013172 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013173 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013174 int i;
13175
13176 /* return list with matched string and submatches */
13177 for (i = 0; i < NSUBEXP; ++i)
13178 {
13179 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013180 {
13181 if (list_append_string(rettv->vval.v_list,
13182 (char_u *)"", 0) == FAIL)
13183 break;
13184 }
13185 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013186 regmatch.startp[i],
13187 (int)(regmatch.endp[i] - regmatch.startp[i]))
13188 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013189 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013190 }
13191 }
13192 else if (type == 2)
13193 {
13194 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013195 if (l != NULL)
13196 copy_tv(&li->li_tv, rettv);
13197 else
13198 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013200 }
13201 else if (l != NULL)
13202 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 else
13204 {
13205 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 (varnumber_T)(regmatch.startp[0] - str);
13208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013211 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 }
13213 }
13214 vim_free(regmatch.regprog);
13215 }
13216
13217theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013218 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 p_cpo = save_cpo;
13220}
13221
13222/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013223 * "match()" function
13224 */
13225 static void
13226f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 typval_T *argvars;
13228 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013229{
13230 find_some_match(argvars, rettv, 1);
13231}
13232
13233/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013234 * "matchadd()" function
13235 */
13236 static void
13237f_matchadd(argvars, rettv)
13238 typval_T *argvars;
13239 typval_T *rettv;
13240{
13241#ifdef FEAT_SEARCH_EXTRA
13242 char_u buf[NUMBUFLEN];
13243 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13244 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13245 int prio = 10; /* default priority */
13246 int id = -1;
13247 int error = FALSE;
13248
13249 rettv->vval.v_number = -1;
13250
13251 if (grp == NULL || pat == NULL)
13252 return;
13253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013254 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013255 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013256 if (argvars[3].v_type != VAR_UNKNOWN)
13257 id = get_tv_number_chk(&argvars[3], &error);
13258 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013259 if (error == TRUE)
13260 return;
13261 if (id >= 1 && id <= 3)
13262 {
13263 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13264 return;
13265 }
13266
13267 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13268#endif
13269}
13270
13271/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013272 * "matcharg()" function
13273 */
13274 static void
13275f_matcharg(argvars, rettv)
13276 typval_T *argvars;
13277 typval_T *rettv;
13278{
13279 if (rettv_list_alloc(rettv) == OK)
13280 {
13281#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013282 int id = get_tv_number(&argvars[0]);
13283 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013284
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013285 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013286 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013287 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13288 {
13289 list_append_string(rettv->vval.v_list,
13290 syn_id2name(m->hlg_id), -1);
13291 list_append_string(rettv->vval.v_list, m->pattern, -1);
13292 }
13293 else
13294 {
13295 list_append_string(rettv->vval.v_list, NUL, -1);
13296 list_append_string(rettv->vval.v_list, NUL, -1);
13297 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013298 }
13299#endif
13300 }
13301}
13302
13303/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013304 * "matchdelete()" function
13305 */
13306 static void
13307f_matchdelete(argvars, rettv)
13308 typval_T *argvars;
13309 typval_T *rettv;
13310{
13311#ifdef FEAT_SEARCH_EXTRA
13312 rettv->vval.v_number = match_delete(curwin,
13313 (int)get_tv_number(&argvars[0]), TRUE);
13314#endif
13315}
13316
13317/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013318 * "matchend()" function
13319 */
13320 static void
13321f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *argvars;
13323 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013324{
13325 find_some_match(argvars, rettv, 0);
13326}
13327
13328/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013329 * "matchlist()" function
13330 */
13331 static void
13332f_matchlist(argvars, rettv)
13333 typval_T *argvars;
13334 typval_T *rettv;
13335{
13336 find_some_match(argvars, rettv, 3);
13337}
13338
13339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013340 * "matchstr()" function
13341 */
13342 static void
13343f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013344 typval_T *argvars;
13345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013346{
13347 find_some_match(argvars, rettv, 2);
13348}
13349
Bram Moolenaar33570922005-01-25 22:26:29 +000013350static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013351
13352 static void
13353max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *argvars;
13355 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013356 int domax;
13357{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013358 long n = 0;
13359 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013361
13362 if (argvars[0].v_type == VAR_LIST)
13363 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 list_T *l;
13365 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013366
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013367 l = argvars[0].vval.v_list;
13368 if (l != NULL)
13369 {
13370 li = l->lv_first;
13371 if (li != NULL)
13372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013373 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013374 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013375 {
13376 li = li->li_next;
13377 if (li == NULL)
13378 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013380 if (domax ? i > n : i < n)
13381 n = i;
13382 }
13383 }
13384 }
13385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013386 else if (argvars[0].v_type == VAR_DICT)
13387 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013390 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013391 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013392
13393 d = argvars[0].vval.v_dict;
13394 if (d != NULL)
13395 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013396 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013401 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013403 if (first)
13404 {
13405 n = i;
13406 first = FALSE;
13407 }
13408 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013409 n = i;
13410 }
13411 }
13412 }
13413 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013414 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013415 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013416 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013417}
13418
13419/*
13420 * "max()" function
13421 */
13422 static void
13423f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 typval_T *argvars;
13425 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013426{
13427 max_min(argvars, rettv, TRUE);
13428}
13429
13430/*
13431 * "min()" function
13432 */
13433 static void
13434f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013435 typval_T *argvars;
13436 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013437{
13438 max_min(argvars, rettv, FALSE);
13439}
13440
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013441static int mkdir_recurse __ARGS((char_u *dir, int prot));
13442
13443/*
13444 * Create the directory in which "dir" is located, and higher levels when
13445 * needed.
13446 */
13447 static int
13448mkdir_recurse(dir, prot)
13449 char_u *dir;
13450 int prot;
13451{
13452 char_u *p;
13453 char_u *updir;
13454 int r = FAIL;
13455
13456 /* Get end of directory name in "dir".
13457 * We're done when it's "/" or "c:/". */
13458 p = gettail_sep(dir);
13459 if (p <= get_past_head(dir))
13460 return OK;
13461
13462 /* If the directory exists we're done. Otherwise: create it.*/
13463 updir = vim_strnsave(dir, (int)(p - dir));
13464 if (updir == NULL)
13465 return FAIL;
13466 if (mch_isdir(updir))
13467 r = OK;
13468 else if (mkdir_recurse(updir, prot) == OK)
13469 r = vim_mkdir_emsg(updir, prot);
13470 vim_free(updir);
13471 return r;
13472}
13473
13474#ifdef vim_mkdir
13475/*
13476 * "mkdir()" function
13477 */
13478 static void
13479f_mkdir(argvars, rettv)
13480 typval_T *argvars;
13481 typval_T *rettv;
13482{
13483 char_u *dir;
13484 char_u buf[NUMBUFLEN];
13485 int prot = 0755;
13486
13487 rettv->vval.v_number = FAIL;
13488 if (check_restricted() || check_secure())
13489 return;
13490
13491 dir = get_tv_string_buf(&argvars[0], buf);
13492 if (argvars[1].v_type != VAR_UNKNOWN)
13493 {
13494 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495 prot = get_tv_number_chk(&argvars[2], NULL);
13496 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013497 mkdir_recurse(dir, prot);
13498 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013500}
13501#endif
13502
Bram Moolenaar0d660222005-01-07 21:51:51 +000013503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 * "mode()" function
13505 */
13506/*ARGSUSED*/
13507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 typval_T *argvars;
13510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013512 char_u buf[3];
13513
13514 buf[1] = NUL;
13515 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516
13517#ifdef FEAT_VISUAL
13518 if (VIsual_active)
13519 {
13520 if (VIsual_select)
13521 buf[0] = VIsual_mode + 's' - 'v';
13522 else
13523 buf[0] = VIsual_mode;
13524 }
13525 else
13526#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013527 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13528 || State == CONFIRM)
13529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013531 if (State == ASKMORE)
13532 buf[1] = 'm';
13533 else if (State == CONFIRM)
13534 buf[1] = '?';
13535 }
13536 else if (State == EXTERNCMD)
13537 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 else if (State & INSERT)
13539 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013540#ifdef FEAT_VREPLACE
13541 if (State & VREPLACE_FLAG)
13542 {
13543 buf[0] = 'R';
13544 buf[1] = 'v';
13545 }
13546 else
13547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 if (State & REPLACE_FLAG)
13549 buf[0] = 'R';
13550 else
13551 buf[0] = 'i';
13552 }
13553 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013556 if (exmode_active)
13557 buf[1] = 'v';
13558 }
13559 else if (exmode_active)
13560 {
13561 buf[0] = 'c';
13562 buf[1] = 'e';
13563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013567 if (finish_op)
13568 buf[1] = 'o';
13569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013571 /* Clear out the minor mode when the argument is not a non-zero number or
13572 * non-empty string. */
13573 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013574 buf[1] = NUL;
13575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->vval.v_string = vim_strsave(buf);
13577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578}
13579
13580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013581 * "nextnonblank()" function
13582 */
13583 static void
13584f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *argvars;
13586 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013587{
13588 linenr_T lnum;
13589
13590 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013593 {
13594 lnum = 0;
13595 break;
13596 }
13597 if (*skipwhite(ml_get(lnum)) != NUL)
13598 break;
13599 }
13600 rettv->vval.v_number = lnum;
13601}
13602
13603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 * "nr2char()" function
13605 */
13606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013608 typval_T *argvars;
13609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
13611 char_u buf[NUMBUFLEN];
13612
13613#ifdef FEAT_MBYTE
13614 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 else
13617#endif
13618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 buf[1] = NUL;
13621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->v_type = VAR_STRING;
13623 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013624}
13625
13626/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013627 * "pathshorten()" function
13628 */
13629 static void
13630f_pathshorten(argvars, rettv)
13631 typval_T *argvars;
13632 typval_T *rettv;
13633{
13634 char_u *p;
13635
13636 rettv->v_type = VAR_STRING;
13637 p = get_tv_string_chk(&argvars[0]);
13638 if (p == NULL)
13639 rettv->vval.v_string = NULL;
13640 else
13641 {
13642 p = vim_strsave(p);
13643 rettv->vval.v_string = p;
13644 if (p != NULL)
13645 shorten_dir(p);
13646 }
13647}
13648
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013649#ifdef FEAT_FLOAT
13650/*
13651 * "pow()" function
13652 */
13653 static void
13654f_pow(argvars, rettv)
13655 typval_T *argvars;
13656 typval_T *rettv;
13657{
13658 float_T fx, fy;
13659
13660 rettv->v_type = VAR_FLOAT;
13661 if (get_float_arg(argvars, &fx) == OK
13662 && get_float_arg(&argvars[1], &fy) == OK)
13663 rettv->vval.v_float = pow(fx, fy);
13664 else
13665 rettv->vval.v_float = 0.0;
13666}
13667#endif
13668
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013670 * "prevnonblank()" function
13671 */
13672 static void
13673f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013674 typval_T *argvars;
13675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676{
13677 linenr_T lnum;
13678
13679 lnum = get_tv_lnum(argvars);
13680 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13681 lnum = 0;
13682 else
13683 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13684 --lnum;
13685 rettv->vval.v_number = lnum;
13686}
13687
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013688#ifdef HAVE_STDARG_H
13689/* This dummy va_list is here because:
13690 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13691 * - locally in the function results in a "used before set" warning
13692 * - using va_start() to initialize it gives "function with fixed args" error */
13693static va_list ap;
13694#endif
13695
Bram Moolenaar8c711452005-01-14 21:53:12 +000013696/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013697 * "printf()" function
13698 */
13699 static void
13700f_printf(argvars, rettv)
13701 typval_T *argvars;
13702 typval_T *rettv;
13703{
13704 rettv->v_type = VAR_STRING;
13705 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013706#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013707 {
13708 char_u buf[NUMBUFLEN];
13709 int len;
13710 char_u *s;
13711 int saved_did_emsg = did_emsg;
13712 char *fmt;
13713
13714 /* Get the required length, allocate the buffer and do it for real. */
13715 did_emsg = FALSE;
13716 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013717 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013718 if (!did_emsg)
13719 {
13720 s = alloc(len + 1);
13721 if (s != NULL)
13722 {
13723 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013724 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013725 }
13726 }
13727 did_emsg |= saved_did_emsg;
13728 }
13729#endif
13730}
13731
13732/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013733 * "pumvisible()" function
13734 */
13735/*ARGSUSED*/
13736 static void
13737f_pumvisible(argvars, rettv)
13738 typval_T *argvars;
13739 typval_T *rettv;
13740{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013741#ifdef FEAT_INS_EXPAND
13742 if (pum_visible())
13743 rettv->vval.v_number = 1;
13744#endif
13745}
13746
13747/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013748 * "range()" function
13749 */
13750 static void
13751f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 typval_T *argvars;
13753 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013754{
13755 long start;
13756 long end;
13757 long stride = 1;
13758 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013759 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013762 if (argvars[1].v_type == VAR_UNKNOWN)
13763 {
13764 end = start - 1;
13765 start = 0;
13766 }
13767 else
13768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013772 }
13773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 if (error)
13775 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013776 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013777 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013778 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013779 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013780 else
13781 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013782 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013783 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013784 if (list_append_number(rettv->vval.v_list,
13785 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013786 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013787 }
13788}
13789
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013790/*
13791 * "readfile()" function
13792 */
13793 static void
13794f_readfile(argvars, rettv)
13795 typval_T *argvars;
13796 typval_T *rettv;
13797{
13798 int binary = FALSE;
13799 char_u *fname;
13800 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013801 listitem_T *li;
13802#define FREAD_SIZE 200 /* optimized for text lines */
13803 char_u buf[FREAD_SIZE];
13804 int readlen; /* size of last fread() */
13805 int buflen; /* nr of valid chars in buf[] */
13806 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13807 int tolist; /* first byte in buf[] still to be put in list */
13808 int chop; /* how many CR to chop off */
13809 char_u *prev = NULL; /* previously read bytes, if any */
13810 int prevlen = 0; /* length of "prev" if not NULL */
13811 char_u *s;
13812 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013813 long maxline = MAXLNUM;
13814 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013815
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013816 if (argvars[1].v_type != VAR_UNKNOWN)
13817 {
13818 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13819 binary = TRUE;
13820 if (argvars[2].v_type != VAR_UNKNOWN)
13821 maxline = get_tv_number(&argvars[2]);
13822 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013823
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013824 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013825 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013826
13827 /* Always open the file in binary mode, library functions have a mind of
13828 * their own about CR-LF conversion. */
13829 fname = get_tv_string(&argvars[0]);
13830 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13831 {
13832 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13833 return;
13834 }
13835
13836 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013837 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013838 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013839 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013840 buflen = filtd + readlen;
13841 tolist = 0;
13842 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13843 {
13844 if (buf[filtd] == '\n' || readlen <= 0)
13845 {
13846 /* Only when in binary mode add an empty list item when the
13847 * last line ends in a '\n'. */
13848 if (!binary && readlen == 0 && filtd == 0)
13849 break;
13850
13851 /* Found end-of-line or end-of-file: add a text line to the
13852 * list. */
13853 chop = 0;
13854 if (!binary)
13855 while (filtd - chop - 1 >= tolist
13856 && buf[filtd - chop - 1] == '\r')
13857 ++chop;
13858 len = filtd - tolist - chop;
13859 if (prev == NULL)
13860 s = vim_strnsave(buf + tolist, len);
13861 else
13862 {
13863 s = alloc((unsigned)(prevlen + len + 1));
13864 if (s != NULL)
13865 {
13866 mch_memmove(s, prev, prevlen);
13867 vim_free(prev);
13868 prev = NULL;
13869 mch_memmove(s + prevlen, buf + tolist, len);
13870 s[prevlen + len] = NUL;
13871 }
13872 }
13873 tolist = filtd + 1;
13874
13875 li = listitem_alloc();
13876 if (li == NULL)
13877 {
13878 vim_free(s);
13879 break;
13880 }
13881 li->li_tv.v_type = VAR_STRING;
13882 li->li_tv.v_lock = 0;
13883 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013884 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013885
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013886 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013887 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013888 if (readlen <= 0)
13889 break;
13890 }
13891 else if (buf[filtd] == NUL)
13892 buf[filtd] = '\n';
13893 }
13894 if (readlen <= 0)
13895 break;
13896
13897 if (tolist == 0)
13898 {
13899 /* "buf" is full, need to move text to an allocated buffer */
13900 if (prev == NULL)
13901 {
13902 prev = vim_strnsave(buf, buflen);
13903 prevlen = buflen;
13904 }
13905 else
13906 {
13907 s = alloc((unsigned)(prevlen + buflen));
13908 if (s != NULL)
13909 {
13910 mch_memmove(s, prev, prevlen);
13911 mch_memmove(s + prevlen, buf, buflen);
13912 vim_free(prev);
13913 prev = s;
13914 prevlen += buflen;
13915 }
13916 }
13917 filtd = 0;
13918 }
13919 else
13920 {
13921 mch_memmove(buf, buf + tolist, buflen - tolist);
13922 filtd -= tolist;
13923 }
13924 }
13925
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013926 /*
13927 * For a negative line count use only the lines at the end of the file,
13928 * free the rest.
13929 */
13930 if (maxline < 0)
13931 while (cnt > -maxline)
13932 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013933 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013934 --cnt;
13935 }
13936
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013937 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013938 fclose(fd);
13939}
13940
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013941#if defined(FEAT_RELTIME)
13942static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13943
13944/*
13945 * Convert a List to proftime_T.
13946 * Return FAIL when there is something wrong.
13947 */
13948 static int
13949list2proftime(arg, tm)
13950 typval_T *arg;
13951 proftime_T *tm;
13952{
13953 long n1, n2;
13954 int error = FALSE;
13955
13956 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13957 || arg->vval.v_list->lv_len != 2)
13958 return FAIL;
13959 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13960 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13961# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013962 tm->HighPart = n1;
13963 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013964# else
13965 tm->tv_sec = n1;
13966 tm->tv_usec = n2;
13967# endif
13968 return error ? FAIL : OK;
13969}
13970#endif /* FEAT_RELTIME */
13971
13972/*
13973 * "reltime()" function
13974 */
13975 static void
13976f_reltime(argvars, rettv)
13977 typval_T *argvars;
13978 typval_T *rettv;
13979{
13980#ifdef FEAT_RELTIME
13981 proftime_T res;
13982 proftime_T start;
13983
13984 if (argvars[0].v_type == VAR_UNKNOWN)
13985 {
13986 /* No arguments: get current time. */
13987 profile_start(&res);
13988 }
13989 else if (argvars[1].v_type == VAR_UNKNOWN)
13990 {
13991 if (list2proftime(&argvars[0], &res) == FAIL)
13992 return;
13993 profile_end(&res);
13994 }
13995 else
13996 {
13997 /* Two arguments: compute the difference. */
13998 if (list2proftime(&argvars[0], &start) == FAIL
13999 || list2proftime(&argvars[1], &res) == FAIL)
14000 return;
14001 profile_sub(&res, &start);
14002 }
14003
14004 if (rettv_list_alloc(rettv) == OK)
14005 {
14006 long n1, n2;
14007
14008# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014009 n1 = res.HighPart;
14010 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014011# else
14012 n1 = res.tv_sec;
14013 n2 = res.tv_usec;
14014# endif
14015 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14016 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14017 }
14018#endif
14019}
14020
14021/*
14022 * "reltimestr()" function
14023 */
14024 static void
14025f_reltimestr(argvars, rettv)
14026 typval_T *argvars;
14027 typval_T *rettv;
14028{
14029#ifdef FEAT_RELTIME
14030 proftime_T tm;
14031#endif
14032
14033 rettv->v_type = VAR_STRING;
14034 rettv->vval.v_string = NULL;
14035#ifdef FEAT_RELTIME
14036 if (list2proftime(&argvars[0], &tm) == OK)
14037 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14038#endif
14039}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014040
Bram Moolenaar0d660222005-01-07 21:51:51 +000014041#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14042static void make_connection __ARGS((void));
14043static int check_connection __ARGS((void));
14044
14045 static void
14046make_connection()
14047{
14048 if (X_DISPLAY == NULL
14049# ifdef FEAT_GUI
14050 && !gui.in_use
14051# endif
14052 )
14053 {
14054 x_force_connect = TRUE;
14055 setup_term_clip();
14056 x_force_connect = FALSE;
14057 }
14058}
14059
14060 static int
14061check_connection()
14062{
14063 make_connection();
14064 if (X_DISPLAY == NULL)
14065 {
14066 EMSG(_("E240: No connection to Vim server"));
14067 return FAIL;
14068 }
14069 return OK;
14070}
14071#endif
14072
14073#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014074static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075
14076 static void
14077remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 typval_T *argvars;
14079 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014080 int expr;
14081{
14082 char_u *server_name;
14083 char_u *keys;
14084 char_u *r = NULL;
14085 char_u buf[NUMBUFLEN];
14086# ifdef WIN32
14087 HWND w;
14088# else
14089 Window w;
14090# endif
14091
14092 if (check_restricted() || check_secure())
14093 return;
14094
14095# ifdef FEAT_X11
14096 if (check_connection() == FAIL)
14097 return;
14098# endif
14099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100 server_name = get_tv_string_chk(&argvars[0]);
14101 if (server_name == NULL)
14102 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103 keys = get_tv_string_buf(&argvars[1], buf);
14104# ifdef WIN32
14105 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14106# else
14107 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14108 < 0)
14109# endif
14110 {
14111 if (r != NULL)
14112 EMSG(r); /* sending worked but evaluation failed */
14113 else
14114 EMSG2(_("E241: Unable to send to %s"), server_name);
14115 return;
14116 }
14117
14118 rettv->vval.v_string = r;
14119
14120 if (argvars[2].v_type != VAR_UNKNOWN)
14121 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014123 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014125
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014126 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 v.di_tv.v_type = VAR_STRING;
14128 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014129 idvar = get_tv_string_chk(&argvars[2]);
14130 if (idvar != NULL)
14131 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014133 }
14134}
14135#endif
14136
14137/*
14138 * "remote_expr()" function
14139 */
14140/*ARGSUSED*/
14141 static void
14142f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014145{
14146 rettv->v_type = VAR_STRING;
14147 rettv->vval.v_string = NULL;
14148#ifdef FEAT_CLIENTSERVER
14149 remote_common(argvars, rettv, TRUE);
14150#endif
14151}
14152
14153/*
14154 * "remote_foreground()" function
14155 */
14156/*ARGSUSED*/
14157 static void
14158f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162#ifdef FEAT_CLIENTSERVER
14163# ifdef WIN32
14164 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 {
14166 char_u *server_name = get_tv_string_chk(&argvars[0]);
14167
14168 if (server_name != NULL)
14169 serverForeground(server_name);
14170 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014171# else
14172 /* Send a foreground() expression to the server. */
14173 argvars[1].v_type = VAR_STRING;
14174 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14175 argvars[2].v_type = VAR_UNKNOWN;
14176 remote_common(argvars, rettv, TRUE);
14177 vim_free(argvars[1].vval.v_string);
14178# endif
14179#endif
14180}
14181
14182/*ARGSUSED*/
14183 static void
14184f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 typval_T *argvars;
14186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014187{
14188#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014190 char_u *s = NULL;
14191# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014192 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195
14196 if (check_restricted() || check_secure())
14197 {
14198 rettv->vval.v_number = -1;
14199 return;
14200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 serverid = get_tv_string_chk(&argvars[0]);
14202 if (serverid == NULL)
14203 {
14204 rettv->vval.v_number = -1;
14205 return; /* type error; errmsg already given */
14206 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014207# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014208 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014209 if (n == 0)
14210 rettv->vval.v_number = -1;
14211 else
14212 {
14213 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14214 rettv->vval.v_number = (s != NULL);
14215 }
14216# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014217 if (check_connection() == FAIL)
14218 return;
14219
14220 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222# endif
14223
14224 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 char_u *retvar;
14227
Bram Moolenaar33570922005-01-25 22:26:29 +000014228 v.di_tv.v_type = VAR_STRING;
14229 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 retvar = get_tv_string_chk(&argvars[1]);
14231 if (retvar != NULL)
14232 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014233 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234 }
14235#else
14236 rettv->vval.v_number = -1;
14237#endif
14238}
14239
14240/*ARGSUSED*/
14241 static void
14242f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014243 typval_T *argvars;
14244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014245{
14246 char_u *r = NULL;
14247
14248#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 char_u *serverid = get_tv_string_chk(&argvars[0]);
14250
14251 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014252 {
14253# ifdef WIN32
14254 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014255 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014257 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258 if (n != 0)
14259 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14260 if (r == NULL)
14261# else
14262 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014264# endif
14265 EMSG(_("E277: Unable to read a server reply"));
14266 }
14267#endif
14268 rettv->v_type = VAR_STRING;
14269 rettv->vval.v_string = r;
14270}
14271
14272/*
14273 * "remote_send()" function
14274 */
14275/*ARGSUSED*/
14276 static void
14277f_remote_send(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 rettv->v_type = VAR_STRING;
14282 rettv->vval.v_string = NULL;
14283#ifdef FEAT_CLIENTSERVER
14284 remote_common(argvars, rettv, FALSE);
14285#endif
14286}
14287
14288/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014289 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014290 */
14291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 typval_T *argvars;
14294 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014295{
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 list_T *l;
14297 listitem_T *item, *item2;
14298 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014299 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014300 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014301 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014302 dict_T *d;
14303 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014304
Bram Moolenaar8c711452005-01-14 21:53:12 +000014305 if (argvars[0].v_type == VAR_DICT)
14306 {
14307 if (argvars[2].v_type != VAR_UNKNOWN)
14308 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014309 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014310 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 key = get_tv_string_chk(&argvars[1]);
14313 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 di = dict_find(d, key, -1);
14316 if (di == NULL)
14317 EMSG2(_(e_dictkey), key);
14318 else
14319 {
14320 *rettv = di->di_tv;
14321 init_tv(&di->di_tv);
14322 dictitem_remove(d, di);
14323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014324 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014325 }
14326 }
14327 else if (argvars[0].v_type != VAR_LIST)
14328 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014329 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014330 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332 int error = FALSE;
14333
14334 idx = get_tv_number_chk(&argvars[1], &error);
14335 if (error)
14336 ; /* type error: do nothing, errmsg already given */
14337 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014338 EMSGN(_(e_listidx), idx);
14339 else
14340 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014341 if (argvars[2].v_type == VAR_UNKNOWN)
14342 {
14343 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014344 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014345 *rettv = item->li_tv;
14346 vim_free(item);
14347 }
14348 else
14349 {
14350 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 end = get_tv_number_chk(&argvars[2], &error);
14352 if (error)
14353 ; /* type error: do nothing */
14354 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014355 EMSGN(_(e_listidx), end);
14356 else
14357 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014358 int cnt = 0;
14359
14360 for (li = item; li != NULL; li = li->li_next)
14361 {
14362 ++cnt;
14363 if (li == item2)
14364 break;
14365 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014366 if (li == NULL) /* didn't find "item2" after "item" */
14367 EMSG(_(e_invrange));
14368 else
14369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014370 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014371 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014372 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014373 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014374 l->lv_first = item;
14375 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014376 item->li_prev = NULL;
14377 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014378 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014379 }
14380 }
14381 }
14382 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014383 }
14384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385}
14386
14387/*
14388 * "rename({from}, {to})" function
14389 */
14390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 typval_T *argvars;
14393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394{
14395 char_u buf[NUMBUFLEN];
14396
14397 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14401 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
14404/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014405 * "repeat()" function
14406 */
14407/*ARGSUSED*/
14408 static void
14409f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 typval_T *argvars;
14411 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014412{
14413 char_u *p;
14414 int n;
14415 int slen;
14416 int len;
14417 char_u *r;
14418 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014419
14420 n = get_tv_number(&argvars[1]);
14421 if (argvars[0].v_type == VAR_LIST)
14422 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014423 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014424 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014425 if (list_extend(rettv->vval.v_list,
14426 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014428 }
14429 else
14430 {
14431 p = get_tv_string(&argvars[0]);
14432 rettv->v_type = VAR_STRING;
14433 rettv->vval.v_string = NULL;
14434
14435 slen = (int)STRLEN(p);
14436 len = slen * n;
14437 if (len <= 0)
14438 return;
14439
14440 r = alloc(len + 1);
14441 if (r != NULL)
14442 {
14443 for (i = 0; i < n; i++)
14444 mch_memmove(r + i * slen, p, (size_t)slen);
14445 r[len] = NUL;
14446 }
14447
14448 rettv->vval.v_string = r;
14449 }
14450}
14451
14452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 * "resolve()" function
14454 */
14455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 typval_T *argvars;
14458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459{
14460 char_u *p;
14461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463#ifdef FEAT_SHORTCUT
14464 {
14465 char_u *v = NULL;
14466
14467 v = mch_resolve_shortcut(p);
14468 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 }
14473#else
14474# ifdef HAVE_READLINK
14475 {
14476 char_u buf[MAXPATHL + 1];
14477 char_u *cpy;
14478 int len;
14479 char_u *remain = NULL;
14480 char_u *q;
14481 int is_relative_to_current = FALSE;
14482 int has_trailing_pathsep = FALSE;
14483 int limit = 100;
14484
14485 p = vim_strsave(p);
14486
14487 if (p[0] == '.' && (vim_ispathsep(p[1])
14488 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14489 is_relative_to_current = TRUE;
14490
14491 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014492 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 has_trailing_pathsep = TRUE;
14494
14495 q = getnextcomp(p);
14496 if (*q != NUL)
14497 {
14498 /* Separate the first path component in "p", and keep the
14499 * remainder (beginning with the path separator). */
14500 remain = vim_strsave(q - 1);
14501 q[-1] = NUL;
14502 }
14503
14504 for (;;)
14505 {
14506 for (;;)
14507 {
14508 len = readlink((char *)p, (char *)buf, MAXPATHL);
14509 if (len <= 0)
14510 break;
14511 buf[len] = NUL;
14512
14513 if (limit-- == 0)
14514 {
14515 vim_free(p);
14516 vim_free(remain);
14517 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 goto fail;
14520 }
14521
14522 /* Ensure that the result will have a trailing path separator
14523 * if the argument has one. */
14524 if (remain == NULL && has_trailing_pathsep)
14525 add_pathsep(buf);
14526
14527 /* Separate the first path component in the link value and
14528 * concatenate the remainders. */
14529 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14530 if (*q != NUL)
14531 {
14532 if (remain == NULL)
14533 remain = vim_strsave(q - 1);
14534 else
14535 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014536 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 if (cpy != NULL)
14538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 vim_free(remain);
14540 remain = cpy;
14541 }
14542 }
14543 q[-1] = NUL;
14544 }
14545
14546 q = gettail(p);
14547 if (q > p && *q == NUL)
14548 {
14549 /* Ignore trailing path separator. */
14550 q[-1] = NUL;
14551 q = gettail(p);
14552 }
14553 if (q > p && !mch_isFullName(buf))
14554 {
14555 /* symlink is relative to directory of argument */
14556 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14557 if (cpy != NULL)
14558 {
14559 STRCPY(cpy, p);
14560 STRCPY(gettail(cpy), buf);
14561 vim_free(p);
14562 p = cpy;
14563 }
14564 }
14565 else
14566 {
14567 vim_free(p);
14568 p = vim_strsave(buf);
14569 }
14570 }
14571
14572 if (remain == NULL)
14573 break;
14574
14575 /* Append the first path component of "remain" to "p". */
14576 q = getnextcomp(remain + 1);
14577 len = q - remain - (*q != NUL);
14578 cpy = vim_strnsave(p, STRLEN(p) + len);
14579 if (cpy != NULL)
14580 {
14581 STRNCAT(cpy, remain, len);
14582 vim_free(p);
14583 p = cpy;
14584 }
14585 /* Shorten "remain". */
14586 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014587 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 else
14589 {
14590 vim_free(remain);
14591 remain = NULL;
14592 }
14593 }
14594
14595 /* If the result is a relative path name, make it explicitly relative to
14596 * the current directory if and only if the argument had this form. */
14597 if (!vim_ispathsep(*p))
14598 {
14599 if (is_relative_to_current
14600 && *p != NUL
14601 && !(p[0] == '.'
14602 && (p[1] == NUL
14603 || vim_ispathsep(p[1])
14604 || (p[1] == '.'
14605 && (p[2] == NUL
14606 || vim_ispathsep(p[2]))))))
14607 {
14608 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014609 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 if (cpy != NULL)
14611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 vim_free(p);
14613 p = cpy;
14614 }
14615 }
14616 else if (!is_relative_to_current)
14617 {
14618 /* Strip leading "./". */
14619 q = p;
14620 while (q[0] == '.' && vim_ispathsep(q[1]))
14621 q += 2;
14622 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014623 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 }
14625 }
14626
14627 /* Ensure that the result will have no trailing path separator
14628 * if the argument had none. But keep "/" or "//". */
14629 if (!has_trailing_pathsep)
14630 {
14631 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014632 if (after_pathsep(p, q))
14633 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 }
14635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 }
14638# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640# endif
14641#endif
14642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644
14645#ifdef HAVE_READLINK
14646fail:
14647#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649}
14650
14651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 */
14654 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014656 typval_T *argvars;
14657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
Bram Moolenaar33570922005-01-25 22:26:29 +000014659 list_T *l;
14660 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662 if (argvars[0].v_type != VAR_LIST)
14663 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014664 else if ((l = argvars[0].vval.v_list) != NULL
14665 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666 {
14667 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014668 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014669 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670 while (li != NULL)
14671 {
14672 ni = li->li_prev;
14673 list_append(l, li);
14674 li = ni;
14675 }
14676 rettv->vval.v_list = l;
14677 rettv->v_type = VAR_LIST;
14678 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014679 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681}
14682
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014683#define SP_NOMOVE 0x01 /* don't move cursor */
14684#define SP_REPEAT 0x02 /* repeat to find outer pair */
14685#define SP_RETCOUNT 0x04 /* return matchcount */
14686#define SP_SETPCMARK 0x08 /* set previous context mark */
14687#define SP_START 0x10 /* accept match at start position */
14688#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14689#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014690
Bram Moolenaar33570922005-01-25 22:26:29 +000014691static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692
14693/*
14694 * Get flags for a search function.
14695 * Possibly sets "p_ws".
14696 * Returns BACKWARD, FORWARD or zero (for an error).
14697 */
14698 static int
14699get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014700 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701 int *flagsp;
14702{
14703 int dir = FORWARD;
14704 char_u *flags;
14705 char_u nbuf[NUMBUFLEN];
14706 int mask;
14707
14708 if (varp->v_type != VAR_UNKNOWN)
14709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014710 flags = get_tv_string_buf_chk(varp, nbuf);
14711 if (flags == NULL)
14712 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 while (*flags != NUL)
14714 {
14715 switch (*flags)
14716 {
14717 case 'b': dir = BACKWARD; break;
14718 case 'w': p_ws = TRUE; break;
14719 case 'W': p_ws = FALSE; break;
14720 default: mask = 0;
14721 if (flagsp != NULL)
14722 switch (*flags)
14723 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014724 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014725 case 'e': mask = SP_END; break;
14726 case 'm': mask = SP_RETCOUNT; break;
14727 case 'n': mask = SP_NOMOVE; break;
14728 case 'p': mask = SP_SUBPAT; break;
14729 case 'r': mask = SP_REPEAT; break;
14730 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 }
14732 if (mask == 0)
14733 {
14734 EMSG2(_(e_invarg2), flags);
14735 dir = 0;
14736 }
14737 else
14738 *flagsp |= mask;
14739 }
14740 if (dir == 0)
14741 break;
14742 ++flags;
14743 }
14744 }
14745 return dir;
14746}
14747
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014749 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014751 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014752search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014753 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014754 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014755 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014757 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 char_u *pat;
14759 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014760 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 int save_p_ws = p_ws;
14762 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014763 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014764 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014765 proftime_T tm;
14766#ifdef FEAT_RELTIME
14767 long time_limit = 0;
14768#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014769 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014770 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014773 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014774 if (dir == 0)
14775 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014776 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014777 if (flags & SP_START)
14778 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014779 if (flags & SP_END)
14780 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014781
Bram Moolenaar76929292008-01-06 19:07:36 +000014782 /* Optional arguments: line number to stop searching and timeout. */
14783 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014784 {
14785 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14786 if (lnum_stop < 0)
14787 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014788#ifdef FEAT_RELTIME
14789 if (argvars[3].v_type != VAR_UNKNOWN)
14790 {
14791 time_limit = get_tv_number_chk(&argvars[3], NULL);
14792 if (time_limit < 0)
14793 goto theend;
14794 }
14795#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014796 }
14797
Bram Moolenaar76929292008-01-06 19:07:36 +000014798#ifdef FEAT_RELTIME
14799 /* Set the time limit, if there is one. */
14800 profile_setlimit(time_limit, &tm);
14801#endif
14802
Bram Moolenaar231334e2005-07-25 20:46:57 +000014803 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014804 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014805 * Check to make sure only those flags are set.
14806 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14807 * flags cannot be set. Check for that condition also.
14808 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014809 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014810 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014812 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014813 goto theend;
14814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014816 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014817 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014818 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014819 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014821 if (flags & SP_SUBPAT)
14822 retval = subpatnum;
14823 else
14824 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014825 if (flags & SP_SETPCMARK)
14826 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014828 if (match_pos != NULL)
14829 {
14830 /* Store the match cursor position */
14831 match_pos->lnum = pos.lnum;
14832 match_pos->col = pos.col + 1;
14833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 /* "/$" will put the cursor after the end of the line, may need to
14835 * correct that here */
14836 check_cursor();
14837 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014838
14839 /* If 'n' flag is used: restore cursor position. */
14840 if (flags & SP_NOMOVE)
14841 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014842 else
14843 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014844theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014846
14847 return retval;
14848}
14849
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014850#ifdef FEAT_FLOAT
14851/*
14852 * "round({float})" function
14853 */
14854 static void
14855f_round(argvars, rettv)
14856 typval_T *argvars;
14857 typval_T *rettv;
14858{
14859 float_T f;
14860
14861 rettv->v_type = VAR_FLOAT;
14862 if (get_float_arg(argvars, &f) == OK)
14863 /* round() is not in C90, use ceil() or floor() instead. */
14864 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14865 else
14866 rettv->vval.v_float = 0.0;
14867}
14868#endif
14869
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014870/*
14871 * "search()" function
14872 */
14873 static void
14874f_search(argvars, rettv)
14875 typval_T *argvars;
14876 typval_T *rettv;
14877{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014878 int flags = 0;
14879
14880 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881}
14882
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014884 * "searchdecl()" function
14885 */
14886 static void
14887f_searchdecl(argvars, rettv)
14888 typval_T *argvars;
14889 typval_T *rettv;
14890{
14891 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014892 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014893 int error = FALSE;
14894 char_u *name;
14895
14896 rettv->vval.v_number = 1; /* default: FAIL */
14897
14898 name = get_tv_string_chk(&argvars[0]);
14899 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014900 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014901 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014902 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14903 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14904 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014905 if (!error && name != NULL)
14906 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014907 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014908}
14909
14910/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014911 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014913 static int
14914searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014915 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014916 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917{
14918 char_u *spat, *mpat, *epat;
14919 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 int dir;
14922 int flags = 0;
14923 char_u nbuf1[NUMBUFLEN];
14924 char_u nbuf2[NUMBUFLEN];
14925 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014926 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014927 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014928 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 spat = get_tv_string_chk(&argvars[0]);
14932 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14933 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14934 if (spat == NULL || mpat == NULL || epat == NULL)
14935 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 /* Handle the optional fourth argument: flags */
14938 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014939 if (dir == 0)
14940 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014941
14942 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014943 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14944 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014945 if ((flags & (SP_END | SP_SUBPAT)) != 0
14946 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014947 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014948 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014949 goto theend;
14950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014952 /* Using 'r' implies 'W', otherwise it doesn't work. */
14953 if (flags & SP_REPEAT)
14954 p_ws = FALSE;
14955
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014956 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014957 if (argvars[3].v_type == VAR_UNKNOWN
14958 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 skip = (char_u *)"";
14960 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014962 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014963 if (argvars[5].v_type != VAR_UNKNOWN)
14964 {
14965 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14966 if (lnum_stop < 0)
14967 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014968#ifdef FEAT_RELTIME
14969 if (argvars[6].v_type != VAR_UNKNOWN)
14970 {
14971 time_limit = get_tv_number_chk(&argvars[6], NULL);
14972 if (time_limit < 0)
14973 goto theend;
14974 }
14975#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014976 }
14977 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978 if (skip == NULL)
14979 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014981 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014982 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014983
14984theend:
14985 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014986
14987 return retval;
14988}
14989
14990/*
14991 * "searchpair()" function
14992 */
14993 static void
14994f_searchpair(argvars, rettv)
14995 typval_T *argvars;
14996 typval_T *rettv;
14997{
14998 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14999}
15000
15001/*
15002 * "searchpairpos()" function
15003 */
15004 static void
15005f_searchpairpos(argvars, rettv)
15006 typval_T *argvars;
15007 typval_T *rettv;
15008{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015009 pos_T match_pos;
15010 int lnum = 0;
15011 int col = 0;
15012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015014 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015015
15016 if (searchpair_cmn(argvars, &match_pos) > 0)
15017 {
15018 lnum = match_pos.lnum;
15019 col = match_pos.col;
15020 }
15021
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015022 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15023 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015024}
15025
15026/*
15027 * Search for a start/middle/end thing.
15028 * Used by searchpair(), see its documentation for the details.
15029 * Returns 0 or -1 for no match,
15030 */
15031 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015032do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15033 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015034 char_u *spat; /* start pattern */
15035 char_u *mpat; /* middle pattern */
15036 char_u *epat; /* end pattern */
15037 int dir; /* BACKWARD or FORWARD */
15038 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015039 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015040 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015041 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015042 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015043{
15044 char_u *save_cpo;
15045 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15046 long retval = 0;
15047 pos_T pos;
15048 pos_T firstpos;
15049 pos_T foundpos;
15050 pos_T save_cursor;
15051 pos_T save_pos;
15052 int n;
15053 int r;
15054 int nest = 1;
15055 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015056 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015057 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015058
15059 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15060 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015061 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015062
Bram Moolenaar76929292008-01-06 19:07:36 +000015063#ifdef FEAT_RELTIME
15064 /* Set the time limit, if there is one. */
15065 profile_setlimit(time_limit, &tm);
15066#endif
15067
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015068 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15069 * start/middle/end (pat3, for the top pair). */
15070 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15071 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15072 if (pat2 == NULL || pat3 == NULL)
15073 goto theend;
15074 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15075 if (*mpat == NUL)
15076 STRCPY(pat3, pat2);
15077 else
15078 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15079 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015080 if (flags & SP_START)
15081 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015082
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 save_cursor = curwin->w_cursor;
15084 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015085 clearpos(&firstpos);
15086 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 pat = pat3;
15088 for (;;)
15089 {
15090 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015091 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15093 /* didn't find it or found the first match again: FAIL */
15094 break;
15095
15096 if (firstpos.lnum == 0)
15097 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015098 if (equalpos(pos, foundpos))
15099 {
15100 /* Found the same position again. Can happen with a pattern that
15101 * has "\zs" at the end and searching backwards. Advance one
15102 * character and try again. */
15103 if (dir == BACKWARD)
15104 decl(&pos);
15105 else
15106 incl(&pos);
15107 }
15108 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015110 /* clear the start flag to avoid getting stuck here */
15111 options &= ~SEARCH_START;
15112
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 /* If the skip pattern matches, ignore this match. */
15114 if (*skip != NUL)
15115 {
15116 save_pos = curwin->w_cursor;
15117 curwin->w_cursor = pos;
15118 r = eval_to_bool(skip, &err, NULL, FALSE);
15119 curwin->w_cursor = save_pos;
15120 if (err)
15121 {
15122 /* Evaluating {skip} caused an error, break here. */
15123 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015124 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 break;
15126 }
15127 if (r)
15128 continue;
15129 }
15130
15131 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15132 {
15133 /* Found end when searching backwards or start when searching
15134 * forward: nested pair. */
15135 ++nest;
15136 pat = pat2; /* nested, don't search for middle */
15137 }
15138 else
15139 {
15140 /* Found end when searching forward or start when searching
15141 * backward: end of (nested) pair; or found middle in outer pair. */
15142 if (--nest == 1)
15143 pat = pat3; /* outer level, search for middle */
15144 }
15145
15146 if (nest == 0)
15147 {
15148 /* Found the match: return matchcount or line number. */
15149 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015150 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015152 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015153 if (flags & SP_SETPCMARK)
15154 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 curwin->w_cursor = pos;
15156 if (!(flags & SP_REPEAT))
15157 break;
15158 nest = 1; /* search for next unmatched */
15159 }
15160 }
15161
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015162 if (match_pos != NULL)
15163 {
15164 /* Store the match cursor position */
15165 match_pos->lnum = curwin->w_cursor.lnum;
15166 match_pos->col = curwin->w_cursor.col + 1;
15167 }
15168
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015170 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 curwin->w_cursor = save_cursor;
15172
15173theend:
15174 vim_free(pat2);
15175 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015176 if (p_cpo == empty_option)
15177 p_cpo = save_cpo;
15178 else
15179 /* Darn, evaluating the {skip} expression changed the value. */
15180 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015181
15182 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183}
15184
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015185/*
15186 * "searchpos()" function
15187 */
15188 static void
15189f_searchpos(argvars, rettv)
15190 typval_T *argvars;
15191 typval_T *rettv;
15192{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015193 pos_T match_pos;
15194 int lnum = 0;
15195 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015196 int n;
15197 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015198
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015199 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015200 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015201
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015202 n = search_cmn(argvars, &match_pos, &flags);
15203 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015204 {
15205 lnum = match_pos.lnum;
15206 col = match_pos.col;
15207 }
15208
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015209 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15210 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015211 if (flags & SP_SUBPAT)
15212 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015213}
15214
15215
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216/*ARGSUSED*/
15217 static void
15218f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 typval_T *argvars;
15220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222#ifdef FEAT_CLIENTSERVER
15223 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 char_u *server = get_tv_string_chk(&argvars[0]);
15225 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 if (server == NULL || reply == NULL)
15229 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230 if (check_restricted() || check_secure())
15231 return;
15232# ifdef FEAT_X11
15233 if (check_connection() == FAIL)
15234 return;
15235# endif
15236
15237 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239 EMSG(_("E258: Unable to send to client"));
15240 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242 rettv->vval.v_number = 0;
15243#else
15244 rettv->vval.v_number = -1;
15245#endif
15246}
15247
15248/*ARGSUSED*/
15249 static void
15250f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *argvars;
15252 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253{
15254 char_u *r = NULL;
15255
15256#ifdef FEAT_CLIENTSERVER
15257# ifdef WIN32
15258 r = serverGetVimNames();
15259# else
15260 make_connection();
15261 if (X_DISPLAY != NULL)
15262 r = serverGetVimNames(X_DISPLAY);
15263# endif
15264#endif
15265 rettv->v_type = VAR_STRING;
15266 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267}
15268
15269/*
15270 * "setbufvar()" function
15271 */
15272/*ARGSUSED*/
15273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015275 typval_T *argvars;
15276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277{
15278 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015281 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 char_u nbuf[NUMBUFLEN];
15283
15284 if (check_restricted() || check_secure())
15285 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15287 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 varp = &argvars[2];
15290
15291 if (buf != NULL && varname != NULL && varp != NULL)
15292 {
15293 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295
15296 if (*varname == '&')
15297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015298 long numval;
15299 char_u *strval;
15300 int error = FALSE;
15301
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015303 numval = get_tv_number_chk(varp, &error);
15304 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 if (!error && strval != NULL)
15306 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 }
15308 else
15309 {
15310 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15311 if (bufvarname != NULL)
15312 {
15313 STRCPY(bufvarname, "b:");
15314 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015315 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 vim_free(bufvarname);
15317 }
15318 }
15319
15320 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323}
15324
15325/*
15326 * "setcmdpos()" function
15327 */
15328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015329f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015330 typval_T *argvars;
15331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 int pos = (int)get_tv_number(&argvars[0]) - 1;
15334
15335 if (pos >= 0)
15336 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337}
15338
15339/*
15340 * "setline()" function
15341 */
15342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015343f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015344 typval_T *argvars;
15345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346{
15347 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015348 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015349 list_T *l = NULL;
15350 listitem_T *li = NULL;
15351 long added = 0;
15352 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015354 lnum = get_tv_lnum(&argvars[0]);
15355 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015357 l = argvars[1].vval.v_list;
15358 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015360 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015361 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015362
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015363 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015364 for (;;)
15365 {
15366 if (l != NULL)
15367 {
15368 /* list argument, get next string */
15369 if (li == NULL)
15370 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015371 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015372 li = li->li_next;
15373 }
15374
15375 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015376 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015377 break;
15378 if (lnum <= curbuf->b_ml.ml_line_count)
15379 {
15380 /* existing line, replace it */
15381 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15382 {
15383 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015384 if (lnum == curwin->w_cursor.lnum)
15385 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015386 rettv->vval.v_number = 0; /* OK */
15387 }
15388 }
15389 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15390 {
15391 /* lnum is one past the last line, append the line */
15392 ++added;
15393 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15394 rettv->vval.v_number = 0; /* OK */
15395 }
15396
15397 if (l == NULL) /* only one string argument */
15398 break;
15399 ++lnum;
15400 }
15401
15402 if (added > 0)
15403 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404}
15405
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015406static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15407
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015409 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015410 */
15411/*ARGSUSED*/
15412 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015413set_qf_ll_list(wp, list_arg, action_arg, rettv)
15414 win_T *wp;
15415 typval_T *list_arg;
15416 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015417 typval_T *rettv;
15418{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015419#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015420 char_u *act;
15421 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015422#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015423
Bram Moolenaar2641f772005-03-25 21:58:17 +000015424 rettv->vval.v_number = -1;
15425
15426#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015427 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015428 EMSG(_(e_listreq));
15429 else
15430 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015431 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015432
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015433 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015434 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015435 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015436 if (act == NULL)
15437 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015438 if (*act == 'a' || *act == 'r')
15439 action = *act;
15440 }
15441
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015442 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015443 rettv->vval.v_number = 0;
15444 }
15445#endif
15446}
15447
15448/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015449 * "setloclist()" function
15450 */
15451/*ARGSUSED*/
15452 static void
15453f_setloclist(argvars, rettv)
15454 typval_T *argvars;
15455 typval_T *rettv;
15456{
15457 win_T *win;
15458
15459 rettv->vval.v_number = -1;
15460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015461 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015462 if (win != NULL)
15463 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15464}
15465
15466/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015467 * "setmatches()" function
15468 */
15469 static void
15470f_setmatches(argvars, rettv)
15471 typval_T *argvars;
15472 typval_T *rettv;
15473{
15474#ifdef FEAT_SEARCH_EXTRA
15475 list_T *l;
15476 listitem_T *li;
15477 dict_T *d;
15478
15479 rettv->vval.v_number = -1;
15480 if (argvars[0].v_type != VAR_LIST)
15481 {
15482 EMSG(_(e_listreq));
15483 return;
15484 }
15485 if ((l = argvars[0].vval.v_list) != NULL)
15486 {
15487
15488 /* To some extent make sure that we are dealing with a list from
15489 * "getmatches()". */
15490 li = l->lv_first;
15491 while (li != NULL)
15492 {
15493 if (li->li_tv.v_type != VAR_DICT
15494 || (d = li->li_tv.vval.v_dict) == NULL)
15495 {
15496 EMSG(_(e_invarg));
15497 return;
15498 }
15499 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15500 && dict_find(d, (char_u *)"pattern", -1) != NULL
15501 && dict_find(d, (char_u *)"priority", -1) != NULL
15502 && dict_find(d, (char_u *)"id", -1) != NULL))
15503 {
15504 EMSG(_(e_invarg));
15505 return;
15506 }
15507 li = li->li_next;
15508 }
15509
15510 clear_matches(curwin);
15511 li = l->lv_first;
15512 while (li != NULL)
15513 {
15514 d = li->li_tv.vval.v_dict;
15515 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15516 get_dict_string(d, (char_u *)"pattern", FALSE),
15517 (int)get_dict_number(d, (char_u *)"priority"),
15518 (int)get_dict_number(d, (char_u *)"id"));
15519 li = li->li_next;
15520 }
15521 rettv->vval.v_number = 0;
15522 }
15523#endif
15524}
15525
15526/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015527 * "setpos()" function
15528 */
15529/*ARGSUSED*/
15530 static void
15531f_setpos(argvars, rettv)
15532 typval_T *argvars;
15533 typval_T *rettv;
15534{
15535 pos_T pos;
15536 int fnum;
15537 char_u *name;
15538
Bram Moolenaar08250432008-02-13 11:42:46 +000015539 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015540 name = get_tv_string_chk(argvars);
15541 if (name != NULL)
15542 {
15543 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15544 {
15545 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015546 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015547 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015548 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015549 if (fnum == curbuf->b_fnum)
15550 {
15551 curwin->w_cursor = pos;
15552 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015553 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015554 }
15555 else
15556 EMSG(_(e_invarg));
15557 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015558 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15559 {
15560 /* set mark */
15561 if (setmark_pos(name[1], &pos, fnum) == OK)
15562 rettv->vval.v_number = 0;
15563 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015564 else
15565 EMSG(_(e_invarg));
15566 }
15567 }
15568}
15569
15570/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015571 * "setqflist()" function
15572 */
15573/*ARGSUSED*/
15574 static void
15575f_setqflist(argvars, rettv)
15576 typval_T *argvars;
15577 typval_T *rettv;
15578{
15579 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15580}
15581
15582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 * "setreg()" function
15584 */
15585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015587 typval_T *argvars;
15588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589{
15590 int regname;
15591 char_u *strregname;
15592 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015593 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 int append;
15595 char_u yank_type;
15596 long block_len;
15597
15598 block_len = -1;
15599 yank_type = MAUTO;
15600 append = FALSE;
15601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015602 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015603 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015605 if (strregname == NULL)
15606 return; /* type error; errmsg already given */
15607 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 if (regname == 0 || regname == '@')
15609 regname = '"';
15610 else if (regname == '=')
15611 return;
15612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015615 stropt = get_tv_string_chk(&argvars[2]);
15616 if (stropt == NULL)
15617 return; /* type error */
15618 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 switch (*stropt)
15620 {
15621 case 'a': case 'A': /* append */
15622 append = TRUE;
15623 break;
15624 case 'v': case 'c': /* character-wise selection */
15625 yank_type = MCHAR;
15626 break;
15627 case 'V': case 'l': /* line-wise selection */
15628 yank_type = MLINE;
15629 break;
15630#ifdef FEAT_VISUAL
15631 case 'b': case Ctrl_V: /* block-wise selection */
15632 yank_type = MBLOCK;
15633 if (VIM_ISDIGIT(stropt[1]))
15634 {
15635 ++stropt;
15636 block_len = getdigits(&stropt) - 1;
15637 --stropt;
15638 }
15639 break;
15640#endif
15641 }
15642 }
15643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015644 strval = get_tv_string_chk(&argvars[1]);
15645 if (strval != NULL)
15646 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015648 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649}
15650
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015651/*
15652 * "settabwinvar()" function
15653 */
15654 static void
15655f_settabwinvar(argvars, rettv)
15656 typval_T *argvars;
15657 typval_T *rettv;
15658{
15659 setwinvar(argvars, rettv, 1);
15660}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661
15662/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015663 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015666f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015667 typval_T *argvars;
15668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015670 setwinvar(argvars, rettv, 0);
15671}
15672
15673/*
15674 * "setwinvar()" and "settabwinvar()" functions
15675 */
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015676/*ARGSUSED*/
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015677 static void
15678setwinvar(argvars, rettv, off)
15679 typval_T *argvars;
15680 typval_T *rettv;
15681 int off;
15682{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 win_T *win;
15684#ifdef FEAT_WINDOWS
15685 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015686 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687#endif
15688 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015689 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015691 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692
15693 if (check_restricted() || check_secure())
15694 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015695
15696#ifdef FEAT_WINDOWS
15697 if (off == 1)
15698 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15699 else
15700 tp = curtab;
15701#endif
15702 win = find_win_by_nr(&argvars[off], tp);
15703 varname = get_tv_string_chk(&argvars[off + 1]);
15704 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705
15706 if (win != NULL && varname != NULL && varp != NULL)
15707 {
15708#ifdef FEAT_WINDOWS
15709 /* set curwin to be our win, temporarily */
15710 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015711 save_curtab = curtab;
15712 goto_tabpage_tp(tp);
15713 if (!win_valid(win))
15714 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 curwin = win;
15716 curbuf = curwin->w_buffer;
15717#endif
15718
15719 if (*varname == '&')
15720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 long numval;
15722 char_u *strval;
15723 int error = FALSE;
15724
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 numval = get_tv_number_chk(varp, &error);
15727 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 if (!error && strval != NULL)
15729 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 }
15731 else
15732 {
15733 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15734 if (winvarname != NULL)
15735 {
15736 STRCPY(winvarname, "w:");
15737 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015738 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 vim_free(winvarname);
15740 }
15741 }
15742
15743#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015744 /* Restore current tabpage and window, if still valid (autocomands can
15745 * make them invalid). */
15746 if (valid_tabpage(save_curtab))
15747 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 if (win_valid(save_curwin))
15749 {
15750 curwin = save_curwin;
15751 curbuf = curwin->w_buffer;
15752 }
15753#endif
15754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755}
15756
15757/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015758 * "shellescape({string})" function
15759 */
15760 static void
15761f_shellescape(argvars, rettv)
15762 typval_T *argvars;
15763 typval_T *rettv;
15764{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015765 rettv->vval.v_string = vim_strsave_shellescape(
15766 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015767 rettv->v_type = VAR_STRING;
15768}
15769
15770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015771 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 */
15773 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015775 typval_T *argvars;
15776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780 p = get_tv_string(&argvars[0]);
15781 rettv->vval.v_string = vim_strsave(p);
15782 simplify_filename(rettv->vval.v_string); /* simplify in place */
15783 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784}
15785
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015786#ifdef FEAT_FLOAT
15787/*
15788 * "sin()" function
15789 */
15790 static void
15791f_sin(argvars, rettv)
15792 typval_T *argvars;
15793 typval_T *rettv;
15794{
15795 float_T f;
15796
15797 rettv->v_type = VAR_FLOAT;
15798 if (get_float_arg(argvars, &f) == OK)
15799 rettv->vval.v_float = sin(f);
15800 else
15801 rettv->vval.v_float = 0.0;
15802}
15803#endif
15804
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805static int
15806#ifdef __BORLANDC__
15807 _RTLENTRYF
15808#endif
15809 item_compare __ARGS((const void *s1, const void *s2));
15810static int
15811#ifdef __BORLANDC__
15812 _RTLENTRYF
15813#endif
15814 item_compare2 __ARGS((const void *s1, const void *s2));
15815
15816static int item_compare_ic;
15817static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819#define ITEM_COMPARE_FAIL 999
15820
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 static int
15825#ifdef __BORLANDC__
15826_RTLENTRYF
15827#endif
15828item_compare(s1, s2)
15829 const void *s1;
15830 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832 char_u *p1, *p2;
15833 char_u *tofree1, *tofree2;
15834 int res;
15835 char_u numbuf1[NUMBUFLEN];
15836 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015838 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15839 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015840 if (p1 == NULL)
15841 p1 = (char_u *)"";
15842 if (p2 == NULL)
15843 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 if (item_compare_ic)
15845 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847 res = STRCMP(p1, p2);
15848 vim_free(tofree1);
15849 vim_free(tofree2);
15850 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851}
15852
15853 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854#ifdef __BORLANDC__
15855_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015857item_compare2(s1, s2)
15858 const void *s1;
15859 const void *s2;
15860{
15861 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015862 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015863 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 /* shortcut after failure in previous call; compare all items equal */
15867 if (item_compare_func_err)
15868 return 0;
15869
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015870 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15871 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015872 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15873 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874
15875 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015876 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015877 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015878 clear_tv(&argv[0]);
15879 clear_tv(&argv[1]);
15880
15881 if (res == FAIL)
15882 res = ITEM_COMPARE_FAIL;
15883 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015884 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15885 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015886 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015887 clear_tv(&rettv);
15888 return res;
15889}
15890
15891/*
15892 * "sort({list})" function
15893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 typval_T *argvars;
15897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898{
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 list_T *l;
15900 listitem_T *li;
15901 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902 long len;
15903 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904
Bram Moolenaar0d660222005-01-07 21:51:51 +000015905 if (argvars[0].v_type != VAR_LIST)
15906 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 else
15908 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015909 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015910 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015911 return;
15912 rettv->vval.v_list = l;
15913 rettv->v_type = VAR_LIST;
15914 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916 len = list_len(l);
15917 if (len <= 1)
15918 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920 item_compare_ic = FALSE;
15921 item_compare_func = NULL;
15922 if (argvars[1].v_type != VAR_UNKNOWN)
15923 {
15924 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015926 else
15927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015928 int error = FALSE;
15929
15930 i = get_tv_number_chk(&argvars[1], &error);
15931 if (error)
15932 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 if (i == 1)
15934 item_compare_ic = TRUE;
15935 else
15936 item_compare_func = get_tv_string(&argvars[1]);
15937 }
15938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015941 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 if (ptrs == NULL)
15943 return;
15944 i = 0;
15945 for (li = l->lv_first; li != NULL; li = li->li_next)
15946 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015949 /* test the compare function */
15950 if (item_compare_func != NULL
15951 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15952 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015953 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955 {
15956 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015958 item_compare_func == NULL ? item_compare : item_compare2);
15959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015960 if (!item_compare_func_err)
15961 {
15962 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015963 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015964 l->lv_len = 0;
15965 for (i = 0; i < len; ++i)
15966 list_append(l, ptrs[i]);
15967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015968 }
15969
15970 vim_free(ptrs);
15971 }
15972}
15973
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015974/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015975 * "soundfold({word})" function
15976 */
15977 static void
15978f_soundfold(argvars, rettv)
15979 typval_T *argvars;
15980 typval_T *rettv;
15981{
15982 char_u *s;
15983
15984 rettv->v_type = VAR_STRING;
15985 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015986#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015987 rettv->vval.v_string = eval_soundfold(s);
15988#else
15989 rettv->vval.v_string = vim_strsave(s);
15990#endif
15991}
15992
15993/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015994 * "spellbadword()" function
15995 */
15996/* ARGSUSED */
15997 static void
15998f_spellbadword(argvars, rettv)
15999 typval_T *argvars;
16000 typval_T *rettv;
16001{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016002 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016003 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016004 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016005
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016006 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016007 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016008
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016009#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016010 if (argvars[0].v_type == VAR_UNKNOWN)
16011 {
16012 /* Find the start and length of the badly spelled word. */
16013 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16014 if (len != 0)
16015 word = ml_get_cursor();
16016 }
16017 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16018 {
16019 char_u *str = get_tv_string_chk(&argvars[0]);
16020 int capcol = -1;
16021
16022 if (str != NULL)
16023 {
16024 /* Check the argument for spelling. */
16025 while (*str != NUL)
16026 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016027 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016028 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016029 {
16030 word = str;
16031 break;
16032 }
16033 str += len;
16034 }
16035 }
16036 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016037#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016038
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016039 list_append_string(rettv->vval.v_list, word, len);
16040 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016041 attr == HLF_SPB ? "bad" :
16042 attr == HLF_SPR ? "rare" :
16043 attr == HLF_SPL ? "local" :
16044 attr == HLF_SPC ? "caps" :
16045 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016046}
16047
16048/*
16049 * "spellsuggest()" function
16050 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016051/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016052 static void
16053f_spellsuggest(argvars, rettv)
16054 typval_T *argvars;
16055 typval_T *rettv;
16056{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016057#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016058 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016059 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016060 int maxcount;
16061 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016062 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016063 listitem_T *li;
16064 int need_capital = FALSE;
16065#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016066
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016067 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016068 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016069
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016070#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016071 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16072 {
16073 str = get_tv_string(&argvars[0]);
16074 if (argvars[1].v_type != VAR_UNKNOWN)
16075 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016076 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016077 if (maxcount <= 0)
16078 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016079 if (argvars[2].v_type != VAR_UNKNOWN)
16080 {
16081 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16082 if (typeerr)
16083 return;
16084 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016085 }
16086 else
16087 maxcount = 25;
16088
Bram Moolenaar4770d092006-01-12 23:22:24 +000016089 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016090
16091 for (i = 0; i < ga.ga_len; ++i)
16092 {
16093 str = ((char_u **)ga.ga_data)[i];
16094
16095 li = listitem_alloc();
16096 if (li == NULL)
16097 vim_free(str);
16098 else
16099 {
16100 li->li_tv.v_type = VAR_STRING;
16101 li->li_tv.v_lock = 0;
16102 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016103 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016104 }
16105 }
16106 ga_clear(&ga);
16107 }
16108#endif
16109}
16110
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016112f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 typval_T *argvars;
16114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016115{
16116 char_u *str;
16117 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016118 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016119 regmatch_T regmatch;
16120 char_u patbuf[NUMBUFLEN];
16121 char_u *save_cpo;
16122 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016123 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016124 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016125 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126
16127 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16128 save_cpo = p_cpo;
16129 p_cpo = (char_u *)"";
16130
16131 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016132 if (argvars[1].v_type != VAR_UNKNOWN)
16133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016134 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16135 if (pat == NULL)
16136 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016137 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016138 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016139 }
16140 if (pat == NULL || *pat == NUL)
16141 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 if (typeerr)
16146 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16149 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016152 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016153 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016154 if (*str == NUL)
16155 match = FALSE; /* empty item at the end */
16156 else
16157 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158 if (match)
16159 end = regmatch.startp[0];
16160 else
16161 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016162 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16163 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016165 if (list_append_string(rettv->vval.v_list, str,
16166 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 }
16169 if (!match)
16170 break;
16171 /* Advance to just after the match. */
16172 if (regmatch.endp[0] > str)
16173 col = 0;
16174 else
16175 {
16176 /* Don't get stuck at the same match. */
16177#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016178 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179#else
16180 col = 1;
16181#endif
16182 }
16183 str = regmatch.endp[0];
16184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190}
16191
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016192#ifdef FEAT_FLOAT
16193/*
16194 * "sqrt()" function
16195 */
16196 static void
16197f_sqrt(argvars, rettv)
16198 typval_T *argvars;
16199 typval_T *rettv;
16200{
16201 float_T f;
16202
16203 rettv->v_type = VAR_FLOAT;
16204 if (get_float_arg(argvars, &f) == OK)
16205 rettv->vval.v_float = sqrt(f);
16206 else
16207 rettv->vval.v_float = 0.0;
16208}
16209
16210/*
16211 * "str2float()" function
16212 */
16213 static void
16214f_str2float(argvars, rettv)
16215 typval_T *argvars;
16216 typval_T *rettv;
16217{
16218 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16219
16220 if (*p == '+')
16221 p = skipwhite(p + 1);
16222 (void)string2float(p, &rettv->vval.v_float);
16223 rettv->v_type = VAR_FLOAT;
16224}
16225#endif
16226
Bram Moolenaar2c932302006-03-18 21:42:09 +000016227/*
16228 * "str2nr()" function
16229 */
16230 static void
16231f_str2nr(argvars, rettv)
16232 typval_T *argvars;
16233 typval_T *rettv;
16234{
16235 int base = 10;
16236 char_u *p;
16237 long n;
16238
16239 if (argvars[1].v_type != VAR_UNKNOWN)
16240 {
16241 base = get_tv_number(&argvars[1]);
16242 if (base != 8 && base != 10 && base != 16)
16243 {
16244 EMSG(_(e_invarg));
16245 return;
16246 }
16247 }
16248
16249 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016250 if (*p == '+')
16251 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016252 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16253 rettv->vval.v_number = n;
16254}
16255
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256#ifdef HAVE_STRFTIME
16257/*
16258 * "strftime({format}[, {time}])" function
16259 */
16260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016262 typval_T *argvars;
16263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264{
16265 char_u result_buf[256];
16266 struct tm *curtime;
16267 time_t seconds;
16268 char_u *p;
16269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016273 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 seconds = time(NULL);
16275 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016276 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 curtime = localtime(&seconds);
16278 /* MSVC returns NULL for an invalid value of seconds. */
16279 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016280 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 else
16282 {
16283# ifdef FEAT_MBYTE
16284 vimconv_T conv;
16285 char_u *enc;
16286
16287 conv.vc_type = CONV_NONE;
16288 enc = enc_locale();
16289 convert_setup(&conv, p_enc, enc);
16290 if (conv.vc_type != CONV_NONE)
16291 p = string_convert(&conv, p, NULL);
16292# endif
16293 if (p != NULL)
16294 (void)strftime((char *)result_buf, sizeof(result_buf),
16295 (char *)p, curtime);
16296 else
16297 result_buf[0] = NUL;
16298
16299# ifdef FEAT_MBYTE
16300 if (conv.vc_type != CONV_NONE)
16301 vim_free(p);
16302 convert_setup(&conv, enc, p_enc);
16303 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016304 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 else
16306# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016307 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308
16309# ifdef FEAT_MBYTE
16310 /* Release conversion descriptors */
16311 convert_setup(&conv, NULL, NULL);
16312 vim_free(enc);
16313# endif
16314 }
16315}
16316#endif
16317
16318/*
16319 * "stridx()" function
16320 */
16321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016322f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016323 typval_T *argvars;
16324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325{
16326 char_u buf[NUMBUFLEN];
16327 char_u *needle;
16328 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016331 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 needle = get_tv_string_chk(&argvars[1]);
16334 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016335 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016336 if (needle == NULL || haystack == NULL)
16337 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338
Bram Moolenaar33570922005-01-25 22:26:29 +000016339 if (argvars[2].v_type != VAR_UNKNOWN)
16340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016341 int error = FALSE;
16342
16343 start_idx = get_tv_number_chk(&argvars[2], &error);
16344 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016345 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016346 if (start_idx >= 0)
16347 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016348 }
16349
16350 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16351 if (pos != NULL)
16352 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353}
16354
16355/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016356 * "string()" function
16357 */
16358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016359f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 typval_T *argvars;
16361 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016362{
16363 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016364 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016367 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016368 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016369 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371}
16372
16373/*
16374 * "strlen()" function
16375 */
16376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016377f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016378 typval_T *argvars;
16379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016381 rettv->vval.v_number = (varnumber_T)(STRLEN(
16382 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383}
16384
16385/*
16386 * "strpart()" function
16387 */
16388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016389f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 typval_T *argvars;
16391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
16393 char_u *p;
16394 int n;
16395 int len;
16396 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016397 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016399 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 slen = (int)STRLEN(p);
16401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 n = get_tv_number_chk(&argvars[1], &error);
16403 if (error)
16404 len = 0;
16405 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016406 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 else
16408 len = slen - n; /* default len: all bytes that are available. */
16409
16410 /*
16411 * Only return the overlap between the specified part and the actual
16412 * string.
16413 */
16414 if (n < 0)
16415 {
16416 len += n;
16417 n = 0;
16418 }
16419 else if (n > slen)
16420 n = slen;
16421 if (len < 0)
16422 len = 0;
16423 else if (n + len > slen)
16424 len = slen - n;
16425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 rettv->v_type = VAR_STRING;
16427 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428}
16429
16430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431 * "strridx()" function
16432 */
16433 static void
16434f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016435 typval_T *argvars;
16436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437{
16438 char_u buf[NUMBUFLEN];
16439 char_u *needle;
16440 char_u *haystack;
16441 char_u *rest;
16442 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016443 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 needle = get_tv_string_chk(&argvars[1]);
16446 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447
16448 rettv->vval.v_number = -1;
16449 if (needle == NULL || haystack == NULL)
16450 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016451
16452 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016453 if (argvars[2].v_type != VAR_UNKNOWN)
16454 {
16455 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016456 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016457 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016459 }
16460 else
16461 end_idx = haystack_len;
16462
Bram Moolenaar0d660222005-01-07 21:51:51 +000016463 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016464 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016466 lastmatch = haystack + end_idx;
16467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016469 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470 for (rest = haystack; *rest != '\0'; ++rest)
16471 {
16472 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016473 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 break;
16475 lastmatch = rest;
16476 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016477 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478
16479 if (lastmatch == NULL)
16480 rettv->vval.v_number = -1;
16481 else
16482 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16483}
16484
16485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 * "strtrans()" function
16487 */
16488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 typval_T *argvars;
16491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493 rettv->v_type = VAR_STRING;
16494 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495}
16496
16497/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 * "submatch()" function
16499 */
16500 static void
16501f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016502 typval_T *argvars;
16503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504{
16505 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506 rettv->vval.v_string =
16507 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508}
16509
16510/*
16511 * "substitute()" function
16512 */
16513 static void
16514f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 typval_T *argvars;
16516 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517{
16518 char_u patbuf[NUMBUFLEN];
16519 char_u subbuf[NUMBUFLEN];
16520 char_u flagsbuf[NUMBUFLEN];
16521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 char_u *str = get_tv_string_chk(&argvars[0]);
16523 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16524 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16525 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16526
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16529 rettv->vval.v_string = NULL;
16530 else
16531 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532}
16533
16534/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016535 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 */
16537/*ARGSUSED*/
16538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016540 typval_T *argvars;
16541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542{
16543 int id = 0;
16544#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016545 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 long col;
16547 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016548 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 lnum = get_tv_lnum(argvars); /* -1 on type error */
16551 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16552 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016555 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016556 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557#endif
16558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016559 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560}
16561
16562/*
16563 * "synIDattr(id, what [, mode])" function
16564 */
16565/*ARGSUSED*/
16566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016567f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016568 typval_T *argvars;
16569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570{
16571 char_u *p = NULL;
16572#ifdef FEAT_SYN_HL
16573 int id;
16574 char_u *what;
16575 char_u *mode;
16576 char_u modebuf[NUMBUFLEN];
16577 int modec;
16578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579 id = get_tv_number(&argvars[0]);
16580 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 modec = TOLOWER_ASC(mode[0]);
16585 if (modec != 't' && modec != 'c'
16586#ifdef FEAT_GUI
16587 && modec != 'g'
16588#endif
16589 )
16590 modec = 0; /* replace invalid with current */
16591 }
16592 else
16593 {
16594#ifdef FEAT_GUI
16595 if (gui.in_use)
16596 modec = 'g';
16597 else
16598#endif
16599 if (t_colors > 1)
16600 modec = 'c';
16601 else
16602 modec = 't';
16603 }
16604
16605
16606 switch (TOLOWER_ASC(what[0]))
16607 {
16608 case 'b':
16609 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16610 p = highlight_color(id, what, modec);
16611 else /* bold */
16612 p = highlight_has_attr(id, HL_BOLD, modec);
16613 break;
16614
16615 case 'f': /* fg[#] */
16616 p = highlight_color(id, what, modec);
16617 break;
16618
16619 case 'i':
16620 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16621 p = highlight_has_attr(id, HL_INVERSE, modec);
16622 else /* italic */
16623 p = highlight_has_attr(id, HL_ITALIC, modec);
16624 break;
16625
16626 case 'n': /* name */
16627 p = get_highlight_name(NULL, id - 1);
16628 break;
16629
16630 case 'r': /* reverse */
16631 p = highlight_has_attr(id, HL_INVERSE, modec);
16632 break;
16633
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016634 case 's':
16635 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16636 p = highlight_color(id, what, modec);
16637 else /* standout */
16638 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 break;
16640
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016641 case 'u':
16642 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16643 /* underline */
16644 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16645 else
16646 /* undercurl */
16647 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 break;
16649 }
16650
16651 if (p != NULL)
16652 p = vim_strsave(p);
16653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016654 rettv->v_type = VAR_STRING;
16655 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656}
16657
16658/*
16659 * "synIDtrans(id)" function
16660 */
16661/*ARGSUSED*/
16662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016664 typval_T *argvars;
16665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666{
16667 int id;
16668
16669#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671
16672 if (id > 0)
16673 id = syn_get_final_id(id);
16674 else
16675#endif
16676 id = 0;
16677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679}
16680
16681/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016682 * "synstack(lnum, col)" function
16683 */
16684/*ARGSUSED*/
16685 static void
16686f_synstack(argvars, rettv)
16687 typval_T *argvars;
16688 typval_T *rettv;
16689{
16690#ifdef FEAT_SYN_HL
16691 long lnum;
16692 long col;
16693 int i;
16694 int id;
16695#endif
16696
16697 rettv->v_type = VAR_LIST;
16698 rettv->vval.v_list = NULL;
16699
16700#ifdef FEAT_SYN_HL
16701 lnum = get_tv_lnum(argvars); /* -1 on type error */
16702 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16703
16704 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016705 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016706 && rettv_list_alloc(rettv) != FAIL)
16707 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016708 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016709 for (i = 0; ; ++i)
16710 {
16711 id = syn_get_stack_item(i);
16712 if (id < 0)
16713 break;
16714 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16715 break;
16716 }
16717 }
16718#endif
16719}
16720
16721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 * "system()" function
16723 */
16724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 typval_T *argvars;
16727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016729 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016731 char_u *infile = NULL;
16732 char_u buf[NUMBUFLEN];
16733 int err = FALSE;
16734 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016736 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016737 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016739 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016740 {
16741 /*
16742 * Write the string to a temp file, to be used for input of the shell
16743 * command.
16744 */
16745 if ((infile = vim_tempname('i')) == NULL)
16746 {
16747 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016748 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016749 }
16750
16751 fd = mch_fopen((char *)infile, WRITEBIN);
16752 if (fd == NULL)
16753 {
16754 EMSG2(_(e_notopen), infile);
16755 goto done;
16756 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016757 p = get_tv_string_buf_chk(&argvars[1], buf);
16758 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016759 {
16760 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016762 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016763 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16764 err = TRUE;
16765 if (fclose(fd) != 0)
16766 err = TRUE;
16767 if (err)
16768 {
16769 EMSG(_("E677: Error writing temp file"));
16770 goto done;
16771 }
16772 }
16773
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016774 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16775 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016776
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777#ifdef USE_CR
16778 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016779 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 {
16781 char_u *s;
16782
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016783 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 {
16785 if (*s == CAR)
16786 *s = NL;
16787 }
16788 }
16789#else
16790# ifdef USE_CRNL
16791 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016792 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 {
16794 char_u *s, *d;
16795
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016796 d = res;
16797 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 {
16799 if (s[0] == CAR && s[1] == NL)
16800 ++s;
16801 *d++ = *s;
16802 }
16803 *d = NUL;
16804 }
16805# endif
16806#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016807
16808done:
16809 if (infile != NULL)
16810 {
16811 mch_remove(infile);
16812 vim_free(infile);
16813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016814 rettv->v_type = VAR_STRING;
16815 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816}
16817
16818/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016819 * "tabpagebuflist()" function
16820 */
16821/* ARGSUSED */
16822 static void
16823f_tabpagebuflist(argvars, rettv)
16824 typval_T *argvars;
16825 typval_T *rettv;
16826{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016827#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016828 tabpage_T *tp;
16829 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016830
16831 if (argvars[0].v_type == VAR_UNKNOWN)
16832 wp = firstwin;
16833 else
16834 {
16835 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16836 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016837 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016838 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016839 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016840 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016841 for (; wp != NULL; wp = wp->w_next)
16842 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016843 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016844 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016845 }
16846#endif
16847}
16848
16849
16850/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016851 * "tabpagenr()" function
16852 */
16853/* ARGSUSED */
16854 static void
16855f_tabpagenr(argvars, rettv)
16856 typval_T *argvars;
16857 typval_T *rettv;
16858{
16859 int nr = 1;
16860#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016861 char_u *arg;
16862
16863 if (argvars[0].v_type != VAR_UNKNOWN)
16864 {
16865 arg = get_tv_string_chk(&argvars[0]);
16866 nr = 0;
16867 if (arg != NULL)
16868 {
16869 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016870 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016871 else
16872 EMSG2(_(e_invexpr2), arg);
16873 }
16874 }
16875 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016876 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016877#endif
16878 rettv->vval.v_number = nr;
16879}
16880
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016881
16882#ifdef FEAT_WINDOWS
16883static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16884
16885/*
16886 * Common code for tabpagewinnr() and winnr().
16887 */
16888 static int
16889get_winnr(tp, argvar)
16890 tabpage_T *tp;
16891 typval_T *argvar;
16892{
16893 win_T *twin;
16894 int nr = 1;
16895 win_T *wp;
16896 char_u *arg;
16897
16898 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16899 if (argvar->v_type != VAR_UNKNOWN)
16900 {
16901 arg = get_tv_string_chk(argvar);
16902 if (arg == NULL)
16903 nr = 0; /* type error; errmsg already given */
16904 else if (STRCMP(arg, "$") == 0)
16905 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16906 else if (STRCMP(arg, "#") == 0)
16907 {
16908 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16909 if (twin == NULL)
16910 nr = 0;
16911 }
16912 else
16913 {
16914 EMSG2(_(e_invexpr2), arg);
16915 nr = 0;
16916 }
16917 }
16918
16919 if (nr > 0)
16920 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16921 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016922 {
16923 if (wp == NULL)
16924 {
16925 /* didn't find it in this tabpage */
16926 nr = 0;
16927 break;
16928 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016929 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016930 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016931 return nr;
16932}
16933#endif
16934
16935/*
16936 * "tabpagewinnr()" function
16937 */
16938/* ARGSUSED */
16939 static void
16940f_tabpagewinnr(argvars, rettv)
16941 typval_T *argvars;
16942 typval_T *rettv;
16943{
16944 int nr = 1;
16945#ifdef FEAT_WINDOWS
16946 tabpage_T *tp;
16947
16948 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16949 if (tp == NULL)
16950 nr = 0;
16951 else
16952 nr = get_winnr(tp, &argvars[1]);
16953#endif
16954 rettv->vval.v_number = nr;
16955}
16956
16957
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016958/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016959 * "tagfiles()" function
16960 */
16961/*ARGSUSED*/
16962 static void
16963f_tagfiles(argvars, rettv)
16964 typval_T *argvars;
16965 typval_T *rettv;
16966{
16967 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016968 tagname_T tn;
16969 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016970
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016971 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016972 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016973
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016974 for (first = TRUE; ; first = FALSE)
16975 if (get_tagfname(&tn, first, fname) == FAIL
16976 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016977 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016978 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016979}
16980
16981/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016982 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016983 */
16984 static void
16985f_taglist(argvars, rettv)
16986 typval_T *argvars;
16987 typval_T *rettv;
16988{
16989 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016990
16991 tag_pattern = get_tv_string(&argvars[0]);
16992
16993 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016994 if (*tag_pattern == NUL)
16995 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016996
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016997 if (rettv_list_alloc(rettv) == OK)
16998 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016999}
17000
17001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 * "tempname()" function
17003 */
17004/*ARGSUSED*/
17005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017006f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 typval_T *argvars;
17008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009{
17010 static int x = 'A';
17011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017012 rettv->v_type = VAR_STRING;
17013 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014
17015 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17016 * names. Skip 'I' and 'O', they are used for shell redirection. */
17017 do
17018 {
17019 if (x == 'Z')
17020 x = '0';
17021 else if (x == '9')
17022 x = 'A';
17023 else
17024 {
17025#ifdef EBCDIC
17026 if (x == 'I')
17027 x = 'J';
17028 else if (x == 'R')
17029 x = 'S';
17030 else
17031#endif
17032 ++x;
17033 }
17034 } while (x == 'I' || x == 'O');
17035}
17036
17037/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017038 * "test(list)" function: Just checking the walls...
17039 */
17040/*ARGSUSED*/
17041 static void
17042f_test(argvars, rettv)
17043 typval_T *argvars;
17044 typval_T *rettv;
17045{
17046 /* Used for unit testing. Change the code below to your liking. */
17047#if 0
17048 listitem_T *li;
17049 list_T *l;
17050 char_u *bad, *good;
17051
17052 if (argvars[0].v_type != VAR_LIST)
17053 return;
17054 l = argvars[0].vval.v_list;
17055 if (l == NULL)
17056 return;
17057 li = l->lv_first;
17058 if (li == NULL)
17059 return;
17060 bad = get_tv_string(&li->li_tv);
17061 li = li->li_next;
17062 if (li == NULL)
17063 return;
17064 good = get_tv_string(&li->li_tv);
17065 rettv->vval.v_number = test_edit_score(bad, good);
17066#endif
17067}
17068
17069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 * "tolower(string)" function
17071 */
17072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017073f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017074 typval_T *argvars;
17075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076{
17077 char_u *p;
17078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079 p = vim_strsave(get_tv_string(&argvars[0]));
17080 rettv->v_type = VAR_STRING;
17081 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
17083 if (p != NULL)
17084 while (*p != NUL)
17085 {
17086#ifdef FEAT_MBYTE
17087 int l;
17088
17089 if (enc_utf8)
17090 {
17091 int c, lc;
17092
17093 c = utf_ptr2char(p);
17094 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017095 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 /* TODO: reallocate string when byte count changes. */
17097 if (utf_char2len(lc) == l)
17098 utf_char2bytes(lc, p);
17099 p += l;
17100 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017101 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 p += l; /* skip multi-byte character */
17103 else
17104#endif
17105 {
17106 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17107 ++p;
17108 }
17109 }
17110}
17111
17112/*
17113 * "toupper(string)" function
17114 */
17115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 typval_T *argvars;
17118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017121 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122}
17123
17124/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017125 * "tr(string, fromstr, tostr)" function
17126 */
17127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 typval_T *argvars;
17130 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017131{
17132 char_u *instr;
17133 char_u *fromstr;
17134 char_u *tostr;
17135 char_u *p;
17136#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017137 int inlen;
17138 int fromlen;
17139 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017140 int idx;
17141 char_u *cpstr;
17142 int cplen;
17143 int first = TRUE;
17144#endif
17145 char_u buf[NUMBUFLEN];
17146 char_u buf2[NUMBUFLEN];
17147 garray_T ga;
17148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17151 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017152
17153 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 rettv->v_type = VAR_STRING;
17155 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 if (fromstr == NULL || tostr == NULL)
17157 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017158 ga_init2(&ga, (int)sizeof(char), 80);
17159
17160#ifdef FEAT_MBYTE
17161 if (!has_mbyte)
17162#endif
17163 /* not multi-byte: fromstr and tostr must be the same length */
17164 if (STRLEN(fromstr) != STRLEN(tostr))
17165 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017166#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017167error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017168#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017169 EMSG2(_(e_invarg2), fromstr);
17170 ga_clear(&ga);
17171 return;
17172 }
17173
17174 /* fromstr and tostr have to contain the same number of chars */
17175 while (*instr != NUL)
17176 {
17177#ifdef FEAT_MBYTE
17178 if (has_mbyte)
17179 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017180 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017181 cpstr = instr;
17182 cplen = inlen;
17183 idx = 0;
17184 for (p = fromstr; *p != NUL; p += fromlen)
17185 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017186 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017187 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17188 {
17189 for (p = tostr; *p != NUL; p += tolen)
17190 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017191 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017192 if (idx-- == 0)
17193 {
17194 cplen = tolen;
17195 cpstr = p;
17196 break;
17197 }
17198 }
17199 if (*p == NUL) /* tostr is shorter than fromstr */
17200 goto error;
17201 break;
17202 }
17203 ++idx;
17204 }
17205
17206 if (first && cpstr == instr)
17207 {
17208 /* Check that fromstr and tostr have the same number of
17209 * (multi-byte) characters. Done only once when a character
17210 * of instr doesn't appear in fromstr. */
17211 first = FALSE;
17212 for (p = tostr; *p != NUL; p += tolen)
17213 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017214 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017215 --idx;
17216 }
17217 if (idx != 0)
17218 goto error;
17219 }
17220
17221 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017222 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017223 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017224
17225 instr += inlen;
17226 }
17227 else
17228#endif
17229 {
17230 /* When not using multi-byte chars we can do it faster. */
17231 p = vim_strchr(fromstr, *instr);
17232 if (p != NULL)
17233 ga_append(&ga, tostr[p - fromstr]);
17234 else
17235 ga_append(&ga, *instr);
17236 ++instr;
17237 }
17238 }
17239
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017240 /* add a terminating NUL */
17241 ga_grow(&ga, 1);
17242 ga_append(&ga, NUL);
17243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017244 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017245}
17246
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017247#ifdef FEAT_FLOAT
17248/*
17249 * "trunc({float})" function
17250 */
17251 static void
17252f_trunc(argvars, rettv)
17253 typval_T *argvars;
17254 typval_T *rettv;
17255{
17256 float_T f;
17257
17258 rettv->v_type = VAR_FLOAT;
17259 if (get_float_arg(argvars, &f) == OK)
17260 /* trunc() is not in C90, use floor() or ceil() instead. */
17261 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17262 else
17263 rettv->vval.v_float = 0.0;
17264}
17265#endif
17266
Bram Moolenaar8299df92004-07-10 09:47:34 +000017267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 * "type(expr)" function
17269 */
17270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017271f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017272 typval_T *argvars;
17273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017275 int n;
17276
17277 switch (argvars[0].v_type)
17278 {
17279 case VAR_NUMBER: n = 0; break;
17280 case VAR_STRING: n = 1; break;
17281 case VAR_FUNC: n = 2; break;
17282 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017283 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017284#ifdef FEAT_FLOAT
17285 case VAR_FLOAT: n = 5; break;
17286#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017287 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17288 }
17289 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290}
17291
17292/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017293 * "values(dict)" function
17294 */
17295 static void
17296f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017297 typval_T *argvars;
17298 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017299{
17300 dict_list(argvars, rettv, 1);
17301}
17302
17303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 * "virtcol(string)" function
17305 */
17306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017307f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 typval_T *argvars;
17309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310{
17311 colnr_T vcol = 0;
17312 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017313 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017315 fp = var2fpos(&argvars[0], FALSE, &fnum);
17316 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17317 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 {
17319 getvvcol(curwin, fp, NULL, NULL, &vcol);
17320 ++vcol;
17321 }
17322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017323 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324}
17325
17326/*
17327 * "visualmode()" function
17328 */
17329/*ARGSUSED*/
17330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 typval_T *argvars;
17333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334{
17335#ifdef FEAT_VISUAL
17336 char_u str[2];
17337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 str[0] = curbuf->b_visual_mode_eval;
17340 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342
17343 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017344 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346#endif
17347}
17348
17349/*
17350 * "winbufnr(nr)" function
17351 */
17352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017353f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 typval_T *argvars;
17355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356{
17357 win_T *wp;
17358
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017359 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364}
17365
17366/*
17367 * "wincol()" function
17368 */
17369/*ARGSUSED*/
17370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 typval_T *argvars;
17373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
17375 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017376 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377}
17378
17379/*
17380 * "winheight(nr)" function
17381 */
17382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 typval_T *argvars;
17385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386{
17387 win_T *wp;
17388
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017389 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017391 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394}
17395
17396/*
17397 * "winline()" function
17398 */
17399/*ARGSUSED*/
17400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 typval_T *argvars;
17403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404{
17405 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407}
17408
17409/*
17410 * "winnr()" function
17411 */
17412/* ARGSUSED */
17413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 typval_T *argvars;
17416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417{
17418 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017419
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017421 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424}
17425
17426/*
17427 * "winrestcmd()" function
17428 */
17429/* ARGSUSED */
17430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 typval_T *argvars;
17433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434{
17435#ifdef FEAT_WINDOWS
17436 win_T *wp;
17437 int winnr = 1;
17438 garray_T ga;
17439 char_u buf[50];
17440
17441 ga_init2(&ga, (int)sizeof(char), 70);
17442 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17443 {
17444 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17445 ga_concat(&ga, buf);
17446# ifdef FEAT_VERTSPLIT
17447 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17448 ga_concat(&ga, buf);
17449# endif
17450 ++winnr;
17451 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017452 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017454 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017458 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459}
17460
17461/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017462 * "winrestview()" function
17463 */
17464/* ARGSUSED */
17465 static void
17466f_winrestview(argvars, rettv)
17467 typval_T *argvars;
17468 typval_T *rettv;
17469{
17470 dict_T *dict;
17471
17472 if (argvars[0].v_type != VAR_DICT
17473 || (dict = argvars[0].vval.v_dict) == NULL)
17474 EMSG(_(e_invarg));
17475 else
17476 {
17477 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17478 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17479#ifdef FEAT_VIRTUALEDIT
17480 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17481#endif
17482 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017483 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017484
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017485 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017486#ifdef FEAT_DIFF
17487 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17488#endif
17489 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17490 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17491
17492 check_cursor();
17493 changed_cline_bef_curs();
17494 invalidate_botline();
17495 redraw_later(VALID);
17496
17497 if (curwin->w_topline == 0)
17498 curwin->w_topline = 1;
17499 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17500 curwin->w_topline = curbuf->b_ml.ml_line_count;
17501#ifdef FEAT_DIFF
17502 check_topfill(curwin, TRUE);
17503#endif
17504 }
17505}
17506
17507/*
17508 * "winsaveview()" function
17509 */
17510/* ARGSUSED */
17511 static void
17512f_winsaveview(argvars, rettv)
17513 typval_T *argvars;
17514 typval_T *rettv;
17515{
17516 dict_T *dict;
17517
17518 dict = dict_alloc();
17519 if (dict == NULL)
17520 return;
17521 rettv->v_type = VAR_DICT;
17522 rettv->vval.v_dict = dict;
17523 ++dict->dv_refcount;
17524
17525 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17526 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17527#ifdef FEAT_VIRTUALEDIT
17528 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17529#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017530 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017531 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17532
17533 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17534#ifdef FEAT_DIFF
17535 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17536#endif
17537 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17538 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17539}
17540
17541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 * "winwidth(nr)" function
17543 */
17544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 typval_T *argvars;
17547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548{
17549 win_T *wp;
17550
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017551 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 else
17555#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017556 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559#endif
17560}
17561
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017563 * "writefile()" function
17564 */
17565 static void
17566f_writefile(argvars, rettv)
17567 typval_T *argvars;
17568 typval_T *rettv;
17569{
17570 int binary = FALSE;
17571 char_u *fname;
17572 FILE *fd;
17573 listitem_T *li;
17574 char_u *s;
17575 int ret = 0;
17576 int c;
17577
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017578 if (check_restricted() || check_secure())
17579 return;
17580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017581 if (argvars[0].v_type != VAR_LIST)
17582 {
17583 EMSG2(_(e_listarg), "writefile()");
17584 return;
17585 }
17586 if (argvars[0].vval.v_list == NULL)
17587 return;
17588
17589 if (argvars[2].v_type != VAR_UNKNOWN
17590 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17591 binary = TRUE;
17592
17593 /* Always open the file in binary mode, library functions have a mind of
17594 * their own about CR-LF conversion. */
17595 fname = get_tv_string(&argvars[1]);
17596 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17597 {
17598 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17599 ret = -1;
17600 }
17601 else
17602 {
17603 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17604 li = li->li_next)
17605 {
17606 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17607 {
17608 if (*s == '\n')
17609 c = putc(NUL, fd);
17610 else
17611 c = putc(*s, fd);
17612 if (c == EOF)
17613 {
17614 ret = -1;
17615 break;
17616 }
17617 }
17618 if (!binary || li->li_next != NULL)
17619 if (putc('\n', fd) == EOF)
17620 {
17621 ret = -1;
17622 break;
17623 }
17624 if (ret < 0)
17625 {
17626 EMSG(_(e_write));
17627 break;
17628 }
17629 }
17630 fclose(fd);
17631 }
17632
17633 rettv->vval.v_number = ret;
17634}
17635
17636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017638 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 */
17640 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017641var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017643 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017644 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017646 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017648 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649
Bram Moolenaara5525202006-03-02 22:52:09 +000017650 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017651 if (varp->v_type == VAR_LIST)
17652 {
17653 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017654 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017655 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017656 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017657
17658 l = varp->vval.v_list;
17659 if (l == NULL)
17660 return NULL;
17661
17662 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017663 pos.lnum = list_find_nr(l, 0L, &error);
17664 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017665 return NULL; /* invalid line number */
17666
17667 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017668 pos.col = list_find_nr(l, 1L, &error);
17669 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017670 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017671 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017672
17673 /* We accept "$" for the column number: last column. */
17674 li = list_find(l, 1L);
17675 if (li != NULL && li->li_tv.v_type == VAR_STRING
17676 && li->li_tv.vval.v_string != NULL
17677 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17678 pos.col = len + 1;
17679
Bram Moolenaara5525202006-03-02 22:52:09 +000017680 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017681 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017682 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017683 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017684
Bram Moolenaara5525202006-03-02 22:52:09 +000017685#ifdef FEAT_VIRTUALEDIT
17686 /* Get the virtual offset. Defaults to zero. */
17687 pos.coladd = list_find_nr(l, 2L, &error);
17688 if (error)
17689 pos.coladd = 0;
17690#endif
17691
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017692 return &pos;
17693 }
17694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017695 name = get_tv_string_chk(varp);
17696 if (name == NULL)
17697 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017698 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017700#ifdef FEAT_VISUAL
17701 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17702 {
17703 if (VIsual_active)
17704 return &VIsual;
17705 return &curwin->w_cursor;
17706 }
17707#endif
17708 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017710 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17712 return NULL;
17713 return pp;
17714 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017715
17716#ifdef FEAT_VIRTUALEDIT
17717 pos.coladd = 0;
17718#endif
17719
Bram Moolenaar477933c2007-07-17 14:32:23 +000017720 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017721 {
17722 pos.col = 0;
17723 if (name[1] == '0') /* "w0": first visible line */
17724 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017725 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017726 pos.lnum = curwin->w_topline;
17727 return &pos;
17728 }
17729 else if (name[1] == '$') /* "w$": last visible line */
17730 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017731 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017732 pos.lnum = curwin->w_botline - 1;
17733 return &pos;
17734 }
17735 }
17736 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017738 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 {
17740 pos.lnum = curbuf->b_ml.ml_line_count;
17741 pos.col = 0;
17742 }
17743 else
17744 {
17745 pos.lnum = curwin->w_cursor.lnum;
17746 pos.col = (colnr_T)STRLEN(ml_get_curline());
17747 }
17748 return &pos;
17749 }
17750 return NULL;
17751}
17752
17753/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017754 * Convert list in "arg" into a position and optional file number.
17755 * When "fnump" is NULL there is no file number, only 3 items.
17756 * Note that the column is passed on as-is, the caller may want to decrement
17757 * it to use 1 for the first column.
17758 * Return FAIL when conversion is not possible, doesn't check the position for
17759 * validity.
17760 */
17761 static int
17762list2fpos(arg, posp, fnump)
17763 typval_T *arg;
17764 pos_T *posp;
17765 int *fnump;
17766{
17767 list_T *l = arg->vval.v_list;
17768 long i = 0;
17769 long n;
17770
Bram Moolenaarbde35262006-07-23 20:12:24 +000017771 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17772 * when "fnump" isn't NULL and "coladd" is optional. */
17773 if (arg->v_type != VAR_LIST
17774 || l == NULL
17775 || l->lv_len < (fnump == NULL ? 2 : 3)
17776 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017777 return FAIL;
17778
17779 if (fnump != NULL)
17780 {
17781 n = list_find_nr(l, i++, NULL); /* fnum */
17782 if (n < 0)
17783 return FAIL;
17784 if (n == 0)
17785 n = curbuf->b_fnum; /* current buffer */
17786 *fnump = n;
17787 }
17788
17789 n = list_find_nr(l, i++, NULL); /* lnum */
17790 if (n < 0)
17791 return FAIL;
17792 posp->lnum = n;
17793
17794 n = list_find_nr(l, i++, NULL); /* col */
17795 if (n < 0)
17796 return FAIL;
17797 posp->col = n;
17798
17799#ifdef FEAT_VIRTUALEDIT
17800 n = list_find_nr(l, i, NULL);
17801 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017802 posp->coladd = 0;
17803 else
17804 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017805#endif
17806
17807 return OK;
17808}
17809
17810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 * Get the length of an environment variable name.
17812 * Advance "arg" to the first character after the name.
17813 * Return 0 for error.
17814 */
17815 static int
17816get_env_len(arg)
17817 char_u **arg;
17818{
17819 char_u *p;
17820 int len;
17821
17822 for (p = *arg; vim_isIDc(*p); ++p)
17823 ;
17824 if (p == *arg) /* no name found */
17825 return 0;
17826
17827 len = (int)(p - *arg);
17828 *arg = p;
17829 return len;
17830}
17831
17832/*
17833 * Get the length of the name of a function or internal variable.
17834 * "arg" is advanced to the first non-white character after the name.
17835 * Return 0 if something is wrong.
17836 */
17837 static int
17838get_id_len(arg)
17839 char_u **arg;
17840{
17841 char_u *p;
17842 int len;
17843
17844 /* Find the end of the name. */
17845 for (p = *arg; eval_isnamec(*p); ++p)
17846 ;
17847 if (p == *arg) /* no name found */
17848 return 0;
17849
17850 len = (int)(p - *arg);
17851 *arg = skipwhite(p);
17852
17853 return len;
17854}
17855
17856/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017857 * Get the length of the name of a variable or function.
17858 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017860 * Return -1 if curly braces expansion failed.
17861 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 * If the name contains 'magic' {}'s, expand them and return the
17863 * expanded name in an allocated string via 'alias' - caller must free.
17864 */
17865 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017866get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 char_u **arg;
17868 char_u **alias;
17869 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017870 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871{
17872 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 char_u *p;
17874 char_u *expr_start;
17875 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876
17877 *alias = NULL; /* default to no alias */
17878
17879 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17880 && (*arg)[2] == (int)KE_SNR)
17881 {
17882 /* hard coded <SNR>, already translated */
17883 *arg += 3;
17884 return get_id_len(arg) + 3;
17885 }
17886 len = eval_fname_script(*arg);
17887 if (len > 0)
17888 {
17889 /* literal "<SID>", "s:" or "<SNR>" */
17890 *arg += len;
17891 }
17892
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017894 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017896 p = find_name_end(*arg, &expr_start, &expr_end,
17897 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 if (expr_start != NULL)
17899 {
17900 char_u *temp_string;
17901
17902 if (!evaluate)
17903 {
17904 len += (int)(p - *arg);
17905 *arg = skipwhite(p);
17906 return len;
17907 }
17908
17909 /*
17910 * Include any <SID> etc in the expanded string:
17911 * Thus the -len here.
17912 */
17913 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17914 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017915 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 *alias = temp_string;
17917 *arg = skipwhite(p);
17918 return (int)STRLEN(temp_string);
17919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920
17921 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017922 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 EMSG2(_(e_invexpr2), *arg);
17924
17925 return len;
17926}
17927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017928/*
17929 * Find the end of a variable or function name, taking care of magic braces.
17930 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17931 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017932 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 * Return a pointer to just after the name. Equal to "arg" if there is no
17934 * valid name.
17935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017937find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 char_u *arg;
17939 char_u **expr_start;
17940 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017941 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017943 int mb_nest = 0;
17944 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 char_u *p;
17946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017947 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949 *expr_start = NULL;
17950 *expr_end = NULL;
17951 }
17952
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017953 /* Quick check for valid starting character. */
17954 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17955 return arg;
17956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017957 for (p = arg; *p != NUL
17958 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017959 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017960 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017961 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017962 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017964 if (*p == '\'')
17965 {
17966 /* skip over 'string' to avoid counting [ and ] inside it. */
17967 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17968 ;
17969 if (*p == NUL)
17970 break;
17971 }
17972 else if (*p == '"')
17973 {
17974 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17975 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17976 if (*p == '\\' && p[1] != NUL)
17977 ++p;
17978 if (*p == NUL)
17979 break;
17980 }
17981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017982 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017984 if (*p == '[')
17985 ++br_nest;
17986 else if (*p == ']')
17987 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 if (*p == '{')
17993 {
17994 mb_nest++;
17995 if (expr_start != NULL && *expr_start == NULL)
17996 *expr_start = p;
17997 }
17998 else if (*p == '}')
17999 {
18000 mb_nest--;
18001 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18002 *expr_end = p;
18003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 }
18006
18007 return p;
18008}
18009
18010/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018011 * Expands out the 'magic' {}'s in a variable/function name.
18012 * Note that this can call itself recursively, to deal with
18013 * constructs like foo{bar}{baz}{bam}
18014 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18015 * "in_start" ^
18016 * "expr_start" ^
18017 * "expr_end" ^
18018 * "in_end" ^
18019 *
18020 * Returns a new allocated string, which the caller must free.
18021 * Returns NULL for failure.
18022 */
18023 static char_u *
18024make_expanded_name(in_start, expr_start, expr_end, in_end)
18025 char_u *in_start;
18026 char_u *expr_start;
18027 char_u *expr_end;
18028 char_u *in_end;
18029{
18030 char_u c1;
18031 char_u *retval = NULL;
18032 char_u *temp_result;
18033 char_u *nextcmd = NULL;
18034
18035 if (expr_end == NULL || in_end == NULL)
18036 return NULL;
18037 *expr_start = NUL;
18038 *expr_end = NUL;
18039 c1 = *in_end;
18040 *in_end = NUL;
18041
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018042 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018043 if (temp_result != NULL && nextcmd == NULL)
18044 {
18045 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18046 + (in_end - expr_end) + 1));
18047 if (retval != NULL)
18048 {
18049 STRCPY(retval, in_start);
18050 STRCAT(retval, temp_result);
18051 STRCAT(retval, expr_end + 1);
18052 }
18053 }
18054 vim_free(temp_result);
18055
18056 *in_end = c1; /* put char back for error messages */
18057 *expr_start = '{';
18058 *expr_end = '}';
18059
18060 if (retval != NULL)
18061 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018062 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018063 if (expr_start != NULL)
18064 {
18065 /* Further expansion! */
18066 temp_result = make_expanded_name(retval, expr_start,
18067 expr_end, temp_result);
18068 vim_free(retval);
18069 retval = temp_result;
18070 }
18071 }
18072
18073 return retval;
18074}
18075
18076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018078 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 */
18080 static int
18081eval_isnamec(c)
18082 int c;
18083{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018084 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18085}
18086
18087/*
18088 * Return TRUE if character "c" can be used as the first character in a
18089 * variable or function name (excluding '{' and '}').
18090 */
18091 static int
18092eval_isnamec1(c)
18093 int c;
18094{
18095 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096}
18097
18098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 * Set number v: variable to "val".
18100 */
18101 void
18102set_vim_var_nr(idx, val)
18103 int idx;
18104 long val;
18105{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018106 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107}
18108
18109/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018110 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 */
18112 long
18113get_vim_var_nr(idx)
18114 int idx;
18115{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018116 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117}
18118
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018119/*
18120 * Get string v: variable value. Uses a static buffer, can only be used once.
18121 */
18122 char_u *
18123get_vim_var_str(idx)
18124 int idx;
18125{
18126 return get_tv_string(&vimvars[idx].vv_tv);
18127}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018128
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018130 * Get List v: variable value. Caller must take care of reference count when
18131 * needed.
18132 */
18133 list_T *
18134get_vim_var_list(idx)
18135 int idx;
18136{
18137 return vimvars[idx].vv_list;
18138}
18139
18140/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018141 * Set v:count to "count" and v:count1 to "count1".
18142 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 */
18144 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018145set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 long count;
18147 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018148 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018150 if (set_prevcount)
18151 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018152 vimvars[VV_COUNT].vv_nr = count;
18153 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154}
18155
18156/*
18157 * Set string v: variable to a copy of "val".
18158 */
18159 void
18160set_vim_var_string(idx, val, len)
18161 int idx;
18162 char_u *val;
18163 int len; /* length of "val" to use or -1 (whole string) */
18164{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018165 /* Need to do this (at least) once, since we can't initialize a union.
18166 * Will always be invoked when "v:progname" is set. */
18167 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18168
Bram Moolenaare9a41262005-01-15 22:18:47 +000018169 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018171 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018173 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018175 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176}
18177
18178/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018179 * Set List v: variable to "val".
18180 */
18181 void
18182set_vim_var_list(idx, val)
18183 int idx;
18184 list_T *val;
18185{
18186 list_unref(vimvars[idx].vv_list);
18187 vimvars[idx].vv_list = val;
18188 if (val != NULL)
18189 ++val->lv_refcount;
18190}
18191
18192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 * Set v:register if needed.
18194 */
18195 void
18196set_reg_var(c)
18197 int c;
18198{
18199 char_u regname;
18200
18201 if (c == 0 || c == ' ')
18202 regname = '"';
18203 else
18204 regname = c;
18205 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018206 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 set_vim_var_string(VV_REG, &regname, 1);
18208}
18209
18210/*
18211 * Get or set v:exception. If "oldval" == NULL, return the current value.
18212 * Otherwise, restore the value to "oldval" and return NULL.
18213 * Must always be called in pairs to save and restore v:exception! Does not
18214 * take care of memory allocations.
18215 */
18216 char_u *
18217v_exception(oldval)
18218 char_u *oldval;
18219{
18220 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018221 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222
Bram Moolenaare9a41262005-01-15 22:18:47 +000018223 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 return NULL;
18225}
18226
18227/*
18228 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18229 * Otherwise, restore the value to "oldval" and return NULL.
18230 * Must always be called in pairs to save and restore v:throwpoint! Does not
18231 * take care of memory allocations.
18232 */
18233 char_u *
18234v_throwpoint(oldval)
18235 char_u *oldval;
18236{
18237 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018238 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239
Bram Moolenaare9a41262005-01-15 22:18:47 +000018240 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 return NULL;
18242}
18243
18244#if defined(FEAT_AUTOCMD) || defined(PROTO)
18245/*
18246 * Set v:cmdarg.
18247 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18248 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18249 * Must always be called in pairs!
18250 */
18251 char_u *
18252set_cmdarg(eap, oldarg)
18253 exarg_T *eap;
18254 char_u *oldarg;
18255{
18256 char_u *oldval;
18257 char_u *newval;
18258 unsigned len;
18259
Bram Moolenaare9a41262005-01-15 22:18:47 +000018260 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018261 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018263 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018264 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018265 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 }
18267
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018268 if (eap->force_bin == FORCE_BIN)
18269 len = 6;
18270 else if (eap->force_bin == FORCE_NOBIN)
18271 len = 8;
18272 else
18273 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018274
18275 if (eap->read_edit)
18276 len += 7;
18277
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018278 if (eap->force_ff != 0)
18279 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18280# ifdef FEAT_MBYTE
18281 if (eap->force_enc != 0)
18282 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018283 if (eap->bad_char != 0)
18284 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018285# endif
18286
18287 newval = alloc(len + 1);
18288 if (newval == NULL)
18289 return NULL;
18290
18291 if (eap->force_bin == FORCE_BIN)
18292 sprintf((char *)newval, " ++bin");
18293 else if (eap->force_bin == FORCE_NOBIN)
18294 sprintf((char *)newval, " ++nobin");
18295 else
18296 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018297
18298 if (eap->read_edit)
18299 STRCAT(newval, " ++edit");
18300
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018301 if (eap->force_ff != 0)
18302 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18303 eap->cmd + eap->force_ff);
18304# ifdef FEAT_MBYTE
18305 if (eap->force_enc != 0)
18306 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18307 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018308 if (eap->bad_char != 0)
18309 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18310 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018311# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018312 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018313 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314}
18315#endif
18316
18317/*
18318 * Get the value of internal variable "name".
18319 * Return OK or FAIL.
18320 */
18321 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018322get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 char_u *name;
18324 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018325 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018326 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327{
18328 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018329 typval_T *tv = NULL;
18330 typval_T atv;
18331 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333
18334 /* truncate the name, so that we can use strcmp() */
18335 cc = name[len];
18336 name[len] = NUL;
18337
18338 /*
18339 * Check for "b:changedtick".
18340 */
18341 if (STRCMP(name, "b:changedtick") == 0)
18342 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018343 atv.v_type = VAR_NUMBER;
18344 atv.vval.v_number = curbuf->b_changedtick;
18345 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 }
18347
18348 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 * Check for user-defined variables.
18350 */
18351 else
18352 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018353 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018355 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 }
18357
Bram Moolenaare9a41262005-01-15 22:18:47 +000018358 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018360 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018361 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 ret = FAIL;
18363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018365 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366
18367 name[len] = cc;
18368
18369 return ret;
18370}
18371
18372/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018373 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18374 * Also handle function call with Funcref variable: func(expr)
18375 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18376 */
18377 static int
18378handle_subscript(arg, rettv, evaluate, verbose)
18379 char_u **arg;
18380 typval_T *rettv;
18381 int evaluate; /* do more than finding the end */
18382 int verbose; /* give error messages */
18383{
18384 int ret = OK;
18385 dict_T *selfdict = NULL;
18386 char_u *s;
18387 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018388 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018389
18390 while (ret == OK
18391 && (**arg == '['
18392 || (**arg == '.' && rettv->v_type == VAR_DICT)
18393 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18394 && !vim_iswhite(*(*arg - 1)))
18395 {
18396 if (**arg == '(')
18397 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018398 /* need to copy the funcref so that we can clear rettv */
18399 functv = *rettv;
18400 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018401
18402 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018403 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018404 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018405 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18406 &len, evaluate, selfdict);
18407
18408 /* Clear the funcref afterwards, so that deleting it while
18409 * evaluating the arguments is possible (see test55). */
18410 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018411
18412 /* Stop the expression evaluation when immediately aborting on
18413 * error, or when an interrupt occurred or an exception was thrown
18414 * but not caught. */
18415 if (aborting())
18416 {
18417 if (ret == OK)
18418 clear_tv(rettv);
18419 ret = FAIL;
18420 }
18421 dict_unref(selfdict);
18422 selfdict = NULL;
18423 }
18424 else /* **arg == '[' || **arg == '.' */
18425 {
18426 dict_unref(selfdict);
18427 if (rettv->v_type == VAR_DICT)
18428 {
18429 selfdict = rettv->vval.v_dict;
18430 if (selfdict != NULL)
18431 ++selfdict->dv_refcount;
18432 }
18433 else
18434 selfdict = NULL;
18435 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18436 {
18437 clear_tv(rettv);
18438 ret = FAIL;
18439 }
18440 }
18441 }
18442 dict_unref(selfdict);
18443 return ret;
18444}
18445
18446/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018447 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018448 * value).
18449 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018451alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018452{
Bram Moolenaar33570922005-01-25 22:26:29 +000018453 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018454}
18455
18456/*
18457 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 * The string "s" must have been allocated, it is consumed.
18459 * Return NULL for out of memory, the variable otherwise.
18460 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018461 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018462alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 char_u *s;
18464{
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 rettv = alloc_tv();
18468 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470 rettv->v_type = VAR_STRING;
18471 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 }
18473 else
18474 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018475 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476}
18477
18478/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018479 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018481 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018483 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484{
18485 if (varp != NULL)
18486 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018487 switch (varp->v_type)
18488 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018489 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018490 func_unref(varp->vval.v_string);
18491 /*FALLTHROUGH*/
18492 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018493 vim_free(varp->vval.v_string);
18494 break;
18495 case VAR_LIST:
18496 list_unref(varp->vval.v_list);
18497 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018498 case VAR_DICT:
18499 dict_unref(varp->vval.v_dict);
18500 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018501 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018502#ifdef FEAT_FLOAT
18503 case VAR_FLOAT:
18504#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018505 case VAR_UNKNOWN:
18506 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018507 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018508 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018509 break;
18510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 vim_free(varp);
18512 }
18513}
18514
18515/*
18516 * Free the memory for a variable value and set the value to NULL or 0.
18517 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018518 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018519clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018520 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521{
18522 if (varp != NULL)
18523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018524 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018526 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018527 func_unref(varp->vval.v_string);
18528 /*FALLTHROUGH*/
18529 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018530 vim_free(varp->vval.v_string);
18531 varp->vval.v_string = NULL;
18532 break;
18533 case VAR_LIST:
18534 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018535 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018536 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018537 case VAR_DICT:
18538 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018539 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018540 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018542 varp->vval.v_number = 0;
18543 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018544#ifdef FEAT_FLOAT
18545 case VAR_FLOAT:
18546 varp->vval.v_float = 0.0;
18547 break;
18548#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018549 case VAR_UNKNOWN:
18550 break;
18551 default:
18552 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018554 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 }
18556}
18557
18558/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559 * Set the value of a variable to NULL without freeing items.
18560 */
18561 static void
18562init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018563 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018564{
18565 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018566 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567}
18568
18569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 * Get the number value of a variable.
18571 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018572 * For incompatible types, return 0.
18573 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18574 * caller of incompatible types: it sets *denote to TRUE if "denote"
18575 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 */
18577 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018579 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018581 int error = FALSE;
18582
18583 return get_tv_number_chk(varp, &error); /* return 0L on error */
18584}
18585
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018586 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018587get_tv_number_chk(varp, denote)
18588 typval_T *varp;
18589 int *denote;
18590{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018591 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018593 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018595 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018596 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018597#ifdef FEAT_FLOAT
18598 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018599 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018600 break;
18601#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018602 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018603 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018604 break;
18605 case VAR_STRING:
18606 if (varp->vval.v_string != NULL)
18607 vim_str2nr(varp->vval.v_string, NULL, NULL,
18608 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018610 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018611 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018612 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018613 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018614 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018615 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018616 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018617 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620 if (denote == NULL) /* useful for values that must be unsigned */
18621 n = -1;
18622 else
18623 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018624 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625}
18626
18627/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018628 * Get the lnum from the first argument.
18629 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018630 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 */
18632 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635{
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 linenr_T lnum;
18638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018639 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 if (lnum == 0) /* no valid number, try using line() */
18641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018642 rettv.v_type = VAR_NUMBER;
18643 f_line(argvars, &rettv);
18644 lnum = rettv.vval.v_number;
18645 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 }
18647 return lnum;
18648}
18649
18650/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018651 * Get the lnum from the first argument.
18652 * Also accepts "$", then "buf" is used.
18653 * Returns 0 on error.
18654 */
18655 static linenr_T
18656get_tv_lnum_buf(argvars, buf)
18657 typval_T *argvars;
18658 buf_T *buf;
18659{
18660 if (argvars[0].v_type == VAR_STRING
18661 && argvars[0].vval.v_string != NULL
18662 && argvars[0].vval.v_string[0] == '$'
18663 && buf != NULL)
18664 return buf->b_ml.ml_line_count;
18665 return get_tv_number_chk(&argvars[0], NULL);
18666}
18667
18668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 * Get the string value of a variable.
18670 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018671 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18672 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 * If the String variable has never been set, return an empty string.
18674 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018675 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18676 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 */
18678 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018679get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018680 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681{
18682 static char_u mybuf[NUMBUFLEN];
18683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685}
18686
18687 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018690 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018692 char_u *res = get_tv_string_buf_chk(varp, buf);
18693
18694 return res != NULL ? res : (char_u *)"";
18695}
18696
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018697 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018698get_tv_string_chk(varp)
18699 typval_T *varp;
18700{
18701 static char_u mybuf[NUMBUFLEN];
18702
18703 return get_tv_string_buf_chk(varp, mybuf);
18704}
18705
18706 static char_u *
18707get_tv_string_buf_chk(varp, buf)
18708 typval_T *varp;
18709 char_u *buf;
18710{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018711 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018713 case VAR_NUMBER:
18714 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18715 return buf;
18716 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018717 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018718 break;
18719 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018720 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018721 break;
18722 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018723 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018724 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018725#ifdef FEAT_FLOAT
18726 case VAR_FLOAT:
18727 EMSG(_("E806: using Float as a String"));
18728 break;
18729#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018730 case VAR_STRING:
18731 if (varp->vval.v_string != NULL)
18732 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018733 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018734 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018738 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739}
18740
18741/*
18742 * Find variable "name" in the list of variables.
18743 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018744 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018745 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018746 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018748 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018749find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018754 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755
Bram Moolenaara7043832005-01-21 11:56:39 +000018756 ht = find_var_ht(name, &varname);
18757 if (htp != NULL)
18758 *htp = ht;
18759 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018761 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762}
18763
18764/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018766 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018768 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018769find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018770 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018771 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018772 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018773{
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 hashitem_T *hi;
18775
18776 if (*varname == NUL)
18777 {
18778 /* Must be something like "s:", otherwise "ht" would be NULL. */
18779 switch (varname[-2])
18780 {
18781 case 's': return &SCRIPT_SV(current_SID).sv_var;
18782 case 'g': return &globvars_var;
18783 case 'v': return &vimvars_var;
18784 case 'b': return &curbuf->b_bufvar;
18785 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018786#ifdef FEAT_WINDOWS
18787 case 't': return &curtab->tp_winvar;
18788#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018789 case 'l': return current_funccal == NULL
18790 ? NULL : &current_funccal->l_vars_var;
18791 case 'a': return current_funccal == NULL
18792 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 }
18794 return NULL;
18795 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018796
18797 hi = hash_find(ht, varname);
18798 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018799 {
18800 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018801 * worked find the variable again. Don't auto-load a script if it was
18802 * loaded already, otherwise it would be loaded every time when
18803 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018804 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018805 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018806 hi = hash_find(ht, varname);
18807 if (HASHITEM_EMPTY(hi))
18808 return NULL;
18809 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018811}
18812
18813/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018815 * Set "varname" to the start of name without ':'.
18816 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018818find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 char_u *name;
18820 char_u **varname;
18821{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018822 hashitem_T *hi;
18823
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 if (name[1] != ':')
18825 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018826 /* The name must not start with a colon or #. */
18827 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 return NULL;
18829 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018830
18831 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018832 hi = hash_find(&compat_hashtab, name);
18833 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018834 return &compat_hashtab;
18835
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 return &globvarht; /* global variable */
18838 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 }
18840 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018841 if (*name == 'g') /* global variable */
18842 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018843 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18844 */
18845 if (vim_strchr(name + 2, ':') != NULL
18846 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018847 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018849 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018852#ifdef FEAT_WINDOWS
18853 if (*name == 't') /* tab page variable */
18854 return &curtab->tp_vars.dv_hashtab;
18855#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018856 if (*name == 'v') /* v: variable */
18857 return &vimvarht;
18858 if (*name == 'a' && current_funccal != NULL) /* function argument */
18859 return &current_funccal->l_avars.dv_hashtab;
18860 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18861 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 if (*name == 's' /* script variable */
18863 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18864 return &SCRIPT_VARS(current_SID);
18865 return NULL;
18866}
18867
18868/*
18869 * Get the string value of a (global/local) variable.
18870 * Returns NULL when it doesn't exist.
18871 */
18872 char_u *
18873get_var_value(name)
18874 char_u *name;
18875{
Bram Moolenaar33570922005-01-25 22:26:29 +000018876 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877
Bram Moolenaara7043832005-01-21 11:56:39 +000018878 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 if (v == NULL)
18880 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882}
18883
18884/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886 * sourcing this script and when executing functions defined in the script.
18887 */
18888 void
18889new_script_vars(id)
18890 scid_T id;
18891{
Bram Moolenaara7043832005-01-21 11:56:39 +000018892 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018893 hashtab_T *ht;
18894 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018895
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18897 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018898 /* Re-allocating ga_data means that an ht_array pointing to
18899 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018900 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018901 for (i = 1; i <= ga_scripts.ga_len; ++i)
18902 {
18903 ht = &SCRIPT_VARS(i);
18904 if (ht->ht_mask == HT_INIT_SIZE - 1)
18905 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 sv = &SCRIPT_SV(i);
18907 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018908 }
18909
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 while (ga_scripts.ga_len < id)
18911 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18913 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 }
18916 }
18917}
18918
18919/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018920 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18921 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 */
18923 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018924init_var_dict(dict, dict_var)
18925 dict_T *dict;
18926 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927{
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018929 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 dict_var->di_tv.vval.v_dict = dict;
18931 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018932 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018933 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18934 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935}
18936
18937/*
18938 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018939 * Frees all allocated variables and the value they contain.
18940 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 */
18942 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018943vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 hashtab_T *ht;
18945{
18946 vars_clear_ext(ht, TRUE);
18947}
18948
18949/*
18950 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18951 */
18952 static void
18953vars_clear_ext(ht, free_val)
18954 hashtab_T *ht;
18955 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956{
Bram Moolenaara7043832005-01-21 11:56:39 +000018957 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018958 hashitem_T *hi;
18959 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960
Bram Moolenaar33570922005-01-25 22:26:29 +000018961 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018962 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018963 for (hi = ht->ht_array; todo > 0; ++hi)
18964 {
18965 if (!HASHITEM_EMPTY(hi))
18966 {
18967 --todo;
18968
Bram Moolenaar33570922005-01-25 22:26:29 +000018969 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018970 * ht_array might change then. hash_clear() takes care of it
18971 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018972 v = HI2DI(hi);
18973 if (free_val)
18974 clear_tv(&v->di_tv);
18975 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18976 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018977 }
18978 }
18979 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018980 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981}
18982
Bram Moolenaara7043832005-01-21 11:56:39 +000018983/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 * Delete a variable from hashtab "ht" at item "hi".
18985 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018988delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 hashtab_T *ht;
18990 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991{
Bram Moolenaar33570922005-01-25 22:26:29 +000018992 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018993
18994 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018995 clear_tv(&di->di_tv);
18996 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997}
18998
18999/*
19000 * List the value of one internal variable.
19001 */
19002 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019003list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019006 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019008 char_u *tofree;
19009 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019010 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019011
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019012 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019013 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019014 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019015 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016}
19017
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019019list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 char_u *prefix;
19021 char_u *name;
19022 int type;
19023 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019024 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025{
Bram Moolenaar31859182007-08-14 20:41:13 +000019026 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19027 msg_start();
19028 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 if (name != NULL) /* "a:" vars don't have a name stored */
19030 msg_puts(name);
19031 msg_putchar(' ');
19032 msg_advance(22);
19033 if (type == VAR_NUMBER)
19034 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019035 else if (type == VAR_FUNC)
19036 msg_putchar('*');
19037 else if (type == VAR_LIST)
19038 {
19039 msg_putchar('[');
19040 if (*string == '[')
19041 ++string;
19042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019043 else if (type == VAR_DICT)
19044 {
19045 msg_putchar('{');
19046 if (*string == '{')
19047 ++string;
19048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 else
19050 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019051
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019053
19054 if (type == VAR_FUNC)
19055 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019056 if (*first)
19057 {
19058 msg_clr_eos();
19059 *first = FALSE;
19060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061}
19062
19063/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019064 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 * If the variable already exists, the value is updated.
19066 * Otherwise the variable is created.
19067 */
19068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019071 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019072 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073{
Bram Moolenaar33570922005-01-25 22:26:29 +000019074 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019076 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019077 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019080 {
19081 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19082 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19083 ? name[2] : name[0]))
19084 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019085 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019086 return;
19087 }
19088 if (function_exists(name))
19089 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019090 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019091 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019092 return;
19093 }
19094 }
19095
Bram Moolenaara7043832005-01-21 11:56:39 +000019096 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019097 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019098 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019099 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019100 return;
19101 }
19102
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019103 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019106 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019107 if (var_check_ro(v->di_flags, name)
19108 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019109 return;
19110 if (v->di_tv.v_type != tv->v_type
19111 && !((v->di_tv.v_type == VAR_STRING
19112 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019114 || tv->v_type == VAR_NUMBER))
19115#ifdef FEAT_FLOAT
19116 && !((v->di_tv.v_type == VAR_NUMBER
19117 || v->di_tv.v_type == VAR_FLOAT)
19118 && (tv->v_type == VAR_NUMBER
19119 || tv->v_type == VAR_FLOAT))
19120#endif
19121 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019122 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019123 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019124 return;
19125 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019126
19127 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019128 * Handle setting internal v: variables separately: we don't change
19129 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019130 */
19131 if (ht == &vimvarht)
19132 {
19133 if (v->di_tv.v_type == VAR_STRING)
19134 {
19135 vim_free(v->di_tv.vval.v_string);
19136 if (copy || tv->v_type != VAR_STRING)
19137 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19138 else
19139 {
19140 /* Take over the string to avoid an extra alloc/free. */
19141 v->di_tv.vval.v_string = tv->vval.v_string;
19142 tv->vval.v_string = NULL;
19143 }
19144 }
19145 else if (v->di_tv.v_type != VAR_NUMBER)
19146 EMSG2(_(e_intern2), "set_var()");
19147 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019148 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019149 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019150 if (STRCMP(varname, "searchforward") == 0)
19151 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19152 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 return;
19154 }
19155
19156 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 }
19158 else /* add a new variable */
19159 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019160 /* Can't add "v:" variable. */
19161 if (ht == &vimvarht)
19162 {
19163 EMSG2(_(e_illvar), name);
19164 return;
19165 }
19166
Bram Moolenaar92124a32005-06-17 22:03:40 +000019167 /* Make sure the variable name is valid. */
19168 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019169 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19170 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019171 {
19172 EMSG2(_(e_illvar), varname);
19173 return;
19174 }
19175
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019176 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19177 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019178 if (v == NULL)
19179 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019183 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 return;
19185 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019186 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019188
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019189 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019190 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019191 else
19192 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019194 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019195 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197}
19198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019200 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 * Also give an error message.
19202 */
19203 static int
19204var_check_ro(flags, name)
19205 int flags;
19206 char_u *name;
19207{
19208 if (flags & DI_FLAGS_RO)
19209 {
19210 EMSG2(_(e_readonlyvar), name);
19211 return TRUE;
19212 }
19213 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19214 {
19215 EMSG2(_(e_readonlysbx), name);
19216 return TRUE;
19217 }
19218 return FALSE;
19219}
19220
19221/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019222 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19223 * Also give an error message.
19224 */
19225 static int
19226var_check_fixed(flags, name)
19227 int flags;
19228 char_u *name;
19229{
19230 if (flags & DI_FLAGS_FIX)
19231 {
19232 EMSG2(_("E795: Cannot delete variable %s"), name);
19233 return TRUE;
19234 }
19235 return FALSE;
19236}
19237
19238/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019239 * Return TRUE if typeval "tv" is set to be locked (immutable).
19240 * Also give an error message, using "name".
19241 */
19242 static int
19243tv_check_lock(lock, name)
19244 int lock;
19245 char_u *name;
19246{
19247 if (lock & VAR_LOCKED)
19248 {
19249 EMSG2(_("E741: Value is locked: %s"),
19250 name == NULL ? (char_u *)_("Unknown") : name);
19251 return TRUE;
19252 }
19253 if (lock & VAR_FIXED)
19254 {
19255 EMSG2(_("E742: Cannot change value of %s"),
19256 name == NULL ? (char_u *)_("Unknown") : name);
19257 return TRUE;
19258 }
19259 return FALSE;
19260}
19261
19262/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019263 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019264 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019265 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019266 * It is OK for "from" and "to" to point to the same item. This is used to
19267 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 typval_T *from;
19272 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019275 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 switch (from->v_type)
19277 {
19278 case VAR_NUMBER:
19279 to->vval.v_number = from->vval.v_number;
19280 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019281#ifdef FEAT_FLOAT
19282 case VAR_FLOAT:
19283 to->vval.v_float = from->vval.v_float;
19284 break;
19285#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019286 case VAR_STRING:
19287 case VAR_FUNC:
19288 if (from->vval.v_string == NULL)
19289 to->vval.v_string = NULL;
19290 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019292 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019293 if (from->v_type == VAR_FUNC)
19294 func_ref(to->vval.v_string);
19295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019296 break;
19297 case VAR_LIST:
19298 if (from->vval.v_list == NULL)
19299 to->vval.v_list = NULL;
19300 else
19301 {
19302 to->vval.v_list = from->vval.v_list;
19303 ++to->vval.v_list->lv_refcount;
19304 }
19305 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019306 case VAR_DICT:
19307 if (from->vval.v_dict == NULL)
19308 to->vval.v_dict = NULL;
19309 else
19310 {
19311 to->vval.v_dict = from->vval.v_dict;
19312 ++to->vval.v_dict->dv_refcount;
19313 }
19314 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019315 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019316 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019317 break;
19318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319}
19320
19321/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019322 * Make a copy of an item.
19323 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019324 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19325 * reference to an already copied list/dict can be used.
19326 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019327 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019328 static int
19329item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019330 typval_T *from;
19331 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019332 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019333 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019334{
19335 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019336 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019337
Bram Moolenaar33570922005-01-25 22:26:29 +000019338 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019339 {
19340 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019341 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019342 }
19343 ++recurse;
19344
19345 switch (from->v_type)
19346 {
19347 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019348#ifdef FEAT_FLOAT
19349 case VAR_FLOAT:
19350#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019351 case VAR_STRING:
19352 case VAR_FUNC:
19353 copy_tv(from, to);
19354 break;
19355 case VAR_LIST:
19356 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019357 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019358 if (from->vval.v_list == NULL)
19359 to->vval.v_list = NULL;
19360 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19361 {
19362 /* use the copy made earlier */
19363 to->vval.v_list = from->vval.v_list->lv_copylist;
19364 ++to->vval.v_list->lv_refcount;
19365 }
19366 else
19367 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19368 if (to->vval.v_list == NULL)
19369 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019370 break;
19371 case VAR_DICT:
19372 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019373 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019374 if (from->vval.v_dict == NULL)
19375 to->vval.v_dict = NULL;
19376 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19377 {
19378 /* use the copy made earlier */
19379 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19380 ++to->vval.v_dict->dv_refcount;
19381 }
19382 else
19383 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19384 if (to->vval.v_dict == NULL)
19385 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019386 break;
19387 default:
19388 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019389 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019390 }
19391 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019392 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019393}
19394
19395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 * ":echo expr1 ..." print each argument separated with a space, add a
19397 * newline at the end.
19398 * ":echon expr1 ..." print each argument plain.
19399 */
19400 void
19401ex_echo(eap)
19402 exarg_T *eap;
19403{
19404 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019406 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 char_u *p;
19408 int needclr = TRUE;
19409 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019410 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411
19412 if (eap->skip)
19413 ++emsg_skip;
19414 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19415 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019416 /* If eval1() causes an error message the text from the command may
19417 * still need to be cleared. E.g., "echo 22,44". */
19418 need_clr_eos = needclr;
19419
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019421 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 {
19423 /*
19424 * Report the invalid expression unless the expression evaluation
19425 * has been cancelled due to an aborting error, an interrupt, or an
19426 * exception.
19427 */
19428 if (!aborting())
19429 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019430 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 break;
19432 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019433 need_clr_eos = FALSE;
19434
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 if (!eap->skip)
19436 {
19437 if (atstart)
19438 {
19439 atstart = FALSE;
19440 /* Call msg_start() after eval1(), evaluating the expression
19441 * may cause a message to appear. */
19442 if (eap->cmdidx == CMD_echo)
19443 msg_start();
19444 }
19445 else if (eap->cmdidx == CMD_echo)
19446 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019447 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019448 if (p != NULL)
19449 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019451 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019453 if (*p != TAB && needclr)
19454 {
19455 /* remove any text still there from the command */
19456 msg_clr_eos();
19457 needclr = FALSE;
19458 }
19459 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 }
19461 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019462 {
19463#ifdef FEAT_MBYTE
19464 if (has_mbyte)
19465 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019466 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019467
19468 (void)msg_outtrans_len_attr(p, i, echo_attr);
19469 p += i - 1;
19470 }
19471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019473 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019476 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019478 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 arg = skipwhite(arg);
19480 }
19481 eap->nextcmd = check_nextcmd(arg);
19482
19483 if (eap->skip)
19484 --emsg_skip;
19485 else
19486 {
19487 /* remove text that may still be there from the command */
19488 if (needclr)
19489 msg_clr_eos();
19490 if (eap->cmdidx == CMD_echo)
19491 msg_end();
19492 }
19493}
19494
19495/*
19496 * ":echohl {name}".
19497 */
19498 void
19499ex_echohl(eap)
19500 exarg_T *eap;
19501{
19502 int id;
19503
19504 id = syn_name2id(eap->arg);
19505 if (id == 0)
19506 echo_attr = 0;
19507 else
19508 echo_attr = syn_id2attr(id);
19509}
19510
19511/*
19512 * ":execute expr1 ..." execute the result of an expression.
19513 * ":echomsg expr1 ..." Print a message
19514 * ":echoerr expr1 ..." Print an error
19515 * Each gets spaces around each argument and a newline at the end for
19516 * echo commands
19517 */
19518 void
19519ex_execute(eap)
19520 exarg_T *eap;
19521{
19522 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019523 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 int ret = OK;
19525 char_u *p;
19526 garray_T ga;
19527 int len;
19528 int save_did_emsg;
19529
19530 ga_init2(&ga, 1, 80);
19531
19532 if (eap->skip)
19533 ++emsg_skip;
19534 while (*arg != NUL && *arg != '|' && *arg != '\n')
19535 {
19536 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 {
19539 /*
19540 * Report the invalid expression unless the expression evaluation
19541 * has been cancelled due to an aborting error, an interrupt, or an
19542 * exception.
19543 */
19544 if (!aborting())
19545 EMSG2(_(e_invexpr2), p);
19546 ret = FAIL;
19547 break;
19548 }
19549
19550 if (!eap->skip)
19551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019552 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 len = (int)STRLEN(p);
19554 if (ga_grow(&ga, len + 2) == FAIL)
19555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 ret = FAIL;
19558 break;
19559 }
19560 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 ga.ga_len += len;
19564 }
19565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019566 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 arg = skipwhite(arg);
19568 }
19569
19570 if (ret != FAIL && ga.ga_data != NULL)
19571 {
19572 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019575 out_flush();
19576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 else if (eap->cmdidx == CMD_echoerr)
19578 {
19579 /* We don't want to abort following commands, restore did_emsg. */
19580 save_did_emsg = did_emsg;
19581 EMSG((char_u *)ga.ga_data);
19582 if (!force_abort)
19583 did_emsg = save_did_emsg;
19584 }
19585 else if (eap->cmdidx == CMD_execute)
19586 do_cmdline((char_u *)ga.ga_data,
19587 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19588 }
19589
19590 ga_clear(&ga);
19591
19592 if (eap->skip)
19593 --emsg_skip;
19594
19595 eap->nextcmd = check_nextcmd(arg);
19596}
19597
19598/*
19599 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19600 * "arg" points to the "&" or '+' when called, to "option" when returning.
19601 * Returns NULL when no option name found. Otherwise pointer to the char
19602 * after the option name.
19603 */
19604 static char_u *
19605find_option_end(arg, opt_flags)
19606 char_u **arg;
19607 int *opt_flags;
19608{
19609 char_u *p = *arg;
19610
19611 ++p;
19612 if (*p == 'g' && p[1] == ':')
19613 {
19614 *opt_flags = OPT_GLOBAL;
19615 p += 2;
19616 }
19617 else if (*p == 'l' && p[1] == ':')
19618 {
19619 *opt_flags = OPT_LOCAL;
19620 p += 2;
19621 }
19622 else
19623 *opt_flags = 0;
19624
19625 if (!ASCII_ISALPHA(*p))
19626 return NULL;
19627 *arg = p;
19628
19629 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19630 p += 4; /* termcap option */
19631 else
19632 while (ASCII_ISALPHA(*p))
19633 ++p;
19634 return p;
19635}
19636
19637/*
19638 * ":function"
19639 */
19640 void
19641ex_function(eap)
19642 exarg_T *eap;
19643{
19644 char_u *theline;
19645 int j;
19646 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 char_u *name = NULL;
19649 char_u *p;
19650 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019651 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 garray_T newargs;
19653 garray_T newlines;
19654 int varargs = FALSE;
19655 int mustend = FALSE;
19656 int flags = 0;
19657 ufunc_T *fp;
19658 int indent;
19659 int nesting;
19660 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 dictitem_T *v;
19662 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019663 static int func_nr = 0; /* number for nameless function */
19664 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019665 hashtab_T *ht;
19666 int todo;
19667 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019668 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669
19670 /*
19671 * ":function" without argument: list functions.
19672 */
19673 if (ends_excmd(*eap->arg))
19674 {
19675 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019676 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019677 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019678 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019679 {
19680 if (!HASHITEM_EMPTY(hi))
19681 {
19682 --todo;
19683 fp = HI2UF(hi);
19684 if (!isdigit(*fp->uf_name))
19685 list_func_head(fp, FALSE);
19686 }
19687 }
19688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 eap->nextcmd = check_nextcmd(eap->arg);
19690 return;
19691 }
19692
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019693 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019694 * ":function /pat": list functions matching pattern.
19695 */
19696 if (*eap->arg == '/')
19697 {
19698 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19699 if (!eap->skip)
19700 {
19701 regmatch_T regmatch;
19702
19703 c = *p;
19704 *p = NUL;
19705 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19706 *p = c;
19707 if (regmatch.regprog != NULL)
19708 {
19709 regmatch.rm_ic = p_ic;
19710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019711 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019712 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19713 {
19714 if (!HASHITEM_EMPTY(hi))
19715 {
19716 --todo;
19717 fp = HI2UF(hi);
19718 if (!isdigit(*fp->uf_name)
19719 && vim_regexec(&regmatch, fp->uf_name, 0))
19720 list_func_head(fp, FALSE);
19721 }
19722 }
19723 }
19724 }
19725 if (*p == '/')
19726 ++p;
19727 eap->nextcmd = check_nextcmd(p);
19728 return;
19729 }
19730
19731 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019732 * Get the function name. There are these situations:
19733 * func normal function name
19734 * "name" == func, "fudi.fd_dict" == NULL
19735 * dict.func new dictionary entry
19736 * "name" == NULL, "fudi.fd_dict" set,
19737 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19738 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019739 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019740 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19741 * dict.func existing dict entry that's not a Funcref
19742 * "name" == NULL, "fudi.fd_dict" set,
19743 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019746 name = trans_function_name(&p, eap->skip, 0, &fudi);
19747 paren = (vim_strchr(p, '(') != NULL);
19748 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 {
19750 /*
19751 * Return on an invalid expression in braces, unless the expression
19752 * evaluation has been cancelled due to an aborting error, an
19753 * interrupt, or an exception.
19754 */
19755 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019756 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019757 if (!eap->skip && fudi.fd_newkey != NULL)
19758 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019759 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 else
19763 eap->skip = TRUE;
19764 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019765
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 /* An error in a function call during evaluation of an expression in magic
19767 * braces should not cause the function not to be defined. */
19768 saved_did_emsg = did_emsg;
19769 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770
19771 /*
19772 * ":function func" with only function name: list function.
19773 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019774 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 {
19776 if (!ends_excmd(*skipwhite(p)))
19777 {
19778 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019779 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 }
19781 eap->nextcmd = check_nextcmd(p);
19782 if (eap->nextcmd != NULL)
19783 *p = NUL;
19784 if (!eap->skip && !got_int)
19785 {
19786 fp = find_func(name);
19787 if (fp != NULL)
19788 {
19789 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019790 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019792 if (FUNCLINE(fp, j) == NULL)
19793 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 msg_putchar('\n');
19795 msg_outnum((long)(j + 1));
19796 if (j < 9)
19797 msg_putchar(' ');
19798 if (j < 99)
19799 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019800 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 out_flush(); /* show a line at a time */
19802 ui_breakcheck();
19803 }
19804 if (!got_int)
19805 {
19806 msg_putchar('\n');
19807 msg_puts((char_u *)" endfunction");
19808 }
19809 }
19810 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019811 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019813 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 }
19815
19816 /*
19817 * ":function name(arg1, arg2)" Define function.
19818 */
19819 p = skipwhite(p);
19820 if (*p != '(')
19821 {
19822 if (!eap->skip)
19823 {
19824 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019825 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 }
19827 /* attempt to continue by skipping some text */
19828 if (vim_strchr(p, '(') != NULL)
19829 p = vim_strchr(p, '(');
19830 }
19831 p = skipwhite(p + 1);
19832
19833 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19834 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19835
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019836 if (!eap->skip)
19837 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019838 /* Check the name of the function. Unless it's a dictionary function
19839 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019840 if (name != NULL)
19841 arg = name;
19842 else
19843 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019844 if (arg != NULL && (fudi.fd_di == NULL
19845 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019846 {
19847 if (*arg == K_SPECIAL)
19848 j = 3;
19849 else
19850 j = 0;
19851 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19852 : eval_isnamec(arg[j])))
19853 ++j;
19854 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000019855 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019856 }
19857 }
19858
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 /*
19860 * Isolate the arguments: "arg1, arg2, ...)"
19861 */
19862 while (*p != ')')
19863 {
19864 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19865 {
19866 varargs = TRUE;
19867 p += 3;
19868 mustend = TRUE;
19869 }
19870 else
19871 {
19872 arg = p;
19873 while (ASCII_ISALNUM(*p) || *p == '_')
19874 ++p;
19875 if (arg == p || isdigit(*arg)
19876 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19877 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19878 {
19879 if (!eap->skip)
19880 EMSG2(_("E125: Illegal argument: %s"), arg);
19881 break;
19882 }
19883 if (ga_grow(&newargs, 1) == FAIL)
19884 goto erret;
19885 c = *p;
19886 *p = NUL;
19887 arg = vim_strsave(arg);
19888 if (arg == NULL)
19889 goto erret;
19890 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19891 *p = c;
19892 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 if (*p == ',')
19894 ++p;
19895 else
19896 mustend = TRUE;
19897 }
19898 p = skipwhite(p);
19899 if (mustend && *p != ')')
19900 {
19901 if (!eap->skip)
19902 EMSG2(_(e_invarg2), eap->arg);
19903 break;
19904 }
19905 }
19906 ++p; /* skip the ')' */
19907
Bram Moolenaare9a41262005-01-15 22:18:47 +000019908 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 for (;;)
19910 {
19911 p = skipwhite(p);
19912 if (STRNCMP(p, "range", 5) == 0)
19913 {
19914 flags |= FC_RANGE;
19915 p += 5;
19916 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019917 else if (STRNCMP(p, "dict", 4) == 0)
19918 {
19919 flags |= FC_DICT;
19920 p += 4;
19921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 else if (STRNCMP(p, "abort", 5) == 0)
19923 {
19924 flags |= FC_ABORT;
19925 p += 5;
19926 }
19927 else
19928 break;
19929 }
19930
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019931 /* When there is a line break use what follows for the function body.
19932 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19933 if (*p == '\n')
19934 line_arg = p + 1;
19935 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 EMSG(_(e_trailing));
19937
19938 /*
19939 * Read the body of the function, until ":endfunction" is found.
19940 */
19941 if (KeyTyped)
19942 {
19943 /* Check if the function already exists, don't let the user type the
19944 * whole function before telling him it doesn't work! For a script we
19945 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019946 if (!eap->skip && !eap->forceit)
19947 {
19948 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19949 EMSG(_(e_funcdict));
19950 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019951 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019954 if (!eap->skip && did_emsg)
19955 goto erret;
19956
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 msg_putchar('\n'); /* don't overwrite the function name */
19958 cmdline_row = msg_row;
19959 }
19960
19961 indent = 2;
19962 nesting = 0;
19963 for (;;)
19964 {
19965 msg_scroll = TRUE;
19966 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019967 sourcing_lnum_off = sourcing_lnum;
19968
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019969 if (line_arg != NULL)
19970 {
19971 /* Use eap->arg, split up in parts by line breaks. */
19972 theline = line_arg;
19973 p = vim_strchr(theline, '\n');
19974 if (p == NULL)
19975 line_arg += STRLEN(line_arg);
19976 else
19977 {
19978 *p = NUL;
19979 line_arg = p + 1;
19980 }
19981 }
19982 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 theline = getcmdline(':', 0L, indent);
19984 else
19985 theline = eap->getline(':', eap->cookie, indent);
19986 if (KeyTyped)
19987 lines_left = Rows - 1;
19988 if (theline == NULL)
19989 {
19990 EMSG(_("E126: Missing :endfunction"));
19991 goto erret;
19992 }
19993
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019994 /* Detect line continuation: sourcing_lnum increased more than one. */
19995 if (sourcing_lnum > sourcing_lnum_off + 1)
19996 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19997 else
19998 sourcing_lnum_off = 0;
19999
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 if (skip_until != NULL)
20001 {
20002 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20003 * don't check for ":endfunc". */
20004 if (STRCMP(theline, skip_until) == 0)
20005 {
20006 vim_free(skip_until);
20007 skip_until = NULL;
20008 }
20009 }
20010 else
20011 {
20012 /* skip ':' and blanks*/
20013 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20014 ;
20015
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020016 /* Check for "endfunction". */
20017 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020019 if (line_arg == NULL)
20020 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 break;
20022 }
20023
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020024 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 * at "end". */
20026 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20027 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020028 else if (STRNCMP(p, "if", 2) == 0
20029 || STRNCMP(p, "wh", 2) == 0
20030 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 || STRNCMP(p, "try", 3) == 0)
20032 indent += 2;
20033
20034 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020035 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020037 if (*p == '!')
20038 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 p += eval_fname_script(p);
20040 if (ASCII_ISALPHA(*p))
20041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020042 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 if (*skipwhite(p) == '(')
20044 {
20045 ++nesting;
20046 indent += 2;
20047 }
20048 }
20049 }
20050
20051 /* Check for ":append" or ":insert". */
20052 p = skip_range(p, NULL);
20053 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20054 || (p[0] == 'i'
20055 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20056 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20057 skip_until = vim_strsave((char_u *)".");
20058
20059 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20060 arg = skipwhite(skiptowhite(p));
20061 if (arg[0] == '<' && arg[1] =='<'
20062 && ((p[0] == 'p' && p[1] == 'y'
20063 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20064 || (p[0] == 'p' && p[1] == 'e'
20065 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20066 || (p[0] == 't' && p[1] == 'c'
20067 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20068 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20069 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020070 || (p[0] == 'm' && p[1] == 'z'
20071 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 ))
20073 {
20074 /* ":python <<" continues until a dot, like ":append" */
20075 p = skipwhite(arg + 2);
20076 if (*p == NUL)
20077 skip_until = vim_strsave((char_u *)".");
20078 else
20079 skip_until = vim_strsave(p);
20080 }
20081 }
20082
20083 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020084 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020085 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020086 if (line_arg == NULL)
20087 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020089 }
20090
20091 /* Copy the line to newly allocated memory. get_one_sourceline()
20092 * allocates 250 bytes per line, this saves 80% on average. The cost
20093 * is an extra alloc/free. */
20094 p = vim_strsave(theline);
20095 if (p != NULL)
20096 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020097 if (line_arg == NULL)
20098 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020099 theline = p;
20100 }
20101
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020102 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20103
20104 /* Add NULL lines for continuation lines, so that the line count is
20105 * equal to the index in the growarray. */
20106 while (sourcing_lnum_off-- > 0)
20107 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020108
20109 /* Check for end of eap->arg. */
20110 if (line_arg != NULL && *line_arg == NUL)
20111 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 }
20113
20114 /* Don't define the function when skipping commands or when an error was
20115 * detected. */
20116 if (eap->skip || did_emsg)
20117 goto erret;
20118
20119 /*
20120 * If there are no errors, add the function
20121 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020122 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020123 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020124 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020126 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020127 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020128 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020129 goto erret;
20130 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020132 fp = find_func(name);
20133 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020135 if (!eap->forceit)
20136 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020137 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020138 goto erret;
20139 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020140 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020141 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020142 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020143 name);
20144 goto erret;
20145 }
20146 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020147 ga_clear_strings(&(fp->uf_args));
20148 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020149 vim_free(name);
20150 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 }
20153 else
20154 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020155 char numbuf[20];
20156
20157 fp = NULL;
20158 if (fudi.fd_newkey == NULL && !eap->forceit)
20159 {
20160 EMSG(_(e_funcdict));
20161 goto erret;
20162 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020163 if (fudi.fd_di == NULL)
20164 {
20165 /* Can't add a function to a locked dictionary */
20166 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20167 goto erret;
20168 }
20169 /* Can't change an existing function if it is locked */
20170 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20171 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020172
20173 /* Give the function a sequential number. Can only be used with a
20174 * Funcref! */
20175 vim_free(name);
20176 sprintf(numbuf, "%d", ++func_nr);
20177 name = vim_strsave((char_u *)numbuf);
20178 if (name == NULL)
20179 goto erret;
20180 }
20181
20182 if (fp == NULL)
20183 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020184 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020185 {
20186 int slen, plen;
20187 char_u *scriptname;
20188
20189 /* Check that the autoload name matches the script name. */
20190 j = FAIL;
20191 if (sourcing_name != NULL)
20192 {
20193 scriptname = autoload_name(name);
20194 if (scriptname != NULL)
20195 {
20196 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020197 plen = (int)STRLEN(p);
20198 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020199 if (slen > plen && fnamecmp(p,
20200 sourcing_name + slen - plen) == 0)
20201 j = OK;
20202 vim_free(scriptname);
20203 }
20204 }
20205 if (j == FAIL)
20206 {
20207 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20208 goto erret;
20209 }
20210 }
20211
20212 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 if (fp == NULL)
20214 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020215
20216 if (fudi.fd_dict != NULL)
20217 {
20218 if (fudi.fd_di == NULL)
20219 {
20220 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020221 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020222 if (fudi.fd_di == NULL)
20223 {
20224 vim_free(fp);
20225 goto erret;
20226 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020227 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20228 {
20229 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020230 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020231 goto erret;
20232 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020233 }
20234 else
20235 /* overwrite existing dict entry */
20236 clear_tv(&fudi.fd_di->di_tv);
20237 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020238 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020239 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020240 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020241
20242 /* behave like "dict" was used */
20243 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020244 }
20245
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020247 STRCPY(fp->uf_name, name);
20248 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020250 fp->uf_args = newargs;
20251 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020252#ifdef FEAT_PROFILE
20253 fp->uf_tml_count = NULL;
20254 fp->uf_tml_total = NULL;
20255 fp->uf_tml_self = NULL;
20256 fp->uf_profiling = FALSE;
20257 if (prof_def_func())
20258 func_do_profile(fp);
20259#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020260 fp->uf_varargs = varargs;
20261 fp->uf_flags = flags;
20262 fp->uf_calls = 0;
20263 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020264 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265
20266erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 ga_clear_strings(&newargs);
20268 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020269ret_free:
20270 vim_free(skip_until);
20271 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274}
20275
20276/*
20277 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020278 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020280 * flags:
20281 * TFN_INT: internal function name OK
20282 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 * Advances "pp" to just after the function name (if no error).
20284 */
20285 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020286trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 char_u **pp;
20288 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020289 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020292 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 char_u *start;
20294 char_u *end;
20295 int lead;
20296 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020298 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020299
20300 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020303
20304 /* Check for hard coded <SNR>: already translated function ID (from a user
20305 * command). */
20306 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20307 && (*pp)[2] == (int)KE_SNR)
20308 {
20309 *pp += 3;
20310 len = get_id_len(pp) + 3;
20311 return vim_strnsave(start, len);
20312 }
20313
20314 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20315 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020317 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020319
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020320 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20321 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020322 if (end == start)
20323 {
20324 if (!skip)
20325 EMSG(_("E129: Function name required"));
20326 goto theend;
20327 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020328 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020329 {
20330 /*
20331 * Report an invalid expression in braces, unless the expression
20332 * evaluation has been cancelled due to an aborting error, an
20333 * interrupt, or an exception.
20334 */
20335 if (!aborting())
20336 {
20337 if (end != NULL)
20338 EMSG2(_(e_invarg2), start);
20339 }
20340 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020341 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020342 goto theend;
20343 }
20344
20345 if (lv.ll_tv != NULL)
20346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020347 if (fdp != NULL)
20348 {
20349 fdp->fd_dict = lv.ll_dict;
20350 fdp->fd_newkey = lv.ll_newkey;
20351 lv.ll_newkey = NULL;
20352 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020354 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20355 {
20356 name = vim_strsave(lv.ll_tv->vval.v_string);
20357 *pp = end;
20358 }
20359 else
20360 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020361 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20362 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020363 EMSG(_(e_funcref));
20364 else
20365 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020366 name = NULL;
20367 }
20368 goto theend;
20369 }
20370
20371 if (lv.ll_name == NULL)
20372 {
20373 /* Error found, but continue after the function name. */
20374 *pp = end;
20375 goto theend;
20376 }
20377
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020378 /* Check if the name is a Funcref. If so, use the value. */
20379 if (lv.ll_exp_name != NULL)
20380 {
20381 len = (int)STRLEN(lv.ll_exp_name);
20382 name = deref_func_name(lv.ll_exp_name, &len);
20383 if (name == lv.ll_exp_name)
20384 name = NULL;
20385 }
20386 else
20387 {
20388 len = (int)(end - *pp);
20389 name = deref_func_name(*pp, &len);
20390 if (name == *pp)
20391 name = NULL;
20392 }
20393 if (name != NULL)
20394 {
20395 name = vim_strsave(name);
20396 *pp = end;
20397 goto theend;
20398 }
20399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020400 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020401 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020402 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020403 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20404 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20405 {
20406 /* When there was "s:" already or the name expanded to get a
20407 * leading "s:" then remove it. */
20408 lv.ll_name += 2;
20409 len -= 2;
20410 lead = 2;
20411 }
20412 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020413 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020414 {
20415 if (lead == 2) /* skip over "s:" */
20416 lv.ll_name += 2;
20417 len = (int)(end - lv.ll_name);
20418 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020419
20420 /*
20421 * Copy the function name to allocated memory.
20422 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20423 * Accept <SNR>123_name() outside a script.
20424 */
20425 if (skip)
20426 lead = 0; /* do nothing */
20427 else if (lead > 0)
20428 {
20429 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020430 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20431 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020432 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020433 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020434 if (current_SID <= 0)
20435 {
20436 EMSG(_(e_usingsid));
20437 goto theend;
20438 }
20439 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20440 lead += (int)STRLEN(sid_buf);
20441 }
20442 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020443 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020444 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020445 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020446 goto theend;
20447 }
20448 name = alloc((unsigned)(len + lead + 1));
20449 if (name != NULL)
20450 {
20451 if (lead > 0)
20452 {
20453 name[0] = K_SPECIAL;
20454 name[1] = KS_EXTRA;
20455 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020456 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020457 STRCPY(name + 3, sid_buf);
20458 }
20459 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20460 name[len + lead] = NUL;
20461 }
20462 *pp = end;
20463
20464theend:
20465 clear_lval(&lv);
20466 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467}
20468
20469/*
20470 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20471 * Return 2 if "p" starts with "s:".
20472 * Return 0 otherwise.
20473 */
20474 static int
20475eval_fname_script(p)
20476 char_u *p;
20477{
20478 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20479 || STRNICMP(p + 1, "SNR>", 4) == 0))
20480 return 5;
20481 if (p[0] == 's' && p[1] == ':')
20482 return 2;
20483 return 0;
20484}
20485
20486/*
20487 * Return TRUE if "p" starts with "<SID>" or "s:".
20488 * Only works if eval_fname_script() returned non-zero for "p"!
20489 */
20490 static int
20491eval_fname_sid(p)
20492 char_u *p;
20493{
20494 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20495}
20496
20497/*
20498 * List the head of the function: "name(arg1, arg2)".
20499 */
20500 static void
20501list_func_head(fp, indent)
20502 ufunc_T *fp;
20503 int indent;
20504{
20505 int j;
20506
20507 msg_start();
20508 if (indent)
20509 MSG_PUTS(" ");
20510 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020511 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 {
20513 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020514 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 }
20516 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020517 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020519 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 {
20521 if (j)
20522 MSG_PUTS(", ");
20523 msg_puts(FUNCARG(fp, j));
20524 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020525 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 {
20527 if (j)
20528 MSG_PUTS(", ");
20529 MSG_PUTS("...");
20530 }
20531 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020532 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020533 if (p_verbose > 0)
20534 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535}
20536
20537/*
20538 * Find a function by name, return pointer to it in ufuncs.
20539 * Return NULL for unknown function.
20540 */
20541 static ufunc_T *
20542find_func(name)
20543 char_u *name;
20544{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020545 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020547 hi = hash_find(&func_hashtab, name);
20548 if (!HASHITEM_EMPTY(hi))
20549 return HI2UF(hi);
20550 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551}
20552
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020553#if defined(EXITFREE) || defined(PROTO)
20554 void
20555free_all_functions()
20556{
20557 hashitem_T *hi;
20558
20559 /* Need to start all over every time, because func_free() may change the
20560 * hash table. */
20561 while (func_hashtab.ht_used > 0)
20562 for (hi = func_hashtab.ht_array; ; ++hi)
20563 if (!HASHITEM_EMPTY(hi))
20564 {
20565 func_free(HI2UF(hi));
20566 break;
20567 }
20568}
20569#endif
20570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020571/*
20572 * Return TRUE if a function "name" exists.
20573 */
20574 static int
20575function_exists(name)
20576 char_u *name;
20577{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020578 char_u *nm = name;
20579 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020580 int n = FALSE;
20581
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020582 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020583 nm = skipwhite(nm);
20584
20585 /* Only accept "funcname", "funcname ", "funcname (..." and
20586 * "funcname(...", not "funcname!...". */
20587 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020588 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020589 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020591 else
20592 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020593 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020594 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020595 return n;
20596}
20597
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020598/*
20599 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020600 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020601 */
20602 static int
20603builtin_function(name)
20604 char_u *name;
20605{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020606 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20607 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020608}
20609
Bram Moolenaar05159a02005-02-26 23:04:13 +000020610#if defined(FEAT_PROFILE) || defined(PROTO)
20611/*
20612 * Start profiling function "fp".
20613 */
20614 static void
20615func_do_profile(fp)
20616 ufunc_T *fp;
20617{
20618 fp->uf_tm_count = 0;
20619 profile_zero(&fp->uf_tm_self);
20620 profile_zero(&fp->uf_tm_total);
20621 if (fp->uf_tml_count == NULL)
20622 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20623 (sizeof(int) * fp->uf_lines.ga_len));
20624 if (fp->uf_tml_total == NULL)
20625 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20626 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20627 if (fp->uf_tml_self == NULL)
20628 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20629 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20630 fp->uf_tml_idx = -1;
20631 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20632 || fp->uf_tml_self == NULL)
20633 return; /* out of memory */
20634
20635 fp->uf_profiling = TRUE;
20636}
20637
20638/*
20639 * Dump the profiling results for all functions in file "fd".
20640 */
20641 void
20642func_dump_profile(fd)
20643 FILE *fd;
20644{
20645 hashitem_T *hi;
20646 int todo;
20647 ufunc_T *fp;
20648 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020649 ufunc_T **sorttab;
20650 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020651
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020652 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020653 if (todo == 0)
20654 return; /* nothing to dump */
20655
Bram Moolenaar73830342005-02-28 22:48:19 +000020656 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20657
Bram Moolenaar05159a02005-02-26 23:04:13 +000020658 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20659 {
20660 if (!HASHITEM_EMPTY(hi))
20661 {
20662 --todo;
20663 fp = HI2UF(hi);
20664 if (fp->uf_profiling)
20665 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020666 if (sorttab != NULL)
20667 sorttab[st_len++] = fp;
20668
Bram Moolenaar05159a02005-02-26 23:04:13 +000020669 if (fp->uf_name[0] == K_SPECIAL)
20670 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20671 else
20672 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20673 if (fp->uf_tm_count == 1)
20674 fprintf(fd, "Called 1 time\n");
20675 else
20676 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20677 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20678 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20679 fprintf(fd, "\n");
20680 fprintf(fd, "count total (s) self (s)\n");
20681
20682 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20683 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020684 if (FUNCLINE(fp, i) == NULL)
20685 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020686 prof_func_line(fd, fp->uf_tml_count[i],
20687 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020688 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20689 }
20690 fprintf(fd, "\n");
20691 }
20692 }
20693 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020694
20695 if (sorttab != NULL && st_len > 0)
20696 {
20697 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20698 prof_total_cmp);
20699 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20700 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20701 prof_self_cmp);
20702 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20703 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020704
20705 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020706}
Bram Moolenaar73830342005-02-28 22:48:19 +000020707
20708 static void
20709prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20710 FILE *fd;
20711 ufunc_T **sorttab;
20712 int st_len;
20713 char *title;
20714 int prefer_self; /* when equal print only self time */
20715{
20716 int i;
20717 ufunc_T *fp;
20718
20719 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20720 fprintf(fd, "count total (s) self (s) function\n");
20721 for (i = 0; i < 20 && i < st_len; ++i)
20722 {
20723 fp = sorttab[i];
20724 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20725 prefer_self);
20726 if (fp->uf_name[0] == K_SPECIAL)
20727 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20728 else
20729 fprintf(fd, " %s()\n", fp->uf_name);
20730 }
20731 fprintf(fd, "\n");
20732}
20733
20734/*
20735 * Print the count and times for one function or function line.
20736 */
20737 static void
20738prof_func_line(fd, count, total, self, prefer_self)
20739 FILE *fd;
20740 int count;
20741 proftime_T *total;
20742 proftime_T *self;
20743 int prefer_self; /* when equal print only self time */
20744{
20745 if (count > 0)
20746 {
20747 fprintf(fd, "%5d ", count);
20748 if (prefer_self && profile_equal(total, self))
20749 fprintf(fd, " ");
20750 else
20751 fprintf(fd, "%s ", profile_msg(total));
20752 if (!prefer_self && profile_equal(total, self))
20753 fprintf(fd, " ");
20754 else
20755 fprintf(fd, "%s ", profile_msg(self));
20756 }
20757 else
20758 fprintf(fd, " ");
20759}
20760
20761/*
20762 * Compare function for total time sorting.
20763 */
20764 static int
20765#ifdef __BORLANDC__
20766_RTLENTRYF
20767#endif
20768prof_total_cmp(s1, s2)
20769 const void *s1;
20770 const void *s2;
20771{
20772 ufunc_T *p1, *p2;
20773
20774 p1 = *(ufunc_T **)s1;
20775 p2 = *(ufunc_T **)s2;
20776 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20777}
20778
20779/*
20780 * Compare function for self time sorting.
20781 */
20782 static int
20783#ifdef __BORLANDC__
20784_RTLENTRYF
20785#endif
20786prof_self_cmp(s1, s2)
20787 const void *s1;
20788 const void *s2;
20789{
20790 ufunc_T *p1, *p2;
20791
20792 p1 = *(ufunc_T **)s1;
20793 p2 = *(ufunc_T **)s2;
20794 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20795}
20796
Bram Moolenaar05159a02005-02-26 23:04:13 +000020797#endif
20798
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020799/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020800 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020801 * Return TRUE if a package was loaded.
20802 */
20803 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020804script_autoload(name, reload)
20805 char_u *name;
20806 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020807{
20808 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020809 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020810 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020811 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020812
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020813 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020814 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020815 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020816 return FALSE;
20817
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020818 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020819
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020820 /* Find the name in the list of previously loaded package names. Skip
20821 * "autoload/", it's always the same. */
20822 for (i = 0; i < ga_loaded.ga_len; ++i)
20823 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20824 break;
20825 if (!reload && i < ga_loaded.ga_len)
20826 ret = FALSE; /* was loaded already */
20827 else
20828 {
20829 /* Remember the name if it wasn't loaded already. */
20830 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20831 {
20832 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20833 tofree = NULL;
20834 }
20835
20836 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020837 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020838 ret = TRUE;
20839 }
20840
20841 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020842 return ret;
20843}
20844
20845/*
20846 * Return the autoload script name for a function or variable name.
20847 * Returns NULL when out of memory.
20848 */
20849 static char_u *
20850autoload_name(name)
20851 char_u *name;
20852{
20853 char_u *p;
20854 char_u *scriptname;
20855
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020856 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020857 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20858 if (scriptname == NULL)
20859 return FALSE;
20860 STRCPY(scriptname, "autoload/");
20861 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020862 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020863 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020864 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020865 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020866 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020867}
20868
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20870
20871/*
20872 * Function given to ExpandGeneric() to obtain the list of user defined
20873 * function names.
20874 */
20875 char_u *
20876get_user_func_name(xp, idx)
20877 expand_T *xp;
20878 int idx;
20879{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020880 static long_u done;
20881 static hashitem_T *hi;
20882 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883
20884 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020886 done = 0;
20887 hi = func_hashtab.ht_array;
20888 }
20889 if (done < func_hashtab.ht_used)
20890 {
20891 if (done++ > 0)
20892 ++hi;
20893 while (HASHITEM_EMPTY(hi))
20894 ++hi;
20895 fp = HI2UF(hi);
20896
20897 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20898 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899
20900 cat_func_name(IObuff, fp);
20901 if (xp->xp_context != EXPAND_USER_FUNC)
20902 {
20903 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020904 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 STRCAT(IObuff, ")");
20906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 return IObuff;
20908 }
20909 return NULL;
20910}
20911
20912#endif /* FEAT_CMDL_COMPL */
20913
20914/*
20915 * Copy the function name of "fp" to buffer "buf".
20916 * "buf" must be able to hold the function name plus three bytes.
20917 * Takes care of script-local function names.
20918 */
20919 static void
20920cat_func_name(buf, fp)
20921 char_u *buf;
20922 ufunc_T *fp;
20923{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020924 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 {
20926 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020927 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 }
20929 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020930 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931}
20932
20933/*
20934 * ":delfunction {name}"
20935 */
20936 void
20937ex_delfunction(eap)
20938 exarg_T *eap;
20939{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020940 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 char_u *p;
20942 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020943 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944
20945 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020946 name = trans_function_name(&p, eap->skip, 0, &fudi);
20947 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020949 {
20950 if (fudi.fd_dict != NULL && !eap->skip)
20951 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 if (!ends_excmd(*skipwhite(p)))
20955 {
20956 vim_free(name);
20957 EMSG(_(e_trailing));
20958 return;
20959 }
20960 eap->nextcmd = check_nextcmd(p);
20961 if (eap->nextcmd != NULL)
20962 *p = NUL;
20963
20964 if (!eap->skip)
20965 fp = find_func(name);
20966 vim_free(name);
20967
20968 if (!eap->skip)
20969 {
20970 if (fp == NULL)
20971 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020972 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 return;
20974 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020975 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 {
20977 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20978 return;
20979 }
20980
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020981 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 /* Delete the dict item that refers to the function, it will
20984 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020985 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020987 else
20988 func_free(fp);
20989 }
20990}
20991
20992/*
20993 * Free a function and remove it from the list of functions.
20994 */
20995 static void
20996func_free(fp)
20997 ufunc_T *fp;
20998{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020999 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021000
21001 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021002 ga_clear_strings(&(fp->uf_args));
21003 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021004#ifdef FEAT_PROFILE
21005 vim_free(fp->uf_tml_count);
21006 vim_free(fp->uf_tml_total);
21007 vim_free(fp->uf_tml_self);
21008#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021009
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021010 /* remove the function from the function hashtable */
21011 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21012 if (HASHITEM_EMPTY(hi))
21013 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021014 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021015 hash_remove(&func_hashtab, hi);
21016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021017 vim_free(fp);
21018}
21019
21020/*
21021 * Unreference a Function: decrement the reference count and free it when it
21022 * becomes zero. Only for numbered functions.
21023 */
21024 static void
21025func_unref(name)
21026 char_u *name;
21027{
21028 ufunc_T *fp;
21029
21030 if (name != NULL && isdigit(*name))
21031 {
21032 fp = find_func(name);
21033 if (fp == NULL)
21034 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021035 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021036 {
21037 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021038 * when "uf_calls" becomes zero. */
21039 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021040 func_free(fp);
21041 }
21042 }
21043}
21044
21045/*
21046 * Count a reference to a Function.
21047 */
21048 static void
21049func_ref(name)
21050 char_u *name;
21051{
21052 ufunc_T *fp;
21053
21054 if (name != NULL && isdigit(*name))
21055 {
21056 fp = find_func(name);
21057 if (fp == NULL)
21058 EMSG2(_(e_intern2), "func_ref()");
21059 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021060 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 }
21062}
21063
21064/*
21065 * Call a user function.
21066 */
21067 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021068call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 ufunc_T *fp; /* pointer to function */
21070 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021071 typval_T *argvars; /* arguments */
21072 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 linenr_T firstline; /* first line of range */
21074 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021075 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076{
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 char_u *save_sourcing_name;
21078 linenr_T save_sourcing_lnum;
21079 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021080 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021081 int save_did_emsg;
21082 static int depth = 0;
21083 dictitem_T *v;
21084 int fixvar_idx = 0; /* index in fixvar[] */
21085 int i;
21086 int ai;
21087 char_u numbuf[NUMBUFLEN];
21088 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021089#ifdef FEAT_PROFILE
21090 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021091 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093
21094 /* If depth of calling is getting too high, don't execute the function */
21095 if (depth >= p_mfd)
21096 {
21097 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 rettv->v_type = VAR_NUMBER;
21099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 return;
21101 }
21102 ++depth;
21103
21104 line_breakcheck(); /* check for CTRL-C hit */
21105
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021106 fc = (funccall_T *)alloc(sizeof(funccall_T));
21107 fc->caller = current_funccal;
21108 current_funccal = fc;
21109 fc->func = fp;
21110 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021112 fc->linenr = 0;
21113 fc->returned = FALSE;
21114 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021116 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21117 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021120 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021121 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21122 * each argument variable and saves a lot of time.
21123 */
21124 /*
21125 * Init l: variables.
21126 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021127 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021128 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021129 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021130 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21131 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021132 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021133 name = v->di_key;
21134 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021136 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021137 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021138 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021139 v->di_tv.vval.v_dict = selfdict;
21140 ++selfdict->dv_refcount;
21141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021142
Bram Moolenaar33570922005-01-25 22:26:29 +000021143 /*
21144 * Init a: variables.
21145 * Set a:0 to "argcount".
21146 * Set a:000 to a list with room for the "..." arguments.
21147 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021148 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21149 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021150 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021151 /* Use "name" to avoid a warning from some compiler that checks the
21152 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021153 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021154 name = v->di_key;
21155 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021156 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021157 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021158 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021159 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021160 v->di_tv.vval.v_list = &fc->l_varlist;
21161 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21162 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21163 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021164
21165 /*
21166 * Set a:firstline to "firstline" and a:lastline to "lastline".
21167 * Set a:name to named arguments.
21168 * Set a:N to the "..." arguments.
21169 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021170 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021171 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021172 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021173 (varnumber_T)lastline);
21174 for (i = 0; i < argcount; ++i)
21175 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021176 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021177 if (ai < 0)
21178 /* named argument a:name */
21179 name = FUNCARG(fp, i);
21180 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021182 /* "..." argument a:1, a:2, etc. */
21183 sprintf((char *)numbuf, "%d", ai + 1);
21184 name = numbuf;
21185 }
21186 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21187 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021188 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021189 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21190 }
21191 else
21192 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021193 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21194 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021195 if (v == NULL)
21196 break;
21197 v->di_flags = DI_FLAGS_RO;
21198 }
21199 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021200 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021201
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021202 /* Note: the values are copied directly to avoid alloc/free.
21203 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021204 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021205 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021206
21207 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21208 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021209 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21210 fc->l_listitems[ai].li_tv = argvars[i];
21211 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021212 }
21213 }
21214
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 /* Don't redraw while executing the function. */
21216 ++RedrawingDisabled;
21217 save_sourcing_name = sourcing_name;
21218 save_sourcing_lnum = sourcing_lnum;
21219 sourcing_lnum = 1;
21220 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021221 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 if (sourcing_name != NULL)
21223 {
21224 if (save_sourcing_name != NULL
21225 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21226 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21227 else
21228 STRCPY(sourcing_name, "function ");
21229 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21230
21231 if (p_verbose >= 12)
21232 {
21233 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021234 verbose_enter_scroll();
21235
Bram Moolenaar555b2802005-05-19 21:08:39 +000021236 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 if (p_verbose >= 14)
21238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021240 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021241 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021242 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243
21244 msg_puts((char_u *)"(");
21245 for (i = 0; i < argcount; ++i)
21246 {
21247 if (i > 0)
21248 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021249 if (argvars[i].v_type == VAR_NUMBER)
21250 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 else
21252 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021253 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21254 if (s != NULL)
21255 {
21256 trunc_string(s, buf, MSG_BUF_CLEN);
21257 msg_puts(buf);
21258 vim_free(tofree);
21259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 }
21261 }
21262 msg_puts((char_u *)")");
21263 }
21264 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021265
21266 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 --no_wait_return;
21268 }
21269 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021270#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021271 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021272 {
21273 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21274 func_do_profile(fp);
21275 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021276 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021277 {
21278 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021279 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021280 profile_zero(&fp->uf_tm_children);
21281 }
21282 script_prof_save(&wait_start);
21283 }
21284#endif
21285
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021287 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288 save_did_emsg = did_emsg;
21289 did_emsg = FALSE;
21290
21291 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021292 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21294
21295 --RedrawingDisabled;
21296
21297 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021298 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021300 clear_tv(rettv);
21301 rettv->v_type = VAR_NUMBER;
21302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 }
21304
Bram Moolenaar05159a02005-02-26 23:04:13 +000021305#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021306 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021307 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021308 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021309 profile_end(&call_start);
21310 profile_sub_wait(&wait_start, &call_start);
21311 profile_add(&fp->uf_tm_total, &call_start);
21312 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021313 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021314 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021315 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21316 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021317 }
21318 }
21319#endif
21320
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 /* when being verbose, mention the return value */
21322 if (p_verbose >= 12)
21323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021325 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021328 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021329 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021330 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021331 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021334 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021335 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021336 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021337 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021338
Bram Moolenaar555b2802005-05-19 21:08:39 +000021339 /* The value may be very long. Skip the middle part, so that we
21340 * have some idea how it starts and ends. smsg() would always
21341 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021342 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021343 if (s != NULL)
21344 {
21345 trunc_string(s, buf, MSG_BUF_CLEN);
21346 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21347 vim_free(tofree);
21348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 }
21350 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021351
21352 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 --no_wait_return;
21354 }
21355
21356 vim_free(sourcing_name);
21357 sourcing_name = save_sourcing_name;
21358 sourcing_lnum = save_sourcing_lnum;
21359 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021360#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021361 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021362 script_prof_restore(&wait_start);
21363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364
21365 if (p_verbose >= 12 && sourcing_name != NULL)
21366 {
21367 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021368 verbose_enter_scroll();
21369
Bram Moolenaar555b2802005-05-19 21:08:39 +000021370 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021372
21373 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 --no_wait_return;
21375 }
21376
21377 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021378 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021380
21381 /* if the a:000 list and the a: dict are not referenced we can free the
21382 * funccall_T and what's in it. */
21383 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21384 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21385 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21386 {
21387 free_funccal(fc, FALSE);
21388 }
21389 else
21390 {
21391 hashitem_T *hi;
21392 listitem_T *li;
21393 int todo;
21394
21395 /* "fc" is still in use. This can happen when returning "a:000" or
21396 * assigning "l:" to a global variable.
21397 * Link "fc" in the list for garbage collection later. */
21398 fc->caller = previous_funccal;
21399 previous_funccal = fc;
21400
21401 /* Make a copy of the a: variables, since we didn't do that above. */
21402 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21403 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21404 {
21405 if (!HASHITEM_EMPTY(hi))
21406 {
21407 --todo;
21408 v = HI2DI(hi);
21409 copy_tv(&v->di_tv, &v->di_tv);
21410 }
21411 }
21412
21413 /* Make a copy of the a:000 items, since we didn't do that above. */
21414 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21415 copy_tv(&li->li_tv, &li->li_tv);
21416 }
21417}
21418
21419/*
21420 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021421 * referenced from anywhere.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021422 */
21423 static int
21424can_free_funccal(fc, copyID)
21425 funccall_T *fc;
21426 int copyID;
21427{
21428 return (fc->l_varlist.lv_copyID != copyID
21429 && fc->l_vars.dv_copyID != copyID
21430 && fc->l_avars.dv_copyID != copyID);
21431}
21432
21433/*
21434 * Free "fc" and what it contains.
21435 */
21436 static void
21437free_funccal(fc, free_val)
21438 funccall_T *fc;
21439 int free_val; /* a: vars were allocated */
21440{
21441 listitem_T *li;
21442
21443 /* The a: variables typevals may not have been allocated, only free the
21444 * allocated variables. */
21445 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21446
21447 /* free all l: variables */
21448 vars_clear(&fc->l_vars.dv_hashtab);
21449
21450 /* Free the a:000 variables if they were allocated. */
21451 if (free_val)
21452 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21453 clear_tv(&li->li_tv);
21454
21455 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456}
21457
21458/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021459 * Add a number variable "name" to dict "dp" with value "nr".
21460 */
21461 static void
21462add_nr_var(dp, v, name, nr)
21463 dict_T *dp;
21464 dictitem_T *v;
21465 char *name;
21466 varnumber_T nr;
21467{
21468 STRCPY(v->di_key, name);
21469 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21470 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21471 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021472 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 v->di_tv.vval.v_number = nr;
21474}
21475
21476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 * ":return [expr]"
21478 */
21479 void
21480ex_return(eap)
21481 exarg_T *eap;
21482{
21483 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021484 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 int returning = FALSE;
21486
21487 if (current_funccal == NULL)
21488 {
21489 EMSG(_("E133: :return not inside a function"));
21490 return;
21491 }
21492
21493 if (eap->skip)
21494 ++emsg_skip;
21495
21496 eap->nextcmd = NULL;
21497 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021498 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 {
21500 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021501 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 }
21505 /* It's safer to return also on error. */
21506 else if (!eap->skip)
21507 {
21508 /*
21509 * Return unless the expression evaluation has been cancelled due to an
21510 * aborting error, an interrupt, or an exception.
21511 */
21512 if (!aborting())
21513 returning = do_return(eap, FALSE, TRUE, NULL);
21514 }
21515
21516 /* When skipping or the return gets pending, advance to the next command
21517 * in this line (!returning). Otherwise, ignore the rest of the line.
21518 * Following lines will be ignored by get_func_line(). */
21519 if (returning)
21520 eap->nextcmd = NULL;
21521 else if (eap->nextcmd == NULL) /* no argument */
21522 eap->nextcmd = check_nextcmd(arg);
21523
21524 if (eap->skip)
21525 --emsg_skip;
21526}
21527
21528/*
21529 * Return from a function. Possibly makes the return pending. Also called
21530 * for a pending return at the ":endtry" or after returning from an extra
21531 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021532 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021533 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 * FALSE when the return gets pending.
21535 */
21536 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 exarg_T *eap;
21539 int reanimate;
21540 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021541 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542{
21543 int idx;
21544 struct condstack *cstack = eap->cstack;
21545
21546 if (reanimate)
21547 /* Undo the return. */
21548 current_funccal->returned = FALSE;
21549
21550 /*
21551 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21552 * not in its finally clause (which then is to be executed next) is found.
21553 * In this case, make the ":return" pending for execution at the ":endtry".
21554 * Otherwise, return normally.
21555 */
21556 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21557 if (idx >= 0)
21558 {
21559 cstack->cs_pending[idx] = CSTP_RETURN;
21560
21561 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021562 /* A pending return again gets pending. "rettv" points to an
21563 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021565 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 else
21567 {
21568 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021569 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021571 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 {
21575 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021576 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 else
21579 EMSG(_(e_outofmem));
21580 }
21581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021582 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583
21584 if (reanimate)
21585 {
21586 /* The pending return value could be overwritten by a ":return"
21587 * without argument in a finally clause; reset the default
21588 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021589 current_funccal->rettv->v_type = VAR_NUMBER;
21590 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 }
21592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021593 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 }
21595 else
21596 {
21597 current_funccal->returned = TRUE;
21598
21599 /* If the return is carried out now, store the return value. For
21600 * a return immediately after reanimation, the value is already
21601 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021602 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021604 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021605 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021607 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 }
21609 }
21610
21611 return idx < 0;
21612}
21613
21614/*
21615 * Free the variable with a pending return value.
21616 */
21617 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021618discard_pending_return(rettv)
21619 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620{
Bram Moolenaar33570922005-01-25 22:26:29 +000021621 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622}
21623
21624/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * is an allocated string. Used by report_pending() for verbose messages.
21627 */
21628 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021629get_return_cmd(rettv)
21630 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021632 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021633 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021634 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021636 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021637 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021638 if (s == NULL)
21639 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021640
21641 STRCPY(IObuff, ":return ");
21642 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21643 if (STRLEN(s) + 8 >= IOSIZE)
21644 STRCPY(IObuff + IOSIZE - 4, "...");
21645 vim_free(tofree);
21646 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647}
21648
21649/*
21650 * Get next function line.
21651 * Called by do_cmdline() to get the next line.
21652 * Returns allocated string, or NULL for end of function.
21653 */
21654/* ARGSUSED */
21655 char_u *
21656get_func_line(c, cookie, indent)
21657 int c; /* not used */
21658 void *cookie;
21659 int indent; /* not used */
21660{
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021662 ufunc_T *fp = fcp->func;
21663 char_u *retval;
21664 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665
21666 /* If breakpoints have been added/deleted need to check for it. */
21667 if (fcp->dbg_tick != debug_tick)
21668 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021669 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 sourcing_lnum);
21671 fcp->dbg_tick = debug_tick;
21672 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021673#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021674 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021675 func_line_end(cookie);
21676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677
Bram Moolenaar05159a02005-02-26 23:04:13 +000021678 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021679 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21680 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 retval = NULL;
21682 else
21683 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021684 /* Skip NULL lines (continuation lines). */
21685 while (fcp->linenr < gap->ga_len
21686 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21687 ++fcp->linenr;
21688 if (fcp->linenr >= gap->ga_len)
21689 retval = NULL;
21690 else
21691 {
21692 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21693 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021694#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021695 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021696 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021697#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699 }
21700
21701 /* Did we encounter a breakpoint? */
21702 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21703 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021704 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021706 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 sourcing_lnum);
21708 fcp->dbg_tick = debug_tick;
21709 }
21710
21711 return retval;
21712}
21713
Bram Moolenaar05159a02005-02-26 23:04:13 +000021714#if defined(FEAT_PROFILE) || defined(PROTO)
21715/*
21716 * Called when starting to read a function line.
21717 * "sourcing_lnum" must be correct!
21718 * When skipping lines it may not actually be executed, but we won't find out
21719 * until later and we need to store the time now.
21720 */
21721 void
21722func_line_start(cookie)
21723 void *cookie;
21724{
21725 funccall_T *fcp = (funccall_T *)cookie;
21726 ufunc_T *fp = fcp->func;
21727
21728 if (fp->uf_profiling && sourcing_lnum >= 1
21729 && sourcing_lnum <= fp->uf_lines.ga_len)
21730 {
21731 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021732 /* Skip continuation lines. */
21733 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21734 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021735 fp->uf_tml_execed = FALSE;
21736 profile_start(&fp->uf_tml_start);
21737 profile_zero(&fp->uf_tml_children);
21738 profile_get_wait(&fp->uf_tml_wait);
21739 }
21740}
21741
21742/*
21743 * Called when actually executing a function line.
21744 */
21745 void
21746func_line_exec(cookie)
21747 void *cookie;
21748{
21749 funccall_T *fcp = (funccall_T *)cookie;
21750 ufunc_T *fp = fcp->func;
21751
21752 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21753 fp->uf_tml_execed = TRUE;
21754}
21755
21756/*
21757 * Called when done with a function line.
21758 */
21759 void
21760func_line_end(cookie)
21761 void *cookie;
21762{
21763 funccall_T *fcp = (funccall_T *)cookie;
21764 ufunc_T *fp = fcp->func;
21765
21766 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21767 {
21768 if (fp->uf_tml_execed)
21769 {
21770 ++fp->uf_tml_count[fp->uf_tml_idx];
21771 profile_end(&fp->uf_tml_start);
21772 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021773 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021774 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21775 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021776 }
21777 fp->uf_tml_idx = -1;
21778 }
21779}
21780#endif
21781
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782/*
21783 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021784 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 */
21786 int
21787func_has_ended(cookie)
21788 void *cookie;
21789{
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791
21792 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21793 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021794 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 || fcp->returned);
21796}
21797
21798/*
21799 * return TRUE if cookie indicates a function which "abort"s on errors.
21800 */
21801 int
21802func_has_abort(cookie)
21803 void *cookie;
21804{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021805 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806}
21807
21808#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21809typedef enum
21810{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021811 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21812 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21813 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814} var_flavour_T;
21815
21816static var_flavour_T var_flavour __ARGS((char_u *varname));
21817
21818 static var_flavour_T
21819var_flavour(varname)
21820 char_u *varname;
21821{
21822 char_u *p = varname;
21823
21824 if (ASCII_ISUPPER(*p))
21825 {
21826 while (*(++p))
21827 if (ASCII_ISLOWER(*p))
21828 return VAR_FLAVOUR_SESSION;
21829 return VAR_FLAVOUR_VIMINFO;
21830 }
21831 else
21832 return VAR_FLAVOUR_DEFAULT;
21833}
21834#endif
21835
21836#if defined(FEAT_VIMINFO) || defined(PROTO)
21837/*
21838 * Restore global vars that start with a capital from the viminfo file
21839 */
21840 int
21841read_viminfo_varlist(virp, writing)
21842 vir_T *virp;
21843 int writing;
21844{
21845 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021846 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848
21849 if (!writing && (find_viminfo_parameter('!') != NULL))
21850 {
21851 tab = vim_strchr(virp->vir_line + 1, '\t');
21852 if (tab != NULL)
21853 {
21854 *tab++ = '\0'; /* isolate the variable name */
21855 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021856 type = VAR_STRING;
21857#ifdef FEAT_FLOAT
21858 else if (*tab == 'F')
21859 type = VAR_FLOAT;
21860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861
21862 tab = vim_strchr(tab, '\t');
21863 if (tab != NULL)
21864 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021865 tv.v_type = type;
21866 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021867 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021869#ifdef FEAT_FLOAT
21870 else if (type == VAR_FLOAT)
21871 (void)string2float(tab + 1, &tv.vval.v_float);
21872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021874 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021875 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021876 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021877 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 }
21879 }
21880 }
21881
21882 return viminfo_readline(virp);
21883}
21884
21885/*
21886 * Write global vars that start with a capital to the viminfo file
21887 */
21888 void
21889write_viminfo_varlist(fp)
21890 FILE *fp;
21891{
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 hashitem_T *hi;
21893 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021894 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021895 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021896 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021897 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021898 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899
21900 if (find_viminfo_parameter('!') == NULL)
21901 return;
21902
21903 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021904
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021905 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021908 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021910 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021911 this_var = HI2DI(hi);
21912 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021913 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021915 {
21916 case VAR_STRING: s = "STR"; break;
21917 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021918#ifdef FEAT_FLOAT
21919 case VAR_FLOAT: s = "FLO"; break;
21920#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021921 default: continue;
21922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021924 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021925 if (p != NULL)
21926 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021927 vim_free(tofree);
21928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 }
21930 }
21931}
21932#endif
21933
21934#if defined(FEAT_SESSION) || defined(PROTO)
21935 int
21936store_session_globals(fd)
21937 FILE *fd;
21938{
Bram Moolenaar33570922005-01-25 22:26:29 +000021939 hashitem_T *hi;
21940 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021941 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 char_u *p, *t;
21943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021944 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021949 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021950 this_var = HI2DI(hi);
21951 if ((this_var->di_tv.v_type == VAR_NUMBER
21952 || this_var->di_tv.v_type == VAR_STRING)
21953 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021954 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021955 /* Escape special characters with a backslash. Turn a LF and
21956 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021957 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021958 (char_u *)"\\\"\n\r");
21959 if (p == NULL) /* out of memory */
21960 break;
21961 for (t = p; *t != NUL; ++t)
21962 if (*t == '\n')
21963 *t = 'n';
21964 else if (*t == '\r')
21965 *t = 'r';
21966 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 this_var->di_key,
21968 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21969 : ' ',
21970 p,
21971 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21972 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021973 || put_eol(fd) == FAIL)
21974 {
21975 vim_free(p);
21976 return FAIL;
21977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 vim_free(p);
21979 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021980#ifdef FEAT_FLOAT
21981 else if (this_var->di_tv.v_type == VAR_FLOAT
21982 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21983 {
21984 float_T f = this_var->di_tv.vval.v_float;
21985 int sign = ' ';
21986
21987 if (f < 0)
21988 {
21989 f = -f;
21990 sign = '-';
21991 }
21992 if ((fprintf(fd, "let %s = %c&%f",
21993 this_var->di_key, sign, f) < 0)
21994 || put_eol(fd) == FAIL)
21995 return FAIL;
21996 }
21997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 }
21999 }
22000 return OK;
22001}
22002#endif
22003
Bram Moolenaar661b1822005-07-28 22:36:45 +000022004/*
22005 * Display script name where an item was last set.
22006 * Should only be invoked when 'verbose' is non-zero.
22007 */
22008 void
22009last_set_msg(scriptID)
22010 scid_T scriptID;
22011{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022012 char_u *p;
22013
Bram Moolenaar661b1822005-07-28 22:36:45 +000022014 if (scriptID != 0)
22015 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022016 p = home_replace_save(NULL, get_scriptname(scriptID));
22017 if (p != NULL)
22018 {
22019 verbose_enter();
22020 MSG_PUTS(_("\n\tLast set from "));
22021 MSG_PUTS(p);
22022 vim_free(p);
22023 verbose_leave();
22024 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022025 }
22026}
22027
Bram Moolenaard812df62008-11-09 12:46:09 +000022028/*
22029 * List v:oldfiles in a nice way.
22030 */
22031/*ARGSUSED*/
22032 void
22033ex_oldfiles(eap)
22034 exarg_T *eap;
22035{
22036 list_T *l = vimvars[VV_OLDFILES].vv_list;
22037 listitem_T *li;
22038 int nr = 0;
22039
22040 if (l == NULL)
22041 msg((char_u *)_("No old files"));
22042 else
22043 {
22044 msg_start();
22045 msg_scroll = TRUE;
22046 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22047 {
22048 msg_outnum((long)++nr);
22049 MSG_PUTS(": ");
22050 msg_outtrans(get_tv_string(&li->li_tv));
22051 msg_putchar('\n');
22052 out_flush(); /* output one line at a time */
22053 ui_breakcheck();
22054 }
22055 /* Assume "got_int" was set to truncate the listing. */
22056 got_int = FALSE;
22057
22058#ifdef FEAT_BROWSE_CMD
22059 if (cmdmod.browse)
22060 {
22061 quit_more = FALSE;
22062 nr = prompt_for_number(FALSE);
22063 msg_starthere();
22064 if (nr > 0)
22065 {
22066 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22067 (long)nr);
22068
22069 if (p != NULL)
22070 {
22071 p = expand_env_save(p);
22072 eap->arg = p;
22073 eap->cmdidx = CMD_edit;
22074 cmdmod.browse = FALSE;
22075 do_exedit(eap, NULL);
22076 vim_free(p);
22077 }
22078 }
22079 }
22080#endif
22081 }
22082}
22083
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084#endif /* FEAT_EVAL */
22085
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022087#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088
22089#ifdef WIN3264
22090/*
22091 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22092 */
22093static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22094static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22095static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22096
22097/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022098 * Get the short path (8.3) for the filename in "fnamep".
22099 * Only works for a valid file name.
22100 * When the path gets longer "fnamep" is changed and the allocated buffer
22101 * is put in "bufp".
22102 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22103 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104 */
22105 static int
22106get_short_pathname(fnamep, bufp, fnamelen)
22107 char_u **fnamep;
22108 char_u **bufp;
22109 int *fnamelen;
22110{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022111 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 char_u *newbuf;
22113
22114 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 l = GetShortPathName(*fnamep, *fnamep, len);
22116 if (l > len - 1)
22117 {
22118 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022119 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 newbuf = vim_strnsave(*fnamep, l);
22121 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022122 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123
22124 vim_free(*bufp);
22125 *fnamep = *bufp = newbuf;
22126
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022127 /* Really should always succeed, as the buffer is big enough. */
22128 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 }
22130
22131 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022132 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133}
22134
22135/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022136 * Get the short path (8.3) for the filename in "fname". The converted
22137 * path is returned in "bufp".
22138 *
22139 * Some of the directories specified in "fname" may not exist. This function
22140 * will shorten the existing directories at the beginning of the path and then
22141 * append the remaining non-existing path.
22142 *
22143 * fname - Pointer to the filename to shorten. On return, contains the
22144 * pointer to the shortened pathname
22145 * bufp - Pointer to an allocated buffer for the filename.
22146 * fnamelen - Length of the filename pointed to by fname
22147 *
22148 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 */
22150 static int
22151shortpath_for_invalid_fname(fname, bufp, fnamelen)
22152 char_u **fname;
22153 char_u **bufp;
22154 int *fnamelen;
22155{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022156 char_u *short_fname, *save_fname, *pbuf_unused;
22157 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022159 int old_len, len;
22160 int new_len, sfx_len;
22161 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162
22163 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022164 old_len = *fnamelen;
22165 save_fname = vim_strnsave(*fname, old_len);
22166 pbuf_unused = NULL;
22167 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022169 endp = save_fname + old_len - 1; /* Find the end of the copy */
22170 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022172 /*
22173 * Try shortening the supplied path till it succeeds by removing one
22174 * directory at a time from the tail of the path.
22175 */
22176 len = 0;
22177 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022179 /* go back one path-separator */
22180 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22181 --endp;
22182 if (endp <= save_fname)
22183 break; /* processed the complete path */
22184
22185 /*
22186 * Replace the path separator with a NUL and try to shorten the
22187 * resulting path.
22188 */
22189 ch = *endp;
22190 *endp = 0;
22191 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022192 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022193 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22194 {
22195 retval = FAIL;
22196 goto theend;
22197 }
22198 *endp = ch; /* preserve the string */
22199
22200 if (len > 0)
22201 break; /* successfully shortened the path */
22202
22203 /* failed to shorten the path. Skip the path separator */
22204 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 }
22206
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022207 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022209 /*
22210 * Succeeded in shortening the path. Now concatenate the shortened
22211 * path with the remaining path at the tail.
22212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022214 /* Compute the length of the new path. */
22215 sfx_len = (int)(save_endp - endp) + 1;
22216 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022218 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022220 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022222 /* There is not enough space in the currently allocated string,
22223 * copy it to a buffer big enough. */
22224 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022226 {
22227 retval = FAIL;
22228 goto theend;
22229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 }
22231 else
22232 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022233 /* Transfer short_fname to the main buffer (it's big enough),
22234 * unless get_short_pathname() did its work in-place. */
22235 *fname = *bufp = save_fname;
22236 if (short_fname != save_fname)
22237 vim_strncpy(save_fname, short_fname, len);
22238 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022240
22241 /* concat the not-shortened part of the path */
22242 vim_strncpy(*fname + len, endp, sfx_len);
22243 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022245
22246theend:
22247 vim_free(pbuf_unused);
22248 vim_free(save_fname);
22249
22250 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251}
22252
22253/*
22254 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022255 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 */
22257 static int
22258shortpath_for_partial(fnamep, bufp, fnamelen)
22259 char_u **fnamep;
22260 char_u **bufp;
22261 int *fnamelen;
22262{
22263 int sepcount, len, tflen;
22264 char_u *p;
22265 char_u *pbuf, *tfname;
22266 int hasTilde;
22267
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022268 /* Count up the path separators from the RHS.. so we know which part
22269 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022271 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 if (vim_ispathsep(*p))
22273 ++sepcount;
22274
22275 /* Need full path first (use expand_env() to remove a "~/") */
22276 hasTilde = (**fnamep == '~');
22277 if (hasTilde)
22278 pbuf = tfname = expand_env_save(*fnamep);
22279 else
22280 pbuf = tfname = FullName_save(*fnamep, FALSE);
22281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022282 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022284 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286
22287 if (len == 0)
22288 {
22289 /* Don't have a valid filename, so shorten the rest of the
22290 * path if we can. This CAN give us invalid 8.3 filenames, but
22291 * there's not a lot of point in guessing what it might be.
22292 */
22293 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022294 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 }
22297
22298 /* Count the paths backward to find the beginning of the desired string. */
22299 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022300 {
22301#ifdef FEAT_MBYTE
22302 if (has_mbyte)
22303 p -= mb_head_off(tfname, p);
22304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 if (vim_ispathsep(*p))
22306 {
22307 if (sepcount == 0 || (hasTilde && sepcount == 1))
22308 break;
22309 else
22310 sepcount --;
22311 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 if (hasTilde)
22314 {
22315 --p;
22316 if (p >= tfname)
22317 *p = '~';
22318 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022319 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 }
22321 else
22322 ++p;
22323
22324 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22325 vim_free(*bufp);
22326 *fnamelen = (int)STRLEN(p);
22327 *bufp = pbuf;
22328 *fnamep = p;
22329
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022330 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331}
22332#endif /* WIN3264 */
22333
22334/*
22335 * Adjust a filename, according to a string of modifiers.
22336 * *fnamep must be NUL terminated when called. When returning, the length is
22337 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022338 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 * When there is an error, *fnamep is set to NULL.
22340 */
22341 int
22342modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22343 char_u *src; /* string with modifiers */
22344 int *usedlen; /* characters after src that are used */
22345 char_u **fnamep; /* file name so far */
22346 char_u **bufp; /* buffer for allocated file name or NULL */
22347 int *fnamelen; /* length of fnamep */
22348{
22349 int valid = 0;
22350 char_u *tail;
22351 char_u *s, *p, *pbuf;
22352 char_u dirname[MAXPATHL];
22353 int c;
22354 int has_fullname = 0;
22355#ifdef WIN3264
22356 int has_shortname = 0;
22357#endif
22358
22359repeat:
22360 /* ":p" - full path/file_name */
22361 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22362 {
22363 has_fullname = 1;
22364
22365 valid |= VALID_PATH;
22366 *usedlen += 2;
22367
22368 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22369 if ((*fnamep)[0] == '~'
22370#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22371 && ((*fnamep)[1] == '/'
22372# ifdef BACKSLASH_IN_FILENAME
22373 || (*fnamep)[1] == '\\'
22374# endif
22375 || (*fnamep)[1] == NUL)
22376
22377#endif
22378 )
22379 {
22380 *fnamep = expand_env_save(*fnamep);
22381 vim_free(*bufp); /* free any allocated file name */
22382 *bufp = *fnamep;
22383 if (*fnamep == NULL)
22384 return -1;
22385 }
22386
22387 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022388 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 {
22390 if (vim_ispathsep(*p)
22391 && p[1] == '.'
22392 && (p[2] == NUL
22393 || vim_ispathsep(p[2])
22394 || (p[2] == '.'
22395 && (p[3] == NUL || vim_ispathsep(p[3])))))
22396 break;
22397 }
22398
22399 /* FullName_save() is slow, don't use it when not needed. */
22400 if (*p != NUL || !vim_isAbsName(*fnamep))
22401 {
22402 *fnamep = FullName_save(*fnamep, *p != NUL);
22403 vim_free(*bufp); /* free any allocated file name */
22404 *bufp = *fnamep;
22405 if (*fnamep == NULL)
22406 return -1;
22407 }
22408
22409 /* Append a path separator to a directory. */
22410 if (mch_isdir(*fnamep))
22411 {
22412 /* Make room for one or two extra characters. */
22413 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22414 vim_free(*bufp); /* free any allocated file name */
22415 *bufp = *fnamep;
22416 if (*fnamep == NULL)
22417 return -1;
22418 add_pathsep(*fnamep);
22419 }
22420 }
22421
22422 /* ":." - path relative to the current directory */
22423 /* ":~" - path relative to the home directory */
22424 /* ":8" - shortname path - postponed till after */
22425 while (src[*usedlen] == ':'
22426 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22427 {
22428 *usedlen += 2;
22429 if (c == '8')
22430 {
22431#ifdef WIN3264
22432 has_shortname = 1; /* Postpone this. */
22433#endif
22434 continue;
22435 }
22436 pbuf = NULL;
22437 /* Need full path first (use expand_env() to remove a "~/") */
22438 if (!has_fullname)
22439 {
22440 if (c == '.' && **fnamep == '~')
22441 p = pbuf = expand_env_save(*fnamep);
22442 else
22443 p = pbuf = FullName_save(*fnamep, FALSE);
22444 }
22445 else
22446 p = *fnamep;
22447
22448 has_fullname = 0;
22449
22450 if (p != NULL)
22451 {
22452 if (c == '.')
22453 {
22454 mch_dirname(dirname, MAXPATHL);
22455 s = shorten_fname(p, dirname);
22456 if (s != NULL)
22457 {
22458 *fnamep = s;
22459 if (pbuf != NULL)
22460 {
22461 vim_free(*bufp); /* free any allocated file name */
22462 *bufp = pbuf;
22463 pbuf = NULL;
22464 }
22465 }
22466 }
22467 else
22468 {
22469 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22470 /* Only replace it when it starts with '~' */
22471 if (*dirname == '~')
22472 {
22473 s = vim_strsave(dirname);
22474 if (s != NULL)
22475 {
22476 *fnamep = s;
22477 vim_free(*bufp);
22478 *bufp = s;
22479 }
22480 }
22481 }
22482 vim_free(pbuf);
22483 }
22484 }
22485
22486 tail = gettail(*fnamep);
22487 *fnamelen = (int)STRLEN(*fnamep);
22488
22489 /* ":h" - head, remove "/file_name", can be repeated */
22490 /* Don't remove the first "/" or "c:\" */
22491 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22492 {
22493 valid |= VALID_HEAD;
22494 *usedlen += 2;
22495 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022496 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022497 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 *fnamelen = (int)(tail - *fnamep);
22499#ifdef VMS
22500 if (*fnamelen > 0)
22501 *fnamelen += 1; /* the path separator is part of the path */
22502#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022503 if (*fnamelen == 0)
22504 {
22505 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22506 p = vim_strsave((char_u *)".");
22507 if (p == NULL)
22508 return -1;
22509 vim_free(*bufp);
22510 *bufp = *fnamep = tail = p;
22511 *fnamelen = 1;
22512 }
22513 else
22514 {
22515 while (tail > s && !after_pathsep(s, tail))
22516 mb_ptr_back(*fnamep, tail);
22517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 }
22519
22520 /* ":8" - shortname */
22521 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22522 {
22523 *usedlen += 2;
22524#ifdef WIN3264
22525 has_shortname = 1;
22526#endif
22527 }
22528
22529#ifdef WIN3264
22530 /* Check shortname after we have done 'heads' and before we do 'tails'
22531 */
22532 if (has_shortname)
22533 {
22534 pbuf = NULL;
22535 /* Copy the string if it is shortened by :h */
22536 if (*fnamelen < (int)STRLEN(*fnamep))
22537 {
22538 p = vim_strnsave(*fnamep, *fnamelen);
22539 if (p == 0)
22540 return -1;
22541 vim_free(*bufp);
22542 *bufp = *fnamep = p;
22543 }
22544
22545 /* Split into two implementations - makes it easier. First is where
22546 * there isn't a full name already, second is where there is.
22547 */
22548 if (!has_fullname && !vim_isAbsName(*fnamep))
22549 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022550 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 return -1;
22552 }
22553 else
22554 {
22555 int l;
22556
22557 /* Simple case, already have the full-name
22558 * Nearly always shorter, so try first time. */
22559 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022560 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 return -1;
22562
22563 if (l == 0)
22564 {
22565 /* Couldn't find the filename.. search the paths.
22566 */
22567 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022568 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 return -1;
22570 }
22571 *fnamelen = l;
22572 }
22573 }
22574#endif /* WIN3264 */
22575
22576 /* ":t" - tail, just the basename */
22577 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22578 {
22579 *usedlen += 2;
22580 *fnamelen -= (int)(tail - *fnamep);
22581 *fnamep = tail;
22582 }
22583
22584 /* ":e" - extension, can be repeated */
22585 /* ":r" - root, without extension, can be repeated */
22586 while (src[*usedlen] == ':'
22587 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22588 {
22589 /* find a '.' in the tail:
22590 * - for second :e: before the current fname
22591 * - otherwise: The last '.'
22592 */
22593 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22594 s = *fnamep - 2;
22595 else
22596 s = *fnamep + *fnamelen - 1;
22597 for ( ; s > tail; --s)
22598 if (s[0] == '.')
22599 break;
22600 if (src[*usedlen + 1] == 'e') /* :e */
22601 {
22602 if (s > tail)
22603 {
22604 *fnamelen += (int)(*fnamep - (s + 1));
22605 *fnamep = s + 1;
22606#ifdef VMS
22607 /* cut version from the extension */
22608 s = *fnamep + *fnamelen - 1;
22609 for ( ; s > *fnamep; --s)
22610 if (s[0] == ';')
22611 break;
22612 if (s > *fnamep)
22613 *fnamelen = s - *fnamep;
22614#endif
22615 }
22616 else if (*fnamep <= tail)
22617 *fnamelen = 0;
22618 }
22619 else /* :r */
22620 {
22621 if (s > tail) /* remove one extension */
22622 *fnamelen = (int)(s - *fnamep);
22623 }
22624 *usedlen += 2;
22625 }
22626
22627 /* ":s?pat?foo?" - substitute */
22628 /* ":gs?pat?foo?" - global substitute */
22629 if (src[*usedlen] == ':'
22630 && (src[*usedlen + 1] == 's'
22631 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22632 {
22633 char_u *str;
22634 char_u *pat;
22635 char_u *sub;
22636 int sep;
22637 char_u *flags;
22638 int didit = FALSE;
22639
22640 flags = (char_u *)"";
22641 s = src + *usedlen + 2;
22642 if (src[*usedlen + 1] == 'g')
22643 {
22644 flags = (char_u *)"g";
22645 ++s;
22646 }
22647
22648 sep = *s++;
22649 if (sep)
22650 {
22651 /* find end of pattern */
22652 p = vim_strchr(s, sep);
22653 if (p != NULL)
22654 {
22655 pat = vim_strnsave(s, (int)(p - s));
22656 if (pat != NULL)
22657 {
22658 s = p + 1;
22659 /* find end of substitution */
22660 p = vim_strchr(s, sep);
22661 if (p != NULL)
22662 {
22663 sub = vim_strnsave(s, (int)(p - s));
22664 str = vim_strnsave(*fnamep, *fnamelen);
22665 if (sub != NULL && str != NULL)
22666 {
22667 *usedlen = (int)(p + 1 - src);
22668 s = do_string_sub(str, pat, sub, flags);
22669 if (s != NULL)
22670 {
22671 *fnamep = s;
22672 *fnamelen = (int)STRLEN(s);
22673 vim_free(*bufp);
22674 *bufp = s;
22675 didit = TRUE;
22676 }
22677 }
22678 vim_free(sub);
22679 vim_free(str);
22680 }
22681 vim_free(pat);
22682 }
22683 }
22684 /* after using ":s", repeat all the modifiers */
22685 if (didit)
22686 goto repeat;
22687 }
22688 }
22689
22690 return valid;
22691}
22692
22693/*
22694 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22695 * "flags" can be "g" to do a global substitute.
22696 * Returns an allocated string, NULL for error.
22697 */
22698 char_u *
22699do_string_sub(str, pat, sub, flags)
22700 char_u *str;
22701 char_u *pat;
22702 char_u *sub;
22703 char_u *flags;
22704{
22705 int sublen;
22706 regmatch_T regmatch;
22707 int i;
22708 int do_all;
22709 char_u *tail;
22710 garray_T ga;
22711 char_u *ret;
22712 char_u *save_cpo;
22713
22714 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22715 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022716 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717
22718 ga_init2(&ga, 1, 200);
22719
22720 do_all = (flags[0] == 'g');
22721
22722 regmatch.rm_ic = p_ic;
22723 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22724 if (regmatch.regprog != NULL)
22725 {
22726 tail = str;
22727 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22728 {
22729 /*
22730 * Get some space for a temporary buffer to do the substitution
22731 * into. It will contain:
22732 * - The text up to where the match is.
22733 * - The substituted text.
22734 * - The text after the match.
22735 */
22736 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22737 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22738 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22739 {
22740 ga_clear(&ga);
22741 break;
22742 }
22743
22744 /* copy the text up to where the match is */
22745 i = (int)(regmatch.startp[0] - tail);
22746 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22747 /* add the substituted text */
22748 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22749 + ga.ga_len + i, TRUE, TRUE, FALSE);
22750 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 /* avoid getting stuck on a match with an empty string */
22752 if (tail == regmatch.endp[0])
22753 {
22754 if (*tail == NUL)
22755 break;
22756 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22757 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 }
22759 else
22760 {
22761 tail = regmatch.endp[0];
22762 if (*tail == NUL)
22763 break;
22764 }
22765 if (!do_all)
22766 break;
22767 }
22768
22769 if (ga.ga_data != NULL)
22770 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22771
22772 vim_free(regmatch.regprog);
22773 }
22774
22775 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22776 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022777 if (p_cpo == empty_option)
22778 p_cpo = save_cpo;
22779 else
22780 /* Darn, evaluating {sub} expression changed the value. */
22781 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782
22783 return ret;
22784}
22785
22786#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */