blob: b56c34163c344dd2dfaffc82abbf1122ca91e3cd [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 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
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
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000197 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000201/* From user function to hashitem and back. */
202static ufunc_T dumuf;
203#define UF2HIKEY(fp) ((fp)->uf_name)
204#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
205#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
206
207#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
208#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaar33570922005-01-25 22:26:29 +0000210#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
211#define VAR_SHORT_LEN 20 /* short variable name length */
212#define FIXVAR_CNT 12 /* number of fixed variables */
213
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000215typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 ufunc_T *func; /* function being called */
218 int linenr; /* next line to be executed */
219 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000220 struct /* fixed variables for arguments */
221 {
222 dictitem_T var; /* variable (without room for name) */
223 char_u room[VAR_SHORT_LEN]; /* room for the name */
224 } fixvar[FIXVAR_CNT];
225 dict_T l_vars; /* l: local function variables */
226 dictitem_T l_vars_var; /* variable for l: scope */
227 dict_T l_avars; /* a: argument variables */
228 dictitem_T l_avars_var; /* variable for a: scope */
229 list_T l_varlist; /* list for a:000 */
230 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
231 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 linenr_T breakpoint; /* next line with breakpoint or zero */
233 int dbg_tick; /* debug_tick when breakpoint was set */
234 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000235#ifdef FEAT_PROFILE
236 proftime_T prof_child; /* time spent in a child */
237#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000238} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239
240/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000241 * Info used by a ":for" loop.
242 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000243typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000244{
245 int fi_semicolon; /* TRUE if ending in '; var]' */
246 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000247 listwatch_T fi_lw; /* keep an eye on the item used. */
248 list_T *fi_list; /* list being used */
249} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000250
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252 * Struct used by trans_function_name()
253 */
254typedef struct
255{
Bram Moolenaar33570922005-01-25 22:26:29 +0000256 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000257 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000258 dictitem_T *fd_di; /* Dictionary item used */
259} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000260
Bram Moolenaara7043832005-01-21 11:56:39 +0000261
262/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 * Array to hold the value of v: variables.
264 * The value is in a dictitem, so that it can also be used in the v: scope.
265 * The reason to use this table anyway is for very quick access to the
266 * variables with the VV_ defines.
267 */
268#include "version.h"
269
270/* values for vv_flags: */
271#define VV_COMPAT 1 /* compatible, also used without "v:" */
272#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000273#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000275#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000276
277static struct vimvar
278{
279 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 dictitem_T vv_di; /* value and name for key */
281 char vv_filler[16]; /* space for LONGEST name below!!! */
282 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
283} vimvars[VV_LEN] =
284{
285 /*
286 * The order here must match the VV_ defines in vim.h!
287 * Initializing a union does not work, leave tv.vval empty to get zero's.
288 */
289 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
290 {VV_NAME("count1", VAR_NUMBER), VV_RO},
291 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
292 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
293 {VV_NAME("warningmsg", VAR_STRING), 0},
294 {VV_NAME("statusmsg", VAR_STRING), 0},
295 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
296 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
297 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
298 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
299 {VV_NAME("termresponse", VAR_STRING), VV_RO},
300 {VV_NAME("fname", VAR_STRING), VV_RO},
301 {VV_NAME("lang", VAR_STRING), VV_RO},
302 {VV_NAME("lc_time", VAR_STRING), VV_RO},
303 {VV_NAME("ctype", VAR_STRING), VV_RO},
304 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
305 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
306 {VV_NAME("fname_in", VAR_STRING), VV_RO},
307 {VV_NAME("fname_out", VAR_STRING), VV_RO},
308 {VV_NAME("fname_new", VAR_STRING), VV_RO},
309 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
310 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
311 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
312 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
313 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
314 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("progname", VAR_STRING), VV_RO},
316 {VV_NAME("servername", VAR_STRING), VV_RO},
317 {VV_NAME("dying", VAR_NUMBER), VV_RO},
318 {VV_NAME("exception", VAR_STRING), VV_RO},
319 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
320 {VV_NAME("register", VAR_STRING), VV_RO},
321 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
322 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000323 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
324 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000325 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000326 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
327 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000328 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
329 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
330 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
331 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
332 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000333};
334
335/* shorthand */
336#define vv_type vv_di.di_tv.v_type
337#define vv_nr vv_di.di_tv.vval.v_number
338#define vv_str vv_di.di_tv.vval.v_string
339#define vv_tv vv_di.di_tv
340
341/*
342 * The v: variables are stored in dictionary "vimvardict".
343 * "vimvars_var" is the variable that is used for the "l:" scope.
344 */
345static dict_T vimvardict;
346static dictitem_T vimvars_var;
347#define vimvarht vimvardict.dv_hashtab
348
349static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
350static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
351static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
356static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000357static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000358static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
359static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
360static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
361static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
362static list_T *list_alloc __ARGS((void));
363static void list_unref __ARGS((list_T *l));
364static void list_free __ARGS((list_T *l));
365static listitem_T *listitem_alloc __ARGS((void));
366static void listitem_free __ARGS((listitem_T *item));
367static void listitem_remove __ARGS((list_T *l, listitem_T *item));
368static long list_len __ARGS((list_T *l));
369static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
370static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
371static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
372static int string_isa_number __ARGS((char_u *s));
373static listitem_T *list_find __ARGS((list_T *l, long n));
374static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000375static void list_append __ARGS((list_T *l, listitem_T *item));
376static int list_append_tv __ARGS((list_T *l, typval_T *tv));
377static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
378static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
379static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000380static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000381static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
382static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000383static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaard9fba312005-06-26 22:34:35 +0000385static int count_self_ref __ARGS((void *p, int type));
386static int count_ref_in_dict __ARGS((dict_T *d, void *rp, int copyID, garray_T *gap));
387static int count_ref_in_list __ARGS((list_T *l, void *rp, int copyID, garray_T *gap));
388static int count_ref_item __ARGS((typval_T *tv, void *rp, int copyID, garray_T *gap));
389
Bram Moolenaar33570922005-01-25 22:26:29 +0000390static void dict_unref __ARGS((dict_T *d));
391static void dict_free __ARGS((dict_T *d));
392static dictitem_T *dictitem_alloc __ARGS((char_u *key));
393static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
394static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
395static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000396static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000397static int dict_add __ARGS((dict_T *d, dictitem_T *item));
398static long dict_len __ARGS((dict_T *d));
399static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
400static char_u *dict2string __ARGS((typval_T *tv));
401static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402
403static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
404static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
405static char_u *string_quote __ARGS((char_u *str, int function));
406static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int find_internal_func __ARGS((char_u *name));
408static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
409static 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));
410static 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 Moolenaar81bf7082005-02-12 14:31:42 +0000411static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412
413static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
437static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000476static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000504static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000521static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000525#ifdef vim_mkdir
526static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
527#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000533static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000551static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000552static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000556static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
559#ifdef HAVE_STRFTIME
560static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
561#endif
562static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000574static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000590static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591
592static win_T *find_win_by_nr __ARGS((typval_T *vp));
593static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
594static int get_env_len __ARGS((char_u **arg));
595static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000597static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
598#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
599#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
600 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000602static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000603static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
604static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static typval_T *alloc_tv __ARGS((void));
606static typval_T *alloc_string_tv __ARGS((char_u *string));
607static void free_tv __ARGS((typval_T *varp));
608static void clear_tv __ARGS((typval_T *varp));
609static void init_tv __ARGS((typval_T *varp));
610static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
613static char_u *get_tv_string __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000614static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000616static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
620static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
621static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
622static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
623static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
624static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
625static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000626static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000628static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
630static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
631static int eval_fname_script __ARGS((char_u *p));
632static int eval_fname_sid __ARGS((char_u *p));
633static void list_func_head __ARGS((ufunc_T *fp, int indent));
634static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
635static ufunc_T *find_func __ARGS((char_u *name));
636static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000637static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000638#ifdef FEAT_PROFILE
639static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000640static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
641static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
642static int
643# ifdef __BORLANDC__
644 _RTLENTRYF
645# endif
646 prof_total_cmp __ARGS((const void *s1, const void *s2));
647static int
648# ifdef __BORLANDC__
649 _RTLENTRYF
650# endif
651 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000652#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000653static int script_autoload __ARGS((char_u *name));
654static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void func_free __ARGS((ufunc_T *fp));
656static void func_unref __ARGS((char_u *name));
657static void func_ref __ARGS((char_u *name));
658static 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));
659static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
660
661static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
662
663static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
664static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
665static char_u *skip_var_one __ARGS((char_u *arg));
666static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
667static void list_glob_vars __ARGS((void));
668static void list_buf_vars __ARGS((void));
669static void list_win_vars __ARGS((void));
670static void list_vim_vars __ARGS((void));
671static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
672static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
673static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000674static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void clear_lval __ARGS((lval_T *lp));
676static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
677static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
678static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
679static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
680static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000681static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000683static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
684static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000685static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000687/* Character used as separated in autoload function/variable names. */
688#define AUTOLOAD_CHAR '#'
689
Bram Moolenaar33570922005-01-25 22:26:29 +0000690/*
691 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000692 */
693 void
694eval_init()
695{
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 int i;
697 struct vimvar *p;
698
699 init_var_dict(&globvardict, &globvars_var);
700 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000701 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000702 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000703
704 for (i = 0; i < VV_LEN; ++i)
705 {
706 p = &vimvars[i];
707 STRCPY(p->vv_di.di_key, p->vv_name);
708 if (p->vv_flags & VV_RO)
709 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
710 else if (p->vv_flags & VV_RO_SBX)
711 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
712 else
713 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000714
715 /* add to v: scope dict, unless the value is not always available */
716 if (p->vv_type != VAR_UNKNOWN)
717 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000718 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000719 /* add to compat scope dict */
720 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000721 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000722}
723
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000724#if defined(EXITFREE) || defined(PROTO)
725 void
726eval_clear()
727{
728 int i;
729 struct vimvar *p;
730
731 for (i = 0; i < VV_LEN; ++i)
732 {
733 p = &vimvars[i];
734 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000735 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000736 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000737 p->vv_di.di_tv.vval.v_string = NULL;
738 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000739 }
740 hash_clear(&vimvarht);
741 hash_clear(&compat_hashtab);
742
743 /* script-local variables */
744 for (i = 1; i <= ga_scripts.ga_len; ++i)
745 vars_clear(&SCRIPT_VARS(i));
746 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000747 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000748
749 /* global variables */
750 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000751
752 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000753}
754#endif
755
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 * Return the name of the executed function.
758 */
759 char_u *
760func_name(cookie)
761 void *cookie;
762{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000763 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764}
765
766/*
767 * Return the address holding the next breakpoint line for a funccall cookie.
768 */
769 linenr_T *
770func_breakpoint(cookie)
771 void *cookie;
772{
Bram Moolenaar33570922005-01-25 22:26:29 +0000773 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774}
775
776/*
777 * Return the address holding the debug tick for a funccall cookie.
778 */
779 int *
780func_dbg_tick(cookie)
781 void *cookie;
782{
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
787 * Return the nesting level for a funccall cookie.
788 */
789 int
790func_level(cookie)
791 void *cookie;
792{
Bram Moolenaar33570922005-01-25 22:26:29 +0000793 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
795
796/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000797funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799/*
800 * Return TRUE when a function was ended by a ":return" command.
801 */
802 int
803current_func_returned()
804{
805 return current_funccal->returned;
806}
807
808
809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * Set an internal variable to a string value. Creates the variable if it does
811 * not already exist.
812 */
813 void
814set_internal_string_var(name, value)
815 char_u *name;
816 char_u *value;
817{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000818 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821 val = vim_strsave(value);
822 if (val != NULL)
823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000825 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000827 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000828 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830 }
831}
832
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000833static lval_T *redir_lval = NULL;
834static char_u *redir_endp = NULL;
835static char_u *redir_varname = NULL;
836
837/*
838 * Start recording command output to a variable
839 * Returns OK if successfully completed the setup. FAIL otherwise.
840 */
841 int
842var_redir_start(name, append)
843 char_u *name;
844 int append; /* append to an existing variable */
845{
846 int save_emsg;
847 int err;
848 typval_T tv;
849
850 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000851 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000852 {
853 EMSG(_(e_invarg));
854 return FAIL;
855 }
856
857 redir_varname = vim_strsave(name);
858 if (redir_varname == NULL)
859 return FAIL;
860
861 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
862 if (redir_lval == NULL)
863 {
864 var_redir_stop();
865 return FAIL;
866 }
867
868 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000869 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
870 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000871 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
872 {
873 if (redir_endp != NULL && *redir_endp != NUL)
874 /* Trailing characters are present after the variable name */
875 EMSG(_(e_trailing));
876 else
877 EMSG(_(e_invarg));
878 var_redir_stop();
879 return FAIL;
880 }
881
882 /* check if we can write to the variable: set it to or append an empty
883 * string */
884 save_emsg = did_emsg;
885 did_emsg = FALSE;
886 tv.v_type = VAR_STRING;
887 tv.vval.v_string = (char_u *)"";
888 if (append)
889 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
890 else
891 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
892 err = did_emsg;
893 did_emsg += save_emsg;
894 if (err)
895 {
896 var_redir_stop();
897 return FAIL;
898 }
899 if (redir_lval->ll_newkey != NULL)
900 {
901 /* Dictionary item was created, don't do it again. */
902 vim_free(redir_lval->ll_newkey);
903 redir_lval->ll_newkey = NULL;
904 }
905
906 return OK;
907}
908
909/*
910 * Append "value[len]" to the variable set by var_redir_start().
911 */
912 void
913var_redir_str(value, len)
914 char_u *value;
915 int len;
916{
917 char_u *val;
918 typval_T tv;
919 int save_emsg;
920 int err;
921
922 if (redir_lval == NULL)
923 return;
924
925 if (len == -1)
926 /* Append the entire string */
927 val = vim_strsave(value);
928 else
929 /* Append only the specified number of characters */
930 val = vim_strnsave(value, len);
931 if (val == NULL)
932 return;
933
934 tv.v_type = VAR_STRING;
935 tv.vval.v_string = val;
936
937 save_emsg = did_emsg;
938 did_emsg = FALSE;
939 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
940 err = did_emsg;
941 did_emsg += save_emsg;
942 if (err)
943 var_redir_stop();
944
945 vim_free(tv.vval.v_string);
946}
947
948/*
949 * Stop redirecting command output to a variable.
950 */
951 void
952var_redir_stop()
953{
954 if (redir_lval != NULL)
955 {
956 clear_lval(redir_lval);
957 vim_free(redir_lval);
958 redir_lval = NULL;
959 }
960 vim_free(redir_varname);
961 redir_varname = NULL;
962}
963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964# if defined(FEAT_MBYTE) || defined(PROTO)
965 int
966eval_charconvert(enc_from, enc_to, fname_from, fname_to)
967 char_u *enc_from;
968 char_u *enc_to;
969 char_u *fname_from;
970 char_u *fname_to;
971{
972 int err = FALSE;
973
974 set_vim_var_string(VV_CC_FROM, enc_from, -1);
975 set_vim_var_string(VV_CC_TO, enc_to, -1);
976 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
977 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
978 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
979 err = TRUE;
980 set_vim_var_string(VV_CC_FROM, NULL, -1);
981 set_vim_var_string(VV_CC_TO, NULL, -1);
982 set_vim_var_string(VV_FNAME_IN, NULL, -1);
983 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
984
985 if (err)
986 return FAIL;
987 return OK;
988}
989# endif
990
991# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
992 int
993eval_printexpr(fname, args)
994 char_u *fname;
995 char_u *args;
996{
997 int err = FALSE;
998
999 set_vim_var_string(VV_FNAME_IN, fname, -1);
1000 set_vim_var_string(VV_CMDARG, args, -1);
1001 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1002 err = TRUE;
1003 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1004 set_vim_var_string(VV_CMDARG, NULL, -1);
1005
1006 if (err)
1007 {
1008 mch_remove(fname);
1009 return FAIL;
1010 }
1011 return OK;
1012}
1013# endif
1014
1015# if defined(FEAT_DIFF) || defined(PROTO)
1016 void
1017eval_diff(origfile, newfile, outfile)
1018 char_u *origfile;
1019 char_u *newfile;
1020 char_u *outfile;
1021{
1022 int err = FALSE;
1023
1024 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1025 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1026 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1027 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1028 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1029 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1030 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1031}
1032
1033 void
1034eval_patch(origfile, difffile, outfile)
1035 char_u *origfile;
1036 char_u *difffile;
1037 char_u *outfile;
1038{
1039 int err;
1040
1041 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1042 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1043 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1044 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1045 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1046 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1047 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1048}
1049# endif
1050
1051/*
1052 * Top level evaluation function, returning a boolean.
1053 * Sets "error" to TRUE if there was an error.
1054 * Return TRUE or FALSE.
1055 */
1056 int
1057eval_to_bool(arg, error, nextcmd, skip)
1058 char_u *arg;
1059 int *error;
1060 char_u **nextcmd;
1061 int skip; /* only parse, don't execute */
1062{
Bram Moolenaar33570922005-01-25 22:26:29 +00001063 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 int retval = FALSE;
1065
1066 if (skip)
1067 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001068 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 else
1071 {
1072 *error = FALSE;
1073 if (!skip)
1074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001075 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079 if (skip)
1080 --emsg_skip;
1081
1082 return retval;
1083}
1084
1085/*
1086 * Top level evaluation function, returning a string. If "skip" is TRUE,
1087 * only parsing to "nextcmd" is done, without reporting errors. Return
1088 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1089 */
1090 char_u *
1091eval_to_string_skip(arg, nextcmd, skip)
1092 char_u *arg;
1093 char_u **nextcmd;
1094 int skip; /* only parse, don't execute */
1095{
Bram Moolenaar33570922005-01-25 22:26:29 +00001096 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 char_u *retval;
1098
1099 if (skip)
1100 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001101 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 retval = NULL;
1103 else
1104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 retval = vim_strsave(get_tv_string(&tv));
1106 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 if (skip)
1109 --emsg_skip;
1110
1111 return retval;
1112}
1113
1114/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001115 * Skip over an expression at "*pp".
1116 * Return FAIL for an error, OK otherwise.
1117 */
1118 int
1119skip_expr(pp)
1120 char_u **pp;
1121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001123
1124 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001126}
1127
1128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 * Top level evaluation function, returning a string.
1130 * Return pointer to allocated memory, or NULL for failure.
1131 */
1132 char_u *
1133eval_to_string(arg, nextcmd)
1134 char_u *arg;
1135 char_u **nextcmd;
1136{
Bram Moolenaar33570922005-01-25 22:26:29 +00001137 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 char_u *retval;
1139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 retval = NULL;
1142 else
1143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 retval = vim_strsave(get_tv_string(&tv));
1145 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 }
1147
1148 return retval;
1149}
1150
1151/*
1152 * Call eval_to_string() with "sandbox" set and not using local variables.
1153 */
1154 char_u *
1155eval_to_string_safe(arg, nextcmd)
1156 char_u *arg;
1157 char_u **nextcmd;
1158{
1159 char_u *retval;
1160 void *save_funccalp;
1161
1162 save_funccalp = save_funccal();
1163 ++sandbox;
1164 retval = eval_to_string(arg, nextcmd);
1165 --sandbox;
1166 restore_funccal(save_funccalp);
1167 return retval;
1168}
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/*
1171 * Top level evaluation function, returning a number.
1172 * Evaluates "expr" silently.
1173 * Returns -1 for an error.
1174 */
1175 int
1176eval_to_number(expr)
1177 char_u *expr;
1178{
Bram Moolenaar33570922005-01-25 22:26:29 +00001179 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001181 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
1183 ++emsg_off;
1184
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001185 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 retval = -1;
1187 else
1188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001189 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 --emsg_off;
1193
1194 return retval;
1195}
1196
1197#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1198/*
1199 * Call some vimL function and return the result as a string
1200 * Uses argv[argc] for the function arguments.
1201 */
1202 char_u *
1203call_vim_function(func, argc, argv, safe)
1204 char_u *func;
1205 int argc;
1206 char_u **argv;
1207 int safe; /* use the sandbox */
1208{
1209 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001210 typval_T rettv;
1211 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 long n;
1213 int len;
1214 int i;
1215 int doesrange;
1216 void *save_funccalp = NULL;
1217
Bram Moolenaar33570922005-01-25 22:26:29 +00001218 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (argvars == NULL)
1220 return NULL;
1221
1222 for (i = 0; i < argc; i++)
1223 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001224 /* Pass a NULL or empty argument as an empty string */
1225 if (argv[i] == NULL || *argv[i] == NUL)
1226 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001227 argvars[i].v_type = VAR_STRING;
1228 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001229 continue;
1230 }
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 /* Recognize a number argument, the others must be strings. */
1233 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1234 if (len != 0 && len == (int)STRLEN(argv[i]))
1235 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001236 argvars[i].v_type = VAR_NUMBER;
1237 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239 else
1240 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001241 argvars[i].v_type = VAR_STRING;
1242 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244 }
1245
1246 if (safe)
1247 {
1248 save_funccalp = save_funccal();
1249 ++sandbox;
1250 }
1251
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1253 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001255 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001256 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 vim_free(argvars);
1260
1261 if (safe)
1262 {
1263 --sandbox;
1264 restore_funccal(save_funccalp);
1265 }
1266 return retval;
1267}
1268#endif
1269
1270/*
1271 * Save the current function call pointer, and set it to NULL.
1272 * Used when executing autocommands and for ":source".
1273 */
1274 void *
1275save_funccal()
1276{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001277 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 current_funccal = NULL;
1280 return (void *)fc;
1281}
1282
1283 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001284restore_funccal(vfc)
1285 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001287 funccall_T *fc = (funccall_T *)vfc;
1288
1289 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290}
1291
Bram Moolenaar05159a02005-02-26 23:04:13 +00001292#if defined(FEAT_PROFILE) || defined(PROTO)
1293/*
1294 * Prepare profiling for entering a child or something else that is not
1295 * counted for the script/function itself.
1296 * Should always be called in pair with prof_child_exit().
1297 */
1298 void
1299prof_child_enter(tm)
1300 proftime_T *tm; /* place to store waittime */
1301{
1302 funccall_T *fc = current_funccal;
1303
1304 if (fc != NULL && fc->func->uf_profiling)
1305 profile_start(&fc->prof_child);
1306 script_prof_save(tm);
1307}
1308
1309/*
1310 * Take care of time spent in a child.
1311 * Should always be called after prof_child_enter().
1312 */
1313 void
1314prof_child_exit(tm)
1315 proftime_T *tm; /* where waittime was stored */
1316{
1317 funccall_T *fc = current_funccal;
1318
1319 if (fc != NULL && fc->func->uf_profiling)
1320 {
1321 profile_end(&fc->prof_child);
1322 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1323 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1324 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1325 }
1326 script_prof_restore(tm);
1327}
1328#endif
1329
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331#ifdef FEAT_FOLDING
1332/*
1333 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1334 * it in "*cp". Doesn't give error messages.
1335 */
1336 int
1337eval_foldexpr(arg, cp)
1338 char_u *arg;
1339 int *cp;
1340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int retval;
1343 char_u *s;
1344
1345 ++emsg_off;
1346 ++sandbox;
1347 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = 0;
1350 else
1351 {
1352 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 if (tv.v_type == VAR_NUMBER)
1354 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001355 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = 0;
1357 else
1358 {
1359 /* If the result is a string, check if there is a non-digit before
1360 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (!VIM_ISDIGIT(*s) && *s != '-')
1363 *cp = *s++;
1364 retval = atol((char *)s);
1365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 --emsg_off;
1369 --sandbox;
1370
1371 return retval;
1372}
1373#endif
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 * ":let" list all variable values
1377 * ":let var1 var2" list variable values
1378 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001379 * ":let var += expr" assignment command.
1380 * ":let var -= expr" assignment command.
1381 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 */
1384 void
1385ex_let(eap)
1386 exarg_T *eap;
1387{
1388 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 int var_count = 0;
1393 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001396 expr = skip_var_list(arg, &var_count, &semicolon);
1397 if (expr == NULL)
1398 return;
1399 expr = vim_strchr(expr, '=');
1400 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001402 /*
1403 * ":let" without "=": list variables
1404 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405 if (*arg == '[')
1406 EMSG(_(e_invarg));
1407 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 /* ":let var1 var2" */
1409 arg = list_arg_vars(eap, arg);
1410 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001413 list_glob_vars();
1414 list_buf_vars();
1415 list_win_vars();
1416 list_vim_vars();
1417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 eap->nextcmd = check_nextcmd(arg);
1419 }
1420 else
1421 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001422 op[0] = '=';
1423 op[1] = NUL;
1424 if (expr > arg)
1425 {
1426 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1427 op[0] = expr[-1]; /* +=, -= or .= */
1428 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (eap->skip)
1432 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (eap->skip)
1435 {
1436 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 --emsg_skip;
1439 }
1440 else if (i != FAIL)
1441 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001443 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 }
1447}
1448
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001449/*
1450 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1451 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001452 * When "nextchars" is not NULL it points to a string with characters that
1453 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1454 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001455 * Returns OK or FAIL;
1456 */
1457 static int
1458ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1459 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001460 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001461 int copy; /* copy values from "tv", don't move */
1462 int semicolon; /* from skip_var_list() */
1463 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001465{
1466 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001467 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001468 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001469 listitem_T *item;
1470 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471
1472 if (*arg != '[')
1473 {
1474 /*
1475 * ":let var = expr" or ":for var in list"
1476 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001478 return FAIL;
1479 return OK;
1480 }
1481
1482 /*
1483 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1484 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001485 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001486 {
1487 EMSG(_(e_listreq));
1488 return FAIL;
1489 }
1490
1491 i = list_len(l);
1492 if (semicolon == 0 && var_count < i)
1493 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001494 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001495 return FAIL;
1496 }
1497 if (var_count - semicolon > i)
1498 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001499 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500 return FAIL;
1501 }
1502
1503 item = l->lv_first;
1504 while (*arg != ']')
1505 {
1506 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001508 item = item->li_next;
1509 if (arg == NULL)
1510 return FAIL;
1511
1512 arg = skipwhite(arg);
1513 if (*arg == ';')
1514 {
1515 /* Put the rest of the list (may be empty) in the var after ';'.
1516 * Create a new list for this. */
1517 l = list_alloc();
1518 if (l == NULL)
1519 return FAIL;
1520 while (item != NULL)
1521 {
1522 list_append_tv(l, &item->li_tv);
1523 item = item->li_next;
1524 }
1525
1526 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001527 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528 ltv.vval.v_list = l;
1529 l->lv_refcount = 1;
1530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1532 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533 clear_tv(&ltv);
1534 if (arg == NULL)
1535 return FAIL;
1536 break;
1537 }
1538 else if (*arg != ',' && *arg != ']')
1539 {
1540 EMSG2(_(e_intern2), "ex_let_vars()");
1541 return FAIL;
1542 }
1543 }
1544
1545 return OK;
1546}
1547
1548/*
1549 * Skip over assignable variable "var" or list of variables "[var, var]".
1550 * Used for ":let varvar = expr" and ":for varvar in expr".
1551 * For "[var, var]" increment "*var_count" for each variable.
1552 * for "[var, var; var]" set "semicolon".
1553 * Return NULL for an error.
1554 */
1555 static char_u *
1556skip_var_list(arg, var_count, semicolon)
1557 char_u *arg;
1558 int *var_count;
1559 int *semicolon;
1560{
1561 char_u *p, *s;
1562
1563 if (*arg == '[')
1564 {
1565 /* "[var, var]": find the matching ']'. */
1566 p = arg;
1567 while (1)
1568 {
1569 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1570 s = skip_var_one(p);
1571 if (s == p)
1572 {
1573 EMSG2(_(e_invarg2), p);
1574 return NULL;
1575 }
1576 ++*var_count;
1577
1578 p = skipwhite(s);
1579 if (*p == ']')
1580 break;
1581 else if (*p == ';')
1582 {
1583 if (*semicolon == 1)
1584 {
1585 EMSG(_("Double ; in list of variables"));
1586 return NULL;
1587 }
1588 *semicolon = 1;
1589 }
1590 else if (*p != ',')
1591 {
1592 EMSG2(_(e_invarg2), p);
1593 return NULL;
1594 }
1595 }
1596 return p + 1;
1597 }
1598 else
1599 return skip_var_one(arg);
1600}
1601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001602/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001603 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1604 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001605 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 static char_u *
1607skip_var_one(arg)
1608 char_u *arg;
1609{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001610 if (*arg == '@' && arg[1] != NUL)
1611 return arg + 2;
1612 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1613 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614}
1615
Bram Moolenaara7043832005-01-21 11:56:39 +00001616/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 * List variables for hashtab "ht" with prefix "prefix".
1618 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001619 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001621list_hashtable_vars(ht, prefix, empty)
1622 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001623 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001625{
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 hashitem_T *hi;
1627 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001628 int todo;
1629
1630 todo = ht->ht_used;
1631 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1632 {
1633 if (!HASHITEM_EMPTY(hi))
1634 {
1635 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001636 di = HI2DI(hi);
1637 if (empty || di->di_tv.v_type != VAR_STRING
1638 || di->di_tv.vval.v_string != NULL)
1639 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001640 }
1641 }
1642}
1643
1644/*
1645 * List global variables.
1646 */
1647 static void
1648list_glob_vars()
1649{
Bram Moolenaar33570922005-01-25 22:26:29 +00001650 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001651}
1652
1653/*
1654 * List buffer variables.
1655 */
1656 static void
1657list_buf_vars()
1658{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001659 char_u numbuf[NUMBUFLEN];
1660
Bram Moolenaar33570922005-01-25 22:26:29 +00001661 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001662
1663 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1664 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001665}
1666
1667/*
1668 * List window variables.
1669 */
1670 static void
1671list_win_vars()
1672{
Bram Moolenaar33570922005-01-25 22:26:29 +00001673 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001674}
1675
1676/*
1677 * List Vim variables.
1678 */
1679 static void
1680list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681{
Bram Moolenaar33570922005-01-25 22:26:29 +00001682 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683}
1684
1685/*
1686 * List variables in "arg".
1687 */
1688 static char_u *
1689list_arg_vars(eap, arg)
1690 exarg_T *eap;
1691 char_u *arg;
1692{
1693 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001694 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001696 char_u *name_start;
1697 char_u *arg_subsc;
1698 char_u *tofree;
1699 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700
1701 while (!ends_excmd(*arg) && !got_int)
1702 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001703 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001705 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001706 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1707 {
1708 emsg_severe = TRUE;
1709 EMSG(_(e_trailing));
1710 break;
1711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001715 /* get_name_len() takes care of expanding curly braces */
1716 name_start = name = arg;
1717 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1718 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001720 /* This is mainly to keep test 49 working: when expanding
1721 * curly braces fails overrule the exception error message. */
1722 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001724 emsg_severe = TRUE;
1725 EMSG2(_(e_invarg2), arg);
1726 break;
1727 }
1728 error = TRUE;
1729 }
1730 else
1731 {
1732 if (tofree != NULL)
1733 name = tofree;
1734 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 else
1737 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001738 /* handle d.key, l[idx], f(expr) */
1739 arg_subsc = arg;
1740 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001741 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001744 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001745 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001746 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001747 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001748 case 'g': list_glob_vars(); break;
1749 case 'b': list_buf_vars(); break;
1750 case 'w': list_win_vars(); break;
1751 case 'v': list_vim_vars(); break;
1752 default:
1753 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001754 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001755 }
1756 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001757 {
1758 char_u numbuf[NUMBUFLEN];
1759 char_u *tf;
1760 int c;
1761 char_u *s;
1762
1763 s = echo_string(&tv, &tf, numbuf);
1764 c = *arg;
1765 *arg = NUL;
1766 list_one_var_a((char_u *)"",
1767 arg == arg_subsc ? name : name_start,
1768 tv.v_type, s == NULL ? (char_u *)"" : s);
1769 *arg = c;
1770 vim_free(tf);
1771 }
1772 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001774 }
1775 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001776
1777 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001779
1780 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 }
1782
1783 return arg;
1784}
1785
1786/*
1787 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1788 * Returns a pointer to the char just after the var name.
1789 * Returns NULL if there is an error.
1790 */
1791 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001794 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798{
1799 int c1;
1800 char_u *name;
1801 char_u *p;
1802 char_u *arg_end = NULL;
1803 int len;
1804 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806
1807 /*
1808 * ":let $VAR = expr": Set environment variable.
1809 */
1810 if (*arg == '$')
1811 {
1812 /* Find the end of the name. */
1813 ++arg;
1814 name = arg;
1815 len = get_env_len(&arg);
1816 if (len == 0)
1817 EMSG2(_(e_invarg2), name - 1);
1818 else
1819 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 if (op != NULL && (*op == '+' || *op == '-'))
1821 EMSG2(_(e_letwrong), op);
1822 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 EMSG(_(e_letunexp));
1825 else
1826 {
1827 c1 = name[len];
1828 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001829 p = get_tv_string_chk(tv);
1830 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 {
1832 int mustfree = FALSE;
1833 char_u *s = vim_getenv(name, &mustfree);
1834
1835 if (s != NULL)
1836 {
1837 p = tofree = concat_str(s, p);
1838 if (mustfree)
1839 vim_free(s);
1840 }
1841 }
1842 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001843 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001845 if (STRICMP(name, "HOME") == 0)
1846 init_homedir();
1847 else if (didset_vim && STRICMP(name, "VIM") == 0)
1848 didset_vim = FALSE;
1849 else if (didset_vimruntime
1850 && STRICMP(name, "VIMRUNTIME") == 0)
1851 didset_vimruntime = FALSE;
1852 arg_end = arg;
1853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 }
1857 }
1858 }
1859
1860 /*
1861 * ":let &option = expr": Set option value.
1862 * ":let &l:option = expr": Set local option value.
1863 * ":let &g:option = expr": Set global option value.
1864 */
1865 else if (*arg == '&')
1866 {
1867 /* Find the end of the name. */
1868 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 if (p == NULL || (endchars != NULL
1870 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 EMSG(_(e_letunexp));
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 long n;
1875 int opt_type;
1876 long numval;
1877 char_u *stringval = NULL;
1878 char_u *s;
1879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 c1 = *p;
1881 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882
1883 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001884 s = get_tv_string_chk(tv); /* != NULL if number or string */
1885 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 {
1887 opt_type = get_option_value(arg, &numval,
1888 &stringval, opt_flags);
1889 if ((opt_type == 1 && *op == '.')
1890 || (opt_type == 0 && *op != '.'))
1891 EMSG2(_(e_letwrong), op);
1892 else
1893 {
1894 if (opt_type == 1) /* number */
1895 {
1896 if (*op == '+')
1897 n = numval + n;
1898 else
1899 n = numval - n;
1900 }
1901 else if (opt_type == 0 && stringval != NULL) /* string */
1902 {
1903 s = concat_str(stringval, s);
1904 vim_free(stringval);
1905 stringval = s;
1906 }
1907 }
1908 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001909 if (s != NULL)
1910 {
1911 set_option_value(arg, n, s, opt_flags);
1912 arg_end = p;
1913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 }
1917 }
1918
1919 /*
1920 * ":let @r = expr": Set register contents.
1921 */
1922 else if (*arg == '@')
1923 {
1924 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 if (op != NULL && (*op == '+' || *op == '-'))
1926 EMSG2(_(e_letwrong), op);
1927 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 EMSG(_(e_letunexp));
1930 else
1931 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 char_u *tofree = NULL;
1933 char_u *s;
1934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001935 p = get_tv_string_chk(tv);
1936 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00001938 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 if (s != NULL)
1940 {
1941 p = tofree = concat_str(s, p);
1942 vim_free(s);
1943 }
1944 }
1945 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001946 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001948 arg_end = arg + 1;
1949 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 }
1952 }
1953
1954 /*
1955 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001956 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001958 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001962 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1966 EMSG(_(e_letunexp));
1967 else
1968 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001970 arg_end = p;
1971 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 }
1975
1976 else
1977 EMSG2(_(e_invarg2), arg);
1978
1979 return arg_end;
1980}
1981
1982/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001983 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1984 */
1985 static int
1986check_changedtick(arg)
1987 char_u *arg;
1988{
1989 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1990 {
1991 EMSG2(_(e_readonlyvar), arg);
1992 return TRUE;
1993 }
1994 return FALSE;
1995}
1996
1997/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001998 * Get an lval: variable, Dict item or List item that can be assigned a value
1999 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2000 * "name.key", "name.key[expr]" etc.
2001 * Indexing only works if "name" is an existing List or Dictionary.
2002 * "name" points to the start of the name.
2003 * If "rettv" is not NULL it points to the value to be assigned.
2004 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2005 * wrong; must end in space or cmd separator.
2006 *
2007 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002008 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002009 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 */
2011 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002012get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002014 typval_T *rettv;
2015 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 int unlet;
2017 int skip;
2018 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002019 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 char_u *expr_start, *expr_end;
2023 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002024 dictitem_T *v;
2025 typval_T var1;
2026 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002028 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002030 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002031 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002034 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002035
2036 if (skip)
2037 {
2038 /* When skipping just find the end of the name. */
2039 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002040 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 }
2042
2043 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002044 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002045 if (expr_start != NULL)
2046 {
2047 /* Don't expand the name when we already know there is an error. */
2048 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2049 && *p != '[' && *p != '.')
2050 {
2051 EMSG(_(e_trailing));
2052 return NULL;
2053 }
2054
2055 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2056 if (lp->ll_exp_name == NULL)
2057 {
2058 /* Report an invalid expression in braces, unless the
2059 * expression evaluation has been cancelled due to an
2060 * aborting error, an interrupt, or an exception. */
2061 if (!aborting() && !quiet)
2062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002063 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002064 EMSG2(_(e_invarg2), name);
2065 return NULL;
2066 }
2067 }
2068 lp->ll_name = lp->ll_exp_name;
2069 }
2070 else
2071 lp->ll_name = name;
2072
2073 /* Without [idx] or .key we are done. */
2074 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2075 return p;
2076
2077 cc = *p;
2078 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 if (v == NULL && !quiet)
2081 EMSG2(_(e_undefvar), lp->ll_name);
2082 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 if (v == NULL)
2084 return NULL;
2085
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 /*
2087 * Loop until no more [idx] or .key is following.
2088 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2093 && !(lp->ll_tv->v_type == VAR_DICT
2094 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 if (!quiet)
2097 EMSG(_("E689: Can only index a List or Dictionary"));
2098 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 if (!quiet)
2103 EMSG(_("E708: [:] must come last"));
2104 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002106
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 len = -1;
2108 if (*p == '.')
2109 {
2110 key = p + 1;
2111 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2112 ;
2113 if (len == 0)
2114 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (!quiet)
2116 EMSG(_(e_emptykey));
2117 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002118 }
2119 p = key + len;
2120 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002121 else
2122 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002123 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002124 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 if (*p == ':')
2126 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002127 else
2128 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 empty1 = FALSE;
2130 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002132 if (get_tv_string_chk(&var1) == NULL)
2133 {
2134 /* not a number or string */
2135 clear_tv(&var1);
2136 return NULL;
2137 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 }
2139
2140 /* Optionally get the second index [ :expr]. */
2141 if (*p == ':')
2142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002143 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002144 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002146 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002147 if (!empty1)
2148 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002150 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 if (rettv != NULL && (rettv->v_type != VAR_LIST
2152 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002153 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 if (!quiet)
2155 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 if (!empty1)
2157 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002159 }
2160 p = skipwhite(p + 1);
2161 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002163 else
2164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002165 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002166 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002168 if (!empty1)
2169 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 if (get_tv_string_chk(&var2) == NULL)
2173 {
2174 /* not a number or string */
2175 if (!empty1)
2176 clear_tv(&var1);
2177 clear_tv(&var2);
2178 return NULL;
2179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002182 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002185
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (!quiet)
2189 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 if (!empty1)
2191 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002195 }
2196
2197 /* Skip to past ']'. */
2198 ++p;
2199 }
2200
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002202 {
2203 if (len == -1)
2204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002206 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 if (*key == NUL)
2208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (!quiet)
2210 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002213 }
2214 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002215 lp->ll_list = NULL;
2216 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002217 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002220 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002222 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002225 if (len == -1)
2226 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002228 }
2229 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002233 if (len == -1)
2234 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002236 p = NULL;
2237 break;
2238 }
2239 if (len == -1)
2240 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 }
2243 else
2244 {
2245 /*
2246 * Get the number and item for the only or first index of the List.
2247 */
2248 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 else
2251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 clear_tv(&var1);
2254 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002255 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 lp->ll_list = lp->ll_tv->vval.v_list;
2257 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2258 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (!quiet)
2261 EMSGN(_(e_listidx), lp->ll_n1);
2262 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002263 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 }
2266
2267 /*
2268 * May need to find the item or absolute index for the second
2269 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 * When no index given: "lp->ll_empty2" is TRUE.
2271 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002276 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 if (ni == NULL)
2281 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 if (!quiet)
2283 EMSGN(_(e_listidx), lp->ll_n2);
2284 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002285 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002287 }
2288
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2290 if (lp->ll_n1 < 0)
2291 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2292 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002293 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 if (!quiet)
2295 EMSGN(_(e_listidx), lp->ll_n2);
2296 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002297 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002298 }
2299
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 }
2303
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 return p;
2305}
2306
2307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002308 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 */
2310 static void
2311clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002312 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313{
2314 vim_free(lp->ll_exp_name);
2315 vim_free(lp->ll_newkey);
2316}
2317
2318/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002319 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 */
2323 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330{
2331 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002332 listitem_T *ni;
2333 listitem_T *ri;
2334 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335
2336 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 cc = *endp;
2341 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 if (op != NULL && *op != '=')
2343 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002344 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002346 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002347 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2348 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 {
2350 if (tv_op(&tv, rettv, op) == OK)
2351 set_var(lp->ll_name, &tv, FALSE);
2352 clear_tv(&tv);
2353 }
2354 }
2355 else
2356 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002360 else if (tv_check_lock(lp->ll_newkey == NULL
2361 ? lp->ll_tv->v_lock
2362 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2363 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 else if (lp->ll_range)
2365 {
2366 /*
2367 * Assign the List values to the list items.
2368 */
2369 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 if (op != NULL && *op != '=')
2372 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2373 else
2374 {
2375 clear_tv(&lp->ll_li->li_tv);
2376 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2377 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 ri = ri->li_next;
2379 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2380 break;
2381 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002382 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 /* Need to add an empty item. */
2384 ni = listitem_alloc();
2385 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 ri = NULL;
2388 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002389 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002391 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 ni->li_tv.vval.v_number = 0;
2393 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002394 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 lp->ll_li = lp->ll_li->li_next;
2396 ++lp->ll_n1;
2397 }
2398 if (ri != NULL)
2399 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 else if (lp->ll_empty2
2401 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 : lp->ll_n1 != lp->ll_n2)
2403 EMSG(_("E711: List value has not enough items"));
2404 }
2405 else
2406 {
2407 /*
2408 * Assign to a List or Dictionary item.
2409 */
2410 if (lp->ll_newkey != NULL)
2411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 if (op != NULL && *op != '=')
2413 {
2414 EMSG2(_(e_letwrong), op);
2415 return;
2416 }
2417
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002419 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 if (di == NULL)
2421 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002422 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2423 {
2424 vim_free(di);
2425 return;
2426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002428 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 else if (op != NULL && *op != '=')
2430 {
2431 tv_op(lp->ll_tv, rettv, op);
2432 return;
2433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 /*
2438 * Assign the value to the variable or list item.
2439 */
2440 if (copy)
2441 copy_tv(rettv, lp->ll_tv);
2442 else
2443 {
2444 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002445 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 }
2448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449}
2450
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2453 * Returns OK or FAIL.
2454 */
2455 static int
2456tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 typval_T *tv1;
2458 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 char_u *op;
2460{
2461 long n;
2462 char_u numbuf[NUMBUFLEN];
2463 char_u *s;
2464
2465 /* Can't do anything with a Funcref or a Dict on the right. */
2466 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2467 {
2468 switch (tv1->v_type)
2469 {
2470 case VAR_DICT:
2471 case VAR_FUNC:
2472 break;
2473
2474 case VAR_LIST:
2475 if (*op != '+' || tv2->v_type != VAR_LIST)
2476 break;
2477 /* List += List */
2478 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2479 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2480 return OK;
2481
2482 case VAR_NUMBER:
2483 case VAR_STRING:
2484 if (tv2->v_type == VAR_LIST)
2485 break;
2486 if (*op == '+' || *op == '-')
2487 {
2488 /* nr += nr or nr -= nr*/
2489 n = get_tv_number(tv1);
2490 if (*op == '+')
2491 n += get_tv_number(tv2);
2492 else
2493 n -= get_tv_number(tv2);
2494 clear_tv(tv1);
2495 tv1->v_type = VAR_NUMBER;
2496 tv1->vval.v_number = n;
2497 }
2498 else
2499 {
2500 /* str .= str */
2501 s = get_tv_string(tv1);
2502 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2503 clear_tv(tv1);
2504 tv1->v_type = VAR_STRING;
2505 tv1->vval.v_string = s;
2506 }
2507 return OK;
2508 }
2509 }
2510
2511 EMSG2(_(e_letwrong), op);
2512 return FAIL;
2513}
2514
2515/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002516 * Add a watcher to a list.
2517 */
2518 static void
2519list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 list_T *l;
2521 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002522{
2523 lw->lw_next = l->lv_watch;
2524 l->lv_watch = lw;
2525}
2526
2527/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002528 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002529 * No warning when it isn't found...
2530 */
2531 static void
2532list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 list_T *l;
2534 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002535{
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002537
2538 lwp = &l->lv_watch;
2539 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2540 {
2541 if (lw == lwrem)
2542 {
2543 *lwp = lw->lw_next;
2544 break;
2545 }
2546 lwp = &lw->lw_next;
2547 }
2548}
2549
2550/*
2551 * Just before removing an item from a list: advance watchers to the next
2552 * item.
2553 */
2554 static void
2555list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 list_T *l;
2557 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002558{
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002560
2561 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2562 if (lw->lw_item == item)
2563 lw->lw_item = item->li_next;
2564}
2565
2566/*
2567 * Evaluate the expression used in a ":for var in expr" command.
2568 * "arg" points to "var".
2569 * Set "*errp" to TRUE for an error, FALSE otherwise;
2570 * Return a pointer that holds the info. Null when there is an error.
2571 */
2572 void *
2573eval_for_line(arg, errp, nextcmdp, skip)
2574 char_u *arg;
2575 int *errp;
2576 char_u **nextcmdp;
2577 int skip;
2578{
Bram Moolenaar33570922005-01-25 22:26:29 +00002579 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002580 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 typval_T tv;
2582 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002583
2584 *errp = TRUE; /* default: there is an error */
2585
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002587 if (fi == NULL)
2588 return NULL;
2589
2590 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2591 if (expr == NULL)
2592 return fi;
2593
2594 expr = skipwhite(expr);
2595 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2596 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002597 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002598 return fi;
2599 }
2600
2601 if (skip)
2602 ++emsg_skip;
2603 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2604 {
2605 *errp = FALSE;
2606 if (!skip)
2607 {
2608 l = tv.vval.v_list;
2609 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002610 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002611 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002612 clear_tv(&tv);
2613 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002614 else
2615 {
2616 fi->fi_list = l;
2617 list_add_watch(l, &fi->fi_lw);
2618 fi->fi_lw.lw_item = l->lv_first;
2619 }
2620 }
2621 }
2622 if (skip)
2623 --emsg_skip;
2624
2625 return fi;
2626}
2627
2628/*
2629 * Use the first item in a ":for" list. Advance to the next.
2630 * Assign the values to the variable (list). "arg" points to the first one.
2631 * Return TRUE when a valid item was found, FALSE when at end of list or
2632 * something wrong.
2633 */
2634 int
2635next_for_item(fi_void, arg)
2636 void *fi_void;
2637 char_u *arg;
2638{
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002640 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002642
2643 item = fi->fi_lw.lw_item;
2644 if (item == NULL)
2645 result = FALSE;
2646 else
2647 {
2648 fi->fi_lw.lw_item = item->li_next;
2649 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2650 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2651 }
2652 return result;
2653}
2654
2655/*
2656 * Free the structure used to store info used by ":for".
2657 */
2658 void
2659free_for_info(fi_void)
2660 void *fi_void;
2661{
Bram Moolenaar33570922005-01-25 22:26:29 +00002662 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002663
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002664 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002665 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002666 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002667 list_unref(fi->fi_list);
2668 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669 vim_free(fi);
2670}
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2673
2674 void
2675set_context_for_expression(xp, arg, cmdidx)
2676 expand_T *xp;
2677 char_u *arg;
2678 cmdidx_T cmdidx;
2679{
2680 int got_eq = FALSE;
2681 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002682 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002684 if (cmdidx == CMD_let)
2685 {
2686 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002687 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002688 {
2689 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002690 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 {
2692 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002694 if (vim_iswhite(*p))
2695 break;
2696 }
2697 return;
2698 }
2699 }
2700 else
2701 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2702 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 while ((xp->xp_pattern = vim_strpbrk(arg,
2704 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2705 {
2706 c = *xp->xp_pattern;
2707 if (c == '&')
2708 {
2709 c = xp->xp_pattern[1];
2710 if (c == '&')
2711 {
2712 ++xp->xp_pattern;
2713 xp->xp_context = cmdidx != CMD_let || got_eq
2714 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2715 }
2716 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002719 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2720 xp->xp_pattern += 2;
2721
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724 else if (c == '$')
2725 {
2726 /* environment variable */
2727 xp->xp_context = EXPAND_ENV_VARS;
2728 }
2729 else if (c == '=')
2730 {
2731 got_eq = TRUE;
2732 xp->xp_context = EXPAND_EXPRESSION;
2733 }
2734 else if (c == '<'
2735 && xp->xp_context == EXPAND_FUNCTIONS
2736 && vim_strchr(xp->xp_pattern, '(') == NULL)
2737 {
2738 /* Function name can start with "<SNR>" */
2739 break;
2740 }
2741 else if (cmdidx != CMD_let || got_eq)
2742 {
2743 if (c == '"') /* string */
2744 {
2745 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2746 if (c == '\\' && xp->xp_pattern[1] != NUL)
2747 ++xp->xp_pattern;
2748 xp->xp_context = EXPAND_NOTHING;
2749 }
2750 else if (c == '\'') /* literal string */
2751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2754 /* skip */ ;
2755 xp->xp_context = EXPAND_NOTHING;
2756 }
2757 else if (c == '|')
2758 {
2759 if (xp->xp_pattern[1] == '|')
2760 {
2761 ++xp->xp_pattern;
2762 xp->xp_context = EXPAND_EXPRESSION;
2763 }
2764 else
2765 xp->xp_context = EXPAND_COMMANDS;
2766 }
2767 else
2768 xp->xp_context = EXPAND_EXPRESSION;
2769 }
2770 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771 /* Doesn't look like something valid, expand as an expression
2772 * anyway. */
2773 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 arg = xp->xp_pattern;
2775 if (*arg != NUL)
2776 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2777 /* skip */ ;
2778 }
2779 xp->xp_pattern = arg;
2780}
2781
2782#endif /* FEAT_CMDL_COMPL */
2783
2784/*
2785 * ":1,25call func(arg1, arg2)" function call.
2786 */
2787 void
2788ex_call(eap)
2789 exarg_T *eap;
2790{
2791 char_u *arg = eap->arg;
2792 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 linenr_T lnum;
2798 int doesrange;
2799 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002802 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2803 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002804 if (tofree == NULL)
2805 return;
2806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002807 /* Increase refcount on dictionary, it could get deleted when evaluating
2808 * the arguments. */
2809 if (fudi.fd_dict != NULL)
2810 ++fudi.fd_dict->dv_refcount;
2811
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2813 len = STRLEN(tofree);
2814 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
Bram Moolenaar532c7802005-01-27 14:44:31 +00002816 /* Skip white space to allow ":call func ()". Not good, but required for
2817 * backward compatibility. */
2818 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002819 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821 if (*startarg != '(')
2822 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002823 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 goto end;
2825 }
2826
2827 /*
2828 * When skipping, evaluate the function once, to find the end of the
2829 * arguments.
2830 * When the function takes a range, this is discovered after the first
2831 * call, and the loop is broken.
2832 */
2833 if (eap->skip)
2834 {
2835 ++emsg_skip;
2836 lnum = eap->line2; /* do it once, also with an invalid range */
2837 }
2838 else
2839 lnum = eap->line1;
2840 for ( ; lnum <= eap->line2; ++lnum)
2841 {
2842 if (!eap->skip && eap->addr_count > 0)
2843 {
2844 curwin->w_cursor.lnum = lnum;
2845 curwin->w_cursor.col = 0;
2846 }
2847 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002848 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002849 eap->line1, eap->line2, &doesrange,
2850 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 failed = TRUE;
2853 break;
2854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002855 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 if (doesrange || eap->skip)
2857 break;
2858 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002859 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002860 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002861 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (aborting())
2863 break;
2864 }
2865 if (eap->skip)
2866 --emsg_skip;
2867
2868 if (!failed)
2869 {
2870 /* Check for trailing illegal characters and a following command. */
2871 if (!ends_excmd(*arg))
2872 {
2873 emsg_severe = TRUE;
2874 EMSG(_(e_trailing));
2875 }
2876 else
2877 eap->nextcmd = check_nextcmd(arg);
2878 }
2879
2880end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002881 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883}
2884
2885/*
2886 * ":unlet[!] var1 ... " command.
2887 */
2888 void
2889ex_unlet(eap)
2890 exarg_T *eap;
2891{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 ex_unletlock(eap, eap->arg, 0);
2893}
2894
2895/*
2896 * ":lockvar" and ":unlockvar" commands
2897 */
2898 void
2899ex_lockvar(eap)
2900 exarg_T *eap;
2901{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903 int deep = 2;
2904
2905 if (eap->forceit)
2906 deep = -1;
2907 else if (vim_isdigit(*arg))
2908 {
2909 deep = getdigits(&arg);
2910 arg = skipwhite(arg);
2911 }
2912
2913 ex_unletlock(eap, arg, deep);
2914}
2915
2916/*
2917 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2918 */
2919 static void
2920ex_unletlock(eap, argstart, deep)
2921 exarg_T *eap;
2922 char_u *argstart;
2923 int deep;
2924{
2925 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 do
2931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002933 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
2934 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 if (lv.ll_name == NULL)
2936 error = TRUE; /* error but continue parsing */
2937 if (name_end == NULL || (!vim_iswhite(*name_end)
2938 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 if (name_end != NULL)
2941 {
2942 emsg_severe = TRUE;
2943 EMSG(_(e_trailing));
2944 }
2945 if (!(eap->skip || error))
2946 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 break;
2948 }
2949
2950 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 {
2952 if (eap->cmdidx == CMD_unlet)
2953 {
2954 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2955 error = TRUE;
2956 }
2957 else
2958 {
2959 if (do_lock_var(&lv, name_end, deep,
2960 eap->cmdidx == CMD_lockvar) == FAIL)
2961 error = TRUE;
2962 }
2963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 if (!eap->skip)
2966 clear_lval(&lv);
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 arg = skipwhite(name_end);
2969 } while (!ends_excmd(*arg));
2970
2971 eap->nextcmd = check_nextcmd(arg);
2972}
2973
Bram Moolenaar8c711452005-01-14 21:53:12 +00002974 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002976 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002978 int forceit;
2979{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 int ret = OK;
2981 int cc;
2982
2983 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 cc = *name_end;
2986 *name_end = NUL;
2987
2988 /* Normal name or expanded name. */
2989 if (check_changedtick(lp->ll_name))
2990 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002991 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002994 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002995 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2996 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 else if (lp->ll_range)
2998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000
3001 /* Delete a range of List items. */
3002 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3003 {
3004 li = lp->ll_li->li_next;
3005 listitem_remove(lp->ll_list, lp->ll_li);
3006 lp->ll_li = li;
3007 ++lp->ll_n1;
3008 }
3009 }
3010 else
3011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 /* unlet a List item. */
3014 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003017 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 }
3019
3020 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021}
3022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
3024 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003025 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
3027 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003028do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003030 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 hashtab_T *ht;
3033 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003034 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar33570922005-01-25 22:26:29 +00003036 ht = find_var_ht(name, &varname);
3037 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003039 hi = hash_find(ht, varname);
3040 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003041 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003042 if (var_check_ro(HI2DI(hi)->di_flags, name))
3043 return FAIL;
3044 delete_var(ht, hi);
3045 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003048 if (forceit)
3049 return OK;
3050 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 return FAIL;
3052}
3053
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003054/*
3055 * Lock or unlock variable indicated by "lp".
3056 * "deep" is the levels to go (-1 for unlimited);
3057 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3058 */
3059 static int
3060do_lock_var(lp, name_end, deep, lock)
3061 lval_T *lp;
3062 char_u *name_end;
3063 int deep;
3064 int lock;
3065{
3066 int ret = OK;
3067 int cc;
3068 dictitem_T *di;
3069
3070 if (deep == 0) /* nothing to do */
3071 return OK;
3072
3073 if (lp->ll_tv == NULL)
3074 {
3075 cc = *name_end;
3076 *name_end = NUL;
3077
3078 /* Normal name or expanded name. */
3079 if (check_changedtick(lp->ll_name))
3080 ret = FAIL;
3081 else
3082 {
3083 di = find_var(lp->ll_name, NULL);
3084 if (di == NULL)
3085 ret = FAIL;
3086 else
3087 {
3088 if (lock)
3089 di->di_flags |= DI_FLAGS_LOCK;
3090 else
3091 di->di_flags &= ~DI_FLAGS_LOCK;
3092 item_lock(&di->di_tv, deep, lock);
3093 }
3094 }
3095 *name_end = cc;
3096 }
3097 else if (lp->ll_range)
3098 {
3099 listitem_T *li = lp->ll_li;
3100
3101 /* (un)lock a range of List items. */
3102 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3103 {
3104 item_lock(&li->li_tv, deep, lock);
3105 li = li->li_next;
3106 ++lp->ll_n1;
3107 }
3108 }
3109 else if (lp->ll_list != NULL)
3110 /* (un)lock a List item. */
3111 item_lock(&lp->ll_li->li_tv, deep, lock);
3112 else
3113 /* un(lock) a Dictionary item. */
3114 item_lock(&lp->ll_di->di_tv, deep, lock);
3115
3116 return ret;
3117}
3118
3119/*
3120 * Lock or unlock an item. "deep" is nr of levels to go.
3121 */
3122 static void
3123item_lock(tv, deep, lock)
3124 typval_T *tv;
3125 int deep;
3126 int lock;
3127{
3128 static int recurse = 0;
3129 list_T *l;
3130 listitem_T *li;
3131 dict_T *d;
3132 hashitem_T *hi;
3133 int todo;
3134
3135 if (recurse >= DICT_MAXNEST)
3136 {
3137 EMSG(_("E743: variable nested too deep for (un)lock"));
3138 return;
3139 }
3140 if (deep == 0)
3141 return;
3142 ++recurse;
3143
3144 /* lock/unlock the item itself */
3145 if (lock)
3146 tv->v_lock |= VAR_LOCKED;
3147 else
3148 tv->v_lock &= ~VAR_LOCKED;
3149
3150 switch (tv->v_type)
3151 {
3152 case VAR_LIST:
3153 if ((l = tv->vval.v_list) != NULL)
3154 {
3155 if (lock)
3156 l->lv_lock |= VAR_LOCKED;
3157 else
3158 l->lv_lock &= ~VAR_LOCKED;
3159 if (deep < 0 || deep > 1)
3160 /* recursive: lock/unlock the items the List contains */
3161 for (li = l->lv_first; li != NULL; li = li->li_next)
3162 item_lock(&li->li_tv, deep - 1, lock);
3163 }
3164 break;
3165 case VAR_DICT:
3166 if ((d = tv->vval.v_dict) != NULL)
3167 {
3168 if (lock)
3169 d->dv_lock |= VAR_LOCKED;
3170 else
3171 d->dv_lock &= ~VAR_LOCKED;
3172 if (deep < 0 || deep > 1)
3173 {
3174 /* recursive: lock/unlock the items the List contains */
3175 todo = d->dv_hashtab.ht_used;
3176 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3177 {
3178 if (!HASHITEM_EMPTY(hi))
3179 {
3180 --todo;
3181 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3182 }
3183 }
3184 }
3185 }
3186 }
3187 --recurse;
3188}
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3191/*
3192 * Delete all "menutrans_" variables.
3193 */
3194 void
3195del_menutrans_vars()
3196{
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003198 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 hash_lock(&globvarht);
3201 todo = globvarht.ht_used;
3202 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003203 {
3204 if (!HASHITEM_EMPTY(hi))
3205 {
3206 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3208 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003209 }
3210 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213#endif
3214
3215#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3216
3217/*
3218 * Local string buffer for the next two functions to store a variable name
3219 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3220 * get_user_var_name().
3221 */
3222
3223static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3224
3225static char_u *varnamebuf = NULL;
3226static int varnamebuflen = 0;
3227
3228/*
3229 * Function to concatenate a prefix and a variable name.
3230 */
3231 static char_u *
3232cat_prefix_varname(prefix, name)
3233 int prefix;
3234 char_u *name;
3235{
3236 int len;
3237
3238 len = (int)STRLEN(name) + 3;
3239 if (len > varnamebuflen)
3240 {
3241 vim_free(varnamebuf);
3242 len += 10; /* some additional space */
3243 varnamebuf = alloc(len);
3244 if (varnamebuf == NULL)
3245 {
3246 varnamebuflen = 0;
3247 return NULL;
3248 }
3249 varnamebuflen = len;
3250 }
3251 *varnamebuf = prefix;
3252 varnamebuf[1] = ':';
3253 STRCPY(varnamebuf + 2, name);
3254 return varnamebuf;
3255}
3256
3257/*
3258 * Function given to ExpandGeneric() to obtain the list of user defined
3259 * (global/buffer/window/built-in) variable names.
3260 */
3261/*ARGSUSED*/
3262 char_u *
3263get_user_var_name(xp, idx)
3264 expand_T *xp;
3265 int idx;
3266{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003267 static long_u gdone;
3268 static long_u bdone;
3269 static long_u wdone;
3270 static int vidx;
3271 static hashitem_T *hi;
3272 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276
3277 /* Global variables */
3278 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003282 else
3283 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003284 while (HASHITEM_EMPTY(hi))
3285 ++hi;
3286 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3287 return cat_prefix_varname('g', hi->hi_key);
3288 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003290
3291 /* b: variables */
3292 ht = &curbuf->b_vars.dv_hashtab;
3293 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003295 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003297 else
3298 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003299 while (HASHITEM_EMPTY(hi))
3300 ++hi;
3301 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003305 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 return (char_u *)"b:changedtick";
3307 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003308
3309 /* w: variables */
3310 ht = &curwin->w_vars.dv_hashtab;
3311 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003313 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003314 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003315 else
3316 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003317 while (HASHITEM_EMPTY(hi))
3318 ++hi;
3319 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003321
3322 /* v: variables */
3323 if (vidx < VV_LEN)
3324 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
3326 vim_free(varnamebuf);
3327 varnamebuf = NULL;
3328 varnamebuflen = 0;
3329 return NULL;
3330}
3331
3332#endif /* FEAT_CMDL_COMPL */
3333
3334/*
3335 * types for expressions.
3336 */
3337typedef enum
3338{
3339 TYPE_UNKNOWN = 0
3340 , TYPE_EQUAL /* == */
3341 , TYPE_NEQUAL /* != */
3342 , TYPE_GREATER /* > */
3343 , TYPE_GEQUAL /* >= */
3344 , TYPE_SMALLER /* < */
3345 , TYPE_SEQUAL /* <= */
3346 , TYPE_MATCH /* =~ */
3347 , TYPE_NOMATCH /* !~ */
3348} exptype_T;
3349
3350/*
3351 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3354 */
3355
3356/*
3357 * Handle zero level expression.
3358 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * Return OK or FAIL.
3361 */
3362 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 char_u **nextcmd;
3367 int evaluate;
3368{
3369 int ret;
3370 char_u *p;
3371
3372 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (ret == FAIL || !ends_excmd(*p))
3375 {
3376 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 /*
3379 * Report the invalid expression unless the expression evaluation has
3380 * been cancelled due to an aborting error, an interrupt, or an
3381 * exception.
3382 */
3383 if (!aborting())
3384 EMSG2(_(e_invexpr2), arg);
3385 ret = FAIL;
3386 }
3387 if (nextcmd != NULL)
3388 *nextcmd = check_nextcmd(p);
3389
3390 return ret;
3391}
3392
3393/*
3394 * Handle top level expression:
3395 * expr1 ? expr0 : expr0
3396 *
3397 * "arg" must point to the first non-white of the expression.
3398 * "arg" is advanced to the next non-white after the recognized expression.
3399 *
3400 * Return OK or FAIL.
3401 */
3402 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 int evaluate;
3407{
3408 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 /*
3412 * Get the first variable.
3413 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003414 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return FAIL;
3416
3417 if ((*arg)[0] == '?')
3418 {
3419 result = FALSE;
3420 if (evaluate)
3421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003422 int error = FALSE;
3423
3424 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003427 if (error)
3428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
3430
3431 /*
3432 * Get the second variable.
3433 */
3434 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return FAIL;
3437
3438 /*
3439 * Check for the ":".
3440 */
3441 if ((*arg)[0] != ':')
3442 {
3443 EMSG(_("E109: Missing ':' after '?'"));
3444 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 return FAIL;
3447 }
3448
3449 /*
3450 * Get the third variable.
3451 */
3452 *arg = skipwhite(*arg + 1);
3453 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3454 {
3455 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 return FAIL;
3458 }
3459 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003460 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462
3463 return OK;
3464}
3465
3466/*
3467 * Handle first level expression:
3468 * expr2 || expr2 || expr2 logical OR
3469 *
3470 * "arg" must point to the first non-white of the expression.
3471 * "arg" is advanced to the next non-white after the recognized expression.
3472 *
3473 * Return OK or FAIL.
3474 */
3475 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int evaluate;
3480{
Bram Moolenaar33570922005-01-25 22:26:29 +00003481 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 long result;
3483 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003484 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 /*
3487 * Get the first variable.
3488 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003489 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 return FAIL;
3491
3492 /*
3493 * Repeat until there is no following "||".
3494 */
3495 first = TRUE;
3496 result = FALSE;
3497 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3498 {
3499 if (evaluate && first)
3500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003501 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003504 if (error)
3505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 first = FALSE;
3507 }
3508
3509 /*
3510 * Get the second variable.
3511 */
3512 *arg = skipwhite(*arg + 2);
3513 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3514 return FAIL;
3515
3516 /*
3517 * Compute the result.
3518 */
3519 if (evaluate && !result)
3520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003521 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003524 if (error)
3525 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 if (evaluate)
3528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003529 rettv->v_type = VAR_NUMBER;
3530 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 }
3533
3534 return OK;
3535}
3536
3537/*
3538 * Handle second level expression:
3539 * expr3 && expr3 && expr3 logical AND
3540 *
3541 * "arg" must point to the first non-white of the expression.
3542 * "arg" is advanced to the next non-white after the recognized expression.
3543 *
3544 * Return OK or FAIL.
3545 */
3546 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 int evaluate;
3551{
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 long result;
3554 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003555 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 /*
3558 * Get the first variable.
3559 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 return FAIL;
3562
3563 /*
3564 * Repeat until there is no following "&&".
3565 */
3566 first = TRUE;
3567 result = TRUE;
3568 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3569 {
3570 if (evaluate && first)
3571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003572 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575 if (error)
3576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 first = FALSE;
3578 }
3579
3580 /*
3581 * Get the second variable.
3582 */
3583 *arg = skipwhite(*arg + 2);
3584 if (eval4(arg, &var2, evaluate && result) == FAIL)
3585 return FAIL;
3586
3587 /*
3588 * Compute the result.
3589 */
3590 if (evaluate && result)
3591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003592 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003595 if (error)
3596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 if (evaluate)
3599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 rettv->v_type = VAR_NUMBER;
3601 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 }
3604
3605 return OK;
3606}
3607
3608/*
3609 * Handle third level expression:
3610 * var1 == var2
3611 * var1 =~ var2
3612 * var1 != var2
3613 * var1 !~ var2
3614 * var1 > var2
3615 * var1 >= var2
3616 * var1 < var2
3617 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003618 * var1 is var2
3619 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 *
3621 * "arg" must point to the first non-white of the expression.
3622 * "arg" is advanced to the next non-white after the recognized expression.
3623 *
3624 * Return OK or FAIL.
3625 */
3626 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 int evaluate;
3631{
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 char_u *p;
3634 int i;
3635 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003636 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 int len = 2;
3638 long n1, n2;
3639 char_u *s1, *s2;
3640 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3641 regmatch_T regmatch;
3642 int ic;
3643 char_u *save_cpo;
3644
3645 /*
3646 * Get the first variable.
3647 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003648 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650
3651 p = *arg;
3652 switch (p[0])
3653 {
3654 case '=': if (p[1] == '=')
3655 type = TYPE_EQUAL;
3656 else if (p[1] == '~')
3657 type = TYPE_MATCH;
3658 break;
3659 case '!': if (p[1] == '=')
3660 type = TYPE_NEQUAL;
3661 else if (p[1] == '~')
3662 type = TYPE_NOMATCH;
3663 break;
3664 case '>': if (p[1] != '=')
3665 {
3666 type = TYPE_GREATER;
3667 len = 1;
3668 }
3669 else
3670 type = TYPE_GEQUAL;
3671 break;
3672 case '<': if (p[1] != '=')
3673 {
3674 type = TYPE_SMALLER;
3675 len = 1;
3676 }
3677 else
3678 type = TYPE_SEQUAL;
3679 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003680 case 'i': if (p[1] == 's')
3681 {
3682 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3683 len = 5;
3684 if (!vim_isIDc(p[len]))
3685 {
3686 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3687 type_is = TRUE;
3688 }
3689 }
3690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 /*
3694 * If there is a comparitive operator, use it.
3695 */
3696 if (type != TYPE_UNKNOWN)
3697 {
3698 /* extra question mark appended: ignore case */
3699 if (p[len] == '?')
3700 {
3701 ic = TRUE;
3702 ++len;
3703 }
3704 /* extra '#' appended: match case */
3705 else if (p[len] == '#')
3706 {
3707 ic = FALSE;
3708 ++len;
3709 }
3710 /* nothing appened: use 'ignorecase' */
3711 else
3712 ic = p_ic;
3713
3714 /*
3715 * Get the second variable.
3716 */
3717 *arg = skipwhite(p + len);
3718 if (eval5(arg, &var2, evaluate) == FAIL)
3719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 return FAIL;
3722 }
3723
3724 if (evaluate)
3725 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003726 if (type_is && rettv->v_type != var2.v_type)
3727 {
3728 /* For "is" a different type always means FALSE, for "notis"
3729 * it means TRUE. */
3730 n1 = (type == TYPE_NEQUAL);
3731 }
3732 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3733 {
3734 if (type_is)
3735 {
3736 n1 = (rettv->v_type == var2.v_type
3737 && rettv->vval.v_list == var2.vval.v_list);
3738 if (type == TYPE_NEQUAL)
3739 n1 = !n1;
3740 }
3741 else if (rettv->v_type != var2.v_type
3742 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3743 {
3744 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003745 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003746 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003747 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003748 clear_tv(rettv);
3749 clear_tv(&var2);
3750 return FAIL;
3751 }
3752 else
3753 {
3754 /* Compare two Lists for being equal or unequal. */
3755 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3756 if (type == TYPE_NEQUAL)
3757 n1 = !n1;
3758 }
3759 }
3760
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003761 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3762 {
3763 if (type_is)
3764 {
3765 n1 = (rettv->v_type == var2.v_type
3766 && rettv->vval.v_dict == var2.vval.v_dict);
3767 if (type == TYPE_NEQUAL)
3768 n1 = !n1;
3769 }
3770 else if (rettv->v_type != var2.v_type
3771 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3772 {
3773 if (rettv->v_type != var2.v_type)
3774 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3775 else
3776 EMSG(_("E736: Invalid operation for Dictionary"));
3777 clear_tv(rettv);
3778 clear_tv(&var2);
3779 return FAIL;
3780 }
3781 else
3782 {
3783 /* Compare two Dictionaries for being equal or unequal. */
3784 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3785 if (type == TYPE_NEQUAL)
3786 n1 = !n1;
3787 }
3788 }
3789
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003790 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3791 {
3792 if (rettv->v_type != var2.v_type
3793 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3794 {
3795 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003796 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003797 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003798 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003799 clear_tv(rettv);
3800 clear_tv(&var2);
3801 return FAIL;
3802 }
3803 else
3804 {
3805 /* Compare two Funcrefs for being equal or unequal. */
3806 if (rettv->vval.v_string == NULL
3807 || var2.vval.v_string == NULL)
3808 n1 = FALSE;
3809 else
3810 n1 = STRCMP(rettv->vval.v_string,
3811 var2.vval.v_string) == 0;
3812 if (type == TYPE_NEQUAL)
3813 n1 = !n1;
3814 }
3815 }
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 /*
3818 * If one of the two variables is a number, compare as a number.
3819 * When using "=~" or "!~", always compare as string.
3820 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003821 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 n1 = get_tv_number(rettv);
3825 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 switch (type)
3827 {
3828 case TYPE_EQUAL: n1 = (n1 == n2); break;
3829 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3830 case TYPE_GREATER: n1 = (n1 > n2); break;
3831 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3832 case TYPE_SMALLER: n1 = (n1 < n2); break;
3833 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3834 case TYPE_UNKNOWN:
3835 case TYPE_MATCH:
3836 case TYPE_NOMATCH: break; /* avoid gcc warning */
3837 }
3838 }
3839 else
3840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 s1 = get_tv_string_buf(rettv, buf1);
3842 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3844 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3845 else
3846 i = 0;
3847 n1 = FALSE;
3848 switch (type)
3849 {
3850 case TYPE_EQUAL: n1 = (i == 0); break;
3851 case TYPE_NEQUAL: n1 = (i != 0); break;
3852 case TYPE_GREATER: n1 = (i > 0); break;
3853 case TYPE_GEQUAL: n1 = (i >= 0); break;
3854 case TYPE_SMALLER: n1 = (i < 0); break;
3855 case TYPE_SEQUAL: n1 = (i <= 0); break;
3856
3857 case TYPE_MATCH:
3858 case TYPE_NOMATCH:
3859 /* avoid 'l' flag in 'cpoptions' */
3860 save_cpo = p_cpo;
3861 p_cpo = (char_u *)"";
3862 regmatch.regprog = vim_regcomp(s2,
3863 RE_MAGIC + RE_STRING);
3864 regmatch.rm_ic = ic;
3865 if (regmatch.regprog != NULL)
3866 {
3867 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3868 vim_free(regmatch.regprog);
3869 if (type == TYPE_NOMATCH)
3870 n1 = !n1;
3871 }
3872 p_cpo = save_cpo;
3873 break;
3874
3875 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3876 }
3877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003878 clear_tv(rettv);
3879 clear_tv(&var2);
3880 rettv->v_type = VAR_NUMBER;
3881 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 }
3884
3885 return OK;
3886}
3887
3888/*
3889 * Handle fourth level expression:
3890 * + number addition
3891 * - number subtraction
3892 * . string concatenation
3893 *
3894 * "arg" must point to the first non-white of the expression.
3895 * "arg" is advanced to the next non-white after the recognized expression.
3896 *
3897 * Return OK or FAIL.
3898 */
3899 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 int evaluate;
3904{
Bram Moolenaar33570922005-01-25 22:26:29 +00003905 typval_T var2;
3906 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 int op;
3908 long n1, n2;
3909 char_u *s1, *s2;
3910 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3911 char_u *p;
3912
3913 /*
3914 * Get the first variable.
3915 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003916 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 return FAIL;
3918
3919 /*
3920 * Repeat computing, until no '+', '-' or '.' is following.
3921 */
3922 for (;;)
3923 {
3924 op = **arg;
3925 if (op != '+' && op != '-' && op != '.')
3926 break;
3927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003928 if (op != '+' || rettv->v_type != VAR_LIST)
3929 {
3930 /* For "list + ...", an illegal use of the first operand as
3931 * a number cannot be determined before evaluating the 2nd
3932 * operand: if this is also a list, all is ok.
3933 * For "something . ...", "something - ..." or "non-list + ...",
3934 * we know that the first operand needs to be a string or number
3935 * without evaluating the 2nd operand. So check before to avoid
3936 * side effects after an error. */
3937 if (evaluate && get_tv_string_chk(rettv) == NULL)
3938 {
3939 clear_tv(rettv);
3940 return FAIL;
3941 }
3942 }
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 /*
3945 * Get the second variable.
3946 */
3947 *arg = skipwhite(*arg + 1);
3948 if (eval6(arg, &var2, evaluate) == FAIL)
3949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 return FAIL;
3952 }
3953
3954 if (evaluate)
3955 {
3956 /*
3957 * Compute the result.
3958 */
3959 if (op == '.')
3960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003961 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
3962 s2 = get_tv_string_buf_chk(&var2, buf2);
3963 if (s2 == NULL) /* type error ? */
3964 {
3965 clear_tv(rettv);
3966 clear_tv(&var2);
3967 return FAIL;
3968 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003969 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970 clear_tv(rettv);
3971 rettv->v_type = VAR_STRING;
3972 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003974 else if (op == '+' && rettv->v_type == VAR_LIST
3975 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003976 {
3977 /* concatenate Lists */
3978 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3979 &var3) == FAIL)
3980 {
3981 clear_tv(rettv);
3982 clear_tv(&var2);
3983 return FAIL;
3984 }
3985 clear_tv(rettv);
3986 *rettv = var3;
3987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 else
3989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003990 int error = FALSE;
3991
3992 n1 = get_tv_number_chk(rettv, &error);
3993 if (error)
3994 {
3995 /* This can only happen for "list + non-list".
3996 * For "non-list + ..." or "something - ...", we returned
3997 * before evaluating the 2nd operand. */
3998 clear_tv(rettv);
3999 return FAIL;
4000 }
4001 n2 = get_tv_number_chk(&var2, &error);
4002 if (error)
4003 {
4004 clear_tv(rettv);
4005 clear_tv(&var2);
4006 return FAIL;
4007 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (op == '+')
4010 n1 = n1 + n2;
4011 else
4012 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004013 rettv->v_type = VAR_NUMBER;
4014 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 }
4019 return OK;
4020}
4021
4022/*
4023 * Handle fifth level expression:
4024 * * number multiplication
4025 * / number division
4026 * % number modulo
4027 *
4028 * "arg" must point to the first non-white of the expression.
4029 * "arg" is advanced to the next non-white after the recognized expression.
4030 *
4031 * Return OK or FAIL.
4032 */
4033 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 int evaluate;
4038{
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 int op;
4041 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004042 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044 /*
4045 * Get the first variable.
4046 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return FAIL;
4049
4050 /*
4051 * Repeat computing, until no '*', '/' or '%' is following.
4052 */
4053 for (;;)
4054 {
4055 op = **arg;
4056 if (op != '*' && op != '/' && op != '%')
4057 break;
4058
4059 if (evaluate)
4060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004061 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 if (error)
4064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066 else
4067 n1 = 0;
4068
4069 /*
4070 * Get the second variable.
4071 */
4072 *arg = skipwhite(*arg + 1);
4073 if (eval7(arg, &var2, evaluate) == FAIL)
4074 return FAIL;
4075
4076 if (evaluate)
4077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004080 if (error)
4081 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 /*
4084 * Compute the result.
4085 */
4086 if (op == '*')
4087 n1 = n1 * n2;
4088 else if (op == '/')
4089 {
4090 if (n2 == 0) /* give an error message? */
4091 n1 = 0x7fffffffL;
4092 else
4093 n1 = n1 / n2;
4094 }
4095 else
4096 {
4097 if (n2 == 0) /* give an error message? */
4098 n1 = 0;
4099 else
4100 n1 = n1 % n2;
4101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 rettv->v_type = VAR_NUMBER;
4103 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
4105 }
4106
4107 return OK;
4108}
4109
4110/*
4111 * Handle sixth level expression:
4112 * number number constant
4113 * "string" string contstant
4114 * 'string' literal string contstant
4115 * &option-name option value
4116 * @r register contents
4117 * identifier variable value
4118 * function() function call
4119 * $VAR environment variable
4120 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004121 * [expr, expr] List
4122 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 *
4124 * Also handle:
4125 * ! in front logical NOT
4126 * - in front unary minus
4127 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128 * trailing [] subscript in String or List
4129 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 *
4131 * "arg" must point to the first non-white of the expression.
4132 * "arg" is advanced to the next non-white after the recognized expression.
4133 *
4134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int evaluate;
4141{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 long n;
4143 int len;
4144 char_u *s;
4145 int val;
4146 char_u *start_leader, *end_leader;
4147 int ret = OK;
4148 char_u *alias;
4149
4150 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004152 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /*
4157 * Skip '!' and '-' characters. They are handled later.
4158 */
4159 start_leader = *arg;
4160 while (**arg == '!' || **arg == '-' || **arg == '+')
4161 *arg = skipwhite(*arg + 1);
4162 end_leader = *arg;
4163
4164 switch (**arg)
4165 {
4166 /*
4167 * Number constant.
4168 */
4169 case '0':
4170 case '1':
4171 case '2':
4172 case '3':
4173 case '4':
4174 case '5':
4175 case '6':
4176 case '7':
4177 case '8':
4178 case '9':
4179 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4180 *arg += len;
4181 if (evaluate)
4182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 rettv->v_type = VAR_NUMBER;
4184 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 break;
4187
4188 /*
4189 * String constant: "string".
4190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 break;
4193
4194 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004195 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004198 break;
4199
4200 /*
4201 * List: [expr, expr]
4202 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 break;
4205
4206 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207 * Dictionary: {key: val, key: val}
4208 */
4209 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4210 break;
4211
4212 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004213 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004215 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 break;
4217
4218 /*
4219 * Environment variable: $VAR.
4220 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 break;
4223
4224 /*
4225 * Register contents: @r.
4226 */
4227 case '@': ++*arg;
4228 if (evaluate)
4229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004231 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 if (**arg != NUL)
4234 ++*arg;
4235 break;
4236
4237 /*
4238 * nested expression: (expression).
4239 */
4240 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 if (**arg == ')')
4243 ++*arg;
4244 else if (ret == OK)
4245 {
4246 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 ret = FAIL;
4249 }
4250 break;
4251
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 break;
4254 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004255
4256 if (ret == NOTDONE)
4257 {
4258 /*
4259 * Must be a variable or function name.
4260 * Can also be a curly-braces kind of name: {expr}.
4261 */
4262 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004263 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004264 if (alias != NULL)
4265 s = alias;
4266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004267 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004268 ret = FAIL;
4269 else
4270 {
4271 if (**arg == '(') /* recursive! */
4272 {
4273 /* If "s" is the name of a variable of type VAR_FUNC
4274 * use its contents. */
4275 s = deref_func_name(s, &len);
4276
4277 /* Invoke the function. */
4278 ret = get_func_tv(s, len, rettv, arg,
4279 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004280 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004281 /* Stop the expression evaluation when immediately
4282 * aborting on error, or when an interrupt occurred or
4283 * an exception was thrown but not caught. */
4284 if (aborting())
4285 {
4286 if (ret == OK)
4287 clear_tv(rettv);
4288 ret = FAIL;
4289 }
4290 }
4291 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004292 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004293 else
4294 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004295 }
4296
4297 if (alias != NULL)
4298 vim_free(alias);
4299 }
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 *arg = skipwhite(*arg);
4302
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004303 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4304 * expr(expr). */
4305 if (ret == OK)
4306 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 /*
4309 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4310 */
4311 if (ret == OK && evaluate && end_leader > start_leader)
4312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 int error = FALSE;
4314
4315 val = get_tv_number_chk(rettv, &error);
4316 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 clear_tv(rettv);
4319 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 else
4322 {
4323 while (end_leader > start_leader)
4324 {
4325 --end_leader;
4326 if (*end_leader == '!')
4327 val = !val;
4328 else if (*end_leader == '-')
4329 val = -val;
4330 }
4331 clear_tv(rettv);
4332 rettv->v_type = VAR_NUMBER;
4333 rettv->vval.v_number = val;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336
4337 return ret;
4338}
4339
4340/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004341 * Evaluate an "[expr]" or "[expr:expr]" index.
4342 * "*arg" points to the '['.
4343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4344 */
4345 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004346eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004347 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004348 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004349 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004350 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004351{
4352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004355 long len = -1;
4356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004359
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004362 if (verbose)
4363 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004364 return FAIL;
4365 }
4366
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004368 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 /*
4370 * dict.name
4371 */
4372 key = *arg + 1;
4373 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4374 ;
4375 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004376 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004378 }
4379 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004380 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004381 /*
4382 * something[idx]
4383 *
4384 * Get the (first) variable from inside the [].
4385 */
4386 *arg = skipwhite(*arg + 1);
4387 if (**arg == ':')
4388 empty1 = TRUE;
4389 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4390 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4392 {
4393 /* not a number or string */
4394 clear_tv(&var1);
4395 return FAIL;
4396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004397
4398 /*
4399 * Get the second variable from inside the [:].
4400 */
4401 if (**arg == ':')
4402 {
4403 range = TRUE;
4404 *arg = skipwhite(*arg + 1);
4405 if (**arg == ']')
4406 empty2 = TRUE;
4407 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 if (!empty1)
4410 clear_tv(&var1);
4411 return FAIL;
4412 }
4413 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4414 {
4415 /* not a number or string */
4416 if (!empty1)
4417 clear_tv(&var1);
4418 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004419 return FAIL;
4420 }
4421 }
4422
4423 /* Check for the ']'. */
4424 if (**arg != ']')
4425 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004426 if (verbose)
4427 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004428 clear_tv(&var1);
4429 if (range)
4430 clear_tv(&var2);
4431 return FAIL;
4432 }
4433 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 }
4435
4436 if (evaluate)
4437 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 n1 = 0;
4439 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 n1 = get_tv_number(&var1);
4442 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443 }
4444 if (range)
4445 {
4446 if (empty2)
4447 n2 = -1;
4448 else
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 n2 = get_tv_number(&var2);
4451 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 }
4453 }
4454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 {
4457 case VAR_NUMBER:
4458 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 len = (long)STRLEN(s);
4461 if (range)
4462 {
4463 /* The resulting variable is a substring. If the indexes
4464 * are out of range the result is empty. */
4465 if (n1 < 0)
4466 {
4467 n1 = len + n1;
4468 if (n1 < 0)
4469 n1 = 0;
4470 }
4471 if (n2 < 0)
4472 n2 = len + n2;
4473 else if (n2 >= len)
4474 n2 = len;
4475 if (n1 >= len || n2 < 0 || n1 > n2)
4476 s = NULL;
4477 else
4478 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4479 }
4480 else
4481 {
4482 /* The resulting variable is a string of a single
4483 * character. If the index is too big or negative the
4484 * result is empty. */
4485 if (n1 >= len || n1 < 0)
4486 s = NULL;
4487 else
4488 s = vim_strnsave(s + n1, 1);
4489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 clear_tv(rettv);
4491 rettv->v_type = VAR_STRING;
4492 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004493 break;
4494
4495 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497 if (n1 < 0)
4498 n1 = len + n1;
4499 if (!empty1 && (n1 < 0 || n1 >= len))
4500 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004501 if (verbose)
4502 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 return FAIL;
4504 }
4505 if (range)
4506 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004507 list_T *l;
4508 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004509
4510 if (n2 < 0)
4511 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004512 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004514 if (verbose)
4515 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004516 return FAIL;
4517 }
4518 l = list_alloc();
4519 if (l == NULL)
4520 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522 n1 <= n2; ++n1)
4523 {
4524 if (list_append_tv(l, &item->li_tv) == FAIL)
4525 {
4526 list_free(l);
4527 return FAIL;
4528 }
4529 item = item->li_next;
4530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 clear_tv(rettv);
4532 rettv->v_type = VAR_LIST;
4533 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004534 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535 }
4536 else
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 clear_tv(rettv);
4541 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 }
4543 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544
4545 case VAR_DICT:
4546 if (range)
4547 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004548 if (verbose)
4549 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 if (len == -1)
4551 clear_tv(&var1);
4552 return FAIL;
4553 }
4554 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004555 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556
4557 if (len == -1)
4558 {
4559 key = get_tv_string(&var1);
4560 if (*key == NUL)
4561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004562 if (verbose)
4563 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004564 clear_tv(&var1);
4565 return FAIL;
4566 }
4567 }
4568
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004569 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004571 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004572 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 if (len == -1)
4574 clear_tv(&var1);
4575 if (item == NULL)
4576 return FAIL;
4577
4578 copy_tv(&item->di_tv, &var1);
4579 clear_tv(rettv);
4580 *rettv = var1;
4581 }
4582 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
4584 }
4585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 return OK;
4587}
4588
4589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 * Get an option value.
4591 * "arg" points to the '&' or '+' before the option name.
4592 * "arg" is advanced to character after the option name.
4593 * Return OK or FAIL.
4594 */
4595 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004596get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004598 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 int evaluate;
4600{
4601 char_u *option_end;
4602 long numval;
4603 char_u *stringval;
4604 int opt_type;
4605 int c;
4606 int working = (**arg == '+'); /* has("+option") */
4607 int ret = OK;
4608 int opt_flags;
4609
4610 /*
4611 * Isolate the option name and find its value.
4612 */
4613 option_end = find_option_end(arg, &opt_flags);
4614 if (option_end == NULL)
4615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 EMSG2(_("E112: Option name missing: %s"), *arg);
4618 return FAIL;
4619 }
4620
4621 if (!evaluate)
4622 {
4623 *arg = option_end;
4624 return OK;
4625 }
4626
4627 c = *option_end;
4628 *option_end = NUL;
4629 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
4632 if (opt_type == -3) /* invalid name */
4633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004634 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 EMSG2(_("E113: Unknown option: %s"), *arg);
4636 ret = FAIL;
4637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 if (opt_type == -2) /* hidden string option */
4641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 rettv->v_type = VAR_STRING;
4643 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 else if (opt_type == -1) /* hidden number option */
4646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 rettv->v_type = VAR_NUMBER;
4648 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650 else if (opt_type == 1) /* number option */
4651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 rettv->v_type = VAR_NUMBER;
4653 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 else /* string option */
4656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 rettv->v_type = VAR_STRING;
4658 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 }
4661 else if (working && (opt_type == -2 || opt_type == -1))
4662 ret = FAIL;
4663
4664 *option_end = c; /* put back for error messages */
4665 *arg = option_end;
4666
4667 return ret;
4668}
4669
4670/*
4671 * Allocate a variable for a string constant.
4672 * Return OK or FAIL.
4673 */
4674 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 int evaluate;
4679{
4680 char_u *p;
4681 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 int extra = 0;
4683
4684 /*
4685 * Find the end of the string, skipping backslashed characters.
4686 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004687 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 if (*p == '\\' && p[1] != NUL)
4690 {
4691 ++p;
4692 /* A "\<x>" form occupies at least 4 characters, and produces up
4693 * to 6 characters: reserve space for 2 extra */
4694 if (*p == '<')
4695 extra += 2;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698
4699 if (*p != '"')
4700 {
4701 EMSG2(_("E114: Missing quote: %s"), *arg);
4702 return FAIL;
4703 }
4704
4705 /* If only parsing, set *arg and return here */
4706 if (!evaluate)
4707 {
4708 *arg = p + 1;
4709 return OK;
4710 }
4711
4712 /*
4713 * Copy the string into allocated memory, handling backslashed
4714 * characters.
4715 */
4716 name = alloc((unsigned)(p - *arg + extra));
4717 if (name == NULL)
4718 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 rettv->v_type = VAR_STRING;
4720 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
Bram Moolenaar8c711452005-01-14 21:53:12 +00004722 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
4724 if (*p == '\\')
4725 {
4726 switch (*++p)
4727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004728 case 'b': *name++ = BS; ++p; break;
4729 case 'e': *name++ = ESC; ++p; break;
4730 case 'f': *name++ = FF; ++p; break;
4731 case 'n': *name++ = NL; ++p; break;
4732 case 'r': *name++ = CAR; ++p; break;
4733 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 case 'X': /* hex: "\x1", "\x12" */
4736 case 'x':
4737 case 'u': /* Unicode: "\u0023" */
4738 case 'U':
4739 if (vim_isxdigit(p[1]))
4740 {
4741 int n, nr;
4742 int c = toupper(*p);
4743
4744 if (c == 'X')
4745 n = 2;
4746 else
4747 n = 4;
4748 nr = 0;
4749 while (--n >= 0 && vim_isxdigit(p[1]))
4750 {
4751 ++p;
4752 nr = (nr << 4) + hex2nr(*p);
4753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#ifdef FEAT_MBYTE
4756 /* For "\u" store the number according to
4757 * 'encoding'. */
4758 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 else
4761#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 break;
4765
4766 /* octal: "\1", "\12", "\123" */
4767 case '0':
4768 case '1':
4769 case '2':
4770 case '3':
4771 case '4':
4772 case '5':
4773 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774 case '7': *name = *p++ - '0';
4775 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 *name = (*name << 3) + *p++ - '0';
4778 if (*p >= '0' && *p <= '7')
4779 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 break;
4783
4784 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 if (extra != 0)
4787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 break;
4790 }
4791 /* FALLTHROUGH */
4792
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 break;
4795 }
4796 }
4797 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 *arg = p + 1;
4803
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return OK;
4805}
4806
4807/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 * Return OK or FAIL.
4810 */
4811 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int evaluate;
4816{
4817 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004818 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004819 int reduce = 0;
4820
4821 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004823 */
4824 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4825 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004826 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004827 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004829 break;
4830 ++reduce;
4831 ++p;
4832 }
4833 }
4834
Bram Moolenaar8c711452005-01-14 21:53:12 +00004835 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004837 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004838 return FAIL;
4839 }
4840
Bram Moolenaar8c711452005-01-14 21:53:12 +00004841 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004842 if (!evaluate)
4843 {
4844 *arg = p + 1;
4845 return OK;
4846 }
4847
4848 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004850 */
4851 str = alloc((unsigned)((p - *arg) - reduce));
4852 if (str == NULL)
4853 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004854 rettv->v_type = VAR_STRING;
4855 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004856
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004861 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004862 break;
4863 ++p;
4864 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004867 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004868 *arg = p + 1;
4869
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004870 return OK;
4871}
4872
4873/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874 * Allocate a variable for a List and fill it from "*arg".
4875 * Return OK or FAIL.
4876 */
4877 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004880 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 int evaluate;
4882{
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 list_T *l = NULL;
4884 typval_T tv;
4885 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886
4887 if (evaluate)
4888 {
4889 l = list_alloc();
4890 if (l == NULL)
4891 return FAIL;
4892 }
4893
4894 *arg = skipwhite(*arg + 1);
4895 while (**arg != ']' && **arg != NUL)
4896 {
4897 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4898 goto failret;
4899 if (evaluate)
4900 {
4901 item = listitem_alloc();
4902 if (item != NULL)
4903 {
4904 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004905 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 list_append(l, item);
4907 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004908 else
4909 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004910 }
4911
4912 if (**arg == ']')
4913 break;
4914 if (**arg != ',')
4915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 goto failret;
4918 }
4919 *arg = skipwhite(*arg + 1);
4920 }
4921
4922 if (**arg != ']')
4923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004925failret:
4926 if (evaluate)
4927 list_free(l);
4928 return FAIL;
4929 }
4930
4931 *arg = skipwhite(*arg + 1);
4932 if (evaluate)
4933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 rettv->v_type = VAR_LIST;
4935 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004936 ++l->lv_refcount;
4937 }
4938
4939 return OK;
4940}
4941
4942/*
4943 * Allocate an empty header for a list.
4944 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004945 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004946list_alloc()
4947{
Bram Moolenaar33570922005-01-25 22:26:29 +00004948 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949}
4950
4951/*
4952 * Unreference a list: decrement the reference count and free it when it
4953 * becomes zero.
4954 */
4955 static void
4956list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004957 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958{
Bram Moolenaard9fba312005-06-26 22:34:35 +00004959 int selfref;
4960
4961 if (l != NULL && l->lv_refcount != DEL_REFCOUNT)
4962 {
4963 if (--l->lv_refcount > 0)
4964 {
4965 /* Check if the dict contains references to itself. These need to
4966 * be subtracted from the reference count to find out if we can
4967 * delete the dict. */
4968 selfref = count_self_ref(l, VAR_LIST);
4969 }
4970 else
4971 selfref = 0;
4972 if (l->lv_refcount - selfref == 0)
4973 /* No references to the list now, free it. */
4974 list_free(l);
4975 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976}
4977
4978/*
4979 * Free a list, including all items it points to.
4980 * Ignores the reference count.
4981 */
4982 static void
4983list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004984 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985{
Bram Moolenaar33570922005-01-25 22:26:29 +00004986 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987
Bram Moolenaard9fba312005-06-26 22:34:35 +00004988 /* Avoid that recursive reference to the list frees us again. */
4989 l->lv_refcount = DEL_REFCOUNT;
4990
4991 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004992 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00004993 /* Remove the item before deleting it. */
4994 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 listitem_free(item);
4996 }
4997 vim_free(l);
4998}
4999
5000/*
5001 * Allocate a list item.
5002 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005003 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005004listitem_alloc()
5005{
Bram Moolenaar33570922005-01-25 22:26:29 +00005006 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007}
5008
5009/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005010 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 */
5012 static void
5013listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005017 vim_free(item);
5018}
5019
5020/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005021 * Remove a list item from a List and free it. Also clears the value.
5022 */
5023 static void
5024listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005025 list_T *l;
5026 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005027{
5028 list_remove(l, item, item);
5029 listitem_free(item);
5030}
5031
5032/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 * Get the number of items in a list.
5034 */
5035 static long
5036list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005037 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005038{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005039 if (l == NULL)
5040 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005041 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005042}
5043
5044/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005045 * Return TRUE when two lists have exactly the same values.
5046 */
5047 static int
5048list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005049 list_T *l1;
5050 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005051 int ic; /* ignore case for strings */
5052{
Bram Moolenaar33570922005-01-25 22:26:29 +00005053 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005055 if (list_len(l1) != list_len(l2))
5056 return FALSE;
5057
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005058 for (item1 = l1->lv_first, item2 = l2->lv_first;
5059 item1 != NULL && item2 != NULL;
5060 item1 = item1->li_next, item2 = item2->li_next)
5061 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5062 return FALSE;
5063 return item1 == NULL && item2 == NULL;
5064}
5065
5066/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005067 * Return TRUE when two dictionaries have exactly the same key/values.
5068 */
5069 static int
5070dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005071 dict_T *d1;
5072 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005073 int ic; /* ignore case for strings */
5074{
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 hashitem_T *hi;
5076 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005077 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005078
5079 if (dict_len(d1) != dict_len(d2))
5080 return FALSE;
5081
Bram Moolenaar33570922005-01-25 22:26:29 +00005082 todo = d1->dv_hashtab.ht_used;
5083 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005085 if (!HASHITEM_EMPTY(hi))
5086 {
5087 item2 = dict_find(d2, hi->hi_key, -1);
5088 if (item2 == NULL)
5089 return FALSE;
5090 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5091 return FALSE;
5092 --todo;
5093 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005094 }
5095 return TRUE;
5096}
5097
5098/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005099 * Return TRUE if "tv1" and "tv2" have the same value.
5100 * Compares the items just like "==" would compare them.
5101 */
5102 static int
5103tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005104 typval_T *tv1;
5105 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005106 int ic; /* ignore case */
5107{
5108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005109 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005110
5111 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
5112 {
5113 /* recursive! */
5114 if (tv1->v_type != tv2->v_type
5115 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
5116 return FALSE;
5117 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005118 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
5119 {
5120 /* recursive! */
5121 if (tv1->v_type != tv2->v_type
5122 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
5123 return FALSE;
5124 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005125 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
5126 {
5127 if (tv1->v_type != tv2->v_type
5128 || tv1->vval.v_string == NULL
5129 || tv2->vval.v_string == NULL
5130 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
5131 return FALSE;
5132 }
5133 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
5134 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005135 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
5136 * Don't consider "4x" to be equal to 4. */
5137 if ((tv1->v_type == VAR_STRING
5138 && !string_isa_number(tv1->vval.v_string))
5139 || (tv2->v_type == VAR_STRING
5140 && !string_isa_number(tv2->vval.v_string)))
5141 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005142 if (get_tv_number(tv1) != get_tv_number(tv2))
5143 return FALSE;
5144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005145 else
5146 {
5147 s1 = get_tv_string_buf(tv1, buf1);
5148 s2 = get_tv_string_buf(tv2, buf2);
5149 if ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) != 0)
5150 return FALSE;
5151 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005152 return TRUE;
5153}
5154
5155/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005156 * Return TRUE if "tv" is a number without other non-white characters.
5157 */
5158 static int
5159string_isa_number(s)
5160 char_u *s;
5161{
5162 int len;
5163
5164 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
5165 return len > 0 && *skipwhite(s + len) == NUL;
5166}
5167
5168/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 * Locate item with index "n" in list "l" and return it.
5170 * A negative index is counted from the end; -1 is the last item.
5171 * Returns NULL when "n" is out of range.
5172 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005173 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005175 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 long n;
5177{
Bram Moolenaar33570922005-01-25 22:26:29 +00005178 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 long idx;
5180
5181 if (l == NULL)
5182 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005183
5184 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005186 n = l->lv_len + n;
5187
5188 /* Check for index out of range. */
5189 if (n < 0 || n >= l->lv_len)
5190 return NULL;
5191
5192 /* When there is a cached index may start search from there. */
5193 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005195 if (n < l->lv_idx / 2)
5196 {
5197 /* closest to the start of the list */
5198 item = l->lv_first;
5199 idx = 0;
5200 }
5201 else if (n > (l->lv_idx + l->lv_len) / 2)
5202 {
5203 /* closest to the end of the list */
5204 item = l->lv_last;
5205 idx = l->lv_len - 1;
5206 }
5207 else
5208 {
5209 /* closest to the cached index */
5210 item = l->lv_idx_item;
5211 idx = l->lv_idx;
5212 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213 }
5214 else
5215 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005216 if (n < l->lv_len / 2)
5217 {
5218 /* closest to the start of the list */
5219 item = l->lv_first;
5220 idx = 0;
5221 }
5222 else
5223 {
5224 /* closest to the end of the list */
5225 item = l->lv_last;
5226 idx = l->lv_len - 1;
5227 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005229
5230 while (n > idx)
5231 {
5232 /* search forward */
5233 item = item->li_next;
5234 ++idx;
5235 }
5236 while (n < idx)
5237 {
5238 /* search backward */
5239 item = item->li_prev;
5240 --idx;
5241 }
5242
5243 /* cache the used index */
5244 l->lv_idx = idx;
5245 l->lv_idx_item = item;
5246
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 return item;
5248}
5249
5250/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005251 * Locate "item" list "l" and return its index.
5252 * Returns -1 when "item" is not in the list.
5253 */
5254 static long
5255list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005256 list_T *l;
5257 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005258{
5259 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005261
5262 if (l == NULL)
5263 return -1;
5264 idx = 0;
5265 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5266 ++idx;
5267 if (li == NULL)
5268 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005269 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005270}
5271
5272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 * Append item "item" to the end of list "l".
5274 */
5275 static void
5276list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005277 list_T *l;
5278 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279{
5280 if (l->lv_last == NULL)
5281 {
5282 /* empty list */
5283 l->lv_first = item;
5284 l->lv_last = item;
5285 item->li_prev = NULL;
5286 }
5287 else
5288 {
5289 l->lv_last->li_next = item;
5290 item->li_prev = l->lv_last;
5291 l->lv_last = item;
5292 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005293 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 item->li_next = NULL;
5295}
5296
5297/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005299 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 */
5301 static int
5302list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005303 list_T *l;
5304 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005306 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307
Bram Moolenaar05159a02005-02-26 23:04:13 +00005308 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005310 copy_tv(tv, &li->li_tv);
5311 list_append(l, li);
5312 return OK;
5313}
5314
5315/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005316 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005317 * Return FAIL when out of memory.
5318 */
5319 int
5320list_append_dict(list, dict)
5321 list_T *list;
5322 dict_T *dict;
5323{
5324 listitem_T *li = listitem_alloc();
5325
5326 if (li == NULL)
5327 return FAIL;
5328 li->li_tv.v_type = VAR_DICT;
5329 li->li_tv.v_lock = 0;
5330 li->li_tv.vval.v_dict = dict;
5331 list_append(list, li);
5332 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 return OK;
5334}
5335
5336/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005337 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005338 * If "item" is NULL append at the end.
5339 * Return FAIL when out of memory.
5340 */
5341 static int
5342list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005343 list_T *l;
5344 typval_T *tv;
5345 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005346{
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348
5349 if (ni == NULL)
5350 return FAIL;
5351 copy_tv(tv, &ni->li_tv);
5352 if (item == NULL)
5353 /* Append new item at end of list. */
5354 list_append(l, ni);
5355 else
5356 {
5357 /* Insert new item before existing item. */
5358 ni->li_prev = item->li_prev;
5359 ni->li_next = item;
5360 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005361 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005362 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005363 ++l->lv_idx;
5364 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005365 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005366 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005367 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005368 l->lv_idx_item = NULL;
5369 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005370 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005371 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005372 }
5373 return OK;
5374}
5375
5376/*
5377 * Extend "l1" with "l2".
5378 * If "bef" is NULL append at the end, otherwise insert before this item.
5379 * Returns FAIL when out of memory.
5380 */
5381 static int
5382list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005383 list_T *l1;
5384 list_T *l2;
5385 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386{
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005388
5389 for (item = l2->lv_first; item != NULL; item = item->li_next)
5390 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5391 return FAIL;
5392 return OK;
5393}
5394
5395/*
5396 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5397 * Return FAIL when out of memory.
5398 */
5399 static int
5400list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005401 list_T *l1;
5402 list_T *l2;
5403 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005404{
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005406
5407 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005408 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005409 if (l == NULL)
5410 return FAIL;
5411 tv->v_type = VAR_LIST;
5412 tv->vval.v_list = l;
5413
5414 /* append all items from the second list */
5415 return list_extend(l, l2, NULL);
5416}
5417
5418/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005419 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005421 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 * Returns NULL when out of memory.
5423 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005424 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005425list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005428 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429{
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 list_T *copy;
5431 listitem_T *item;
5432 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433
5434 if (orig == NULL)
5435 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436
5437 copy = list_alloc();
5438 if (copy != NULL)
5439 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005440 if (copyID != 0)
5441 {
5442 /* Do this before adding the items, because one of the items may
5443 * refer back to this list. */
5444 orig->lv_copyID = copyID;
5445 orig->lv_copylist = copy;
5446 }
5447 for (item = orig->lv_first; item != NULL && !got_int;
5448 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 {
5450 ni = listitem_alloc();
5451 if (ni == NULL)
5452 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005453 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005454 {
5455 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5456 {
5457 vim_free(ni);
5458 break;
5459 }
5460 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 list_append(copy, ni);
5464 }
5465 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005466 if (item != NULL)
5467 {
5468 list_unref(copy);
5469 copy = NULL;
5470 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 return copy;
5474}
5475
5476/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005477 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005478 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005480 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005481list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005482 list_T *l;
5483 listitem_T *item;
5484 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485{
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005488 /* notify watchers */
5489 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005491 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005492 list_fix_watch(l, ip);
5493 if (ip == item2)
5494 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005496
5497 if (item2->li_next == NULL)
5498 l->lv_last = item->li_prev;
5499 else
5500 item2->li_next->li_prev = item->li_prev;
5501 if (item->li_prev == NULL)
5502 l->lv_first = item2->li_next;
5503 else
5504 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005505 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506}
5507
5508/*
5509 * Return an allocated string with the string representation of a list.
5510 * May return NULL.
5511 */
5512 static char_u *
5513list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515{
5516 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517
5518 if (tv->vval.v_list == NULL)
5519 return NULL;
5520 ga_init2(&ga, (int)sizeof(char), 80);
5521 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005522 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5523 {
5524 vim_free(ga.ga_data);
5525 return NULL;
5526 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 ga_append(&ga, ']');
5528 ga_append(&ga, NUL);
5529 return (char_u *)ga.ga_data;
5530}
5531
5532/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005533 * Join list "l" into a string in "*gap", using separator "sep".
5534 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005535 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005536 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005537 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005538list_join(gap, l, sep, echo)
5539 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005540 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005541 char_u *sep;
5542 int echo;
5543{
5544 int first = TRUE;
5545 char_u *tofree;
5546 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005548 char_u *s;
5549
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005550 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005551 {
5552 if (first)
5553 first = FALSE;
5554 else
5555 ga_concat(gap, sep);
5556
5557 if (echo)
5558 s = echo_string(&item->li_tv, &tofree, numbuf);
5559 else
5560 s = tv2string(&item->li_tv, &tofree, numbuf);
5561 if (s != NULL)
5562 ga_concat(gap, s);
5563 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005564 if (s == NULL)
5565 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005566 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005567 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005568}
5569
5570/*
Bram Moolenaard9fba312005-06-26 22:34:35 +00005571 * Count the number of references for list/dict "p" inside itself.
5572 * This is used to find out if there are no more references elsewhere.
5573 * The tricky bit is that we must not count references in lists/dicts that are
5574 * used elsewhere, but we can only know by counting their references...
5575 * This is a bit slow, but required to avoid leaking memory.
5576 */
5577 static int
5578count_self_ref(p, type)
5579 void *p;
5580 int type;
5581{
5582 garray_T ga;
5583 typval_T *tv;
5584 int selfref;
5585 int i;
5586 int n;
5587
5588 ga_init2(&ga, sizeof(typval_T *), 10);
5589 if (type == VAR_DICT)
5590 selfref = count_ref_in_dict(p, p, ++current_copyID, &ga);
5591 else
5592 selfref = count_ref_in_list(p, p, ++current_copyID, &ga);
5593 for (i = 0; i < ga.ga_len; ++i)
5594 {
5595 tv = ((typval_T **)ga.ga_data)[i];
5596 if (tv->v_type == VAR_DICT)
5597 {
5598 n = count_ref_in_dict(tv->vval.v_dict, tv->vval.v_dict,
5599 ++current_copyID, NULL);
5600 if (n < tv->vval.v_dict->dv_refcount)
5601 {
5602 selfref = 0;
5603 break;
5604 }
5605 }
5606 else
5607 {
5608 n = count_ref_in_list(tv->vval.v_list, tv->vval.v_list,
5609 ++current_copyID, NULL);
5610 if (n < tv->vval.v_list->lv_refcount)
5611 {
5612 selfref = 0;
5613 break;
5614 }
5615 }
5616 }
5617
5618 ga_clear(&ga);
5619 return selfref;
5620}
5621
5622/*
5623 * Count number of references to "rp" in dictionary "d" and its members.
5624 * We use "copyID" to avoid recursing into the same list/dict twice.
5625 */
5626 static int
5627count_ref_in_dict(d, rp, copyID, gap)
5628 dict_T *d;
5629 void *rp;
5630 int copyID;
5631 garray_T *gap;
5632{
5633 int todo;
5634 hashitem_T *hi;
5635 int n = 0;
5636
5637 todo = d->dv_hashtab.ht_used;
5638 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5639 if (!HASHITEM_EMPTY(hi))
5640 {
5641 --todo;
5642 n += count_ref_item(&HI2DI(hi)->di_tv, rp, copyID, gap);
5643 }
5644 return n;
5645}
5646
5647/*
5648 * Count number of references to "rp" in list "l" and its members.
5649 * We use "copyID" to avoid recursing into the same list/dict twice.
5650 */
5651 static int
5652count_ref_in_list(l, rp, copyID, gap)
5653 list_T *l;
5654 void *rp;
5655 int copyID;
5656 garray_T *gap;
5657{
5658 listitem_T *li;
5659 int n = 0;
5660
5661 for (li = l->lv_first; li != NULL; li = li->li_next)
5662 n += count_ref_item(&li->li_tv, rp, copyID, gap);
5663 return n;
5664}
5665
5666/*
5667 * Count number of references to "rp" in item "tv" and any members.
5668 * We use "copyID" to avoid recursing into the same list/dict twice.
5669 * When "gap" is not NULL store items that require checking for only
5670 * references inside the structure.
5671 */
5672 static int
5673count_ref_item(tv, rp, copyID, gap)
5674 typval_T *tv;
5675 void *rp;
5676 int copyID;
5677 garray_T *gap;
5678{
5679 dict_T *dd;
5680 list_T *ll;
5681 int n;
5682
5683 switch (tv->v_type)
5684 {
5685 case VAR_DICT:
5686 dd = tv->vval.v_dict;
5687 if (dd == rp)
5688 return 1; /* match, count it */
5689 if (dd->dv_copyID == copyID)
5690 return 0; /* already inspected this dict */
5691 dd->dv_copyID = copyID;
5692 n = count_ref_in_dict(dd, rp, copyID, gap);
5693 if (n > 0 && gap != NULL && dd->dv_refcount > 1)
5694 {
5695 /* We must later check that the references to this dict are
5696 * all in the structure we are freeing. */
5697 if (ga_grow(gap, 1) == FAIL)
5698 return 0;
5699 ((typval_T **)gap->ga_data)[gap->ga_len++] = tv;
5700 }
5701 return n;
5702
5703 case VAR_LIST:
5704 ll = tv->vval.v_list;
5705 if (ll == rp)
5706 return 1; /* match, count it */
5707 if (ll->lv_copyID == copyID)
5708 return 0; /* already inspected this list */
5709 ll->lv_copyID = copyID;
5710 n = count_ref_in_list(ll, rp, copyID, gap);
5711 if (n > 0 && gap != NULL && ll->lv_refcount > 1)
5712 {
5713 /* We must later check that the references to this list are
5714 * all in the structure we are freeing. */
5715 if (ga_grow(gap, 1) == FAIL)
5716 return 0;
5717 ((typval_T **)gap->ga_data)[gap->ga_len++] = tv;
5718 }
5719 return n;
5720 }
5721 return 0;
5722}
5723
5724/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 * Allocate an empty header for a dictionary.
5726 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005727 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728dict_alloc()
5729{
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005731
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005733 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005734 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005735 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005736 d->dv_lock = 0;
5737 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005738 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005739 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005740 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741}
5742
5743/*
5744 * Unreference a Dictionary: decrement the reference count and free it when it
5745 * becomes zero.
5746 */
5747 static void
5748dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750{
Bram Moolenaard9fba312005-06-26 22:34:35 +00005751 int selfref;
5752
5753 if (d != NULL && d->dv_refcount != DEL_REFCOUNT)
5754 {
5755 if (--d->dv_refcount > 0)
5756 {
5757 /* Check if the dict contains references to itself. These need to
5758 * be subtracted from the reference count to find out if we can
5759 * delete the dict. */
5760 selfref = count_self_ref(d, VAR_DICT);
5761 }
5762 else
5763 selfref = 0;
5764 if (d->dv_refcount - selfref == 0)
5765 /* No references to the dict now, free it. */
5766 dict_free(d);
5767 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768}
5769
5770/*
5771 * Free a Dictionary, including all items it contains.
5772 * Ignores the reference count.
5773 */
5774 static void
5775dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005778 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005780 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781
Bram Moolenaard9fba312005-06-26 22:34:35 +00005782 /* Avoid that recursive reference to the dict frees us again. */
5783 d->dv_refcount = DEL_REFCOUNT;
5784
5785 /* Lock the hashtab, we don't want it to resize while looping through it.
5786 * */
5787 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 todo = d->dv_hashtab.ht_used;
5789 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005791 if (!HASHITEM_EMPTY(hi))
5792 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005793 /* Remove the item before deleting it, just in case there is
5794 * something recursive causing trouble. */
5795 di = HI2DI(hi);
5796 hash_remove(&d->dv_hashtab, hi);
5797 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005798 --todo;
5799 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 vim_free(d);
5803}
5804
5805/*
5806 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005807 * The "key" is copied to the new item.
5808 * Note that the value of the item "di_tv" still needs to be initialized!
5809 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005812dictitem_alloc(key)
5813 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814{
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005816
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005818 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005819 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005820 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005821 di->di_flags = 0;
5822 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005823 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824}
5825
5826/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005827 * Make a copy of a Dictionary item.
5828 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005829 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005830dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005832{
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005834
Bram Moolenaar33570922005-01-25 22:26:29 +00005835 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836 if (di != NULL)
5837 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005838 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005839 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005840 copy_tv(&org->di_tv, &di->di_tv);
5841 }
5842 return di;
5843}
5844
5845/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005846 * Remove item "item" from Dictionary "dict" and free it.
5847 */
5848 static void
5849dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 dict_T *dict;
5851 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005854
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005856 if (HASHITEM_EMPTY(hi))
5857 EMSG2(_(e_intern2), "dictitem_remove()");
5858 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005860 dictitem_free(item);
5861}
5862
5863/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 * Free a dict item. Also clears the value.
5865 */
5866 static void
5867dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 clear_tv(&item->di_tv);
5871 vim_free(item);
5872}
5873
5874/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005875 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5876 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005877 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005878 * Returns NULL when out of memory.
5879 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005881dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005882 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005883 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005885{
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 dict_T *copy;
5887 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005888 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890
5891 if (orig == NULL)
5892 return NULL;
5893
5894 copy = dict_alloc();
5895 if (copy != NULL)
5896 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005897 if (copyID != 0)
5898 {
5899 orig->dv_copyID = copyID;
5900 orig->dv_copydict = copy;
5901 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005903 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005904 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005905 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005907 --todo;
5908
5909 di = dictitem_alloc(hi->hi_key);
5910 if (di == NULL)
5911 break;
5912 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005913 {
5914 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5915 copyID) == FAIL)
5916 {
5917 vim_free(di);
5918 break;
5919 }
5920 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005921 else
5922 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5923 if (dict_add(copy, di) == FAIL)
5924 {
5925 dictitem_free(di);
5926 break;
5927 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005930
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005932 if (todo > 0)
5933 {
5934 dict_unref(copy);
5935 copy = NULL;
5936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005937 }
5938
5939 return copy;
5940}
5941
5942/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005944 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005946 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 dict_T *d;
5949 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952}
5953
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005955 * Add a number or string entry to dictionary "d".
5956 * When "str" is NULL use number "nr", otherwise use "str".
5957 * Returns FAIL when out of memory and when key already exists.
5958 */
5959 int
5960dict_add_nr_str(d, key, nr, str)
5961 dict_T *d;
5962 char *key;
5963 long nr;
5964 char_u *str;
5965{
5966 dictitem_T *item;
5967
5968 item = dictitem_alloc((char_u *)key);
5969 if (item == NULL)
5970 return FAIL;
5971 item->di_tv.v_lock = 0;
5972 if (str == NULL)
5973 {
5974 item->di_tv.v_type = VAR_NUMBER;
5975 item->di_tv.vval.v_number = nr;
5976 }
5977 else
5978 {
5979 item->di_tv.v_type = VAR_STRING;
5980 item->di_tv.vval.v_string = vim_strsave(str);
5981 }
5982 if (dict_add(d, item) == FAIL)
5983 {
5984 dictitem_free(item);
5985 return FAIL;
5986 }
5987 return OK;
5988}
5989
5990/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991 * Get the number of items in a Dictionary.
5992 */
5993 static long
5994dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 if (d == NULL)
5998 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000}
6001
6002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006003 * Find item "key[len]" in Dictionary "d".
6004 * If "len" is negative use strlen(key).
6005 * Returns NULL when not found.
6006 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006008dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006010 char_u *key;
6011 int len;
6012{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006013#define AKEYLEN 200
6014 char_u buf[AKEYLEN];
6015 char_u *akey;
6016 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006018
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006019 if (len < 0)
6020 akey = key;
6021 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006023 tofree = akey = vim_strnsave(key, len);
6024 if (akey == NULL)
6025 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006026 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006027 else
6028 {
6029 /* Avoid a malloc/free by using buf[]. */
6030 STRNCPY(buf, key, len);
6031 buf[len] = NUL;
6032 akey = buf;
6033 }
6034
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036 vim_free(tofree);
6037 if (HASHITEM_EMPTY(hi))
6038 return NULL;
6039 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006040}
6041
6042/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006043 * Get a string item from a dictionary in allocated memory.
6044 * Returns NULL if the entry doesn't exist or out of memory.
6045 */
6046 char_u *
6047get_dict_string(d, key)
6048 dict_T *d;
6049 char_u *key;
6050{
6051 dictitem_T *di;
6052
6053 di = dict_find(d, key, -1);
6054 if (di == NULL)
6055 return NULL;
6056 return vim_strsave(get_tv_string(&di->di_tv));
6057}
6058
6059/*
6060 * Get a number item from a dictionary.
6061 * Returns 0 if the entry doesn't exist or out of memory.
6062 */
6063 long
6064get_dict_number(d, key)
6065 dict_T *d;
6066 char_u *key;
6067{
6068 dictitem_T *di;
6069
6070 di = dict_find(d, key, -1);
6071 if (di == NULL)
6072 return 0;
6073 return get_tv_number(&di->di_tv);
6074}
6075
6076/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077 * Return an allocated string with the string representation of a Dictionary.
6078 * May return NULL.
6079 */
6080 static char_u *
6081dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006083{
6084 garray_T ga;
6085 int first = TRUE;
6086 char_u *tofree;
6087 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006089 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006091 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006092
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006093 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006094 return NULL;
6095 ga_init2(&ga, (int)sizeof(char), 80);
6096 ga_append(&ga, '{');
6097
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006099 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 --todo;
6104
6105 if (first)
6106 first = FALSE;
6107 else
6108 ga_concat(&ga, (char_u *)", ");
6109
6110 tofree = string_quote(hi->hi_key, FALSE);
6111 if (tofree != NULL)
6112 {
6113 ga_concat(&ga, tofree);
6114 vim_free(tofree);
6115 }
6116 ga_concat(&ga, (char_u *)": ");
6117 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6118 if (s != NULL)
6119 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006120 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006121 if (s == NULL)
6122 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006123 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006124 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006125 if (todo > 0)
6126 {
6127 vim_free(ga.ga_data);
6128 return NULL;
6129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006130
6131 ga_append(&ga, '}');
6132 ga_append(&ga, NUL);
6133 return (char_u *)ga.ga_data;
6134}
6135
6136/*
6137 * Allocate a variable for a Dictionary and fill it from "*arg".
6138 * Return OK or FAIL. Returns NOTDONE for {expr}.
6139 */
6140 static int
6141get_dict_tv(arg, rettv, evaluate)
6142 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006144 int evaluate;
6145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 dict_T *d = NULL;
6147 typval_T tvkey;
6148 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006149 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006151 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006153
6154 /*
6155 * First check if it's not a curly-braces thing: {expr}.
6156 * Must do this without evaluating, otherwise a function may be called
6157 * twice. Unfortunately this means we need to call eval1() twice for the
6158 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006160 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 if (*start != '}')
6162 {
6163 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6164 return FAIL;
6165 if (*start == '}')
6166 return NOTDONE;
6167 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006168
6169 if (evaluate)
6170 {
6171 d = dict_alloc();
6172 if (d == NULL)
6173 return FAIL;
6174 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 tvkey.v_type = VAR_UNKNOWN;
6176 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006177
6178 *arg = skipwhite(*arg + 1);
6179 while (**arg != '}' && **arg != NUL)
6180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006181 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006182 goto failret;
6183 if (**arg != ':')
6184 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006185 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006187 goto failret;
6188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006189 key = get_tv_string_buf_chk(&tvkey, buf);
6190 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006192 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6193 if (key != NULL)
6194 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006195 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006196 goto failret;
6197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006198
6199 *arg = skipwhite(*arg + 1);
6200 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006203 goto failret;
6204 }
6205 if (evaluate)
6206 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208 if (item != NULL)
6209 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006210 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006212 clear_tv(&tv);
6213 goto failret;
6214 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 item = dictitem_alloc(key);
6216 clear_tv(&tvkey);
6217 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006218 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006219 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006220 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 if (dict_add(d, item) == FAIL)
6222 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223 }
6224 }
6225
6226 if (**arg == '}')
6227 break;
6228 if (**arg != ',')
6229 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006230 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006231 goto failret;
6232 }
6233 *arg = skipwhite(*arg + 1);
6234 }
6235
6236 if (**arg != '}')
6237 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006238 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006239failret:
6240 if (evaluate)
6241 dict_free(d);
6242 return FAIL;
6243 }
6244
6245 *arg = skipwhite(*arg + 1);
6246 if (evaluate)
6247 {
6248 rettv->v_type = VAR_DICT;
6249 rettv->vval.v_dict = d;
6250 ++d->dv_refcount;
6251 }
6252
6253 return OK;
6254}
6255
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 * Return a string with the string representation of a variable.
6258 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006260 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 * May return NULL;
6262 */
6263 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006264echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006267 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 static int recurse = 0;
6270 char_u *r = NULL;
6271
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006274 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 *tofree = NULL;
6276 return NULL;
6277 }
6278 ++recurse;
6279
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280 switch (tv->v_type)
6281 {
6282 case VAR_FUNC:
6283 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006284 r = tv->vval.v_string;
6285 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 case VAR_LIST:
6287 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 r = *tofree;
6289 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 case VAR_DICT:
6291 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292 r = *tofree;
6293 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 case VAR_STRING:
6295 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296 *tofree = NULL;
6297 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006299 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006300 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006301 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303
6304 --recurse;
6305 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006306}
6307
6308/*
6309 * Return a string with the string representation of a variable.
6310 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6311 * "numbuf" is used for a number.
6312 * Puts quotes around strings, so that they can be parsed back by eval().
6313 * May return NULL;
6314 */
6315 static char_u *
6316tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006318 char_u **tofree;
6319 char_u *numbuf;
6320{
6321 switch (tv->v_type)
6322 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006323 case VAR_FUNC:
6324 *tofree = string_quote(tv->vval.v_string, TRUE);
6325 return *tofree;
6326 case VAR_STRING:
6327 *tofree = string_quote(tv->vval.v_string, FALSE);
6328 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006330 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006331 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006333 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006334 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337}
6338
6339/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 * Return string "str" in ' quotes, doubling ' characters.
6341 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006343 */
6344 static char_u *
6345string_quote(str, function)
6346 char_u *str;
6347 int function;
6348{
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006350 char_u *p, *r, *s;
6351
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 len = (function ? 13 : 3);
6353 if (str != NULL)
6354 {
6355 len += STRLEN(str);
6356 for (p = str; *p != NUL; mb_ptr_adv(p))
6357 if (*p == '\'')
6358 ++len;
6359 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006360 s = r = alloc(len);
6361 if (r != NULL)
6362 {
6363 if (function)
6364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006366 r += 10;
6367 }
6368 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006369 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 if (str != NULL)
6371 for (p = str; *p != NUL; )
6372 {
6373 if (*p == '\'')
6374 *r++ = '\'';
6375 MB_COPY_CHAR(p, r);
6376 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006378 if (function)
6379 *r++ = ')';
6380 *r++ = NUL;
6381 }
6382 return s;
6383}
6384
6385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Get the value of an environment variable.
6387 * "arg" is pointing to the '$'. It is advanced to after the name.
6388 * If the environment variable was not set, silently assume it is empty.
6389 * Always return OK.
6390 */
6391 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006392get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 int evaluate;
6396{
6397 char_u *string = NULL;
6398 int len;
6399 int cc;
6400 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006401 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402
6403 ++*arg;
6404 name = *arg;
6405 len = get_env_len(arg);
6406 if (evaluate)
6407 {
6408 if (len != 0)
6409 {
6410 cc = name[len];
6411 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006412 /* first try vim_getenv(), fast for normal environment vars */
6413 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 {
6416 if (!mustfree)
6417 string = vim_strsave(string);
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 else
6420 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 if (mustfree)
6422 vim_free(string);
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 /* next try expanding things like $VIM and ${HOME} */
6425 string = expand_env_save(name - 1);
6426 if (string != NULL && *string == '$')
6427 {
6428 vim_free(string);
6429 string = NULL;
6430 }
6431 }
6432 name[len] = cc;
6433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006434 rettv->v_type = VAR_STRING;
6435 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437
6438 return OK;
6439}
6440
6441/*
6442 * Array with names and number of arguments of all internal functions
6443 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6444 */
6445static struct fst
6446{
6447 char *f_name; /* function name */
6448 char f_min_argc; /* minimal number of arguments */
6449 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452} functions[] =
6453{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006454 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {"append", 2, 2, f_append},
6456 {"argc", 0, 0, f_argc},
6457 {"argidx", 0, 0, f_argidx},
6458 {"argv", 1, 1, f_argv},
6459 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006460 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {"bufexists", 1, 1, f_bufexists},
6462 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6463 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6464 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6465 {"buflisted", 1, 1, f_buflisted},
6466 {"bufloaded", 1, 1, f_bufloaded},
6467 {"bufname", 1, 1, f_bufname},
6468 {"bufnr", 1, 1, f_bufnr},
6469 {"bufwinnr", 1, 1, f_bufwinnr},
6470 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006471 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 {"char2nr", 1, 1, f_char2nr},
6474 {"cindent", 1, 1, f_cindent},
6475 {"col", 1, 1, f_col},
6476 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006478 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 {"cscope_connection",0,3, f_cscope_connection},
6480 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {"delete", 1, 1, f_delete},
6483 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006484 {"diff_filler", 1, 1, f_diff_filler},
6485 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006486 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 {"eventhandler", 0, 0, f_eventhandler},
6490 {"executable", 1, 1, f_executable},
6491 {"exists", 1, 1, f_exists},
6492 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6495 {"filereadable", 1, 1, f_filereadable},
6496 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006497 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006498 {"finddir", 1, 3, f_finddir},
6499 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {"fnamemodify", 2, 2, f_fnamemodify},
6501 {"foldclosed", 1, 1, f_foldclosed},
6502 {"foldclosedend", 1, 1, f_foldclosedend},
6503 {"foldlevel", 1, 1, f_foldlevel},
6504 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006505 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006508 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {"getbufvar", 2, 2, f_getbufvar},
6510 {"getchar", 0, 1, f_getchar},
6511 {"getcharmod", 0, 0, f_getcharmod},
6512 {"getcmdline", 0, 0, f_getcmdline},
6513 {"getcmdpos", 0, 0, f_getcmdpos},
6514 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006515 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006516 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {"getfsize", 1, 1, f_getfsize},
6518 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006519 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006520 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006521 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006522 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {"getregtype", 0, 1, f_getregtype},
6524 {"getwinposx", 0, 0, f_getwinposx},
6525 {"getwinposy", 0, 0, f_getwinposy},
6526 {"getwinvar", 2, 2, f_getwinvar},
6527 {"glob", 1, 1, f_glob},
6528 {"globpath", 2, 2, f_globpath},
6529 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {"hasmapto", 1, 2, f_hasmapto},
6532 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6533 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6534 {"histadd", 2, 2, f_histadd},
6535 {"histdel", 1, 2, f_histdel},
6536 {"histget", 1, 2, f_histget},
6537 {"histnr", 1, 1, f_histnr},
6538 {"hlID", 1, 1, f_hlID},
6539 {"hlexists", 1, 1, f_hlexists},
6540 {"hostname", 0, 0, f_hostname},
6541 {"iconv", 3, 3, f_iconv},
6542 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {"input", 1, 2, f_input},
6545 {"inputdialog", 1, 3, f_inputdialog},
6546 {"inputrestore", 0, 0, f_inputrestore},
6547 {"inputsave", 0, 0, f_inputsave},
6548 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006551 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006552 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {"libcall", 3, 3, f_libcall},
6558 {"libcallnr", 3, 3, f_libcallnr},
6559 {"line", 1, 1, f_line},
6560 {"line2byte", 1, 1, f_line2byte},
6561 {"lispindent", 1, 1, f_lispindent},
6562 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006563 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {"maparg", 1, 2, f_maparg},
6565 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006566 {"match", 2, 4, f_match},
6567 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006568 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006569 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006570 {"max", 1, 1, f_max},
6571 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006572#ifdef vim_mkdir
6573 {"mkdir", 1, 3, f_mkdir},
6574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {"mode", 0, 0, f_mode},
6576 {"nextnonblank", 1, 1, f_nextnonblank},
6577 {"nr2char", 1, 1, f_nr2char},
6578 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006579 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006580 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {"remote_expr", 2, 3, f_remote_expr},
6582 {"remote_foreground", 1, 1, f_remote_foreground},
6583 {"remote_peek", 1, 2, f_remote_peek},
6584 {"remote_read", 1, 1, f_remote_read},
6585 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006588 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006590 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {"search", 1, 2, f_search},
6592 {"searchpair", 3, 5, f_searchpair},
6593 {"server2client", 2, 2, f_server2client},
6594 {"serverlist", 0, 0, f_serverlist},
6595 {"setbufvar", 3, 3, f_setbufvar},
6596 {"setcmdpos", 1, 1, f_setcmdpos},
6597 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006598 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 {"setreg", 2, 3, f_setreg},
6600 {"setwinvar", 3, 3, f_setwinvar},
6601 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006602 {"sort", 1, 2, f_sort},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006603 {"spellbadword", 0, 0, f_spellbadword},
6604 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006605 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606#ifdef HAVE_STRFTIME
6607 {"strftime", 1, 2, f_strftime},
6608#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006609 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 {"strlen", 1, 1, f_strlen},
6612 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006613 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 {"strtrans", 1, 1, f_strtrans},
6615 {"submatch", 1, 1, f_submatch},
6616 {"substitute", 4, 4, f_substitute},
6617 {"synID", 3, 3, f_synID},
6618 {"synIDattr", 2, 3, f_synIDattr},
6619 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006620 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006621 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 {"tempname", 0, 0, f_tempname},
6623 {"tolower", 1, 1, f_tolower},
6624 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006625 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {"virtcol", 1, 1, f_virtcol},
6629 {"visualmode", 0, 1, f_visualmode},
6630 {"winbufnr", 1, 1, f_winbufnr},
6631 {"wincol", 0, 0, f_wincol},
6632 {"winheight", 1, 1, f_winheight},
6633 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006634 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {"winrestcmd", 0, 0, f_winrestcmd},
6636 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006637 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638};
6639
6640#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6641
6642/*
6643 * Function given to ExpandGeneric() to obtain the list of internal
6644 * or user defined function names.
6645 */
6646 char_u *
6647get_function_name(xp, idx)
6648 expand_T *xp;
6649 int idx;
6650{
6651 static int intidx = -1;
6652 char_u *name;
6653
6654 if (idx == 0)
6655 intidx = -1;
6656 if (intidx < 0)
6657 {
6658 name = get_user_func_name(xp, idx);
6659 if (name != NULL)
6660 return name;
6661 }
6662 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6663 {
6664 STRCPY(IObuff, functions[intidx].f_name);
6665 STRCAT(IObuff, "(");
6666 if (functions[intidx].f_max_argc == 0)
6667 STRCAT(IObuff, ")");
6668 return IObuff;
6669 }
6670
6671 return NULL;
6672}
6673
6674/*
6675 * Function given to ExpandGeneric() to obtain the list of internal or
6676 * user defined variable or function names.
6677 */
6678/*ARGSUSED*/
6679 char_u *
6680get_expr_name(xp, idx)
6681 expand_T *xp;
6682 int idx;
6683{
6684 static int intidx = -1;
6685 char_u *name;
6686
6687 if (idx == 0)
6688 intidx = -1;
6689 if (intidx < 0)
6690 {
6691 name = get_function_name(xp, idx);
6692 if (name != NULL)
6693 return name;
6694 }
6695 return get_user_var_name(xp, ++intidx);
6696}
6697
6698#endif /* FEAT_CMDL_COMPL */
6699
6700/*
6701 * Find internal function in table above.
6702 * Return index, or -1 if not found
6703 */
6704 static int
6705find_internal_func(name)
6706 char_u *name; /* name of the function */
6707{
6708 int first = 0;
6709 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6710 int cmp;
6711 int x;
6712
6713 /*
6714 * Find the function name in the table. Binary search.
6715 */
6716 while (first <= last)
6717 {
6718 x = first + ((unsigned)(last - first) >> 1);
6719 cmp = STRCMP(name, functions[x].f_name);
6720 if (cmp < 0)
6721 last = x - 1;
6722 else if (cmp > 0)
6723 first = x + 1;
6724 else
6725 return x;
6726 }
6727 return -1;
6728}
6729
6730/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6732 * name it contains, otherwise return "name".
6733 */
6734 static char_u *
6735deref_func_name(name, lenp)
6736 char_u *name;
6737 int *lenp;
6738{
Bram Moolenaar33570922005-01-25 22:26:29 +00006739 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 int cc;
6741
6742 cc = name[*lenp];
6743 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006744 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006745 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006747 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006748 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749 {
6750 *lenp = 0;
6751 return (char_u *)""; /* just in case */
6752 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006753 *lenp = STRLEN(v->di_tv.vval.v_string);
6754 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006755 }
6756
6757 return name;
6758}
6759
6760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 * Allocate a variable for the result of a function.
6762 * Return OK or FAIL.
6763 */
6764 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006765get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6766 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 char_u *name; /* name of the function */
6768 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 char_u **arg; /* argument, pointing to the '(' */
6771 linenr_T firstline; /* first line of range */
6772 linenr_T lastline; /* last line of range */
6773 int *doesrange; /* return: function handled range */
6774 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006775 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 char_u *argp;
6778 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 int argcount = 0; /* number of arguments found */
6781
6782 /*
6783 * Get the arguments.
6784 */
6785 argp = *arg;
6786 while (argcount < MAX_FUNC_ARGS)
6787 {
6788 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6789 if (*argp == ')' || *argp == ',' || *argp == NUL)
6790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6792 {
6793 ret = FAIL;
6794 break;
6795 }
6796 ++argcount;
6797 if (*argp != ',')
6798 break;
6799 }
6800 if (*argp == ')')
6801 ++argp;
6802 else
6803 ret = FAIL;
6804
6805 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006807 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 {
6810 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006811 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006812 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006813 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
6816 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 *arg = skipwhite(argp);
6820 return ret;
6821}
6822
6823
6824/*
6825 * Call a function with its resolved parameters
6826 * Return OK or FAIL.
6827 */
6828 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006830 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 char_u *name; /* name of the function */
6832 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006833 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006835 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 linenr_T firstline; /* first line of range */
6837 linenr_T lastline; /* last line of range */
6838 int *doesrange; /* return: function handled range */
6839 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006840 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843#define ERROR_UNKNOWN 0
6844#define ERROR_TOOMANY 1
6845#define ERROR_TOOFEW 2
6846#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847#define ERROR_DICT 4
6848#define ERROR_NONE 5
6849#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 int error = ERROR_NONE;
6851 int i;
6852 int llen;
6853 ufunc_T *fp;
6854 int cc;
6855#define FLEN_FIXED 40
6856 char_u fname_buf[FLEN_FIXED + 1];
6857 char_u *fname;
6858
6859 /*
6860 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6861 * Change <SNR>123_name() to K_SNR 123_name().
6862 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6863 */
6864 cc = name[len];
6865 name[len] = NUL;
6866 llen = eval_fname_script(name);
6867 if (llen > 0)
6868 {
6869 fname_buf[0] = K_SPECIAL;
6870 fname_buf[1] = KS_EXTRA;
6871 fname_buf[2] = (int)KE_SNR;
6872 i = 3;
6873 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6874 {
6875 if (current_SID <= 0)
6876 error = ERROR_SCRIPT;
6877 else
6878 {
6879 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6880 i = (int)STRLEN(fname_buf);
6881 }
6882 }
6883 if (i + STRLEN(name + llen) < FLEN_FIXED)
6884 {
6885 STRCPY(fname_buf + i, name + llen);
6886 fname = fname_buf;
6887 }
6888 else
6889 {
6890 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6891 if (fname == NULL)
6892 error = ERROR_OTHER;
6893 else
6894 {
6895 mch_memmove(fname, fname_buf, (size_t)i);
6896 STRCPY(fname + i, name + llen);
6897 }
6898 }
6899 }
6900 else
6901 fname = name;
6902
6903 *doesrange = FALSE;
6904
6905
6906 /* execute the function if no errors detected and executing */
6907 if (evaluate && error == ERROR_NONE)
6908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 error = ERROR_UNKNOWN;
6911
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006912 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
6914 /*
6915 * User defined function.
6916 */
6917 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006920 /* Trigger FuncUndefined event, may load the function. */
6921 if (fp == NULL
6922 && apply_autocmds(EVENT_FUNCUNDEFINED,
6923 fname, fname, TRUE, NULL)
6924 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006926 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 fp = find_func(fname);
6928 }
6929#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006930 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006931 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006932 {
6933 /* loaded a package, search for the function again */
6934 fp = find_func(fname);
6935 }
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (fp != NULL)
6938 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006939 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006941 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006943 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006945 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006946 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 else
6948 {
6949 /*
6950 * Call the user function.
6951 * Save and restore search patterns, script variables and
6952 * redo buffer.
6953 */
6954 save_search_patterns();
6955 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006956 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006957 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006958 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006959 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6960 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6961 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006962 /* Function was unreferenced while being used, free it
6963 * now. */
6964 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 restoreRedobuff();
6966 restore_search_patterns();
6967 error = ERROR_NONE;
6968 }
6969 }
6970 }
6971 else
6972 {
6973 /*
6974 * Find the function name in the table, call its implementation.
6975 */
6976 i = find_internal_func(fname);
6977 if (i >= 0)
6978 {
6979 if (argcount < functions[i].f_min_argc)
6980 error = ERROR_TOOFEW;
6981 else if (argcount > functions[i].f_max_argc)
6982 error = ERROR_TOOMANY;
6983 else
6984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006985 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006986 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 error = ERROR_NONE;
6988 }
6989 }
6990 }
6991 /*
6992 * The function call (or "FuncUndefined" autocommand sequence) might
6993 * have been aborted by an error, an interrupt, or an explicitly thrown
6994 * exception that has not been caught so far. This situation can be
6995 * tested for by calling aborting(). For an error in an internal
6996 * function or for the "E132" error in call_user_func(), however, the
6997 * throw point at which the "force_abort" flag (temporarily reset by
6998 * emsg()) is normally updated has not been reached yet. We need to
6999 * update that flag first to make aborting() reliable.
7000 */
7001 update_force_abort();
7002 }
7003 if (error == ERROR_NONE)
7004 ret = OK;
7005
7006 /*
7007 * Report an error unless the argument evaluation or function call has been
7008 * cancelled due to an aborting error, an interrupt, or an exception.
7009 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 if (!aborting())
7011 {
7012 switch (error)
7013 {
7014 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007015 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016 break;
7017 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007018 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019 break;
7020 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007021 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022 name);
7023 break;
7024 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007025 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026 name);
7027 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030 name);
7031 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 }
7033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 name[len] = cc;
7036 if (fname != name && fname != fname_buf)
7037 vim_free(fname);
7038
7039 return ret;
7040}
7041
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007042/*
7043 * Give an error message with a function name. Handle <SNR> things.
7044 */
7045 static void
7046emsg_funcname(msg, name)
7047 char *msg;
7048 char_u *name;
7049{
7050 char_u *p;
7051
7052 if (*name == K_SPECIAL)
7053 p = concat_str((char_u *)"<SNR>", name + 3);
7054 else
7055 p = name;
7056 EMSG2(_(msg), p);
7057 if (p != name)
7058 vim_free(p);
7059}
7060
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061/*********************************************
7062 * Implementation of the built-in functions
7063 */
7064
7065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007066 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 */
7068 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007069f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007070 typval_T *argvars;
7071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007076 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007078 if ((l = argvars[0].vval.v_list) != NULL
7079 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7080 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007081 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007082 }
7083 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007084 EMSG(_(e_listreq));
7085}
7086
7087/*
7088 * "append(lnum, string/list)" function
7089 */
7090 static void
7091f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 typval_T *argvars;
7093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007094{
7095 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 list_T *l = NULL;
7098 listitem_T *li = NULL;
7099 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007100 long added = 0;
7101
Bram Moolenaar0d660222005-01-07 21:51:51 +00007102 lnum = get_tv_lnum(argvars);
7103 if (lnum >= 0
7104 && lnum <= curbuf->b_ml.ml_line_count
7105 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007106 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007107 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007108 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007109 l = argvars[1].vval.v_list;
7110 if (l == NULL)
7111 return;
7112 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007114 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007115 for (;;)
7116 {
7117 if (l == NULL)
7118 tv = &argvars[1]; /* append a string */
7119 else if (li == NULL)
7120 break; /* end of list */
7121 else
7122 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 line = get_tv_string_chk(tv);
7124 if (line == NULL) /* type error */
7125 {
7126 rettv->vval.v_number = 1; /* Failed */
7127 break;
7128 }
7129 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007130 ++added;
7131 if (l == NULL)
7132 break;
7133 li = li->li_next;
7134 }
7135
7136 appended_lines_mark(lnum, added);
7137 if (curwin->w_cursor.lnum > lnum)
7138 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007140 else
7141 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142}
7143
7144/*
7145 * "argc()" function
7146 */
7147/* ARGSUSED */
7148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 typval_T *argvars;
7151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154}
7155
7156/*
7157 * "argidx()" function
7158 */
7159/* ARGSUSED */
7160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 typval_T *argvars;
7163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007165 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
7168/*
7169 * "argv(nr)" function
7170 */
7171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007172f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T *argvars;
7174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 int idx;
7177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007178 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007180 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007182 rettv->vval.v_string = NULL;
7183 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184}
7185
7186/*
7187 * "browse(save, title, initdir, default)" function
7188 */
7189/* ARGSUSED */
7190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007191f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 typval_T *argvars;
7193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195#ifdef FEAT_BROWSE
7196 int save;
7197 char_u *title;
7198 char_u *initdir;
7199 char_u *defname;
7200 char_u buf[NUMBUFLEN];
7201 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007204 save = get_tv_number_chk(&argvars[0], &error);
7205 title = get_tv_string_chk(&argvars[1]);
7206 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7207 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007209 if (error || title == NULL || initdir == NULL || defname == NULL)
7210 rettv->vval.v_string = NULL;
7211 else
7212 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007213 do_browse(save ? BROWSE_SAVE : 0,
7214 title, defname, NULL, initdir, NULL, curbuf);
7215#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007216 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007217#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007218 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007219}
7220
7221/*
7222 * "browsedir(title, initdir)" function
7223 */
7224/* ARGSUSED */
7225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007226f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 typval_T *argvars;
7228 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007229{
7230#ifdef FEAT_BROWSE
7231 char_u *title;
7232 char_u *initdir;
7233 char_u buf[NUMBUFLEN];
7234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007235 title = get_tv_string_chk(&argvars[0]);
7236 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007238 if (title == NULL || initdir == NULL)
7239 rettv->vval.v_string = NULL;
7240 else
7241 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007242 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007244 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247}
7248
Bram Moolenaar33570922005-01-25 22:26:29 +00007249static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007250
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251/*
7252 * Find a buffer by number or exact name.
7253 */
7254 static buf_T *
7255find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007260 if (avar->v_type == VAR_NUMBER)
7261 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007262 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007264 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007265 if (buf == NULL)
7266 {
7267 /* No full path name match, try a match with a URL or a "nofile"
7268 * buffer, these don't use the full path. */
7269 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7270 if (buf->b_fname != NULL
7271 && (path_with_url(buf->b_fname)
7272#ifdef FEAT_QUICKFIX
7273 || bt_nofile(buf)
7274#endif
7275 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007276 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007277 break;
7278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280 return buf;
7281}
7282
7283/*
7284 * "bufexists(expr)" function
7285 */
7286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007287f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 typval_T *argvars;
7289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007291 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292}
7293
7294/*
7295 * "buflisted(expr)" function
7296 */
7297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007298f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 typval_T *argvars;
7300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 buf_T *buf;
7303
7304 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306}
7307
7308/*
7309 * "bufloaded(expr)" function
7310 */
7311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 typval_T *argvars;
7314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 buf_T *buf;
7317
7318 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320}
7321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007323
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324/*
7325 * Get buffer by number or pattern.
7326 */
7327 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007328get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007331 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 int save_magic;
7333 char_u *save_cpo;
7334 buf_T *buf;
7335
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007336 if (tv->v_type == VAR_NUMBER)
7337 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007338 if (tv->v_type != VAR_STRING)
7339 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (name == NULL || *name == NUL)
7341 return curbuf;
7342 if (name[0] == '$' && name[1] == NUL)
7343 return lastbuf;
7344
7345 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7346 save_magic = p_magic;
7347 p_magic = TRUE;
7348 save_cpo = p_cpo;
7349 p_cpo = (char_u *)"";
7350
7351 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7352 TRUE, FALSE));
7353
7354 p_magic = save_magic;
7355 p_cpo = save_cpo;
7356
7357 /* If not found, try expanding the name, like done for bufexists(). */
7358 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 return buf;
7362}
7363
7364/*
7365 * "bufname(expr)" function
7366 */
7367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007368f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 typval_T *argvars;
7370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 buf_T *buf;
7373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007374 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 buf = get_buf_tv(&argvars[0]);
7377 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007379 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 --emsg_off;
7383}
7384
7385/*
7386 * "bufnr(expr)" function
7387 */
7388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 typval_T *argvars;
7391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393 buf_T *buf;
7394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007395 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007397 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007401 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 --emsg_off;
7403}
7404
7405/*
7406 * "bufwinnr(nr)" function
7407 */
7408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007409f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 typval_T *argvars;
7411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413#ifdef FEAT_WINDOWS
7414 win_T *wp;
7415 int winnr = 0;
7416#endif
7417 buf_T *buf;
7418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007419 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007421 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422#ifdef FEAT_WINDOWS
7423 for (wp = firstwin; wp; wp = wp->w_next)
7424 {
7425 ++winnr;
7426 if (wp->w_buffer == buf)
7427 break;
7428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#endif
7433 --emsg_off;
7434}
7435
7436/*
7437 * "byte2line(byte)" function
7438 */
7439/*ARGSUSED*/
7440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T *argvars;
7443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
7445#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447#else
7448 long boff = 0;
7449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007450 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 (linenr_T)0, &boff);
7456#endif
7457}
7458
7459/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007460 * "byteidx()" function
7461 */
7462/*ARGSUSED*/
7463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 typval_T *argvars;
7466 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007467{
7468#ifdef FEAT_MBYTE
7469 char_u *t;
7470#endif
7471 char_u *str;
7472 long idx;
7473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007474 str = get_tv_string_chk(&argvars[0]);
7475 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007477 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007478 return;
7479
7480#ifdef FEAT_MBYTE
7481 t = str;
7482 for ( ; idx > 0; idx--)
7483 {
7484 if (*t == NUL) /* EOL reached */
7485 return;
7486 t += mb_ptr2len_check(t);
7487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007489#else
7490 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007492#endif
7493}
7494
7495/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007496 * "call(func, arglist)" function
7497 */
7498 static void
7499f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 typval_T *argvars;
7501 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007502{
7503 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007505 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007507 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007509
7510 rettv->vval.v_number = 0;
7511 if (argvars[1].v_type != VAR_LIST)
7512 {
7513 EMSG(_(e_listreq));
7514 return;
7515 }
7516 if (argvars[1].vval.v_list == NULL)
7517 return;
7518
7519 if (argvars[0].v_type == VAR_FUNC)
7520 func = argvars[0].vval.v_string;
7521 else
7522 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007523 if (*func == NUL)
7524 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007525
Bram Moolenaare9a41262005-01-15 22:18:47 +00007526 if (argvars[2].v_type != VAR_UNKNOWN)
7527 {
7528 if (argvars[2].v_type != VAR_DICT)
7529 {
7530 EMSG(_(e_dictreq));
7531 return;
7532 }
7533 selfdict = argvars[2].vval.v_dict;
7534 }
7535
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007536 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7537 item = item->li_next)
7538 {
7539 if (argc == MAX_FUNC_ARGS)
7540 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007541 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007542 break;
7543 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007544 /* Make a copy of each argument. This is needed to be able to set
7545 * v_lock to VAR_FIXED in the copy without changing the original list.
7546 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007547 copy_tv(&item->li_tv, &argv[argc++]);
7548 }
7549
7550 if (item == NULL)
7551 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7553 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007554
7555 /* Free the arguments. */
7556 while (argc > 0)
7557 clear_tv(&argv[--argc]);
7558}
7559
7560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 * "char2nr(string)" function
7562 */
7563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 typval_T *argvars;
7566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568#ifdef FEAT_MBYTE
7569 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
7572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574}
7575
7576/*
7577 * "cindent(lnum)" function
7578 */
7579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 typval_T *argvars;
7582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
7584#ifdef FEAT_CINDENT
7585 pos_T pos;
7586 linenr_T lnum;
7587
7588 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007589 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7591 {
7592 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 curwin->w_cursor = pos;
7595 }
7596 else
7597#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599}
7600
7601/*
7602 * "col(string)" function
7603 */
7604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *argvars;
7607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 colnr_T col = 0;
7610 pos_T *fp;
7611
7612 fp = var2fpos(&argvars[0], FALSE);
7613 if (fp != NULL)
7614 {
7615 if (fp->col == MAXCOL)
7616 {
7617 /* '> can be MAXCOL, get the length of the line then */
7618 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7619 col = STRLEN(ml_get(fp->lnum)) + 1;
7620 else
7621 col = MAXCOL;
7622 }
7623 else
7624 {
7625 col = fp->col + 1;
7626#ifdef FEAT_VIRTUALEDIT
7627 /* col(".") when the cursor is on the NUL at the end of the line
7628 * because of "coladd" can be seen as an extra column. */
7629 if (virtual_active() && fp == &curwin->w_cursor)
7630 {
7631 char_u *p = ml_get_cursor();
7632
7633 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7634 curwin->w_virtcol - curwin->w_cursor.coladd))
7635 {
7636# ifdef FEAT_MBYTE
7637 int l;
7638
7639 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7640 col += l;
7641# else
7642 if (*p != NUL && p[1] == NUL)
7643 ++col;
7644# endif
7645 }
7646 }
7647#endif
7648 }
7649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651}
7652
7653/*
7654 * "confirm(message, buttons[, default [, type]])" function
7655 */
7656/*ARGSUSED*/
7657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *argvars;
7660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
7662#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7663 char_u *message;
7664 char_u *buttons = NULL;
7665 char_u buf[NUMBUFLEN];
7666 char_u buf2[NUMBUFLEN];
7667 int def = 1;
7668 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007669 char_u *typestr;
7670 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007672 message = get_tv_string_chk(&argvars[0]);
7673 if (message == NULL)
7674 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007677 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7678 if (buttons == NULL)
7679 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007682 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007683 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7686 if (typestr == NULL)
7687 error = TRUE;
7688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007690 switch (TOUPPER_ASC(*typestr))
7691 {
7692 case 'E': type = VIM_ERROR; break;
7693 case 'Q': type = VIM_QUESTION; break;
7694 case 'I': type = VIM_INFO; break;
7695 case 'W': type = VIM_WARNING; break;
7696 case 'G': type = VIM_GENERIC; break;
7697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699 }
7700 }
7701 }
7702
7703 if (buttons == NULL || *buttons == NUL)
7704 buttons = (char_u *)_("&Ok");
7705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007706 if (error)
7707 rettv->vval.v_number = 0;
7708 else
7709 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 def, NULL);
7711#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713#endif
7714}
7715
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716/*
7717 * "copy()" function
7718 */
7719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 typval_T *argvars;
7722 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007724 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
7727/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007728 * "count()" function
7729 */
7730 static void
7731f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *argvars;
7733 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007734{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007735 long n = 0;
7736 int ic = FALSE;
7737
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007739 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 listitem_T *li;
7741 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743
Bram Moolenaare9a41262005-01-15 22:18:47 +00007744 if ((l = argvars[0].vval.v_list) != NULL)
7745 {
7746 li = l->lv_first;
7747 if (argvars[2].v_type != VAR_UNKNOWN)
7748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 int error = FALSE;
7750
7751 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007752 if (argvars[3].v_type != VAR_UNKNOWN)
7753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754 idx = get_tv_number_chk(&argvars[3], &error);
7755 if (!error)
7756 {
7757 li = list_find(l, idx);
7758 if (li == NULL)
7759 EMSGN(_(e_listidx), idx);
7760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 if (error)
7763 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 }
7765
7766 for ( ; li != NULL; li = li->li_next)
7767 if (tv_equal(&li->li_tv, &argvars[1], ic))
7768 ++n;
7769 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007770 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007771 else if (argvars[0].v_type == VAR_DICT)
7772 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 int todo;
7774 dict_T *d;
7775 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007776
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007777 if ((d = argvars[0].vval.v_dict) != NULL)
7778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779 int error = FALSE;
7780
Bram Moolenaare9a41262005-01-15 22:18:47 +00007781 if (argvars[2].v_type != VAR_UNKNOWN)
7782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 if (argvars[3].v_type != VAR_UNKNOWN)
7785 EMSG(_(e_invarg));
7786 }
7787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007788 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007790 {
7791 if (!HASHITEM_EMPTY(hi))
7792 {
7793 --todo;
7794 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7795 ++n;
7796 }
7797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 }
7799 }
7800 else
7801 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007802 rettv->vval.v_number = n;
7803}
7804
7805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7807 *
7808 * Checks the existence of a cscope connection.
7809 */
7810/*ARGSUSED*/
7811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007813 typval_T *argvars;
7814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
7816#ifdef FEAT_CSCOPE
7817 int num = 0;
7818 char_u *dbpath = NULL;
7819 char_u *prepend = NULL;
7820 char_u buf[NUMBUFLEN];
7821
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007822 if (argvars[0].v_type != VAR_UNKNOWN
7823 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825 num = (int)get_tv_number(&argvars[0]);
7826 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007831 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007833 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#endif
7835}
7836
7837/*
7838 * "cursor(lnum, col)" function
7839 *
7840 * Moves the cursor to the specified line and column
7841 */
7842/*ARGSUSED*/
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 long line, col;
7849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007851 col = get_tv_number_chk(&argvars[1], NULL);
7852 if (line < 0 || col < 0)
7853 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (line > 0)
7855 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 if (col > 0)
7857 curwin->w_cursor.col = col - 1;
7858#ifdef FEAT_VIRTUALEDIT
7859 curwin->w_cursor.coladd = 0;
7860#endif
7861
7862 /* Make sure the cursor is in a valid position. */
7863 check_cursor();
7864#ifdef FEAT_MBYTE
7865 /* Correct cursor for multi-byte character. */
7866 if (has_mbyte)
7867 mb_adjust_cursor();
7868#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007869
7870 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871}
7872
7873/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 */
7876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 typval_T *argvars;
7879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007881 int noref = 0;
7882
7883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007884 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007885 if (noref < 0 || noref > 1)
7886 EMSG(_(e_invarg));
7887 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00007888 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * "delete()" function
7893 */
7894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 typval_T *argvars;
7897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007900 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
7904
7905/*
7906 * "did_filetype()" function
7907 */
7908/*ARGSUSED*/
7909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 typval_T *argvars;
7912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#endif
7919}
7920
7921/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007922 * "diff_filler()" function
7923 */
7924/*ARGSUSED*/
7925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007927 typval_T *argvars;
7928 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007929{
7930#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007931 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007932#endif
7933}
7934
7935/*
7936 * "diff_hlID()" function
7937 */
7938/*ARGSUSED*/
7939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 typval_T *argvars;
7942 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007943{
7944#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007946 static linenr_T prev_lnum = 0;
7947 static int changedtick = 0;
7948 static int fnum = 0;
7949 static int change_start = 0;
7950 static int change_end = 0;
7951 static enum hlf_value hlID = 0;
7952 int filler_lines;
7953 int col;
7954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007955 if (lnum < 0) /* ignore type error in {lnum} arg */
7956 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007957 if (lnum != prev_lnum
7958 || changedtick != curbuf->b_changedtick
7959 || fnum != curbuf->b_fnum)
7960 {
7961 /* New line, buffer, change: need to get the values. */
7962 filler_lines = diff_check(curwin, lnum);
7963 if (filler_lines < 0)
7964 {
7965 if (filler_lines == -1)
7966 {
7967 change_start = MAXCOL;
7968 change_end = -1;
7969 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7970 hlID = HLF_ADD; /* added line */
7971 else
7972 hlID = HLF_CHD; /* changed line */
7973 }
7974 else
7975 hlID = HLF_ADD; /* added line */
7976 }
7977 else
7978 hlID = (enum hlf_value)0;
7979 prev_lnum = lnum;
7980 changedtick = curbuf->b_changedtick;
7981 fnum = curbuf->b_fnum;
7982 }
7983
7984 if (hlID == HLF_CHD || hlID == HLF_TXD)
7985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007986 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00007987 if (col >= change_start && col <= change_end)
7988 hlID = HLF_TXD; /* changed text */
7989 else
7990 hlID = HLF_CHD; /* changed line */
7991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007993#endif
7994}
7995
7996/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007997 * "empty({expr})" function
7998 */
7999 static void
8000f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 typval_T *argvars;
8002 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008003{
8004 int n;
8005
8006 switch (argvars[0].v_type)
8007 {
8008 case VAR_STRING:
8009 case VAR_FUNC:
8010 n = argvars[0].vval.v_string == NULL
8011 || *argvars[0].vval.v_string == NUL;
8012 break;
8013 case VAR_NUMBER:
8014 n = argvars[0].vval.v_number == 0;
8015 break;
8016 case VAR_LIST:
8017 n = argvars[0].vval.v_list == NULL
8018 || argvars[0].vval.v_list->lv_first == NULL;
8019 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 case VAR_DICT:
8021 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008024 default:
8025 EMSG2(_(e_intern2), "f_empty()");
8026 n = 0;
8027 }
8028
8029 rettv->vval.v_number = n;
8030}
8031
8032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 * "escape({string}, {chars})" function
8034 */
8035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 typval_T *argvars;
8038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039{
8040 char_u buf[NUMBUFLEN];
8041
Bram Moolenaar758711c2005-02-02 23:11:38 +00008042 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8043 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045}
8046
8047/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008048 * "eval()" function
8049 */
8050/*ARGSUSED*/
8051 static void
8052f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 typval_T *argvars;
8054 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008055{
8056 char_u *s;
8057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008058 s = get_tv_string_chk(&argvars[0]);
8059 if (s != NULL)
8060 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008062 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8063 {
8064 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008065 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008067 else if (*s != NUL)
8068 EMSG(_(e_trailing));
8069}
8070
8071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 * "eventhandler()" function
8073 */
8074/*ARGSUSED*/
8075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008076f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 typval_T *argvars;
8078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008080 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081}
8082
8083/*
8084 * "executable()" function
8085 */
8086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008088 typval_T *argvars;
8089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008091 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092}
8093
8094/*
8095 * "exists()" function
8096 */
8097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008098f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008099 typval_T *argvars;
8100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
8102 char_u *p;
8103 char_u *name;
8104 int n = FALSE;
8105 int len = 0;
8106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (*p == '$') /* environment variable */
8109 {
8110 /* first try "normal" environment variables (fast) */
8111 if (mch_getenv(p + 1) != NULL)
8112 n = TRUE;
8113 else
8114 {
8115 /* try expanding things like $VIM and ${HOME} */
8116 p = expand_env_save(p);
8117 if (p != NULL && *p != '$')
8118 n = TRUE;
8119 vim_free(p);
8120 }
8121 }
8122 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 else if (*p == '*') /* internal or user defined function */
8125 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 }
8128 else if (*p == ':')
8129 {
8130 n = cmd_exists(p + 1);
8131 }
8132 else if (*p == '#')
8133 {
8134#ifdef FEAT_AUTOCMD
8135 name = p + 1;
8136 p = vim_strchr(name, '#');
8137 if (p != NULL)
8138 n = au_exists(name, p, p + 1);
8139 else
8140 n = au_exists(name, name + STRLEN(name), NULL);
8141#endif
8142 }
8143 else /* internal variable */
8144 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008145 char_u *tofree;
8146 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008148 /* get_name_len() takes care of expanding curly braces */
8149 name = p;
8150 len = get_name_len(&p, &tofree, TRUE, FALSE);
8151 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008153 if (tofree != NULL)
8154 name = tofree;
8155 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8156 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008158 /* handle d.key, l[idx], f(expr) */
8159 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8160 if (n)
8161 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 }
8163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008165 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
8167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171/*
8172 * "expand()" function
8173 */
8174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008175f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 typval_T *argvars;
8177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
8179 char_u *s;
8180 int len;
8181 char_u *errormsg;
8182 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8183 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008184 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008186 rettv->v_type = VAR_STRING;
8187 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (*s == '%' || *s == '#' || *s == '<')
8189 {
8190 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008191 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 --emsg_off;
8193 }
8194 else
8195 {
8196 /* When the optional second argument is non-zero, don't remove matches
8197 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008198 if (argvars[1].v_type != VAR_UNKNOWN
8199 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 if (!error)
8202 {
8203 ExpandInit(&xpc);
8204 xpc.xp_context = EXPAND_FILES;
8205 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8206 ExpandCleanup(&xpc);
8207 }
8208 else
8209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 }
8211}
8212
8213/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008214 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008215 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008216 */
8217 static void
8218f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 typval_T *argvars;
8220 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008221{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008222 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008223 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008224 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 list_T *l1, *l2;
8226 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008227 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008228 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008229
Bram Moolenaare9a41262005-01-15 22:18:47 +00008230 l1 = argvars[0].vval.v_list;
8231 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008232 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8233 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008234 {
8235 if (argvars[2].v_type != VAR_UNKNOWN)
8236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008237 before = get_tv_number_chk(&argvars[2], &error);
8238 if (error)
8239 return; /* type error; errmsg already given */
8240
Bram Moolenaar758711c2005-02-02 23:11:38 +00008241 if (before == l1->lv_len)
8242 item = NULL;
8243 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008244 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008245 item = list_find(l1, before);
8246 if (item == NULL)
8247 {
8248 EMSGN(_(e_listidx), before);
8249 return;
8250 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008251 }
8252 }
8253 else
8254 item = NULL;
8255 list_extend(l1, l2, item);
8256
Bram Moolenaare9a41262005-01-15 22:18:47 +00008257 copy_tv(&argvars[0], rettv);
8258 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8261 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008262 dict_T *d1, *d2;
8263 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 char_u *action;
8265 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008266 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008267 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008268
8269 d1 = argvars[0].vval.v_dict;
8270 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008271 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8272 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008273 {
8274 /* Check the third argument. */
8275 if (argvars[2].v_type != VAR_UNKNOWN)
8276 {
8277 static char *(av[]) = {"keep", "force", "error"};
8278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 action = get_tv_string_chk(&argvars[2]);
8280 if (action == NULL)
8281 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 for (i = 0; i < 3; ++i)
8283 if (STRCMP(action, av[i]) == 0)
8284 break;
8285 if (i == 3)
8286 {
8287 EMSGN(_(e_invarg2), action);
8288 return;
8289 }
8290 }
8291 else
8292 action = (char_u *)"force";
8293
8294 /* Go over all entries in the second dict and add them to the
8295 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 todo = d2->dv_hashtab.ht_used;
8297 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008298 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008299 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008301 --todo;
8302 di1 = dict_find(d1, hi2->hi_key, -1);
8303 if (di1 == NULL)
8304 {
8305 di1 = dictitem_copy(HI2DI(hi2));
8306 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8307 dictitem_free(di1);
8308 }
8309 else if (*action == 'e')
8310 {
8311 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8312 break;
8313 }
8314 else if (*action == 'f')
8315 {
8316 clear_tv(&di1->di_tv);
8317 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8318 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008319 }
8320 }
8321
Bram Moolenaare9a41262005-01-15 22:18:47 +00008322 copy_tv(&argvars[0], rettv);
8323 }
8324 }
8325 else
8326 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008327}
8328
8329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 * "filereadable()" function
8331 */
8332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 typval_T *argvars;
8335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 FILE *fd;
8338 char_u *p;
8339 int n;
8340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8343 {
8344 n = TRUE;
8345 fclose(fd);
8346 }
8347 else
8348 n = FALSE;
8349
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351}
8352
8353/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008354 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 * rights to write into.
8356 */
8357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 typval_T *argvars;
8360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008362 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
8364
Bram Moolenaar33570922005-01-25 22:26:29 +00008365static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008366
8367 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008368findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008371 int dir;
8372{
8373#ifdef FEAT_SEARCHPATH
8374 char_u *fname;
8375 char_u *fresult = NULL;
8376 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8377 char_u *p;
8378 char_u pathbuf[NUMBUFLEN];
8379 int count = 1;
8380 int first = TRUE;
8381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008383
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008384 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008386 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8387 if (p == NULL)
8388 count = -1; /* error */
8389 else
8390 {
8391 if (*p != NUL)
8392 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008394 if (argvars[2].v_type != VAR_UNKNOWN)
8395 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8396 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008397 }
8398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008401 do
8402 {
8403 vim_free(fresult);
8404 fresult = find_file_in_path_option(first ? fname : NULL,
8405 first ? (int)STRLEN(fname) : 0,
8406 0, first, path, dir, NULL);
8407 first = FALSE;
8408 } while (--count > 0 && fresult != NULL);
8409 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008412#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008416}
8417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008418static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8419static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008420static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8421static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008422
8423/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008424 * Prepare v: variable "idx" to be used.
8425 * Save the current typeval in "save_tv".
8426 * When not used yet add the variable to the v: hashtable.
8427 */
8428 static void
8429prepare_vimvar(idx, save_tv)
8430 int idx;
8431 typval_T *save_tv;
8432{
8433 *save_tv = vimvars[idx].vv_tv;
8434 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8435 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8436}
8437
8438/*
8439 * Restore v: variable "idx" to typeval "save_tv".
8440 * When no longer defined, remove the variable from the v: hashtable.
8441 */
8442 static void
8443restore_vimvar(idx, save_tv)
8444 int idx;
8445 typval_T *save_tv;
8446{
8447 hashitem_T *hi;
8448
8449 clear_tv(&vimvars[idx].vv_tv);
8450 vimvars[idx].vv_tv = *save_tv;
8451 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8452 {
8453 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8454 if (HASHITEM_EMPTY(hi))
8455 EMSG2(_(e_intern2), "restore_vimvar()");
8456 else
8457 hash_remove(&vimvarht, hi);
8458 }
8459}
8460
8461/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008462 * Implementation of map() and filter().
8463 */
8464 static void
8465filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *argvars;
8467 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008468 int map;
8469{
8470 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008471 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008472 listitem_T *li, *nli;
8473 list_T *l = NULL;
8474 dictitem_T *di;
8475 hashtab_T *ht;
8476 hashitem_T *hi;
8477 dict_T *d = NULL;
8478 typval_T save_val;
8479 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008480 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008481 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008482 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8483
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008484
8485 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008486 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008487 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008488 if ((l = argvars[0].vval.v_list) == NULL
8489 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 return;
8491 }
8492 else if (argvars[0].v_type == VAR_DICT)
8493 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008494 if ((d = argvars[0].vval.v_dict) == NULL
8495 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 return;
8497 }
8498 else
8499 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008500 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 return;
8502 }
8503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008504 expr = get_tv_string_buf_chk(&argvars[1], buf);
8505 /* On type errors, the preceding call has already displayed an error
8506 * message. Avoid a misleading error message for an empty string that
8507 * was not passed as argument. */
8508 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 prepare_vimvar(VV_VAL, &save_val);
8511 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 prepare_vimvar(VV_KEY, &save_key);
8516 vimvars[VV_KEY].vv_type = VAR_STRING;
8517
8518 ht = &d->dv_hashtab;
8519 hash_lock(ht);
8520 todo = ht->ht_used;
8521 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 if (!HASHITEM_EMPTY(hi))
8524 {
8525 --todo;
8526 di = HI2DI(hi);
8527 if (tv_check_lock(di->di_tv.v_lock, msg))
8528 break;
8529 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8530 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8531 break;
8532 if (!map && rem)
8533 dictitem_remove(d, di);
8534 clear_tv(&vimvars[VV_KEY].vv_tv);
8535 }
8536 }
8537 hash_unlock(ht);
8538
8539 restore_vimvar(VV_KEY, &save_key);
8540 }
8541 else
8542 {
8543 for (li = l->lv_first; li != NULL; li = nli)
8544 {
8545 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008546 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008547 nli = li->li_next;
8548 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008549 break;
8550 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008552 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008553 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008555 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557
8558 copy_tv(&argvars[0], rettv);
8559}
8560
8561 static int
8562filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008564 char_u *expr;
8565 int map;
8566 int *remp;
8567{
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569 char_u *s;
8570
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 s = expr;
8573 if (eval1(&s, &rettv, TRUE) == FAIL)
8574 return FAIL;
8575 if (*s != NUL) /* check for trailing chars after expr */
8576 {
8577 EMSG2(_(e_invexpr2), s);
8578 return FAIL;
8579 }
8580 if (map)
8581 {
8582 /* map(): replace the list item value */
8583 clear_tv(tv);
8584 *tv = rettv;
8585 }
8586 else
8587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008588 int error = FALSE;
8589
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008591 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008592 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008593 /* On type error, nothing has been removed; return FAIL to stop the
8594 * loop. The error message was given by get_tv_number_chk(). */
8595 if (error)
8596 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008600}
8601
8602/*
8603 * "filter()" function
8604 */
8605 static void
8606f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 typval_T *argvars;
8608 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008609{
8610 filter_map(argvars, rettv, FALSE);
8611}
8612
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008614 * "finddir({fname}[, {path}[, {count}]])" function
8615 */
8616 static void
8617f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *argvars;
8619 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008620{
8621 findfilendir(argvars, rettv, TRUE);
8622}
8623
8624/*
8625 * "findfile({fname}[, {path}[, {count}]])" function
8626 */
8627 static void
8628f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 typval_T *argvars;
8630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008631{
8632 findfilendir(argvars, rettv, FALSE);
8633}
8634
8635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 * "fnamemodify({fname}, {mods})" function
8637 */
8638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 typval_T *argvars;
8641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 char_u *fname;
8644 char_u *mods;
8645 int usedlen = 0;
8646 int len;
8647 char_u *fbuf = NULL;
8648 char_u buf[NUMBUFLEN];
8649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650 fname = get_tv_string_chk(&argvars[0]);
8651 mods = get_tv_string_buf_chk(&argvars[1], buf);
8652 if (fname == NULL || mods == NULL)
8653 fname = NULL;
8654 else
8655 {
8656 len = (int)STRLEN(fname);
8657 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 vim_free(fbuf);
8666}
8667
Bram Moolenaar33570922005-01-25 22:26:29 +00008668static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
8670/*
8671 * "foldclosed()" function
8672 */
8673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 typval_T *argvars;
8676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 int end;
8678{
8679#ifdef FEAT_FOLDING
8680 linenr_T lnum;
8681 linenr_T first, last;
8682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8685 {
8686 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8687 {
8688 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 return;
8693 }
8694 }
8695#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
8699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700 * "foldclosed()" function
8701 */
8702 static void
8703f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 typval_T *argvars;
8705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008706{
8707 foldclosed_both(argvars, rettv, FALSE);
8708}
8709
8710/*
8711 * "foldclosedend()" function
8712 */
8713 static void
8714f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 typval_T *argvars;
8716 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008717{
8718 foldclosed_both(argvars, rettv, TRUE);
8719}
8720
8721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 * "foldlevel()" function
8723 */
8724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729#ifdef FEAT_FOLDING
8730 linenr_T lnum;
8731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 else
8736#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738}
8739
8740/*
8741 * "foldtext()" function
8742 */
8743/*ARGSUSED*/
8744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749#ifdef FEAT_FOLDING
8750 linenr_T lnum;
8751 char_u *s;
8752 char_u *r;
8753 int len;
8754 char *txt;
8755#endif
8756
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 rettv->v_type = VAR_STRING;
8758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008760 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8761 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8762 <= curbuf->b_ml.ml_line_count
8763 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {
8765 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8767 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {
8769 if (!linewhite(lnum))
8770 break;
8771 ++lnum;
8772 }
8773
8774 /* Find interesting text in this line. */
8775 s = skipwhite(ml_get(lnum));
8776 /* skip C comment-start */
8777 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008780 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008782 {
8783 s = skipwhite(ml_get(lnum + 1));
8784 if (*s == '*')
8785 s = skipwhite(s + 1);
8786 }
8787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 txt = _("+-%s%3ld lines: ");
8789 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008790 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 + 20 /* for %3ld */
8792 + STRLEN(s))); /* concatenated */
8793 if (r != NULL)
8794 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008795 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8796 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8797 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 len = (int)STRLEN(r);
8799 STRCAT(r, s);
8800 /* remove 'foldmarker' and 'commentstring' */
8801 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 }
8804 }
8805#endif
8806}
8807
8808/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008809 * "foldtextresult(lnum)" function
8810 */
8811/*ARGSUSED*/
8812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 typval_T *argvars;
8815 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008816{
8817#ifdef FEAT_FOLDING
8818 linenr_T lnum;
8819 char_u *text;
8820 char_u buf[51];
8821 foldinfo_T foldinfo;
8822 int fold_count;
8823#endif
8824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->v_type = VAR_STRING;
8826 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008827#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008829 /* treat illegal types and illegal string values for {lnum} the same */
8830 if (lnum < 0)
8831 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008832 fold_count = foldedCount(curwin, lnum, &foldinfo);
8833 if (fold_count > 0)
8834 {
8835 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8836 &foldinfo, buf);
8837 if (text == buf)
8838 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008840 }
8841#endif
8842}
8843
8844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 * "foreground()" function
8846 */
8847/*ARGSUSED*/
8848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T *argvars;
8851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854#ifdef FEAT_GUI
8855 if (gui.in_use)
8856 gui_mch_set_foreground();
8857#else
8858# ifdef WIN32
8859 win32_set_foreground();
8860# endif
8861#endif
8862}
8863
8864/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008865 * "function()" function
8866 */
8867/*ARGSUSED*/
8868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 typval_T *argvars;
8871 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008872{
8873 char_u *s;
8874
Bram Moolenaara7043832005-01-21 11:56:39 +00008875 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008877 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008878 EMSG2(_(e_invarg2), s);
8879 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008880 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008881 else
8882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 rettv->vval.v_string = vim_strsave(s);
8884 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008885 }
8886}
8887
8888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008889 * "get()" function
8890 */
8891 static void
8892f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 typval_T *argvars;
8894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008895{
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 listitem_T *li;
8897 list_T *l;
8898 dictitem_T *di;
8899 dict_T *d;
8900 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008901
Bram Moolenaare9a41262005-01-15 22:18:47 +00008902 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008903 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008904 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008906 int error = FALSE;
8907
8908 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
8909 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008911 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008912 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 else if (argvars[0].v_type == VAR_DICT)
8914 {
8915 if ((d = argvars[0].vval.v_dict) != NULL)
8916 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008917 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 if (di != NULL)
8919 tv = &di->di_tv;
8920 }
8921 }
8922 else
8923 EMSG2(_(e_listdictarg), "get()");
8924
8925 if (tv == NULL)
8926 {
8927 if (argvars[2].v_type == VAR_UNKNOWN)
8928 rettv->vval.v_number = 0;
8929 else
8930 copy_tv(&argvars[2], rettv);
8931 }
8932 else
8933 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008934}
8935
8936/*
8937 * "getbufvar()" function
8938 */
8939 static void
8940f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *argvars;
8942 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943{
8944 buf_T *buf;
8945 buf_T *save_curbuf;
8946 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008949 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
8950 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008951 ++emsg_off;
8952 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008953
8954 rettv->v_type = VAR_STRING;
8955 rettv->vval.v_string = NULL;
8956
8957 if (buf != NULL && varname != NULL)
8958 {
8959 if (*varname == '&') /* buffer-local-option */
8960 {
8961 /* set curbuf to be our buf, temporarily */
8962 save_curbuf = curbuf;
8963 curbuf = buf;
8964
8965 get_option_tv(&varname, rettv, TRUE);
8966
8967 /* restore previous notion of curbuf */
8968 curbuf = save_curbuf;
8969 }
8970 else
8971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 if (*varname == NUL)
8973 /* let getbufvar({nr}, "") return the "b:" dictionary. The
8974 * scope prefix before the NUL byte is required by
8975 * find_var_in_ht(). */
8976 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008977 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008978 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008979 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008981 }
8982 }
8983
8984 --emsg_off;
8985}
8986
8987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 * "getchar()" function
8989 */
8990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 typval_T *argvars;
8993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
8998 ++no_mapping;
8999 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 /* getchar(): blocking wait. */
9002 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009003 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 /* getchar(1): only check if char avail */
9005 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 else if (error || vpeekc() == NUL)
9007 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 n = 0;
9009 else
9010 /* getchar(0) and char avail: return char */
9011 n = safe_vgetc();
9012 --no_mapping;
9013 --allow_keys;
9014
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 if (IS_SPECIAL(n) || mod_mask != 0)
9017 {
9018 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9019 int i = 0;
9020
9021 /* Turn a special key into three bytes, plus modifier. */
9022 if (mod_mask != 0)
9023 {
9024 temp[i++] = K_SPECIAL;
9025 temp[i++] = KS_MODIFIER;
9026 temp[i++] = mod_mask;
9027 }
9028 if (IS_SPECIAL(n))
9029 {
9030 temp[i++] = K_SPECIAL;
9031 temp[i++] = K_SECOND(n);
9032 temp[i++] = K_THIRD(n);
9033 }
9034#ifdef FEAT_MBYTE
9035 else if (has_mbyte)
9036 i += (*mb_char2bytes)(n, temp + i);
9037#endif
9038 else
9039 temp[i++] = n;
9040 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->v_type = VAR_STRING;
9042 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044}
9045
9046/*
9047 * "getcharmod()" function
9048 */
9049/*ARGSUSED*/
9050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 typval_T *argvars;
9053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056}
9057
9058/*
9059 * "getcmdline()" function
9060 */
9061/*ARGSUSED*/
9062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 typval_T *argvars;
9065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 rettv->v_type = VAR_STRING;
9068 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
9071/*
9072 * "getcmdpos()" function
9073 */
9074/*ARGSUSED*/
9075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081}
9082
9083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 * "getcwd()" function
9085 */
9086/*ARGSUSED*/
9087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 typval_T *argvars;
9090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092 char_u cwd[MAXPATHL];
9093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 else
9098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102#endif
9103 }
9104}
9105
9106/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009107 * "getfontname()" function
9108 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009109/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 typval_T *argvars;
9113 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009114{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->v_type = VAR_STRING;
9116 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009117#ifdef FEAT_GUI
9118 if (gui.in_use)
9119 {
9120 GuiFont font;
9121 char_u *name = NULL;
9122
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009123 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009124 {
9125 /* Get the "Normal" font. Either the name saved by
9126 * hl_set_font_name() or from the font ID. */
9127 font = gui.norm_font;
9128 name = hl_get_font_name();
9129 }
9130 else
9131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009133 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9134 return;
9135 font = gui_mch_get_font(name, FALSE);
9136 if (font == NOFONT)
9137 return; /* Invalid font name, return empty string. */
9138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009140 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009141 gui_mch_free_font(font);
9142 }
9143#endif
9144}
9145
9146/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009147 * "getfperm({fname})" function
9148 */
9149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009151 typval_T *argvars;
9152 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009153{
9154 char_u *fname;
9155 struct stat st;
9156 char_u *perm = NULL;
9157 char_u flags[] = "rwx";
9158 int i;
9159
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009161
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009163 if (mch_stat((char *)fname, &st) >= 0)
9164 {
9165 perm = vim_strsave((char_u *)"---------");
9166 if (perm != NULL)
9167 {
9168 for (i = 0; i < 9; i++)
9169 {
9170 if (st.st_mode & (1 << (8 - i)))
9171 perm[i] = flags[i % 3];
9172 }
9173 }
9174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009176}
9177
9178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 * "getfsize({fname})" function
9180 */
9181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009183 typval_T *argvars;
9184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 char_u *fname;
9187 struct stat st;
9188
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192
9193 if (mch_stat((char *)fname, &st) >= 0)
9194 {
9195 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 }
9200 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
9205 * "getftime({fname})" function
9206 */
9207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009209 typval_T *argvars;
9210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 char_u *fname;
9213 struct stat st;
9214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216
9217 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221}
9222
9223/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009224 * "getftype({fname})" function
9225 */
9226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009230{
9231 char_u *fname;
9232 struct stat st;
9233 char_u *type = NULL;
9234 char *t;
9235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009239 if (mch_lstat((char *)fname, &st) >= 0)
9240 {
9241#ifdef S_ISREG
9242 if (S_ISREG(st.st_mode))
9243 t = "file";
9244 else if (S_ISDIR(st.st_mode))
9245 t = "dir";
9246# ifdef S_ISLNK
9247 else if (S_ISLNK(st.st_mode))
9248 t = "link";
9249# endif
9250# ifdef S_ISBLK
9251 else if (S_ISBLK(st.st_mode))
9252 t = "bdev";
9253# endif
9254# ifdef S_ISCHR
9255 else if (S_ISCHR(st.st_mode))
9256 t = "cdev";
9257# endif
9258# ifdef S_ISFIFO
9259 else if (S_ISFIFO(st.st_mode))
9260 t = "fifo";
9261# endif
9262# ifdef S_ISSOCK
9263 else if (S_ISSOCK(st.st_mode))
9264 t = "fifo";
9265# endif
9266 else
9267 t = "other";
9268#else
9269# ifdef S_IFMT
9270 switch (st.st_mode & S_IFMT)
9271 {
9272 case S_IFREG: t = "file"; break;
9273 case S_IFDIR: t = "dir"; break;
9274# ifdef S_IFLNK
9275 case S_IFLNK: t = "link"; break;
9276# endif
9277# ifdef S_IFBLK
9278 case S_IFBLK: t = "bdev"; break;
9279# endif
9280# ifdef S_IFCHR
9281 case S_IFCHR: t = "cdev"; break;
9282# endif
9283# ifdef S_IFIFO
9284 case S_IFIFO: t = "fifo"; break;
9285# endif
9286# ifdef S_IFSOCK
9287 case S_IFSOCK: t = "socket"; break;
9288# endif
9289 default: t = "other";
9290 }
9291# else
9292 if (mch_isdir(fname))
9293 t = "dir";
9294 else
9295 t = "file";
9296# endif
9297#endif
9298 type = vim_strsave((char_u *)t);
9299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009301}
9302
9303/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009304 * "getline(lnum)" function
9305 */
9306 static void
9307f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 typval_T *argvars;
9309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310{
9311 linenr_T lnum;
9312 linenr_T end;
9313 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 list_T *l;
9315 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316
9317 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 if (lnum < 0)
9319 rettv->vval.v_number = 0; /* failure; error message already given */
9320 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009321 {
9322 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9323 p = ml_get(lnum);
9324 else
9325 p = (char_u *)"";
9326
9327 rettv->v_type = VAR_STRING;
9328 rettv->vval.v_string = vim_strsave(p);
9329 }
9330 else
9331 {
9332 end = get_tv_lnum(&argvars[1]);
9333 if (end < lnum)
9334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 if (end >= 0) /* else: error message already given */
9336 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009337 rettv->vval.v_number = 0;
9338 }
9339 else
9340 {
9341 l = list_alloc();
9342 if (l != NULL)
9343 {
9344 if (lnum < 1)
9345 lnum = 1;
9346 if (end > curbuf->b_ml.ml_line_count)
9347 end = curbuf->b_ml.ml_line_count;
9348 while (lnum <= end)
9349 {
9350 li = listitem_alloc();
9351 if (li == NULL)
9352 break;
9353 list_append(l, li);
9354 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009355 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009356 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9357 }
9358 rettv->vval.v_list = l;
9359 rettv->v_type = VAR_LIST;
9360 ++l->lv_refcount;
9361 }
9362 }
9363 }
9364}
9365
9366/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009367 * "getqflist()" function
9368 */
9369/*ARGSUSED*/
9370 static void
9371f_getqflist(argvars, rettv)
9372 typval_T *argvars;
9373 typval_T *rettv;
9374{
9375#ifdef FEAT_QUICKFIX
9376 list_T *l;
9377#endif
9378
9379 rettv->vval.v_number = FALSE;
9380#ifdef FEAT_QUICKFIX
9381 l = list_alloc();
9382 if (l != NULL)
9383 {
9384 if (get_errorlist(l) != FAIL)
9385 {
9386 rettv->vval.v_list = l;
9387 rettv->v_type = VAR_LIST;
9388 ++l->lv_refcount;
9389 }
9390 else
9391 list_free(l);
9392 }
9393#endif
9394}
9395
9396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 * "getreg()" function
9398 */
9399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009401 typval_T *argvars;
9402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403{
9404 char_u *strregname;
9405 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009406 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009407 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009409 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 strregname = get_tv_string_chk(&argvars[0]);
9412 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009413 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 regname = (strregname == NULL ? '"' : *strregname);
9419 if (regname == 0)
9420 regname = '"';
9421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 rettv->vval.v_string = error ? NULL :
9424 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425}
9426
9427/*
9428 * "getregtype()" function
9429 */
9430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 typval_T *argvars;
9433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
9435 char_u *strregname;
9436 int regname;
9437 char_u buf[NUMBUFLEN + 2];
9438 long reglen = 0;
9439
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009440 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 {
9442 strregname = get_tv_string_chk(&argvars[0]);
9443 if (strregname == NULL) /* type error; errmsg already given */
9444 {
9445 rettv->v_type = VAR_STRING;
9446 rettv->vval.v_string = NULL;
9447 return;
9448 }
9449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 else
9451 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009452 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453
9454 regname = (strregname == NULL ? '"' : *strregname);
9455 if (regname == 0)
9456 regname = '"';
9457
9458 buf[0] = NUL;
9459 buf[1] = NUL;
9460 switch (get_reg_type(regname, &reglen))
9461 {
9462 case MLINE: buf[0] = 'V'; break;
9463 case MCHAR: buf[0] = 'v'; break;
9464#ifdef FEAT_VISUAL
9465 case MBLOCK:
9466 buf[0] = Ctrl_V;
9467 sprintf((char *)buf + 1, "%ld", reglen + 1);
9468 break;
9469#endif
9470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->v_type = VAR_STRING;
9472 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473}
9474
9475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 * "getwinposx()" function
9477 */
9478/*ARGSUSED*/
9479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009481 typval_T *argvars;
9482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485#ifdef FEAT_GUI
9486 if (gui.in_use)
9487 {
9488 int x, y;
9489
9490 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 }
9493#endif
9494}
9495
9496/*
9497 * "getwinposy()" function
9498 */
9499/*ARGSUSED*/
9500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009502 typval_T *argvars;
9503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506#ifdef FEAT_GUI
9507 if (gui.in_use)
9508 {
9509 int x, y;
9510
9511 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
9514#endif
9515}
9516
9517/*
9518 * "getwinvar()" function
9519 */
9520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009522 typval_T *argvars;
9523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
9525 win_T *win, *oldcurwin;
9526 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 varname = get_tv_string_chk(&argvars[1]);
9531 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->v_type = VAR_STRING;
9534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535
9536 if (win != NULL && varname != NULL)
9537 {
9538 if (*varname == '&') /* window-local-option */
9539 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009540 /* Set curwin to be our win, temporarily. Also set curbuf, so
9541 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 oldcurwin = curwin;
9543 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009544 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
9548 /* restore previous notion of curwin */
9549 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009550 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 }
9552 else
9553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 if (*varname == NUL)
9555 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9556 * scope prefix before the NUL byte is required by
9557 * find_var_in_ht(). */
9558 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009560 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 }
9564 }
9565
9566 --emsg_off;
9567}
9568
9569/*
9570 * "glob()" function
9571 */
9572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009574 typval_T *argvars;
9575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577 expand_T xpc;
9578
9579 ExpandInit(&xpc);
9580 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->v_type = VAR_STRING;
9582 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9584 ExpandCleanup(&xpc);
9585}
9586
9587/*
9588 * "globpath()" function
9589 */
9590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009592 typval_T *argvars;
9593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009596 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 if (file == NULL)
9600 rettv->vval.v_string = NULL;
9601 else
9602 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "has()" function
9607 */
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 int i;
9614 char_u *name;
9615 int n = FALSE;
9616 static char *(has_list[]) =
9617 {
9618#ifdef AMIGA
9619 "amiga",
9620# ifdef FEAT_ARP
9621 "arp",
9622# endif
9623#endif
9624#ifdef __BEOS__
9625 "beos",
9626#endif
9627#ifdef MSDOS
9628# ifdef DJGPP
9629 "dos32",
9630# else
9631 "dos16",
9632# endif
9633#endif
9634#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9635 "mac",
9636#endif
9637#if defined(MACOS_X_UNIX)
9638 "macunix",
9639#endif
9640#ifdef OS2
9641 "os2",
9642#endif
9643#ifdef __QNX__
9644 "qnx",
9645#endif
9646#ifdef RISCOS
9647 "riscos",
9648#endif
9649#ifdef UNIX
9650 "unix",
9651#endif
9652#ifdef VMS
9653 "vms",
9654#endif
9655#ifdef WIN16
9656 "win16",
9657#endif
9658#ifdef WIN32
9659 "win32",
9660#endif
9661#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9662 "win32unix",
9663#endif
9664#ifdef WIN64
9665 "win64",
9666#endif
9667#ifdef EBCDIC
9668 "ebcdic",
9669#endif
9670#ifndef CASE_INSENSITIVE_FILENAME
9671 "fname_case",
9672#endif
9673#ifdef FEAT_ARABIC
9674 "arabic",
9675#endif
9676#ifdef FEAT_AUTOCMD
9677 "autocmd",
9678#endif
9679#ifdef FEAT_BEVAL
9680 "balloon_eval",
9681#endif
9682#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9683 "builtin_terms",
9684# ifdef ALL_BUILTIN_TCAPS
9685 "all_builtin_terms",
9686# endif
9687#endif
9688#ifdef FEAT_BYTEOFF
9689 "byte_offset",
9690#endif
9691#ifdef FEAT_CINDENT
9692 "cindent",
9693#endif
9694#ifdef FEAT_CLIENTSERVER
9695 "clientserver",
9696#endif
9697#ifdef FEAT_CLIPBOARD
9698 "clipboard",
9699#endif
9700#ifdef FEAT_CMDL_COMPL
9701 "cmdline_compl",
9702#endif
9703#ifdef FEAT_CMDHIST
9704 "cmdline_hist",
9705#endif
9706#ifdef FEAT_COMMENTS
9707 "comments",
9708#endif
9709#ifdef FEAT_CRYPT
9710 "cryptv",
9711#endif
9712#ifdef FEAT_CSCOPE
9713 "cscope",
9714#endif
9715#ifdef DEBUG
9716 "debug",
9717#endif
9718#ifdef FEAT_CON_DIALOG
9719 "dialog_con",
9720#endif
9721#ifdef FEAT_GUI_DIALOG
9722 "dialog_gui",
9723#endif
9724#ifdef FEAT_DIFF
9725 "diff",
9726#endif
9727#ifdef FEAT_DIGRAPHS
9728 "digraphs",
9729#endif
9730#ifdef FEAT_DND
9731 "dnd",
9732#endif
9733#ifdef FEAT_EMACS_TAGS
9734 "emacs_tags",
9735#endif
9736 "eval", /* always present, of course! */
9737#ifdef FEAT_EX_EXTRA
9738 "ex_extra",
9739#endif
9740#ifdef FEAT_SEARCH_EXTRA
9741 "extra_search",
9742#endif
9743#ifdef FEAT_FKMAP
9744 "farsi",
9745#endif
9746#ifdef FEAT_SEARCHPATH
9747 "file_in_path",
9748#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009749#if defined(UNIX) && !defined(USE_SYSTEM)
9750 "filterpipe",
9751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752#ifdef FEAT_FIND_ID
9753 "find_in_path",
9754#endif
9755#ifdef FEAT_FOLDING
9756 "folding",
9757#endif
9758#ifdef FEAT_FOOTER
9759 "footer",
9760#endif
9761#if !defined(USE_SYSTEM) && defined(UNIX)
9762 "fork",
9763#endif
9764#ifdef FEAT_GETTEXT
9765 "gettext",
9766#endif
9767#ifdef FEAT_GUI
9768 "gui",
9769#endif
9770#ifdef FEAT_GUI_ATHENA
9771# ifdef FEAT_GUI_NEXTAW
9772 "gui_neXtaw",
9773# else
9774 "gui_athena",
9775# endif
9776#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009777#ifdef FEAT_GUI_KDE
9778 "gui_kde",
9779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef FEAT_GUI_GTK
9781 "gui_gtk",
9782# ifdef HAVE_GTK2
9783 "gui_gtk2",
9784# endif
9785#endif
9786#ifdef FEAT_GUI_MAC
9787 "gui_mac",
9788#endif
9789#ifdef FEAT_GUI_MOTIF
9790 "gui_motif",
9791#endif
9792#ifdef FEAT_GUI_PHOTON
9793 "gui_photon",
9794#endif
9795#ifdef FEAT_GUI_W16
9796 "gui_win16",
9797#endif
9798#ifdef FEAT_GUI_W32
9799 "gui_win32",
9800#endif
9801#ifdef FEAT_HANGULIN
9802 "hangul_input",
9803#endif
9804#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9805 "iconv",
9806#endif
9807#ifdef FEAT_INS_EXPAND
9808 "insert_expand",
9809#endif
9810#ifdef FEAT_JUMPLIST
9811 "jumplist",
9812#endif
9813#ifdef FEAT_KEYMAP
9814 "keymap",
9815#endif
9816#ifdef FEAT_LANGMAP
9817 "langmap",
9818#endif
9819#ifdef FEAT_LIBCALL
9820 "libcall",
9821#endif
9822#ifdef FEAT_LINEBREAK
9823 "linebreak",
9824#endif
9825#ifdef FEAT_LISP
9826 "lispindent",
9827#endif
9828#ifdef FEAT_LISTCMDS
9829 "listcmds",
9830#endif
9831#ifdef FEAT_LOCALMAP
9832 "localmap",
9833#endif
9834#ifdef FEAT_MENU
9835 "menu",
9836#endif
9837#ifdef FEAT_SESSION
9838 "mksession",
9839#endif
9840#ifdef FEAT_MODIFY_FNAME
9841 "modify_fname",
9842#endif
9843#ifdef FEAT_MOUSE
9844 "mouse",
9845#endif
9846#ifdef FEAT_MOUSESHAPE
9847 "mouseshape",
9848#endif
9849#if defined(UNIX) || defined(VMS)
9850# ifdef FEAT_MOUSE_DEC
9851 "mouse_dec",
9852# endif
9853# ifdef FEAT_MOUSE_GPM
9854 "mouse_gpm",
9855# endif
9856# ifdef FEAT_MOUSE_JSB
9857 "mouse_jsbterm",
9858# endif
9859# ifdef FEAT_MOUSE_NET
9860 "mouse_netterm",
9861# endif
9862# ifdef FEAT_MOUSE_PTERM
9863 "mouse_pterm",
9864# endif
9865# ifdef FEAT_MOUSE_XTERM
9866 "mouse_xterm",
9867# endif
9868#endif
9869#ifdef FEAT_MBYTE
9870 "multi_byte",
9871#endif
9872#ifdef FEAT_MBYTE_IME
9873 "multi_byte_ime",
9874#endif
9875#ifdef FEAT_MULTI_LANG
9876 "multi_lang",
9877#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009878#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009879#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009880 "mzscheme",
9881#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883#ifdef FEAT_OLE
9884 "ole",
9885#endif
9886#ifdef FEAT_OSFILETYPE
9887 "osfiletype",
9888#endif
9889#ifdef FEAT_PATH_EXTRA
9890 "path_extra",
9891#endif
9892#ifdef FEAT_PERL
9893#ifndef DYNAMIC_PERL
9894 "perl",
9895#endif
9896#endif
9897#ifdef FEAT_PYTHON
9898#ifndef DYNAMIC_PYTHON
9899 "python",
9900#endif
9901#endif
9902#ifdef FEAT_POSTSCRIPT
9903 "postscript",
9904#endif
9905#ifdef FEAT_PRINTER
9906 "printer",
9907#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009908#ifdef FEAT_PROFILE
9909 "profile",
9910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911#ifdef FEAT_QUICKFIX
9912 "quickfix",
9913#endif
9914#ifdef FEAT_RIGHTLEFT
9915 "rightleft",
9916#endif
9917#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9918 "ruby",
9919#endif
9920#ifdef FEAT_SCROLLBIND
9921 "scrollbind",
9922#endif
9923#ifdef FEAT_CMDL_INFO
9924 "showcmd",
9925 "cmdline_info",
9926#endif
9927#ifdef FEAT_SIGNS
9928 "signs",
9929#endif
9930#ifdef FEAT_SMARTINDENT
9931 "smartindent",
9932#endif
9933#ifdef FEAT_SNIFF
9934 "sniff",
9935#endif
9936#ifdef FEAT_STL_OPT
9937 "statusline",
9938#endif
9939#ifdef FEAT_SUN_WORKSHOP
9940 "sun_workshop",
9941#endif
9942#ifdef FEAT_NETBEANS_INTG
9943 "netbeans_intg",
9944#endif
9945#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009946 "spell",
9947#endif
9948#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 "syntax",
9950#endif
9951#if defined(USE_SYSTEM) || !defined(UNIX)
9952 "system",
9953#endif
9954#ifdef FEAT_TAG_BINS
9955 "tag_binary",
9956#endif
9957#ifdef FEAT_TAG_OLDSTATIC
9958 "tag_old_static",
9959#endif
9960#ifdef FEAT_TAG_ANYWHITE
9961 "tag_any_white",
9962#endif
9963#ifdef FEAT_TCL
9964# ifndef DYNAMIC_TCL
9965 "tcl",
9966# endif
9967#endif
9968#ifdef TERMINFO
9969 "terminfo",
9970#endif
9971#ifdef FEAT_TERMRESPONSE
9972 "termresponse",
9973#endif
9974#ifdef FEAT_TEXTOBJ
9975 "textobjects",
9976#endif
9977#ifdef HAVE_TGETENT
9978 "tgetent",
9979#endif
9980#ifdef FEAT_TITLE
9981 "title",
9982#endif
9983#ifdef FEAT_TOOLBAR
9984 "toolbar",
9985#endif
9986#ifdef FEAT_USR_CMDS
9987 "user-commands", /* was accidentally included in 5.4 */
9988 "user_commands",
9989#endif
9990#ifdef FEAT_VIMINFO
9991 "viminfo",
9992#endif
9993#ifdef FEAT_VERTSPLIT
9994 "vertsplit",
9995#endif
9996#ifdef FEAT_VIRTUALEDIT
9997 "virtualedit",
9998#endif
9999#ifdef FEAT_VISUAL
10000 "visual",
10001#endif
10002#ifdef FEAT_VISUALEXTRA
10003 "visualextra",
10004#endif
10005#ifdef FEAT_VREPLACE
10006 "vreplace",
10007#endif
10008#ifdef FEAT_WILDIGN
10009 "wildignore",
10010#endif
10011#ifdef FEAT_WILDMENU
10012 "wildmenu",
10013#endif
10014#ifdef FEAT_WINDOWS
10015 "windows",
10016#endif
10017#ifdef FEAT_WAK
10018 "winaltkeys",
10019#endif
10020#ifdef FEAT_WRITEBACKUP
10021 "writebackup",
10022#endif
10023#ifdef FEAT_XIM
10024 "xim",
10025#endif
10026#ifdef FEAT_XFONTSET
10027 "xfontset",
10028#endif
10029#ifdef USE_XSMP
10030 "xsmp",
10031#endif
10032#ifdef USE_XSMP_INTERACT
10033 "xsmp_interact",
10034#endif
10035#ifdef FEAT_XCLIPBOARD
10036 "xterm_clipboard",
10037#endif
10038#ifdef FEAT_XTERM_SAVE
10039 "xterm_save",
10040#endif
10041#if defined(UNIX) && defined(FEAT_X11)
10042 "X11",
10043#endif
10044 NULL
10045 };
10046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 for (i = 0; has_list[i] != NULL; ++i)
10049 if (STRICMP(name, has_list[i]) == 0)
10050 {
10051 n = TRUE;
10052 break;
10053 }
10054
10055 if (n == FALSE)
10056 {
10057 if (STRNICMP(name, "patch", 5) == 0)
10058 n = has_patch(atoi((char *)name + 5));
10059 else if (STRICMP(name, "vim_starting") == 0)
10060 n = (starting != 0);
10061#ifdef DYNAMIC_TCL
10062 else if (STRICMP(name, "tcl") == 0)
10063 n = tcl_enabled(FALSE);
10064#endif
10065#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10066 else if (STRICMP(name, "iconv") == 0)
10067 n = iconv_enabled(FALSE);
10068#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010069#ifdef DYNAMIC_MZSCHEME
10070 else if (STRICMP(name, "mzscheme") == 0)
10071 n = mzscheme_enabled(FALSE);
10072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073#ifdef DYNAMIC_RUBY
10074 else if (STRICMP(name, "ruby") == 0)
10075 n = ruby_enabled(FALSE);
10076#endif
10077#ifdef DYNAMIC_PYTHON
10078 else if (STRICMP(name, "python") == 0)
10079 n = python_enabled(FALSE);
10080#endif
10081#ifdef DYNAMIC_PERL
10082 else if (STRICMP(name, "perl") == 0)
10083 n = perl_enabled(FALSE);
10084#endif
10085#ifdef FEAT_GUI
10086 else if (STRICMP(name, "gui_running") == 0)
10087 n = (gui.in_use || gui.starting);
10088# ifdef FEAT_GUI_W32
10089 else if (STRICMP(name, "gui_win32s") == 0)
10090 n = gui_is_win32s();
10091# endif
10092# ifdef FEAT_BROWSE
10093 else if (STRICMP(name, "browse") == 0)
10094 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10095# endif
10096#endif
10097#ifdef FEAT_SYN_HL
10098 else if (STRICMP(name, "syntax_items") == 0)
10099 n = syntax_present(curbuf);
10100#endif
10101#if defined(WIN3264)
10102 else if (STRICMP(name, "win95") == 0)
10103 n = mch_windows95();
10104#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010105#ifdef FEAT_NETBEANS_INTG
10106 else if (STRICMP(name, "netbeans_enabled") == 0)
10107 n = usingNetbeans;
10108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112}
10113
10114/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010115 * "has_key()" function
10116 */
10117 static void
10118f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 typval_T *argvars;
10120 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010121{
10122 rettv->vval.v_number = 0;
10123 if (argvars[0].v_type != VAR_DICT)
10124 {
10125 EMSG(_(e_dictreq));
10126 return;
10127 }
10128 if (argvars[0].vval.v_dict == NULL)
10129 return;
10130
10131 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010132 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010133}
10134
10135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 * "hasmapto()" function
10137 */
10138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010140 typval_T *argvars;
10141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
10143 char_u *name;
10144 char_u *mode;
10145 char_u buf[NUMBUFLEN];
10146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010148 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 mode = (char_u *)"nvo";
10150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
10153 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157}
10158
10159/*
10160 * "histadd()" function
10161 */
10162/*ARGSUSED*/
10163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010165 typval_T *argvars;
10166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167{
10168#ifdef FEAT_CMDHIST
10169 int histype;
10170 char_u *str;
10171 char_u buf[NUMBUFLEN];
10172#endif
10173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (check_restricted() || check_secure())
10176 return;
10177#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10179 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (histype >= 0)
10181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 if (*str != NUL)
10184 {
10185 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 return;
10188 }
10189 }
10190#endif
10191}
10192
10193/*
10194 * "histdel()" function
10195 */
10196/*ARGSUSED*/
10197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010199 typval_T *argvars;
10200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
10202#ifdef FEAT_CMDHIST
10203 int n;
10204 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10208 if (str == NULL)
10209 n = 0;
10210 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010213 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
10218 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 get_tv_string_buf(&argvars[1], buf));
10221 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224#endif
10225}
10226
10227/*
10228 * "histget()" function
10229 */
10230/*ARGSUSED*/
10231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 typval_T *argvars;
10234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236#ifdef FEAT_CMDHIST
10237 int type;
10238 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10242 if (str == NULL)
10243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245 {
10246 type = get_histtype(str);
10247 if (argvars[1].v_type == VAR_UNKNOWN)
10248 idx = get_history_idx(type);
10249 else
10250 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10251 /* -1 on type error */
10252 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258}
10259
10260/*
10261 * "histnr()" function
10262 */
10263/*ARGSUSED*/
10264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010265f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010266 typval_T *argvars;
10267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
10269 int i;
10270
10271#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 char_u *history = get_tv_string_chk(&argvars[0]);
10273
10274 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 if (i >= HIST_CMD && i < HIST_COUNT)
10276 i = get_history_idx(i);
10277 else
10278#endif
10279 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281}
10282
10283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 * "highlightID(name)" function
10285 */
10286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 typval_T *argvars;
10289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292}
10293
10294/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010295 * "highlight_exists()" function
10296 */
10297 static void
10298f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 typval_T *argvars;
10300 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010301{
10302 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10303}
10304
10305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 * "hostname()" function
10307 */
10308/*ARGSUSED*/
10309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010311 typval_T *argvars;
10312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313{
10314 char_u hostname[256];
10315
10316 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->v_type = VAR_STRING;
10318 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319}
10320
10321/*
10322 * iconv() function
10323 */
10324/*ARGSUSED*/
10325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330#ifdef FEAT_MBYTE
10331 char_u buf1[NUMBUFLEN];
10332 char_u buf2[NUMBUFLEN];
10333 char_u *from, *to, *str;
10334 vimconv_T vimconv;
10335#endif
10336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337 rettv->v_type = VAR_STRING;
10338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339
10340#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 str = get_tv_string(&argvars[0]);
10342 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10343 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 vimconv.vc_type = CONV_NONE;
10345 convert_setup(&vimconv, from, to);
10346
10347 /* If the encodings are equal, no conversion needed. */
10348 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010351 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352
10353 convert_setup(&vimconv, NULL, NULL);
10354 vim_free(from);
10355 vim_free(to);
10356#endif
10357}
10358
10359/*
10360 * "indent()" function
10361 */
10362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010363f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010364 typval_T *argvars;
10365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366{
10367 linenr_T lnum;
10368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010369 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374}
10375
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010376/*
10377 * "index()" function
10378 */
10379 static void
10380f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010381 typval_T *argvars;
10382 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010383{
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 list_T *l;
10385 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010386 long idx = 0;
10387 int ic = FALSE;
10388
10389 rettv->vval.v_number = -1;
10390 if (argvars[0].v_type != VAR_LIST)
10391 {
10392 EMSG(_(e_listreq));
10393 return;
10394 }
10395 l = argvars[0].vval.v_list;
10396 if (l != NULL)
10397 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010398 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010399 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 int error = FALSE;
10402
Bram Moolenaar758711c2005-02-02 23:11:38 +000010403 /* Start at specified item. Use the cached index that list_find()
10404 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010406 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010407 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408 ic = get_tv_number_chk(&argvars[3], &error);
10409 if (error)
10410 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010411 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010412
Bram Moolenaar758711c2005-02-02 23:11:38 +000010413 for ( ; item != NULL; item = item->li_next, ++idx)
10414 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010415 {
10416 rettv->vval.v_number = idx;
10417 break;
10418 }
10419 }
10420}
10421
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422static int inputsecret_flag = 0;
10423
10424/*
10425 * "input()" function
10426 * Also handles inputsecret() when inputsecret is set.
10427 */
10428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 char_u *p = NULL;
10435 int c;
10436 char_u buf[NUMBUFLEN];
10437 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441
10442#ifdef NO_CONSOLE_INPUT
10443 /* While starting up, there is no place to enter text. */
10444 if (no_console_input())
10445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 return;
10448 }
10449#endif
10450
10451 cmd_silent = FALSE; /* Want to see the prompt. */
10452 if (prompt != NULL)
10453 {
10454 /* Only the part of the message after the last NL is considered as
10455 * prompt for the command line */
10456 p = vim_strrchr(prompt, '\n');
10457 if (p == NULL)
10458 p = prompt;
10459 else
10460 {
10461 ++p;
10462 c = *p;
10463 *p = NUL;
10464 msg_start();
10465 msg_clr_eos();
10466 msg_puts_attr(prompt, echo_attr);
10467 msg_didout = FALSE;
10468 msg_starthere();
10469 *p = c;
10470 }
10471 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 if (argvars[1].v_type != VAR_UNKNOWN)
10474 {
10475 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10476 if (defstr != NULL)
10477 stuffReadbuffSpec(defstr);
10478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 if (defstr != NULL)
10481 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 /* since the user typed this, no need to wait for return */
10485 need_wait_return = FALSE;
10486 msg_didout = FALSE;
10487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 cmd_silent = cmd_silent_save;
10489}
10490
10491/*
10492 * "inputdialog()" function
10493 */
10494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498{
10499#if defined(FEAT_GUI_TEXTDIALOG)
10500 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10501 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10502 {
10503 char_u *message;
10504 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 message = get_tv_string_chk(&argvars[0]);
10508 if (argvars[1].v_type != VAR_UNKNOWN
10509 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 STRNCPY(IObuff, defstr, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 IObuff[IOSIZE - 1] = NUL;
10513 }
10514 else
10515 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 if (message != NULL && defstr != NULL
10517 && do_dialog(VIM_QUESTION, NULL, message,
10518 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 else
10521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 if (message != NULL && defstr != NULL
10523 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010524 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 rettv->vval.v_string = vim_strsave(
10526 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 }
10532 else
10533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536
10537static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10538
10539/*
10540 * "inputrestore()" function
10541 */
10542/*ARGSUSED*/
10543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010545 typval_T *argvars;
10546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548 if (ga_userinput.ga_len > 0)
10549 {
10550 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10552 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 }
10555 else if (p_verbose > 1)
10556 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010557 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560}
10561
10562/*
10563 * "inputsave()" function
10564 */
10565/*ARGSUSED*/
10566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570{
10571 /* Add an entry to the stack of typehead storage. */
10572 if (ga_grow(&ga_userinput, 1) == OK)
10573 {
10574 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10575 + ga_userinput.ga_len);
10576 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 }
10579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
10583/*
10584 * "inputsecret()" function
10585 */
10586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010588 typval_T *argvars;
10589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590{
10591 ++cmdline_star;
10592 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 --cmdline_star;
10595 --inputsecret_flag;
10596}
10597
10598/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010599 * "insert()" function
10600 */
10601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010603 typval_T *argvars;
10604 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010605{
10606 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 listitem_T *item;
10608 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010610
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010611 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010612 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010613 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010614 else if ((l = argvars[0].vval.v_list) != NULL
10615 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010616 {
10617 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 before = get_tv_number_chk(&argvars[2], &error);
10619 if (error)
10620 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010621
Bram Moolenaar758711c2005-02-02 23:11:38 +000010622 if (before == l->lv_len)
10623 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010624 else
10625 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010626 item = list_find(l, before);
10627 if (item == NULL)
10628 {
10629 EMSGN(_(e_listidx), before);
10630 l = NULL;
10631 }
10632 }
10633 if (l != NULL)
10634 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010635 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010636 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010637 }
10638 }
10639}
10640
10641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 * "isdirectory()" function
10643 */
10644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010646 typval_T *argvars;
10647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650}
10651
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010652/*
10653 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10654 * it refers to a List or Dictionary that is locked.
10655 */
10656 static int
10657tv_islocked(tv)
10658 typval_T *tv;
10659{
10660 return (tv->v_lock & VAR_LOCKED)
10661 || (tv->v_type == VAR_LIST
10662 && tv->vval.v_list != NULL
10663 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10664 || (tv->v_type == VAR_DICT
10665 && tv->vval.v_dict != NULL
10666 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10667}
10668
10669/*
10670 * "islocked()" function
10671 */
10672 static void
10673f_islocked(argvars, rettv)
10674 typval_T *argvars;
10675 typval_T *rettv;
10676{
10677 lval_T lv;
10678 char_u *end;
10679 dictitem_T *di;
10680
10681 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010682 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10683 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010684 if (end != NULL && lv.ll_name != NULL)
10685 {
10686 if (*end != NUL)
10687 EMSG(_(e_trailing));
10688 else
10689 {
10690 if (lv.ll_tv == NULL)
10691 {
10692 if (check_changedtick(lv.ll_name))
10693 rettv->vval.v_number = 1; /* always locked */
10694 else
10695 {
10696 di = find_var(lv.ll_name, NULL);
10697 if (di != NULL)
10698 {
10699 /* Consider a variable locked when:
10700 * 1. the variable itself is locked
10701 * 2. the value of the variable is locked.
10702 * 3. the List or Dict value is locked.
10703 */
10704 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10705 || tv_islocked(&di->di_tv));
10706 }
10707 }
10708 }
10709 else if (lv.ll_range)
10710 EMSG(_("E745: Range not allowed"));
10711 else if (lv.ll_newkey != NULL)
10712 EMSG2(_(e_dictkey), lv.ll_newkey);
10713 else if (lv.ll_list != NULL)
10714 /* List item. */
10715 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10716 else
10717 /* Dictionary item. */
10718 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10719 }
10720 }
10721
10722 clear_lval(&lv);
10723}
10724
Bram Moolenaar33570922005-01-25 22:26:29 +000010725static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010726
10727/*
10728 * Turn a dict into a list:
10729 * "what" == 0: list of keys
10730 * "what" == 1: list of values
10731 * "what" == 2: list of items
10732 */
10733 static void
10734dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010735 typval_T *argvars;
10736 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010737 int what;
10738{
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 list_T *l;
10740 list_T *l2;
10741 dictitem_T *di;
10742 hashitem_T *hi;
10743 listitem_T *li;
10744 listitem_T *li2;
10745 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010746 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010747
10748 rettv->vval.v_number = 0;
10749 if (argvars[0].v_type != VAR_DICT)
10750 {
10751 EMSG(_(e_dictreq));
10752 return;
10753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010754 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010755 return;
10756
10757 l = list_alloc();
10758 if (l == NULL)
10759 return;
10760 rettv->v_type = VAR_LIST;
10761 rettv->vval.v_list = l;
10762 ++l->lv_refcount;
10763
Bram Moolenaar33570922005-01-25 22:26:29 +000010764 todo = d->dv_hashtab.ht_used;
10765 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010767 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010769 --todo;
10770 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010771
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010772 li = listitem_alloc();
10773 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010774 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010775 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010776
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010777 if (what == 0)
10778 {
10779 /* keys() */
10780 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010781 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010782 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10783 }
10784 else if (what == 1)
10785 {
10786 /* values() */
10787 copy_tv(&di->di_tv, &li->li_tv);
10788 }
10789 else
10790 {
10791 /* items() */
10792 l2 = list_alloc();
10793 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010794 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010795 li->li_tv.vval.v_list = l2;
10796 if (l2 == NULL)
10797 break;
10798 ++l2->lv_refcount;
10799
10800 li2 = listitem_alloc();
10801 if (li2 == NULL)
10802 break;
10803 list_append(l2, li2);
10804 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010805 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010806 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10807
10808 li2 = listitem_alloc();
10809 if (li2 == NULL)
10810 break;
10811 list_append(l2, li2);
10812 copy_tv(&di->di_tv, &li2->li_tv);
10813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010814 }
10815 }
10816}
10817
10818/*
10819 * "items(dict)" function
10820 */
10821 static void
10822f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010823 typval_T *argvars;
10824 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010825{
10826 dict_list(argvars, rettv, 2);
10827}
10828
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010830 * "join()" function
10831 */
10832 static void
10833f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010834 typval_T *argvars;
10835 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010836{
10837 garray_T ga;
10838 char_u *sep;
10839
10840 rettv->vval.v_number = 0;
10841 if (argvars[0].v_type != VAR_LIST)
10842 {
10843 EMSG(_(e_listreq));
10844 return;
10845 }
10846 if (argvars[0].vval.v_list == NULL)
10847 return;
10848 if (argvars[1].v_type == VAR_UNKNOWN)
10849 sep = (char_u *)" ";
10850 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010852
10853 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854
10855 if (sep != NULL)
10856 {
10857 ga_init2(&ga, (int)sizeof(char), 80);
10858 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10859 ga_append(&ga, NUL);
10860 rettv->vval.v_string = (char_u *)ga.ga_data;
10861 }
10862 else
10863 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010864}
10865
10866/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010867 * "keys()" function
10868 */
10869 static void
10870f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 typval_T *argvars;
10872 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010873{
10874 dict_list(argvars, rettv, 0);
10875}
10876
10877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 * "last_buffer_nr()" function.
10879 */
10880/*ARGSUSED*/
10881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010883 typval_T *argvars;
10884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886 int n = 0;
10887 buf_T *buf;
10888
10889 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10890 if (n < buf->b_fnum)
10891 n = buf->b_fnum;
10892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010894}
10895
10896/*
10897 * "len()" function
10898 */
10899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010901 typval_T *argvars;
10902 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010903{
10904 switch (argvars[0].v_type)
10905 {
10906 case VAR_STRING:
10907 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_number = (varnumber_T)STRLEN(
10909 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010910 break;
10911 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010913 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914 case VAR_DICT:
10915 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10916 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010917 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010918 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010919 break;
10920 }
10921}
10922
Bram Moolenaar33570922005-01-25 22:26:29 +000010923static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924
10925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 typval_T *argvars;
10928 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010929 int type;
10930{
10931#ifdef FEAT_LIBCALL
10932 char_u *string_in;
10933 char_u **string_result;
10934 int nr_result;
10935#endif
10936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010942
10943 if (check_restricted() || check_secure())
10944 return;
10945
10946#ifdef FEAT_LIBCALL
10947 /* The first two args must be strings, otherwise its meaningless */
10948 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10949 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010950 string_in = NULL;
10951 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010952 string_in = argvars[2].vval.v_string;
10953 if (type == VAR_NUMBER)
10954 string_result = NULL;
10955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 if (mch_libcall(argvars[0].vval.v_string,
10958 argvars[1].vval.v_string,
10959 string_in,
10960 argvars[2].vval.v_number,
10961 string_result,
10962 &nr_result) == OK
10963 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 }
10966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967}
10968
10969/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010970 * "libcall()" function
10971 */
10972 static void
10973f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010974 typval_T *argvars;
10975 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010976{
10977 libcall_common(argvars, rettv, VAR_STRING);
10978}
10979
10980/*
10981 * "libcallnr()" function
10982 */
10983 static void
10984f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *argvars;
10986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987{
10988 libcall_common(argvars, rettv, VAR_NUMBER);
10989}
10990
10991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 * "line(string)" function
10993 */
10994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 typval_T *argvars;
10997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998{
10999 linenr_T lnum = 0;
11000 pos_T *fp;
11001
11002 fp = var2fpos(&argvars[0], TRUE);
11003 if (fp != NULL)
11004 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006}
11007
11008/*
11009 * "line2byte(lnum)" function
11010 */
11011/*ARGSUSED*/
11012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016{
11017#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019#else
11020 linenr_T lnum;
11021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11027 if (rettv->vval.v_number >= 0)
11028 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029#endif
11030}
11031
11032/*
11033 * "lispindent(lnum)" function
11034 */
11035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 typval_T *argvars;
11038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
11040#ifdef FEAT_LISP
11041 pos_T pos;
11042 linenr_T lnum;
11043
11044 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11047 {
11048 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 curwin->w_cursor = pos;
11051 }
11052 else
11053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055}
11056
11057/*
11058 * "localtime()" function
11059 */
11060/*ARGSUSED*/
11061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 typval_T *argvars;
11064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067}
11068
Bram Moolenaar33570922005-01-25 22:26:29 +000011069static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070
11071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011072get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011073 typval_T *argvars;
11074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 int exact;
11076{
11077 char_u *keys;
11078 char_u *which;
11079 char_u buf[NUMBUFLEN];
11080 char_u *keys_buf = NULL;
11081 char_u *rhs;
11082 int mode;
11083 garray_T ga;
11084
11085 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->v_type = VAR_STRING;
11087 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 if (*keys == NUL)
11091 return;
11092
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 else
11096 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 if (which == NULL)
11098 return;
11099
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 mode = get_map_mode(&which, 0);
11101
11102 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011103 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 vim_free(keys_buf);
11105 if (rhs != NULL)
11106 {
11107 ga_init(&ga);
11108 ga.ga_itemsize = 1;
11109 ga.ga_growsize = 40;
11110
11111 while (*rhs != NUL)
11112 ga_concat(&ga, str2special(&rhs, FALSE));
11113
11114 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 }
11117}
11118
11119/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011120 * "map()" function
11121 */
11122 static void
11123f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011126{
11127 filter_map(argvars, rettv, TRUE);
11128}
11129
11130/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 */
11133 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011134f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140
11141/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011142 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 */
11144 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011145f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 typval_T *argvars;
11147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150}
11151
Bram Moolenaar33570922005-01-25 22:26:29 +000011152static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
11154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 int type;
11159{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011160 char_u *str = NULL;
11161 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 char_u *pat;
11163 regmatch_T regmatch;
11164 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011165 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 char_u *save_cpo;
11167 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011168 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011169 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011170 list_T *l = NULL;
11171 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011172 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011173 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
11175 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11176 save_cpo = p_cpo;
11177 p_cpo = (char_u *)"";
11178
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011179 rettv->vval.v_number = -1;
11180 if (type == 3)
11181 {
11182 /* return empty list when there are no matches */
11183 if ((rettv->vval.v_list = list_alloc()) == NULL)
11184 goto theend;
11185 rettv->v_type = VAR_LIST;
11186 ++rettv->vval.v_list->lv_refcount;
11187 }
11188 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 rettv->v_type = VAR_STRING;
11191 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011194 if (argvars[0].v_type == VAR_LIST)
11195 {
11196 if ((l = argvars[0].vval.v_list) == NULL)
11197 goto theend;
11198 li = l->lv_first;
11199 }
11200 else
11201 expr = str = get_tv_string(&argvars[0]);
11202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11204 if (pat == NULL)
11205 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011206
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011207 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 int error = FALSE;
11210
11211 start = get_tv_number_chk(&argvars[2], &error);
11212 if (error)
11213 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011214 if (l != NULL)
11215 {
11216 li = list_find(l, start);
11217 if (li == NULL)
11218 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011219 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011220 }
11221 else
11222 {
11223 if (start < 0)
11224 start = 0;
11225 if (start > (long)STRLEN(str))
11226 goto theend;
11227 str += start;
11228 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011229
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011230 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231 nth = get_tv_number_chk(&argvars[3], &error);
11232 if (error)
11233 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 }
11235
11236 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11237 if (regmatch.regprog != NULL)
11238 {
11239 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011240
11241 while (1)
11242 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011243 if (l != NULL)
11244 {
11245 if (li == NULL)
11246 {
11247 match = FALSE;
11248 break;
11249 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011250 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011251 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011252 if (str == NULL)
11253 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011254 }
11255
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011256 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011257
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011258 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011259 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011260 if (l == NULL && !match)
11261 break;
11262
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011263 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011264 if (l != NULL)
11265 {
11266 li = li->li_next;
11267 ++idx;
11268 }
11269 else
11270 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011271#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011272 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011273#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011274 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011275#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011276 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011277 }
11278
11279 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011281 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011283 int i;
11284
11285 /* return list with matched string and submatches */
11286 for (i = 0; i < NSUBEXP; ++i)
11287 {
11288 if (regmatch.endp[i] == NULL)
11289 break;
11290 li = listitem_alloc();
11291 if (li == NULL)
11292 break;
11293 li->li_tv.v_type = VAR_STRING;
11294 li->li_tv.v_lock = 0;
11295 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11296 (int)(regmatch.endp[i] - regmatch.startp[i]));
11297 list_append(rettv->vval.v_list, li);
11298 }
11299 }
11300 else if (type == 2)
11301 {
11302 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011303 if (l != NULL)
11304 copy_tv(&li->li_tv, rettv);
11305 else
11306 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011308 }
11309 else if (l != NULL)
11310 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 else
11312 {
11313 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 (varnumber_T)(regmatch.startp[0] - str);
11316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 }
11321 }
11322 vim_free(regmatch.regprog);
11323 }
11324
11325theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011326 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 p_cpo = save_cpo;
11328}
11329
11330/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011331 * "match()" function
11332 */
11333 static void
11334f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T *argvars;
11336 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337{
11338 find_some_match(argvars, rettv, 1);
11339}
11340
11341/*
11342 * "matchend()" function
11343 */
11344 static void
11345f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011346 typval_T *argvars;
11347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011348{
11349 find_some_match(argvars, rettv, 0);
11350}
11351
11352/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011353 * "matchlist()" function
11354 */
11355 static void
11356f_matchlist(argvars, rettv)
11357 typval_T *argvars;
11358 typval_T *rettv;
11359{
11360 find_some_match(argvars, rettv, 3);
11361}
11362
11363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011364 * "matchstr()" function
11365 */
11366 static void
11367f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 typval_T *argvars;
11369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370{
11371 find_some_match(argvars, rettv, 2);
11372}
11373
Bram Moolenaar33570922005-01-25 22:26:29 +000011374static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011375
11376 static void
11377max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011378 typval_T *argvars;
11379 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011380 int domax;
11381{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011382 long n = 0;
11383 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011385
11386 if (argvars[0].v_type == VAR_LIST)
11387 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011388 list_T *l;
11389 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011390
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011391 l = argvars[0].vval.v_list;
11392 if (l != NULL)
11393 {
11394 li = l->lv_first;
11395 if (li != NULL)
11396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011398 while (1)
11399 {
11400 li = li->li_next;
11401 if (li == NULL)
11402 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011404 if (domax ? i > n : i < n)
11405 n = i;
11406 }
11407 }
11408 }
11409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011410 else if (argvars[0].v_type == VAR_DICT)
11411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011412 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011413 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011415 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011416
11417 d = argvars[0].vval.v_dict;
11418 if (d != NULL)
11419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 todo = d->dv_hashtab.ht_used;
11421 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011423 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011425 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011426 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011427 if (first)
11428 {
11429 n = i;
11430 first = FALSE;
11431 }
11432 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011433 n = i;
11434 }
11435 }
11436 }
11437 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011438 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011439 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011440 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011441}
11442
11443/*
11444 * "max()" function
11445 */
11446 static void
11447f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011448 typval_T *argvars;
11449 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011450{
11451 max_min(argvars, rettv, TRUE);
11452}
11453
11454/*
11455 * "min()" function
11456 */
11457 static void
11458f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 typval_T *argvars;
11460 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011461{
11462 max_min(argvars, rettv, FALSE);
11463}
11464
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011465static int mkdir_recurse __ARGS((char_u *dir, int prot));
11466
11467/*
11468 * Create the directory in which "dir" is located, and higher levels when
11469 * needed.
11470 */
11471 static int
11472mkdir_recurse(dir, prot)
11473 char_u *dir;
11474 int prot;
11475{
11476 char_u *p;
11477 char_u *updir;
11478 int r = FAIL;
11479
11480 /* Get end of directory name in "dir".
11481 * We're done when it's "/" or "c:/". */
11482 p = gettail_sep(dir);
11483 if (p <= get_past_head(dir))
11484 return OK;
11485
11486 /* If the directory exists we're done. Otherwise: create it.*/
11487 updir = vim_strnsave(dir, (int)(p - dir));
11488 if (updir == NULL)
11489 return FAIL;
11490 if (mch_isdir(updir))
11491 r = OK;
11492 else if (mkdir_recurse(updir, prot) == OK)
11493 r = vim_mkdir_emsg(updir, prot);
11494 vim_free(updir);
11495 return r;
11496}
11497
11498#ifdef vim_mkdir
11499/*
11500 * "mkdir()" function
11501 */
11502 static void
11503f_mkdir(argvars, rettv)
11504 typval_T *argvars;
11505 typval_T *rettv;
11506{
11507 char_u *dir;
11508 char_u buf[NUMBUFLEN];
11509 int prot = 0755;
11510
11511 rettv->vval.v_number = FAIL;
11512 if (check_restricted() || check_secure())
11513 return;
11514
11515 dir = get_tv_string_buf(&argvars[0], buf);
11516 if (argvars[1].v_type != VAR_UNKNOWN)
11517 {
11518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 prot = get_tv_number_chk(&argvars[2], NULL);
11520 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011521 mkdir_recurse(dir, prot);
11522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011524}
11525#endif
11526
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 * "mode()" function
11529 */
11530/*ARGSUSED*/
11531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011532f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 typval_T *argvars;
11534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535{
11536 char_u buf[2];
11537
11538#ifdef FEAT_VISUAL
11539 if (VIsual_active)
11540 {
11541 if (VIsual_select)
11542 buf[0] = VIsual_mode + 's' - 'v';
11543 else
11544 buf[0] = VIsual_mode;
11545 }
11546 else
11547#endif
11548 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11549 buf[0] = 'r';
11550 else if (State & INSERT)
11551 {
11552 if (State & REPLACE_FLAG)
11553 buf[0] = 'R';
11554 else
11555 buf[0] = 'i';
11556 }
11557 else if (State & CMDLINE)
11558 buf[0] = 'c';
11559 else
11560 buf[0] = 'n';
11561
11562 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_string = vim_strsave(buf);
11564 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565}
11566
11567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011568 * "nextnonblank()" function
11569 */
11570 static void
11571f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011572 typval_T *argvars;
11573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011574{
11575 linenr_T lnum;
11576
11577 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580 {
11581 lnum = 0;
11582 break;
11583 }
11584 if (*skipwhite(ml_get(lnum)) != NUL)
11585 break;
11586 }
11587 rettv->vval.v_number = lnum;
11588}
11589
11590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 * "nr2char()" function
11592 */
11593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 typval_T *argvars;
11596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597{
11598 char_u buf[NUMBUFLEN];
11599
11600#ifdef FEAT_MBYTE
11601 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 else
11604#endif
11605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 buf[1] = NUL;
11608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->v_type = VAR_STRING;
11610 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011611}
11612
11613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011614 * "prevnonblank()" function
11615 */
11616 static void
11617f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011618 typval_T *argvars;
11619 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011620{
11621 linenr_T lnum;
11622
11623 lnum = get_tv_lnum(argvars);
11624 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11625 lnum = 0;
11626 else
11627 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11628 --lnum;
11629 rettv->vval.v_number = lnum;
11630}
11631
Bram Moolenaar8c711452005-01-14 21:53:12 +000011632/*
11633 * "range()" function
11634 */
11635 static void
11636f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011637 typval_T *argvars;
11638 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011639{
11640 long start;
11641 long end;
11642 long stride = 1;
11643 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011644 list_T *l;
11645 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011648 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011649 if (argvars[1].v_type == VAR_UNKNOWN)
11650 {
11651 end = start - 1;
11652 start = 0;
11653 }
11654 else
11655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011659 }
11660
11661 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011662 if (error)
11663 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011664 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011665 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011666 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011667 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011668 else
11669 {
11670 l = list_alloc();
11671 if (l != NULL)
11672 {
11673 rettv->v_type = VAR_LIST;
11674 rettv->vval.v_list = l;
11675 ++l->lv_refcount;
11676
11677 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11678 {
11679 li = listitem_alloc();
11680 if (li == NULL)
11681 break;
11682 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011683 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011684 li->li_tv.vval.v_number = i;
11685 list_append(l, li);
11686 }
11687 }
11688 }
11689}
11690
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011691/*
11692 * "readfile()" function
11693 */
11694 static void
11695f_readfile(argvars, rettv)
11696 typval_T *argvars;
11697 typval_T *rettv;
11698{
11699 int binary = FALSE;
11700 char_u *fname;
11701 FILE *fd;
11702 list_T *l;
11703 listitem_T *li;
11704#define FREAD_SIZE 200 /* optimized for text lines */
11705 char_u buf[FREAD_SIZE];
11706 int readlen; /* size of last fread() */
11707 int buflen; /* nr of valid chars in buf[] */
11708 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11709 int tolist; /* first byte in buf[] still to be put in list */
11710 int chop; /* how many CR to chop off */
11711 char_u *prev = NULL; /* previously read bytes, if any */
11712 int prevlen = 0; /* length of "prev" if not NULL */
11713 char_u *s;
11714 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011715 long maxline = MAXLNUM;
11716 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011717
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011718 if (argvars[1].v_type != VAR_UNKNOWN)
11719 {
11720 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11721 binary = TRUE;
11722 if (argvars[2].v_type != VAR_UNKNOWN)
11723 maxline = get_tv_number(&argvars[2]);
11724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011725
11726 l = list_alloc();
11727 if (l == NULL)
11728 return;
11729 rettv->v_type = VAR_LIST;
11730 rettv->vval.v_list = l;
11731 l->lv_refcount = 1;
11732
11733 /* Always open the file in binary mode, library functions have a mind of
11734 * their own about CR-LF conversion. */
11735 fname = get_tv_string(&argvars[0]);
11736 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11737 {
11738 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11739 return;
11740 }
11741
11742 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011743 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011744 {
11745 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11746 buflen = filtd + readlen;
11747 tolist = 0;
11748 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11749 {
11750 if (buf[filtd] == '\n' || readlen <= 0)
11751 {
11752 /* Only when in binary mode add an empty list item when the
11753 * last line ends in a '\n'. */
11754 if (!binary && readlen == 0 && filtd == 0)
11755 break;
11756
11757 /* Found end-of-line or end-of-file: add a text line to the
11758 * list. */
11759 chop = 0;
11760 if (!binary)
11761 while (filtd - chop - 1 >= tolist
11762 && buf[filtd - chop - 1] == '\r')
11763 ++chop;
11764 len = filtd - tolist - chop;
11765 if (prev == NULL)
11766 s = vim_strnsave(buf + tolist, len);
11767 else
11768 {
11769 s = alloc((unsigned)(prevlen + len + 1));
11770 if (s != NULL)
11771 {
11772 mch_memmove(s, prev, prevlen);
11773 vim_free(prev);
11774 prev = NULL;
11775 mch_memmove(s + prevlen, buf + tolist, len);
11776 s[prevlen + len] = NUL;
11777 }
11778 }
11779 tolist = filtd + 1;
11780
11781 li = listitem_alloc();
11782 if (li == NULL)
11783 {
11784 vim_free(s);
11785 break;
11786 }
11787 li->li_tv.v_type = VAR_STRING;
11788 li->li_tv.v_lock = 0;
11789 li->li_tv.vval.v_string = s;
11790 list_append(l, li);
11791
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011792 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011793 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011794 if (readlen <= 0)
11795 break;
11796 }
11797 else if (buf[filtd] == NUL)
11798 buf[filtd] = '\n';
11799 }
11800 if (readlen <= 0)
11801 break;
11802
11803 if (tolist == 0)
11804 {
11805 /* "buf" is full, need to move text to an allocated buffer */
11806 if (prev == NULL)
11807 {
11808 prev = vim_strnsave(buf, buflen);
11809 prevlen = buflen;
11810 }
11811 else
11812 {
11813 s = alloc((unsigned)(prevlen + buflen));
11814 if (s != NULL)
11815 {
11816 mch_memmove(s, prev, prevlen);
11817 mch_memmove(s + prevlen, buf, buflen);
11818 vim_free(prev);
11819 prev = s;
11820 prevlen += buflen;
11821 }
11822 }
11823 filtd = 0;
11824 }
11825 else
11826 {
11827 mch_memmove(buf, buf + tolist, buflen - tolist);
11828 filtd -= tolist;
11829 }
11830 }
11831
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011832 /*
11833 * For a negative line count use only the lines at the end of the file,
11834 * free the rest.
11835 */
11836 if (maxline < 0)
11837 while (cnt > -maxline)
11838 {
11839 listitem_remove(l, l->lv_first);
11840 --cnt;
11841 }
11842
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011843 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011844 fclose(fd);
11845}
11846
11847
Bram Moolenaar0d660222005-01-07 21:51:51 +000011848#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11849static void make_connection __ARGS((void));
11850static int check_connection __ARGS((void));
11851
11852 static void
11853make_connection()
11854{
11855 if (X_DISPLAY == NULL
11856# ifdef FEAT_GUI
11857 && !gui.in_use
11858# endif
11859 )
11860 {
11861 x_force_connect = TRUE;
11862 setup_term_clip();
11863 x_force_connect = FALSE;
11864 }
11865}
11866
11867 static int
11868check_connection()
11869{
11870 make_connection();
11871 if (X_DISPLAY == NULL)
11872 {
11873 EMSG(_("E240: No connection to Vim server"));
11874 return FAIL;
11875 }
11876 return OK;
11877}
11878#endif
11879
11880#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011881static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882
11883 static void
11884remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 typval_T *argvars;
11886 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011887 int expr;
11888{
11889 char_u *server_name;
11890 char_u *keys;
11891 char_u *r = NULL;
11892 char_u buf[NUMBUFLEN];
11893# ifdef WIN32
11894 HWND w;
11895# else
11896 Window w;
11897# endif
11898
11899 if (check_restricted() || check_secure())
11900 return;
11901
11902# ifdef FEAT_X11
11903 if (check_connection() == FAIL)
11904 return;
11905# endif
11906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 server_name = get_tv_string_chk(&argvars[0]);
11908 if (server_name == NULL)
11909 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910 keys = get_tv_string_buf(&argvars[1], buf);
11911# ifdef WIN32
11912 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11913# else
11914 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11915 < 0)
11916# endif
11917 {
11918 if (r != NULL)
11919 EMSG(r); /* sending worked but evaluation failed */
11920 else
11921 EMSG2(_("E241: Unable to send to %s"), server_name);
11922 return;
11923 }
11924
11925 rettv->vval.v_string = r;
11926
11927 if (argvars[2].v_type != VAR_UNKNOWN)
11928 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011929 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000011930 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011932
11933 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011934 v.di_tv.v_type = VAR_STRING;
11935 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011936 idvar = get_tv_string_chk(&argvars[2]);
11937 if (idvar != NULL)
11938 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000011939 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011940 }
11941}
11942#endif
11943
11944/*
11945 * "remote_expr()" function
11946 */
11947/*ARGSUSED*/
11948 static void
11949f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *argvars;
11951 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011952{
11953 rettv->v_type = VAR_STRING;
11954 rettv->vval.v_string = NULL;
11955#ifdef FEAT_CLIENTSERVER
11956 remote_common(argvars, rettv, TRUE);
11957#endif
11958}
11959
11960/*
11961 * "remote_foreground()" function
11962 */
11963/*ARGSUSED*/
11964 static void
11965f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011966 typval_T *argvars;
11967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011968{
11969 rettv->vval.v_number = 0;
11970#ifdef FEAT_CLIENTSERVER
11971# ifdef WIN32
11972 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 {
11974 char_u *server_name = get_tv_string_chk(&argvars[0]);
11975
11976 if (server_name != NULL)
11977 serverForeground(server_name);
11978 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011979# else
11980 /* Send a foreground() expression to the server. */
11981 argvars[1].v_type = VAR_STRING;
11982 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11983 argvars[2].v_type = VAR_UNKNOWN;
11984 remote_common(argvars, rettv, TRUE);
11985 vim_free(argvars[1].vval.v_string);
11986# endif
11987#endif
11988}
11989
11990/*ARGSUSED*/
11991 static void
11992f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011993 typval_T *argvars;
11994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995{
11996#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011998 char_u *s = NULL;
11999# ifdef WIN32
12000 int n = 0;
12001# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012003
12004 if (check_restricted() || check_secure())
12005 {
12006 rettv->vval.v_number = -1;
12007 return;
12008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 serverid = get_tv_string_chk(&argvars[0]);
12010 if (serverid == NULL)
12011 {
12012 rettv->vval.v_number = -1;
12013 return; /* type error; errmsg already given */
12014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012016 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012017 if (n == 0)
12018 rettv->vval.v_number = -1;
12019 else
12020 {
12021 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12022 rettv->vval.v_number = (s != NULL);
12023 }
12024# else
12025 rettv->vval.v_number = 0;
12026 if (check_connection() == FAIL)
12027 return;
12028
12029 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031# endif
12032
12033 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012035 char_u *retvar;
12036
Bram Moolenaar33570922005-01-25 22:26:29 +000012037 v.di_tv.v_type = VAR_STRING;
12038 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 retvar = get_tv_string_chk(&argvars[1]);
12040 if (retvar != NULL)
12041 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043 }
12044#else
12045 rettv->vval.v_number = -1;
12046#endif
12047}
12048
12049/*ARGSUSED*/
12050 static void
12051f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054{
12055 char_u *r = NULL;
12056
12057#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 char_u *serverid = get_tv_string_chk(&argvars[0]);
12059
12060 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012061 {
12062# ifdef WIN32
12063 /* The server's HWND is encoded in the 'id' parameter */
12064 int n = 0;
12065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067 if (n != 0)
12068 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12069 if (r == NULL)
12070# else
12071 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073# endif
12074 EMSG(_("E277: Unable to read a server reply"));
12075 }
12076#endif
12077 rettv->v_type = VAR_STRING;
12078 rettv->vval.v_string = r;
12079}
12080
12081/*
12082 * "remote_send()" function
12083 */
12084/*ARGSUSED*/
12085 static void
12086f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 typval_T *argvars;
12088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089{
12090 rettv->v_type = VAR_STRING;
12091 rettv->vval.v_string = NULL;
12092#ifdef FEAT_CLIENTSERVER
12093 remote_common(argvars, rettv, FALSE);
12094#endif
12095}
12096
12097/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012098 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 */
12100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012102 typval_T *argvars;
12103 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012104{
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 list_T *l;
12106 listitem_T *item, *item2;
12107 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012108 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012109 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012110 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012111 dict_T *d;
12112 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012113
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012114 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012115 if (argvars[0].v_type == VAR_DICT)
12116 {
12117 if (argvars[2].v_type != VAR_UNKNOWN)
12118 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012119 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012120 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012122 key = get_tv_string_chk(&argvars[1]);
12123 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012125 di = dict_find(d, key, -1);
12126 if (di == NULL)
12127 EMSG2(_(e_dictkey), key);
12128 else
12129 {
12130 *rettv = di->di_tv;
12131 init_tv(&di->di_tv);
12132 dictitem_remove(d, di);
12133 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012135 }
12136 }
12137 else if (argvars[0].v_type != VAR_LIST)
12138 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012139 else if ((l = argvars[0].vval.v_list) != NULL
12140 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 int error = FALSE;
12143
12144 idx = get_tv_number_chk(&argvars[1], &error);
12145 if (error)
12146 ; /* type error: do nothing, errmsg already given */
12147 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012148 EMSGN(_(e_listidx), idx);
12149 else
12150 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012151 if (argvars[2].v_type == VAR_UNKNOWN)
12152 {
12153 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012154 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012155 *rettv = item->li_tv;
12156 vim_free(item);
12157 }
12158 else
12159 {
12160 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012161 end = get_tv_number_chk(&argvars[2], &error);
12162 if (error)
12163 ; /* type error: do nothing */
12164 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012165 EMSGN(_(e_listidx), end);
12166 else
12167 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012168 int cnt = 0;
12169
12170 for (li = item; li != NULL; li = li->li_next)
12171 {
12172 ++cnt;
12173 if (li == item2)
12174 break;
12175 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012176 if (li == NULL) /* didn't find "item2" after "item" */
12177 EMSG(_(e_invrange));
12178 else
12179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012180 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012181 l = list_alloc();
12182 if (l != NULL)
12183 {
12184 rettv->v_type = VAR_LIST;
12185 rettv->vval.v_list = l;
12186 l->lv_first = item;
12187 l->lv_last = item2;
12188 l->lv_refcount = 1;
12189 item->li_prev = NULL;
12190 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012191 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012192 }
12193 }
12194 }
12195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012196 }
12197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198}
12199
12200/*
12201 * "rename({from}, {to})" function
12202 */
12203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012205 typval_T *argvars;
12206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207{
12208 char_u buf[NUMBUFLEN];
12209
12210 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12214 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215}
12216
12217/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012218 * "repeat()" function
12219 */
12220/*ARGSUSED*/
12221 static void
12222f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012223 typval_T *argvars;
12224 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012225{
12226 char_u *p;
12227 int n;
12228 int slen;
12229 int len;
12230 char_u *r;
12231 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012232 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012233
12234 n = get_tv_number(&argvars[1]);
12235 if (argvars[0].v_type == VAR_LIST)
12236 {
12237 l = list_alloc();
12238 if (l != NULL && argvars[0].vval.v_list != NULL)
12239 {
12240 l->lv_refcount = 1;
12241 while (n-- > 0)
12242 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12243 break;
12244 }
12245 rettv->v_type = VAR_LIST;
12246 rettv->vval.v_list = l;
12247 }
12248 else
12249 {
12250 p = get_tv_string(&argvars[0]);
12251 rettv->v_type = VAR_STRING;
12252 rettv->vval.v_string = NULL;
12253
12254 slen = (int)STRLEN(p);
12255 len = slen * n;
12256 if (len <= 0)
12257 return;
12258
12259 r = alloc(len + 1);
12260 if (r != NULL)
12261 {
12262 for (i = 0; i < n; i++)
12263 mch_memmove(r + i * slen, p, (size_t)slen);
12264 r[len] = NUL;
12265 }
12266
12267 rettv->vval.v_string = r;
12268 }
12269}
12270
12271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 * "resolve()" function
12273 */
12274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012276 typval_T *argvars;
12277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278{
12279 char_u *p;
12280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282#ifdef FEAT_SHORTCUT
12283 {
12284 char_u *v = NULL;
12285
12286 v = mch_resolve_shortcut(p);
12287 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 }
12292#else
12293# ifdef HAVE_READLINK
12294 {
12295 char_u buf[MAXPATHL + 1];
12296 char_u *cpy;
12297 int len;
12298 char_u *remain = NULL;
12299 char_u *q;
12300 int is_relative_to_current = FALSE;
12301 int has_trailing_pathsep = FALSE;
12302 int limit = 100;
12303
12304 p = vim_strsave(p);
12305
12306 if (p[0] == '.' && (vim_ispathsep(p[1])
12307 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12308 is_relative_to_current = TRUE;
12309
12310 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012311 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 has_trailing_pathsep = TRUE;
12313
12314 q = getnextcomp(p);
12315 if (*q != NUL)
12316 {
12317 /* Separate the first path component in "p", and keep the
12318 * remainder (beginning with the path separator). */
12319 remain = vim_strsave(q - 1);
12320 q[-1] = NUL;
12321 }
12322
12323 for (;;)
12324 {
12325 for (;;)
12326 {
12327 len = readlink((char *)p, (char *)buf, MAXPATHL);
12328 if (len <= 0)
12329 break;
12330 buf[len] = NUL;
12331
12332 if (limit-- == 0)
12333 {
12334 vim_free(p);
12335 vim_free(remain);
12336 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 goto fail;
12339 }
12340
12341 /* Ensure that the result will have a trailing path separator
12342 * if the argument has one. */
12343 if (remain == NULL && has_trailing_pathsep)
12344 add_pathsep(buf);
12345
12346 /* Separate the first path component in the link value and
12347 * concatenate the remainders. */
12348 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12349 if (*q != NUL)
12350 {
12351 if (remain == NULL)
12352 remain = vim_strsave(q - 1);
12353 else
12354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012355 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 if (cpy != NULL)
12357 {
12358 STRCAT(cpy, remain);
12359 vim_free(remain);
12360 remain = cpy;
12361 }
12362 }
12363 q[-1] = NUL;
12364 }
12365
12366 q = gettail(p);
12367 if (q > p && *q == NUL)
12368 {
12369 /* Ignore trailing path separator. */
12370 q[-1] = NUL;
12371 q = gettail(p);
12372 }
12373 if (q > p && !mch_isFullName(buf))
12374 {
12375 /* symlink is relative to directory of argument */
12376 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12377 if (cpy != NULL)
12378 {
12379 STRCPY(cpy, p);
12380 STRCPY(gettail(cpy), buf);
12381 vim_free(p);
12382 p = cpy;
12383 }
12384 }
12385 else
12386 {
12387 vim_free(p);
12388 p = vim_strsave(buf);
12389 }
12390 }
12391
12392 if (remain == NULL)
12393 break;
12394
12395 /* Append the first path component of "remain" to "p". */
12396 q = getnextcomp(remain + 1);
12397 len = q - remain - (*q != NUL);
12398 cpy = vim_strnsave(p, STRLEN(p) + len);
12399 if (cpy != NULL)
12400 {
12401 STRNCAT(cpy, remain, len);
12402 vim_free(p);
12403 p = cpy;
12404 }
12405 /* Shorten "remain". */
12406 if (*q != NUL)
12407 STRCPY(remain, q - 1);
12408 else
12409 {
12410 vim_free(remain);
12411 remain = NULL;
12412 }
12413 }
12414
12415 /* If the result is a relative path name, make it explicitly relative to
12416 * the current directory if and only if the argument had this form. */
12417 if (!vim_ispathsep(*p))
12418 {
12419 if (is_relative_to_current
12420 && *p != NUL
12421 && !(p[0] == '.'
12422 && (p[1] == NUL
12423 || vim_ispathsep(p[1])
12424 || (p[1] == '.'
12425 && (p[2] == NUL
12426 || vim_ispathsep(p[2]))))))
12427 {
12428 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012429 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 if (cpy != NULL)
12431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 vim_free(p);
12433 p = cpy;
12434 }
12435 }
12436 else if (!is_relative_to_current)
12437 {
12438 /* Strip leading "./". */
12439 q = p;
12440 while (q[0] == '.' && vim_ispathsep(q[1]))
12441 q += 2;
12442 if (q > p)
12443 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12444 }
12445 }
12446
12447 /* Ensure that the result will have no trailing path separator
12448 * if the argument had none. But keep "/" or "//". */
12449 if (!has_trailing_pathsep)
12450 {
12451 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012452 if (after_pathsep(p, q))
12453 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454 }
12455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 }
12458# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012459 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460# endif
12461#endif
12462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464
12465#ifdef HAVE_READLINK
12466fail:
12467#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469}
12470
12471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012472 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 */
12474 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012475f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *argvars;
12477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478{
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 list_T *l;
12480 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482 rettv->vval.v_number = 0;
12483 if (argvars[0].v_type != VAR_LIST)
12484 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012485 else if ((l = argvars[0].vval.v_list) != NULL
12486 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 {
12488 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012489 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012490 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012491 while (li != NULL)
12492 {
12493 ni = li->li_prev;
12494 list_append(l, li);
12495 li = ni;
12496 }
12497 rettv->vval.v_list = l;
12498 rettv->v_type = VAR_LIST;
12499 ++l->lv_refcount;
12500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501}
12502
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012503#define SP_NOMOVE 1 /* don't move cursor */
12504#define SP_REPEAT 2 /* repeat to find outer pair */
12505#define SP_RETCOUNT 4 /* return matchcount */
12506
Bram Moolenaar33570922005-01-25 22:26:29 +000012507static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508
12509/*
12510 * Get flags for a search function.
12511 * Possibly sets "p_ws".
12512 * Returns BACKWARD, FORWARD or zero (for an error).
12513 */
12514 static int
12515get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012516 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517 int *flagsp;
12518{
12519 int dir = FORWARD;
12520 char_u *flags;
12521 char_u nbuf[NUMBUFLEN];
12522 int mask;
12523
12524 if (varp->v_type != VAR_UNKNOWN)
12525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012526 flags = get_tv_string_buf_chk(varp, nbuf);
12527 if (flags == NULL)
12528 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529 while (*flags != NUL)
12530 {
12531 switch (*flags)
12532 {
12533 case 'b': dir = BACKWARD; break;
12534 case 'w': p_ws = TRUE; break;
12535 case 'W': p_ws = FALSE; break;
12536 default: mask = 0;
12537 if (flagsp != NULL)
12538 switch (*flags)
12539 {
12540 case 'n': mask = SP_NOMOVE; break;
12541 case 'r': mask = SP_REPEAT; break;
12542 case 'm': mask = SP_RETCOUNT; break;
12543 }
12544 if (mask == 0)
12545 {
12546 EMSG2(_(e_invarg2), flags);
12547 dir = 0;
12548 }
12549 else
12550 *flagsp |= mask;
12551 }
12552 if (dir == 0)
12553 break;
12554 ++flags;
12555 }
12556 }
12557 return dir;
12558}
12559
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560/*
12561 * "search()" function
12562 */
12563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012565 typval_T *argvars;
12566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567{
12568 char_u *pat;
12569 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012570 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 int save_p_ws = p_ws;
12572 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012573 int flags = 0;
12574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012578 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12579 if (dir == 0)
12580 goto theend;
12581 if ((flags & ~SP_NOMOVE) != 0)
12582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012584 goto theend;
12585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012587 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12589 SEARCH_KEEP, RE_SEARCH) != FAIL)
12590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 curwin->w_cursor = pos;
12593 /* "/$" will put the cursor after the end of the line, may need to
12594 * correct that here */
12595 check_cursor();
12596 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012597
12598 /* If 'n' flag is used: restore cursor position. */
12599 if (flags & SP_NOMOVE)
12600 curwin->w_cursor = save_cursor;
12601theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 p_ws = save_p_ws;
12603}
12604
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605/*
12606 * "searchpair()" function
12607 */
12608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012610 typval_T *argvars;
12611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612{
12613 char_u *spat, *mpat, *epat;
12614 char_u *skip;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 char_u *pat, *pat2 = NULL, *pat3 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 pos_T pos;
12617 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012618 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619 pos_T save_cursor;
12620 pos_T save_pos;
12621 int save_p_ws = p_ws;
12622 char_u *save_cpo;
12623 int dir;
12624 int flags = 0;
12625 char_u nbuf1[NUMBUFLEN];
12626 char_u nbuf2[NUMBUFLEN];
12627 char_u nbuf3[NUMBUFLEN];
12628 int n;
12629 int r;
12630 int nest = 1;
12631 int err;
12632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634
12635 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12636 save_cpo = p_cpo;
12637 p_cpo = (char_u *)"";
12638
12639 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 spat = get_tv_string_chk(&argvars[0]);
12641 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12642 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12643 if (spat == NULL || mpat == NULL || epat == NULL)
12644 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
12646 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12647 * start/middle/end (pat3, for the top pair). */
12648 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12649 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12650 if (pat2 == NULL || pat3 == NULL)
12651 goto theend;
12652 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12653 if (*mpat == NUL)
12654 STRCPY(pat3, pat2);
12655 else
12656 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12657 spat, epat, mpat);
12658
12659 /* Handle the optional fourth argument: flags */
12660 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012661 if (dir == 0)
12662 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
12664 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012665 if (argvars[3].v_type == VAR_UNKNOWN
12666 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 skip = (char_u *)"";
12668 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012669 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12670 if (skip == NULL)
12671 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672
12673 save_cursor = curwin->w_cursor;
12674 pos = curwin->w_cursor;
12675 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012676 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 pat = pat3;
12678 for (;;)
12679 {
12680 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12681 SEARCH_KEEP, RE_SEARCH);
12682 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12683 /* didn't find it or found the first match again: FAIL */
12684 break;
12685
12686 if (firstpos.lnum == 0)
12687 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012688 if (equalpos(pos, foundpos))
12689 {
12690 /* Found the same position again. Can happen with a pattern that
12691 * has "\zs" at the end and searching backwards. Advance one
12692 * character and try again. */
12693 if (dir == BACKWARD)
12694 decl(&pos);
12695 else
12696 incl(&pos);
12697 }
12698 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699
12700 /* If the skip pattern matches, ignore this match. */
12701 if (*skip != NUL)
12702 {
12703 save_pos = curwin->w_cursor;
12704 curwin->w_cursor = pos;
12705 r = eval_to_bool(skip, &err, NULL, FALSE);
12706 curwin->w_cursor = save_pos;
12707 if (err)
12708 {
12709 /* Evaluating {skip} caused an error, break here. */
12710 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 break;
12713 }
12714 if (r)
12715 continue;
12716 }
12717
12718 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12719 {
12720 /* Found end when searching backwards or start when searching
12721 * forward: nested pair. */
12722 ++nest;
12723 pat = pat2; /* nested, don't search for middle */
12724 }
12725 else
12726 {
12727 /* Found end when searching forward or start when searching
12728 * backward: end of (nested) pair; or found middle in outer pair. */
12729 if (--nest == 1)
12730 pat = pat3; /* outer level, search for middle */
12731 }
12732
12733 if (nest == 0)
12734 {
12735 /* Found the match: return matchcount or line number. */
12736 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 curwin->w_cursor = pos;
12741 if (!(flags & SP_REPEAT))
12742 break;
12743 nest = 1; /* search for next unmatched */
12744 }
12745 }
12746
12747 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 curwin->w_cursor = save_cursor;
12750
12751theend:
12752 vim_free(pat2);
12753 vim_free(pat3);
12754 p_ws = save_p_ws;
12755 p_cpo = save_cpo;
12756}
12757
Bram Moolenaar0d660222005-01-07 21:51:51 +000012758/*ARGSUSED*/
12759 static void
12760f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 typval_T *argvars;
12762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012764#ifdef FEAT_CLIENTSERVER
12765 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012766 char_u *server = get_tv_string_chk(&argvars[0]);
12767 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768
Bram Moolenaar0d660222005-01-07 21:51:51 +000012769 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 if (server == NULL || reply == NULL)
12771 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012772 if (check_restricted() || check_secure())
12773 return;
12774# ifdef FEAT_X11
12775 if (check_connection() == FAIL)
12776 return;
12777# endif
12778
12779 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012781 EMSG(_("E258: Unable to send to client"));
12782 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012784 rettv->vval.v_number = 0;
12785#else
12786 rettv->vval.v_number = -1;
12787#endif
12788}
12789
12790/*ARGSUSED*/
12791 static void
12792f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012793 typval_T *argvars;
12794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012795{
12796 char_u *r = NULL;
12797
12798#ifdef FEAT_CLIENTSERVER
12799# ifdef WIN32
12800 r = serverGetVimNames();
12801# else
12802 make_connection();
12803 if (X_DISPLAY != NULL)
12804 r = serverGetVimNames(X_DISPLAY);
12805# endif
12806#endif
12807 rettv->v_type = VAR_STRING;
12808 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809}
12810
12811/*
12812 * "setbufvar()" function
12813 */
12814/*ARGSUSED*/
12815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012817 typval_T *argvars;
12818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819{
12820 buf_T *buf;
12821#ifdef FEAT_AUTOCMD
12822 aco_save_T aco;
12823#else
12824 buf_T *save_curbuf;
12825#endif
12826 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 char_u nbuf[NUMBUFLEN];
12829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 rettv->vval.v_number = 0;
12831
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 if (check_restricted() || check_secure())
12833 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12835 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 varp = &argvars[2];
12838
12839 if (buf != NULL && varname != NULL && varp != NULL)
12840 {
12841 /* set curbuf to be our buf, temporarily */
12842#ifdef FEAT_AUTOCMD
12843 aucmd_prepbuf(&aco, buf);
12844#else
12845 save_curbuf = curbuf;
12846 curbuf = buf;
12847#endif
12848
12849 if (*varname == '&')
12850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012851 long numval;
12852 char_u *strval;
12853 int error = FALSE;
12854
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 numval = get_tv_number_chk(varp, &error);
12857 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 if (!error && strval != NULL)
12859 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 }
12861 else
12862 {
12863 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12864 if (bufvarname != NULL)
12865 {
12866 STRCPY(bufvarname, "b:");
12867 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012868 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 vim_free(bufvarname);
12870 }
12871 }
12872
12873 /* reset notion of buffer */
12874#ifdef FEAT_AUTOCMD
12875 aucmd_restbuf(&aco);
12876#else
12877 curbuf = save_curbuf;
12878#endif
12879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
12882/*
12883 * "setcmdpos()" function
12884 */
12885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012890 int pos = (int)get_tv_number(&argvars[0]) - 1;
12891
12892 if (pos >= 0)
12893 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894}
12895
12896/*
12897 * "setline()" function
12898 */
12899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 typval_T *argvars;
12902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903{
12904 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000012905 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012906 list_T *l = NULL;
12907 listitem_T *li = NULL;
12908 long added = 0;
12909 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012911 lnum = get_tv_lnum(&argvars[0]);
12912 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012914 l = argvars[1].vval.v_list;
12915 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012917 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012918 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012919
12920 rettv->vval.v_number = 0; /* OK */
12921 for (;;)
12922 {
12923 if (l != NULL)
12924 {
12925 /* list argument, get next string */
12926 if (li == NULL)
12927 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012928 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012929 li = li->li_next;
12930 }
12931
12932 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012933 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012934 break;
12935 if (lnum <= curbuf->b_ml.ml_line_count)
12936 {
12937 /* existing line, replace it */
12938 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
12939 {
12940 changed_bytes(lnum, 0);
12941 check_cursor_col();
12942 rettv->vval.v_number = 0; /* OK */
12943 }
12944 }
12945 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
12946 {
12947 /* lnum is one past the last line, append the line */
12948 ++added;
12949 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
12950 rettv->vval.v_number = 0; /* OK */
12951 }
12952
12953 if (l == NULL) /* only one string argument */
12954 break;
12955 ++lnum;
12956 }
12957
12958 if (added > 0)
12959 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960}
12961
12962/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012963 * "setqflist()" function
12964 */
12965/*ARGSUSED*/
12966 static void
12967f_setqflist(argvars, rettv)
12968 typval_T *argvars;
12969 typval_T *rettv;
12970{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012971 char_u *act;
12972 int action = ' ';
12973
Bram Moolenaar2641f772005-03-25 21:58:17 +000012974 rettv->vval.v_number = -1;
12975
12976#ifdef FEAT_QUICKFIX
12977 if (argvars[0].v_type != VAR_LIST)
12978 EMSG(_(e_listreq));
12979 else
12980 {
12981 list_T *l = argvars[0].vval.v_list;
12982
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012983 if (argvars[1].v_type == VAR_STRING)
12984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 act = get_tv_string_chk(&argvars[1]);
12986 if (act == NULL)
12987 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012988 if (*act == 'a' || *act == 'r')
12989 action = *act;
12990 }
12991
12992 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012993 rettv->vval.v_number = 0;
12994 }
12995#endif
12996}
12997
12998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 * "setreg()" function
13000 */
13001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 typval_T *argvars;
13004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005{
13006 int regname;
13007 char_u *strregname;
13008 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013009 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 int append;
13011 char_u yank_type;
13012 long block_len;
13013
13014 block_len = -1;
13015 yank_type = MAUTO;
13016 append = FALSE;
13017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 if (strregname == NULL)
13022 return; /* type error; errmsg already given */
13023 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 if (regname == 0 || regname == '@')
13025 regname = '"';
13026 else if (regname == '=')
13027 return;
13028
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 stropt = get_tv_string_chk(&argvars[2]);
13032 if (stropt == NULL)
13033 return; /* type error */
13034 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 switch (*stropt)
13036 {
13037 case 'a': case 'A': /* append */
13038 append = TRUE;
13039 break;
13040 case 'v': case 'c': /* character-wise selection */
13041 yank_type = MCHAR;
13042 break;
13043 case 'V': case 'l': /* line-wise selection */
13044 yank_type = MLINE;
13045 break;
13046#ifdef FEAT_VISUAL
13047 case 'b': case Ctrl_V: /* block-wise selection */
13048 yank_type = MBLOCK;
13049 if (VIM_ISDIGIT(stropt[1]))
13050 {
13051 ++stropt;
13052 block_len = getdigits(&stropt) - 1;
13053 --stropt;
13054 }
13055 break;
13056#endif
13057 }
13058 }
13059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 strval = get_tv_string_chk(&argvars[1]);
13061 if (strval != NULL)
13062 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065}
13066
13067
13068/*
13069 * "setwinvar(expr)" function
13070 */
13071/*ARGSUSED*/
13072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013074 typval_T *argvars;
13075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076{
13077 win_T *win;
13078#ifdef FEAT_WINDOWS
13079 win_T *save_curwin;
13080#endif
13081 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013082 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 char_u nbuf[NUMBUFLEN];
13084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 rettv->vval.v_number = 0;
13086
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 if (check_restricted() || check_secure())
13088 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013090 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 varp = &argvars[2];
13092
13093 if (win != NULL && varname != NULL && varp != NULL)
13094 {
13095#ifdef FEAT_WINDOWS
13096 /* set curwin to be our win, temporarily */
13097 save_curwin = curwin;
13098 curwin = win;
13099 curbuf = curwin->w_buffer;
13100#endif
13101
13102 if (*varname == '&')
13103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013104 long numval;
13105 char_u *strval;
13106 int error = FALSE;
13107
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013109 numval = get_tv_number_chk(varp, &error);
13110 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013111 if (!error && strval != NULL)
13112 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 }
13114 else
13115 {
13116 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13117 if (winvarname != NULL)
13118 {
13119 STRCPY(winvarname, "w:");
13120 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013121 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 vim_free(winvarname);
13123 }
13124 }
13125
13126#ifdef FEAT_WINDOWS
13127 /* Restore current window, if it's still valid (autocomands can make
13128 * it invalid). */
13129 if (win_valid(save_curwin))
13130 {
13131 curwin = save_curwin;
13132 curbuf = curwin->w_buffer;
13133 }
13134#endif
13135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136}
13137
13138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 */
13141 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013143 typval_T *argvars;
13144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013146 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147
Bram Moolenaar0d660222005-01-07 21:51:51 +000013148 p = get_tv_string(&argvars[0]);
13149 rettv->vval.v_string = vim_strsave(p);
13150 simplify_filename(rettv->vval.v_string); /* simplify in place */
13151 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152}
13153
Bram Moolenaar0d660222005-01-07 21:51:51 +000013154static int
13155#ifdef __BORLANDC__
13156 _RTLENTRYF
13157#endif
13158 item_compare __ARGS((const void *s1, const void *s2));
13159static int
13160#ifdef __BORLANDC__
13161 _RTLENTRYF
13162#endif
13163 item_compare2 __ARGS((const void *s1, const void *s2));
13164
13165static int item_compare_ic;
13166static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013167static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013168#define ITEM_COMPARE_FAIL 999
13169
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013171 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013173 static int
13174#ifdef __BORLANDC__
13175_RTLENTRYF
13176#endif
13177item_compare(s1, s2)
13178 const void *s1;
13179 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013181 char_u *p1, *p2;
13182 char_u *tofree1, *tofree2;
13183 int res;
13184 char_u numbuf1[NUMBUFLEN];
13185 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13188 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013189 if (item_compare_ic)
13190 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192 res = STRCMP(p1, p2);
13193 vim_free(tofree1);
13194 vim_free(tofree2);
13195 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013199#ifdef __BORLANDC__
13200_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013202item_compare2(s1, s2)
13203 const void *s1;
13204 const void *s2;
13205{
13206 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T rettv;
13208 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 /* shortcut after failure in previous call; compare all items equal */
13212 if (item_compare_func_err)
13213 return 0;
13214
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013215 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13216 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013217 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13218 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013219
13220 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13221 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013222 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013223 clear_tv(&argv[0]);
13224 clear_tv(&argv[1]);
13225
13226 if (res == FAIL)
13227 res = ITEM_COMPARE_FAIL;
13228 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 /* return value has wrong type */
13230 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13231 if (item_compare_func_err)
13232 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013233 clear_tv(&rettv);
13234 return res;
13235}
13236
13237/*
13238 * "sort({list})" function
13239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013241f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244{
Bram Moolenaar33570922005-01-25 22:26:29 +000013245 list_T *l;
13246 listitem_T *li;
13247 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013248 long len;
13249 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
Bram Moolenaar0d660222005-01-07 21:51:51 +000013251 rettv->vval.v_number = 0;
13252 if (argvars[0].v_type != VAR_LIST)
13253 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 else
13255 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013257 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013258 return;
13259 rettv->vval.v_list = l;
13260 rettv->v_type = VAR_LIST;
13261 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262
Bram Moolenaar0d660222005-01-07 21:51:51 +000013263 len = list_len(l);
13264 if (len <= 1)
13265 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266
Bram Moolenaar0d660222005-01-07 21:51:51 +000013267 item_compare_ic = FALSE;
13268 item_compare_func = NULL;
13269 if (argvars[1].v_type != VAR_UNKNOWN)
13270 {
13271 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013273 else
13274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 int error = FALSE;
13276
13277 i = get_tv_number_chk(&argvars[1], &error);
13278 if (error)
13279 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013280 if (i == 1)
13281 item_compare_ic = TRUE;
13282 else
13283 item_compare_func = get_tv_string(&argvars[1]);
13284 }
13285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286
Bram Moolenaar0d660222005-01-07 21:51:51 +000013287 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013289 if (ptrs == NULL)
13290 return;
13291 i = 0;
13292 for (li = l->lv_first; li != NULL; li = li->li_next)
13293 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013295 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296 /* test the compare function */
13297 if (item_compare_func != NULL
13298 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13299 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013300 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013302 {
13303 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013305 item_compare_func == NULL ? item_compare : item_compare2);
13306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 if (!item_compare_func_err)
13308 {
13309 /* Clear the List and append the items in the sorted order. */
13310 l->lv_first = l->lv_last = NULL;
13311 l->lv_len = 0;
13312 for (i = 0; i < len; ++i)
13313 list_append(l, ptrs[i]);
13314 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013315 }
13316
13317 vim_free(ptrs);
13318 }
13319}
13320
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013321/*
13322 * "spellbadword()" function
13323 */
13324/* ARGSUSED */
13325 static void
13326f_spellbadword(argvars, rettv)
13327 typval_T *argvars;
13328 typval_T *rettv;
13329{
13330 int attr;
13331 char_u *ptr;
13332 int len;
13333
13334 rettv->vval.v_string = NULL;
13335 rettv->v_type = VAR_STRING;
13336
13337#ifdef FEAT_SYN_HL
13338 /* Find the start of the badly spelled word. */
13339 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13340 return;
13341
13342 /* Get the length of the word and copy it. */
13343 ptr = ml_get_cursor();
13344 len = spell_check(curwin, ptr, &attr);
13345 rettv->vval.v_string = vim_strnsave(ptr, len);
13346#endif
13347}
13348
13349/*
13350 * "spellsuggest()" function
13351 */
13352 static void
13353f_spellsuggest(argvars, rettv)
13354 typval_T *argvars;
13355 typval_T *rettv;
13356{
13357 char_u *str;
13358 int maxcount;
13359 garray_T ga;
13360 list_T *l;
13361 listitem_T *li;
13362 int i;
13363
13364 l = list_alloc();
13365 if (l == NULL)
13366 return;
13367 rettv->v_type = VAR_LIST;
13368 rettv->vval.v_list = l;
13369 ++l->lv_refcount;
13370
13371#ifdef FEAT_SYN_HL
13372 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13373 {
13374 str = get_tv_string(&argvars[0]);
13375 if (argvars[1].v_type != VAR_UNKNOWN)
13376 {
13377 maxcount = get_tv_number(&argvars[1]);
13378 if (maxcount <= 0)
13379 return;
13380 }
13381 else
13382 maxcount = 25;
13383
13384 spell_suggest_list(&ga, str, maxcount);
13385
13386 for (i = 0; i < ga.ga_len; ++i)
13387 {
13388 str = ((char_u **)ga.ga_data)[i];
13389
13390 li = listitem_alloc();
13391 if (li == NULL)
13392 vim_free(str);
13393 else
13394 {
13395 li->li_tv.v_type = VAR_STRING;
13396 li->li_tv.v_lock = 0;
13397 li->li_tv.vval.v_string = str;
13398 list_append(l, li);
13399 }
13400 }
13401 ga_clear(&ga);
13402 }
13403#endif
13404}
13405
Bram Moolenaar0d660222005-01-07 21:51:51 +000013406 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013407f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013408 typval_T *argvars;
13409 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013410{
13411 char_u *str;
13412 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013413 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013414 regmatch_T regmatch;
13415 char_u patbuf[NUMBUFLEN];
13416 char_u *save_cpo;
13417 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013418 listitem_T *ni;
13419 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013420 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013421 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013423
13424 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13425 save_cpo = p_cpo;
13426 p_cpo = (char_u *)"";
13427
13428 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013429 if (argvars[1].v_type != VAR_UNKNOWN)
13430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13432 if (pat == NULL)
13433 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013434 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013436 }
13437 if (pat == NULL || *pat == NUL)
13438 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439
13440 l = list_alloc();
13441 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443 rettv->v_type = VAR_LIST;
13444 rettv->vval.v_list = l;
13445 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013446 if (typeerr)
13447 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448
Bram Moolenaar0d660222005-01-07 21:51:51 +000013449 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13450 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013452 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013453 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013454 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013455 if (*str == NUL)
13456 match = FALSE; /* empty item at the end */
13457 else
13458 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013459 if (match)
13460 end = regmatch.startp[0];
13461 else
13462 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013463 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13464 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465 {
13466 ni = listitem_alloc();
13467 if (ni == NULL)
13468 break;
13469 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013470 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013471 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13472 list_append(l, ni);
13473 }
13474 if (!match)
13475 break;
13476 /* Advance to just after the match. */
13477 if (regmatch.endp[0] > str)
13478 col = 0;
13479 else
13480 {
13481 /* Don't get stuck at the same match. */
13482#ifdef FEAT_MBYTE
13483 col = mb_ptr2len_check(regmatch.endp[0]);
13484#else
13485 col = 1;
13486#endif
13487 }
13488 str = regmatch.endp[0];
13489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493
Bram Moolenaar0d660222005-01-07 21:51:51 +000013494 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495}
13496
13497#ifdef HAVE_STRFTIME
13498/*
13499 * "strftime({format}[, {time}])" function
13500 */
13501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *argvars;
13504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505{
13506 char_u result_buf[256];
13507 struct tm *curtime;
13508 time_t seconds;
13509 char_u *p;
13510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 seconds = time(NULL);
13516 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 curtime = localtime(&seconds);
13519 /* MSVC returns NULL for an invalid value of seconds. */
13520 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 else
13523 {
13524# ifdef FEAT_MBYTE
13525 vimconv_T conv;
13526 char_u *enc;
13527
13528 conv.vc_type = CONV_NONE;
13529 enc = enc_locale();
13530 convert_setup(&conv, p_enc, enc);
13531 if (conv.vc_type != CONV_NONE)
13532 p = string_convert(&conv, p, NULL);
13533# endif
13534 if (p != NULL)
13535 (void)strftime((char *)result_buf, sizeof(result_buf),
13536 (char *)p, curtime);
13537 else
13538 result_buf[0] = NUL;
13539
13540# ifdef FEAT_MBYTE
13541 if (conv.vc_type != CONV_NONE)
13542 vim_free(p);
13543 convert_setup(&conv, enc, p_enc);
13544 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 else
13547# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549
13550# ifdef FEAT_MBYTE
13551 /* Release conversion descriptors */
13552 convert_setup(&conv, NULL, NULL);
13553 vim_free(enc);
13554# endif
13555 }
13556}
13557#endif
13558
13559/*
13560 * "stridx()" function
13561 */
13562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *argvars;
13565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566{
13567 char_u buf[NUMBUFLEN];
13568 char_u *needle;
13569 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013570 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 needle = get_tv_string_chk(&argvars[1]);
13575 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013576 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013577 if (needle == NULL || haystack == NULL)
13578 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579
Bram Moolenaar33570922005-01-25 22:26:29 +000013580 if (argvars[2].v_type != VAR_UNKNOWN)
13581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 int error = FALSE;
13583
13584 start_idx = get_tv_number_chk(&argvars[2], &error);
13585 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013587 if (start_idx >= 0)
13588 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013589 }
13590
13591 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13592 if (pos != NULL)
13593 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594}
13595
13596/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013597 * "string()" function
13598 */
13599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *argvars;
13602 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013603{
13604 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013605 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013608 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013609 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611}
13612
13613/*
13614 * "strlen()" function
13615 */
13616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *argvars;
13619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621 rettv->vval.v_number = (varnumber_T)(STRLEN(
13622 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623}
13624
13625/*
13626 * "strpart()" function
13627 */
13628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013630 typval_T *argvars;
13631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632{
13633 char_u *p;
13634 int n;
13635 int len;
13636 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013637 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 slen = (int)STRLEN(p);
13641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 n = get_tv_number_chk(&argvars[1], &error);
13643 if (error)
13644 len = 0;
13645 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 else
13648 len = slen - n; /* default len: all bytes that are available. */
13649
13650 /*
13651 * Only return the overlap between the specified part and the actual
13652 * string.
13653 */
13654 if (n < 0)
13655 {
13656 len += n;
13657 n = 0;
13658 }
13659 else if (n > slen)
13660 n = slen;
13661 if (len < 0)
13662 len = 0;
13663 else if (n + len > slen)
13664 len = slen - n;
13665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->v_type = VAR_STRING;
13667 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668}
13669
13670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013671 * "strridx()" function
13672 */
13673 static void
13674f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013675 typval_T *argvars;
13676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013677{
13678 char_u buf[NUMBUFLEN];
13679 char_u *needle;
13680 char_u *haystack;
13681 char_u *rest;
13682 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013683 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 needle = get_tv_string_chk(&argvars[1]);
13686 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013687 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688
13689 rettv->vval.v_number = -1;
13690 if (needle == NULL || haystack == NULL)
13691 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013692 if (argvars[2].v_type != VAR_UNKNOWN)
13693 {
13694 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013696 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013698 }
13699 else
13700 end_idx = haystack_len;
13701
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013703 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013704 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013705 lastmatch = haystack + end_idx;
13706 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013707 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013708 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013709 for (rest = haystack; *rest != '\0'; ++rest)
13710 {
13711 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013712 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713 break;
13714 lastmatch = rest;
13715 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013716 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013717
13718 if (lastmatch == NULL)
13719 rettv->vval.v_number = -1;
13720 else
13721 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13722}
13723
13724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 * "strtrans()" function
13726 */
13727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 rettv->v_type = VAR_STRING;
13733 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734}
13735
13736/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013737 * "submatch()" function
13738 */
13739 static void
13740f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743{
13744 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745 rettv->vval.v_string =
13746 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747}
13748
13749/*
13750 * "substitute()" function
13751 */
13752 static void
13753f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *argvars;
13755 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013756{
13757 char_u patbuf[NUMBUFLEN];
13758 char_u subbuf[NUMBUFLEN];
13759 char_u flagsbuf[NUMBUFLEN];
13760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761 char_u *str = get_tv_string_chk(&argvars[0]);
13762 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13763 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13764 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13765
Bram Moolenaar0d660222005-01-07 21:51:51 +000013766 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13768 rettv->vval.v_string = NULL;
13769 else
13770 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771}
13772
13773/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013774 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 */
13776/*ARGSUSED*/
13777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *argvars;
13780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781{
13782 int id = 0;
13783#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013784 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 long col;
13786 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000013787 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789 lnum = get_tv_lnum(argvars); /* -1 on type error */
13790 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13791 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013793 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013794 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13795 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796#endif
13797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799}
13800
13801/*
13802 * "synIDattr(id, what [, mode])" function
13803 */
13804/*ARGSUSED*/
13805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
13810 char_u *p = NULL;
13811#ifdef FEAT_SYN_HL
13812 int id;
13813 char_u *what;
13814 char_u *mode;
13815 char_u modebuf[NUMBUFLEN];
13816 int modec;
13817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 id = get_tv_number(&argvars[0]);
13819 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 modec = TOLOWER_ASC(mode[0]);
13824 if (modec != 't' && modec != 'c'
13825#ifdef FEAT_GUI
13826 && modec != 'g'
13827#endif
13828 )
13829 modec = 0; /* replace invalid with current */
13830 }
13831 else
13832 {
13833#ifdef FEAT_GUI
13834 if (gui.in_use)
13835 modec = 'g';
13836 else
13837#endif
13838 if (t_colors > 1)
13839 modec = 'c';
13840 else
13841 modec = 't';
13842 }
13843
13844
13845 switch (TOLOWER_ASC(what[0]))
13846 {
13847 case 'b':
13848 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13849 p = highlight_color(id, what, modec);
13850 else /* bold */
13851 p = highlight_has_attr(id, HL_BOLD, modec);
13852 break;
13853
13854 case 'f': /* fg[#] */
13855 p = highlight_color(id, what, modec);
13856 break;
13857
13858 case 'i':
13859 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13860 p = highlight_has_attr(id, HL_INVERSE, modec);
13861 else /* italic */
13862 p = highlight_has_attr(id, HL_ITALIC, modec);
13863 break;
13864
13865 case 'n': /* name */
13866 p = get_highlight_name(NULL, id - 1);
13867 break;
13868
13869 case 'r': /* reverse */
13870 p = highlight_has_attr(id, HL_INVERSE, modec);
13871 break;
13872
13873 case 's': /* standout */
13874 p = highlight_has_attr(id, HL_STANDOUT, modec);
13875 break;
13876
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013877 case 'u':
13878 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13879 /* underline */
13880 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13881 else
13882 /* undercurl */
13883 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 break;
13885 }
13886
13887 if (p != NULL)
13888 p = vim_strsave(p);
13889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->v_type = VAR_STRING;
13891 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892}
13893
13894/*
13895 * "synIDtrans(id)" function
13896 */
13897/*ARGSUSED*/
13898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013899f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013900 typval_T *argvars;
13901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902{
13903 int id;
13904
13905#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
13908 if (id > 0)
13909 id = syn_get_final_id(id);
13910 else
13911#endif
13912 id = 0;
13913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013914 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915}
13916
13917/*
13918 * "system()" function
13919 */
13920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 typval_T *argvars;
13923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013925 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013927 char_u *infile = NULL;
13928 char_u buf[NUMBUFLEN];
13929 int err = FALSE;
13930 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013932 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013933 {
13934 /*
13935 * Write the string to a temp file, to be used for input of the shell
13936 * command.
13937 */
13938 if ((infile = vim_tempname('i')) == NULL)
13939 {
13940 EMSG(_(e_notmp));
13941 return;
13942 }
13943
13944 fd = mch_fopen((char *)infile, WRITEBIN);
13945 if (fd == NULL)
13946 {
13947 EMSG2(_(e_notopen), infile);
13948 goto done;
13949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 p = get_tv_string_buf_chk(&argvars[1], buf);
13951 if (p == NULL)
13952 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013953 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13954 err = TRUE;
13955 if (fclose(fd) != 0)
13956 err = TRUE;
13957 if (err)
13958 {
13959 EMSG(_("E677: Error writing temp file"));
13960 goto done;
13961 }
13962 }
13963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013965
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966#ifdef USE_CR
13967 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013968 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 {
13970 char_u *s;
13971
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013972 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 {
13974 if (*s == CAR)
13975 *s = NL;
13976 }
13977 }
13978#else
13979# ifdef USE_CRNL
13980 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013981 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 {
13983 char_u *s, *d;
13984
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013985 d = res;
13986 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 {
13988 if (s[0] == CAR && s[1] == NL)
13989 ++s;
13990 *d++ = *s;
13991 }
13992 *d = NUL;
13993 }
13994# endif
13995#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013996
13997done:
13998 if (infile != NULL)
13999 {
14000 mch_remove(infile);
14001 vim_free(infile);
14002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 rettv->v_type = VAR_STRING;
14004 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005}
14006
14007/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014008 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014009 */
14010 static void
14011f_taglist(argvars, rettv)
14012 typval_T *argvars;
14013 typval_T *rettv;
14014{
14015 char_u *tag_pattern;
14016 list_T *l;
14017
14018 tag_pattern = get_tv_string(&argvars[0]);
14019
14020 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 if (*tag_pattern == NUL)
14022 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014023
14024 l = list_alloc();
14025 if (l != NULL)
14026 {
14027 if (get_tags(l, tag_pattern) != FAIL)
14028 {
14029 rettv->vval.v_list = l;
14030 rettv->v_type = VAR_LIST;
14031 ++l->lv_refcount;
14032 }
14033 else
14034 list_free(l);
14035 }
14036}
14037
14038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 * "tempname()" function
14040 */
14041/*ARGSUSED*/
14042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014044 typval_T *argvars;
14045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046{
14047 static int x = 'A';
14048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049 rettv->v_type = VAR_STRING;
14050 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051
14052 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14053 * names. Skip 'I' and 'O', they are used for shell redirection. */
14054 do
14055 {
14056 if (x == 'Z')
14057 x = '0';
14058 else if (x == '9')
14059 x = 'A';
14060 else
14061 {
14062#ifdef EBCDIC
14063 if (x == 'I')
14064 x = 'J';
14065 else if (x == 'R')
14066 x = 'S';
14067 else
14068#endif
14069 ++x;
14070 }
14071 } while (x == 'I' || x == 'O');
14072}
14073
14074/*
14075 * "tolower(string)" function
14076 */
14077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014079 typval_T *argvars;
14080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081{
14082 char_u *p;
14083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084 p = vim_strsave(get_tv_string(&argvars[0]));
14085 rettv->v_type = VAR_STRING;
14086 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087
14088 if (p != NULL)
14089 while (*p != NUL)
14090 {
14091#ifdef FEAT_MBYTE
14092 int l;
14093
14094 if (enc_utf8)
14095 {
14096 int c, lc;
14097
14098 c = utf_ptr2char(p);
14099 lc = utf_tolower(c);
14100 l = utf_ptr2len_check(p);
14101 /* TODO: reallocate string when byte count changes. */
14102 if (utf_char2len(lc) == l)
14103 utf_char2bytes(lc, p);
14104 p += l;
14105 }
14106 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14107 p += l; /* skip multi-byte character */
14108 else
14109#endif
14110 {
14111 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14112 ++p;
14113 }
14114 }
14115}
14116
14117/*
14118 * "toupper(string)" function
14119 */
14120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014121f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 typval_T *argvars;
14123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
14129/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014130 * "tr(string, fromstr, tostr)" function
14131 */
14132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014136{
14137 char_u *instr;
14138 char_u *fromstr;
14139 char_u *tostr;
14140 char_u *p;
14141#ifdef FEAT_MBYTE
14142 int inlen;
14143 int fromlen;
14144 int tolen;
14145 int idx;
14146 char_u *cpstr;
14147 int cplen;
14148 int first = TRUE;
14149#endif
14150 char_u buf[NUMBUFLEN];
14151 char_u buf2[NUMBUFLEN];
14152 garray_T ga;
14153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14156 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014157
14158 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 rettv->v_type = VAR_STRING;
14160 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014161 if (fromstr == NULL || tostr == NULL)
14162 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014163 ga_init2(&ga, (int)sizeof(char), 80);
14164
14165#ifdef FEAT_MBYTE
14166 if (!has_mbyte)
14167#endif
14168 /* not multi-byte: fromstr and tostr must be the same length */
14169 if (STRLEN(fromstr) != STRLEN(tostr))
14170 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014171#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014172error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014173#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014174 EMSG2(_(e_invarg2), fromstr);
14175 ga_clear(&ga);
14176 return;
14177 }
14178
14179 /* fromstr and tostr have to contain the same number of chars */
14180 while (*instr != NUL)
14181 {
14182#ifdef FEAT_MBYTE
14183 if (has_mbyte)
14184 {
14185 inlen = mb_ptr2len_check(instr);
14186 cpstr = instr;
14187 cplen = inlen;
14188 idx = 0;
14189 for (p = fromstr; *p != NUL; p += fromlen)
14190 {
14191 fromlen = mb_ptr2len_check(p);
14192 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14193 {
14194 for (p = tostr; *p != NUL; p += tolen)
14195 {
14196 tolen = mb_ptr2len_check(p);
14197 if (idx-- == 0)
14198 {
14199 cplen = tolen;
14200 cpstr = p;
14201 break;
14202 }
14203 }
14204 if (*p == NUL) /* tostr is shorter than fromstr */
14205 goto error;
14206 break;
14207 }
14208 ++idx;
14209 }
14210
14211 if (first && cpstr == instr)
14212 {
14213 /* Check that fromstr and tostr have the same number of
14214 * (multi-byte) characters. Done only once when a character
14215 * of instr doesn't appear in fromstr. */
14216 first = FALSE;
14217 for (p = tostr; *p != NUL; p += tolen)
14218 {
14219 tolen = mb_ptr2len_check(p);
14220 --idx;
14221 }
14222 if (idx != 0)
14223 goto error;
14224 }
14225
14226 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014227 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014228 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014229
14230 instr += inlen;
14231 }
14232 else
14233#endif
14234 {
14235 /* When not using multi-byte chars we can do it faster. */
14236 p = vim_strchr(fromstr, *instr);
14237 if (p != NULL)
14238 ga_append(&ga, tostr[p - fromstr]);
14239 else
14240 ga_append(&ga, *instr);
14241 ++instr;
14242 }
14243 }
14244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014246}
14247
14248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 * "type(expr)" function
14250 */
14251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014253 typval_T *argvars;
14254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014256 int n;
14257
14258 switch (argvars[0].v_type)
14259 {
14260 case VAR_NUMBER: n = 0; break;
14261 case VAR_STRING: n = 1; break;
14262 case VAR_FUNC: n = 2; break;
14263 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014264 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014265 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14266 }
14267 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268}
14269
14270/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014271 * "values(dict)" function
14272 */
14273 static void
14274f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014275 typval_T *argvars;
14276 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014277{
14278 dict_list(argvars, rettv, 1);
14279}
14280
14281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 * "virtcol(string)" function
14283 */
14284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *argvars;
14287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288{
14289 colnr_T vcol = 0;
14290 pos_T *fp;
14291
14292 fp = var2fpos(&argvars[0], FALSE);
14293 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14294 {
14295 getvvcol(curwin, fp, NULL, NULL, &vcol);
14296 ++vcol;
14297 }
14298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300}
14301
14302/*
14303 * "visualmode()" function
14304 */
14305/*ARGSUSED*/
14306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014308 typval_T *argvars;
14309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310{
14311#ifdef FEAT_VISUAL
14312 char_u str[2];
14313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 str[0] = curbuf->b_visual_mode_eval;
14316 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318
14319 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014320 if ((argvars[0].v_type == VAR_NUMBER
14321 && argvars[0].vval.v_number != 0)
14322 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014323 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 curbuf->b_visual_mode_eval = NUL;
14325#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014326 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327#endif
14328}
14329
14330/*
14331 * "winbufnr(nr)" function
14332 */
14333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 typval_T *argvars;
14336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
14338 win_T *wp;
14339
14340 wp = find_win_by_nr(&argvars[0]);
14341 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014344 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345}
14346
14347/*
14348 * "wincol()" function
14349 */
14350/*ARGSUSED*/
14351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 typval_T *argvars;
14354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355{
14356 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358}
14359
14360/*
14361 * "winheight(nr)" function
14362 */
14363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014365 typval_T *argvars;
14366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367{
14368 win_T *wp;
14369
14370 wp = find_win_by_nr(&argvars[0]);
14371 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375}
14376
14377/*
14378 * "winline()" function
14379 */
14380/*ARGSUSED*/
14381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T *argvars;
14384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385{
14386 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388}
14389
14390/*
14391 * "winnr()" function
14392 */
14393/* ARGSUSED */
14394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014396 typval_T *argvars;
14397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398{
14399 int nr = 1;
14400#ifdef FEAT_WINDOWS
14401 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014402 win_T *twin = curwin;
14403 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014405 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 arg = get_tv_string_chk(&argvars[0]);
14408 if (arg == NULL)
14409 nr = 0; /* type error; errmsg already given */
14410 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014411 twin = lastwin;
14412 else if (STRCMP(arg, "#") == 0)
14413 {
14414 twin = prevwin;
14415 if (prevwin == NULL)
14416 nr = 0;
14417 }
14418 else
14419 {
14420 EMSG2(_(e_invexpr2), arg);
14421 nr = 0;
14422 }
14423 }
14424
14425 if (nr > 0)
14426 for (wp = firstwin; wp != twin; wp = wp->w_next)
14427 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430}
14431
14432/*
14433 * "winrestcmd()" function
14434 */
14435/* ARGSUSED */
14436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 typval_T *argvars;
14439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440{
14441#ifdef FEAT_WINDOWS
14442 win_T *wp;
14443 int winnr = 1;
14444 garray_T ga;
14445 char_u buf[50];
14446
14447 ga_init2(&ga, (int)sizeof(char), 70);
14448 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14449 {
14450 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14451 ga_concat(&ga, buf);
14452# ifdef FEAT_VERTSPLIT
14453 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14454 ga_concat(&ga, buf);
14455# endif
14456 ++winnr;
14457 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014458 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
14467/*
14468 * "winwidth(nr)" function
14469 */
14470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 typval_T *argvars;
14473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474{
14475 win_T *wp;
14476
14477 wp = find_win_by_nr(&argvars[0]);
14478 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 else
14481#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485#endif
14486}
14487
14488 static win_T *
14489find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014490 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491{
14492#ifdef FEAT_WINDOWS
14493 win_T *wp;
14494#endif
14495 int nr;
14496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 nr = get_tv_number_chk(vp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498
14499#ifdef FEAT_WINDOWS
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 if (nr < 0)
14501 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 if (nr == 0)
14503 return curwin;
14504
14505 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14506 if (--nr <= 0)
14507 break;
14508 return wp;
14509#else
14510 if (nr == 0 || nr == 1)
14511 return curwin;
14512 return NULL;
14513#endif
14514}
14515
14516/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014517 * "writefile()" function
14518 */
14519 static void
14520f_writefile(argvars, rettv)
14521 typval_T *argvars;
14522 typval_T *rettv;
14523{
14524 int binary = FALSE;
14525 char_u *fname;
14526 FILE *fd;
14527 listitem_T *li;
14528 char_u *s;
14529 int ret = 0;
14530 int c;
14531
14532 if (argvars[0].v_type != VAR_LIST)
14533 {
14534 EMSG2(_(e_listarg), "writefile()");
14535 return;
14536 }
14537 if (argvars[0].vval.v_list == NULL)
14538 return;
14539
14540 if (argvars[2].v_type != VAR_UNKNOWN
14541 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14542 binary = TRUE;
14543
14544 /* Always open the file in binary mode, library functions have a mind of
14545 * their own about CR-LF conversion. */
14546 fname = get_tv_string(&argvars[1]);
14547 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14548 {
14549 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14550 ret = -1;
14551 }
14552 else
14553 {
14554 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14555 li = li->li_next)
14556 {
14557 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14558 {
14559 if (*s == '\n')
14560 c = putc(NUL, fd);
14561 else
14562 c = putc(*s, fd);
14563 if (c == EOF)
14564 {
14565 ret = -1;
14566 break;
14567 }
14568 }
14569 if (!binary || li->li_next != NULL)
14570 if (putc('\n', fd) == EOF)
14571 {
14572 ret = -1;
14573 break;
14574 }
14575 if (ret < 0)
14576 {
14577 EMSG(_(e_write));
14578 break;
14579 }
14580 }
14581 fclose(fd);
14582 }
14583
14584 rettv->vval.v_number = ret;
14585}
14586
14587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 * Translate a String variable into a position.
14589 */
14590 static pos_T *
14591var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 int lnum; /* TRUE when $ is last line */
14594{
14595 char_u *name;
14596 static pos_T pos;
14597 pos_T *pp;
14598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 name = get_tv_string_chk(varp);
14600 if (name == NULL)
14601 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 if (name[0] == '.') /* cursor */
14603 return &curwin->w_cursor;
14604 if (name[0] == '\'') /* mark */
14605 {
14606 pp = getmark(name[1], FALSE);
14607 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14608 return NULL;
14609 return pp;
14610 }
14611 if (name[0] == '$') /* last column or line */
14612 {
14613 if (lnum)
14614 {
14615 pos.lnum = curbuf->b_ml.ml_line_count;
14616 pos.col = 0;
14617 }
14618 else
14619 {
14620 pos.lnum = curwin->w_cursor.lnum;
14621 pos.col = (colnr_T)STRLEN(ml_get_curline());
14622 }
14623 return &pos;
14624 }
14625 return NULL;
14626}
14627
14628/*
14629 * Get the length of an environment variable name.
14630 * Advance "arg" to the first character after the name.
14631 * Return 0 for error.
14632 */
14633 static int
14634get_env_len(arg)
14635 char_u **arg;
14636{
14637 char_u *p;
14638 int len;
14639
14640 for (p = *arg; vim_isIDc(*p); ++p)
14641 ;
14642 if (p == *arg) /* no name found */
14643 return 0;
14644
14645 len = (int)(p - *arg);
14646 *arg = p;
14647 return len;
14648}
14649
14650/*
14651 * Get the length of the name of a function or internal variable.
14652 * "arg" is advanced to the first non-white character after the name.
14653 * Return 0 if something is wrong.
14654 */
14655 static int
14656get_id_len(arg)
14657 char_u **arg;
14658{
14659 char_u *p;
14660 int len;
14661
14662 /* Find the end of the name. */
14663 for (p = *arg; eval_isnamec(*p); ++p)
14664 ;
14665 if (p == *arg) /* no name found */
14666 return 0;
14667
14668 len = (int)(p - *arg);
14669 *arg = skipwhite(p);
14670
14671 return len;
14672}
14673
14674/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014675 * Get the length of the name of a variable or function.
14676 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 * Return -1 if curly braces expansion failed.
14679 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 * If the name contains 'magic' {}'s, expand them and return the
14681 * expanded name in an allocated string via 'alias' - caller must free.
14682 */
14683 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014684get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685 char_u **arg;
14686 char_u **alias;
14687 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014688 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689{
14690 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 char_u *p;
14692 char_u *expr_start;
14693 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694
14695 *alias = NULL; /* default to no alias */
14696
14697 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14698 && (*arg)[2] == (int)KE_SNR)
14699 {
14700 /* hard coded <SNR>, already translated */
14701 *arg += 3;
14702 return get_id_len(arg) + 3;
14703 }
14704 len = eval_fname_script(*arg);
14705 if (len > 0)
14706 {
14707 /* literal "<SID>", "s:" or "<SNR>" */
14708 *arg += len;
14709 }
14710
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014714 p = find_name_end(*arg, &expr_start, &expr_end,
14715 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 if (expr_start != NULL)
14717 {
14718 char_u *temp_string;
14719
14720 if (!evaluate)
14721 {
14722 len += (int)(p - *arg);
14723 *arg = skipwhite(p);
14724 return len;
14725 }
14726
14727 /*
14728 * Include any <SID> etc in the expanded string:
14729 * Thus the -len here.
14730 */
14731 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14732 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014733 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 *alias = temp_string;
14735 *arg = skipwhite(p);
14736 return (int)STRLEN(temp_string);
14737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738
14739 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014740 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 EMSG2(_(e_invexpr2), *arg);
14742
14743 return len;
14744}
14745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746/*
14747 * Find the end of a variable or function name, taking care of magic braces.
14748 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14749 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014750 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 * Return a pointer to just after the name. Equal to "arg" if there is no
14752 * valid name.
14753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014755find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 char_u *arg;
14757 char_u **expr_start;
14758 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014759 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761 int mb_nest = 0;
14762 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 char_u *p;
14764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014765 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014767 *expr_start = NULL;
14768 *expr_end = NULL;
14769 }
14770
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014771 /* Quick check for valid starting character. */
14772 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14773 return arg;
14774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 for (p = arg; *p != NUL
14776 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014777 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014778 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 || mb_nest != 0
14780 || br_nest != 0); ++p)
14781 {
14782 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 if (*p == '[')
14785 ++br_nest;
14786 else if (*p == ']')
14787 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 if (*p == '{')
14792 {
14793 mb_nest++;
14794 if (expr_start != NULL && *expr_start == NULL)
14795 *expr_start = p;
14796 }
14797 else if (*p == '}')
14798 {
14799 mb_nest--;
14800 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14801 *expr_end = p;
14802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804 }
14805
14806 return p;
14807}
14808
14809/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014810 * Expands out the 'magic' {}'s in a variable/function name.
14811 * Note that this can call itself recursively, to deal with
14812 * constructs like foo{bar}{baz}{bam}
14813 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14814 * "in_start" ^
14815 * "expr_start" ^
14816 * "expr_end" ^
14817 * "in_end" ^
14818 *
14819 * Returns a new allocated string, which the caller must free.
14820 * Returns NULL for failure.
14821 */
14822 static char_u *
14823make_expanded_name(in_start, expr_start, expr_end, in_end)
14824 char_u *in_start;
14825 char_u *expr_start;
14826 char_u *expr_end;
14827 char_u *in_end;
14828{
14829 char_u c1;
14830 char_u *retval = NULL;
14831 char_u *temp_result;
14832 char_u *nextcmd = NULL;
14833
14834 if (expr_end == NULL || in_end == NULL)
14835 return NULL;
14836 *expr_start = NUL;
14837 *expr_end = NUL;
14838 c1 = *in_end;
14839 *in_end = NUL;
14840
14841 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14842 if (temp_result != NULL && nextcmd == NULL)
14843 {
14844 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14845 + (in_end - expr_end) + 1));
14846 if (retval != NULL)
14847 {
14848 STRCPY(retval, in_start);
14849 STRCAT(retval, temp_result);
14850 STRCAT(retval, expr_end + 1);
14851 }
14852 }
14853 vim_free(temp_result);
14854
14855 *in_end = c1; /* put char back for error messages */
14856 *expr_start = '{';
14857 *expr_end = '}';
14858
14859 if (retval != NULL)
14860 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014861 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862 if (expr_start != NULL)
14863 {
14864 /* Further expansion! */
14865 temp_result = make_expanded_name(retval, expr_start,
14866 expr_end, temp_result);
14867 vim_free(retval);
14868 retval = temp_result;
14869 }
14870 }
14871
14872 return retval;
14873}
14874
14875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014877 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 */
14879 static int
14880eval_isnamec(c)
14881 int c;
14882{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014883 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14884}
14885
14886/*
14887 * Return TRUE if character "c" can be used as the first character in a
14888 * variable or function name (excluding '{' and '}').
14889 */
14890 static int
14891eval_isnamec1(c)
14892 int c;
14893{
14894 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895}
14896
14897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 * Set number v: variable to "val".
14899 */
14900 void
14901set_vim_var_nr(idx, val)
14902 int idx;
14903 long val;
14904{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014905 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906}
14907
14908/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014909 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 */
14911 long
14912get_vim_var_nr(idx)
14913 int idx;
14914{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014915 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916}
14917
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014918#if defined(FEAT_AUTOCMD) || defined(PROTO)
14919/*
14920 * Get string v: variable value. Uses a static buffer, can only be used once.
14921 */
14922 char_u *
14923get_vim_var_str(idx)
14924 int idx;
14925{
14926 return get_tv_string(&vimvars[idx].vv_tv);
14927}
14928#endif
14929
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930/*
14931 * Set v:count, v:count1 and v:prevcount.
14932 */
14933 void
14934set_vcount(count, count1)
14935 long count;
14936 long count1;
14937{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014938 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14939 vimvars[VV_COUNT].vv_nr = count;
14940 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941}
14942
14943/*
14944 * Set string v: variable to a copy of "val".
14945 */
14946 void
14947set_vim_var_string(idx, val, len)
14948 int idx;
14949 char_u *val;
14950 int len; /* length of "val" to use or -1 (whole string) */
14951{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014952 /* Need to do this (at least) once, since we can't initialize a union.
14953 * Will always be invoked when "v:progname" is set. */
14954 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14955
Bram Moolenaare9a41262005-01-15 22:18:47 +000014956 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014958 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014960 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014962 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963}
14964
14965/*
14966 * Set v:register if needed.
14967 */
14968 void
14969set_reg_var(c)
14970 int c;
14971{
14972 char_u regname;
14973
14974 if (c == 0 || c == ' ')
14975 regname = '"';
14976 else
14977 regname = c;
14978 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014979 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 set_vim_var_string(VV_REG, &regname, 1);
14981}
14982
14983/*
14984 * Get or set v:exception. If "oldval" == NULL, return the current value.
14985 * Otherwise, restore the value to "oldval" and return NULL.
14986 * Must always be called in pairs to save and restore v:exception! Does not
14987 * take care of memory allocations.
14988 */
14989 char_u *
14990v_exception(oldval)
14991 char_u *oldval;
14992{
14993 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014994 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
Bram Moolenaare9a41262005-01-15 22:18:47 +000014996 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 return NULL;
14998}
14999
15000/*
15001 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15002 * Otherwise, restore the value to "oldval" and return NULL.
15003 * Must always be called in pairs to save and restore v:throwpoint! Does not
15004 * take care of memory allocations.
15005 */
15006 char_u *
15007v_throwpoint(oldval)
15008 char_u *oldval;
15009{
15010 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015011 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012
Bram Moolenaare9a41262005-01-15 22:18:47 +000015013 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 return NULL;
15015}
15016
15017#if defined(FEAT_AUTOCMD) || defined(PROTO)
15018/*
15019 * Set v:cmdarg.
15020 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15021 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15022 * Must always be called in pairs!
15023 */
15024 char_u *
15025set_cmdarg(eap, oldarg)
15026 exarg_T *eap;
15027 char_u *oldarg;
15028{
15029 char_u *oldval;
15030 char_u *newval;
15031 unsigned len;
15032
Bram Moolenaare9a41262005-01-15 22:18:47 +000015033 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015034 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015036 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015037 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015038 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 }
15040
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015041 if (eap->force_bin == FORCE_BIN)
15042 len = 6;
15043 else if (eap->force_bin == FORCE_NOBIN)
15044 len = 8;
15045 else
15046 len = 0;
15047 if (eap->force_ff != 0)
15048 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15049# ifdef FEAT_MBYTE
15050 if (eap->force_enc != 0)
15051 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15052# endif
15053
15054 newval = alloc(len + 1);
15055 if (newval == NULL)
15056 return NULL;
15057
15058 if (eap->force_bin == FORCE_BIN)
15059 sprintf((char *)newval, " ++bin");
15060 else if (eap->force_bin == FORCE_NOBIN)
15061 sprintf((char *)newval, " ++nobin");
15062 else
15063 *newval = NUL;
15064 if (eap->force_ff != 0)
15065 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15066 eap->cmd + eap->force_ff);
15067# ifdef FEAT_MBYTE
15068 if (eap->force_enc != 0)
15069 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15070 eap->cmd + eap->force_enc);
15071# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015072 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015073 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074}
15075#endif
15076
15077/*
15078 * Get the value of internal variable "name".
15079 * Return OK or FAIL.
15080 */
15081 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015082get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 char_u *name;
15084 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015086 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087{
15088 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015089 typval_T *tv = NULL;
15090 typval_T atv;
15091 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
15094 /* truncate the name, so that we can use strcmp() */
15095 cc = name[len];
15096 name[len] = NUL;
15097
15098 /*
15099 * Check for "b:changedtick".
15100 */
15101 if (STRCMP(name, "b:changedtick") == 0)
15102 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015103 atv.v_type = VAR_NUMBER;
15104 atv.vval.v_number = curbuf->b_changedtick;
15105 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 }
15107
15108 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 * Check for user-defined variables.
15110 */
15111 else
15112 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015113 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 }
15117
Bram Moolenaare9a41262005-01-15 22:18:47 +000015118 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015120 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 ret = FAIL;
15123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015125 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126
15127 name[len] = cc;
15128
15129 return ret;
15130}
15131
15132/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015133 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15134 * Also handle function call with Funcref variable: func(expr)
15135 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15136 */
15137 static int
15138handle_subscript(arg, rettv, evaluate, verbose)
15139 char_u **arg;
15140 typval_T *rettv;
15141 int evaluate; /* do more than finding the end */
15142 int verbose; /* give error messages */
15143{
15144 int ret = OK;
15145 dict_T *selfdict = NULL;
15146 char_u *s;
15147 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015148 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015149
15150 while (ret == OK
15151 && (**arg == '['
15152 || (**arg == '.' && rettv->v_type == VAR_DICT)
15153 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15154 && !vim_iswhite(*(*arg - 1)))
15155 {
15156 if (**arg == '(')
15157 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015158 /* need to copy the funcref so that we can clear rettv */
15159 functv = *rettv;
15160 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015161
15162 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015163 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015164 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15166 &len, evaluate, selfdict);
15167
15168 /* Clear the funcref afterwards, so that deleting it while
15169 * evaluating the arguments is possible (see test55). */
15170 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015171
15172 /* Stop the expression evaluation when immediately aborting on
15173 * error, or when an interrupt occurred or an exception was thrown
15174 * but not caught. */
15175 if (aborting())
15176 {
15177 if (ret == OK)
15178 clear_tv(rettv);
15179 ret = FAIL;
15180 }
15181 dict_unref(selfdict);
15182 selfdict = NULL;
15183 }
15184 else /* **arg == '[' || **arg == '.' */
15185 {
15186 dict_unref(selfdict);
15187 if (rettv->v_type == VAR_DICT)
15188 {
15189 selfdict = rettv->vval.v_dict;
15190 if (selfdict != NULL)
15191 ++selfdict->dv_refcount;
15192 }
15193 else
15194 selfdict = NULL;
15195 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15196 {
15197 clear_tv(rettv);
15198 ret = FAIL;
15199 }
15200 }
15201 }
15202 dict_unref(selfdict);
15203 return ret;
15204}
15205
15206/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15208 * value).
15209 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015211alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015212{
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015214}
15215
15216/*
15217 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 * The string "s" must have been allocated, it is consumed.
15219 * Return NULL for out of memory, the variable otherwise.
15220 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015221 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 char_u *s;
15224{
Bram Moolenaar33570922005-01-25 22:26:29 +000015225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015227 rettv = alloc_tv();
15228 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015230 rettv->v_type = VAR_STRING;
15231 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 }
15233 else
15234 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015235 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236}
15237
15238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 */
15241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015243 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244{
15245 if (varp != NULL)
15246 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015247 switch (varp->v_type)
15248 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015249 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015250 func_unref(varp->vval.v_string);
15251 /*FALLTHROUGH*/
15252 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015253 vim_free(varp->vval.v_string);
15254 break;
15255 case VAR_LIST:
15256 list_unref(varp->vval.v_list);
15257 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015258 case VAR_DICT:
15259 dict_unref(varp->vval.v_dict);
15260 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015261 case VAR_NUMBER:
15262 case VAR_UNKNOWN:
15263 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015264 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015265 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015266 break;
15267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 vim_free(varp);
15269 }
15270}
15271
15272/*
15273 * Free the memory for a variable value and set the value to NULL or 0.
15274 */
15275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015277 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278{
15279 if (varp != NULL)
15280 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015281 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015283 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015284 func_unref(varp->vval.v_string);
15285 /*FALLTHROUGH*/
15286 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015287 vim_free(varp->vval.v_string);
15288 varp->vval.v_string = NULL;
15289 break;
15290 case VAR_LIST:
15291 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015292 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015293 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015294 case VAR_DICT:
15295 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015296 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015297 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015298 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015299 varp->vval.v_number = 0;
15300 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 case VAR_UNKNOWN:
15302 break;
15303 default:
15304 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015306 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 }
15308}
15309
15310/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 * Set the value of a variable to NULL without freeing items.
15312 */
15313 static void
15314init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015315 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316{
15317 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319}
15320
15321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 * Get the number value of a variable.
15323 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 * For incompatible types, return 0.
15325 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15326 * caller of incompatible types: it sets *denote to TRUE if "denote"
15327 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 */
15329 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 int error = FALSE;
15334
15335 return get_tv_number_chk(varp, &error); /* return 0L on error */
15336}
15337
15338 static long
15339get_tv_number_chk(varp, denote)
15340 typval_T *varp;
15341 int *denote;
15342{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015343 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015345 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015347 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015349 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015350 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015351 break;
15352 case VAR_STRING:
15353 if (varp->vval.v_string != NULL)
15354 vim_str2nr(varp->vval.v_string, NULL, NULL,
15355 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015356 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015357 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015358 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015359 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015360 case VAR_DICT:
15361 EMSG(_("E728: Using a Dictionary as a number"));
15362 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015363 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015364 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015365 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015367 if (denote == NULL) /* useful for values that must be unsigned */
15368 n = -1;
15369 else
15370 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015371 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372}
15373
15374/*
15375 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015376 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 */
15378 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015379get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015380 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381{
Bram Moolenaar33570922005-01-25 22:26:29 +000015382 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 linenr_T lnum;
15384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015385 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 if (lnum == 0) /* no valid number, try using line() */
15387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv.v_type = VAR_NUMBER;
15389 f_line(argvars, &rettv);
15390 lnum = rettv.vval.v_number;
15391 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 }
15393 return lnum;
15394}
15395
15396/*
15397 * Get the string value of a variable.
15398 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015399 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15400 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 * If the String variable has never been set, return an empty string.
15402 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015403 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15404 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 */
15406 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015408 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409{
15410 static char_u mybuf[NUMBUFLEN];
15411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413}
15414
15415 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015417 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015420 char_u *res = get_tv_string_buf_chk(varp, buf);
15421
15422 return res != NULL ? res : (char_u *)"";
15423}
15424
15425 static char_u *
15426get_tv_string_chk(varp)
15427 typval_T *varp;
15428{
15429 static char_u mybuf[NUMBUFLEN];
15430
15431 return get_tv_string_buf_chk(varp, mybuf);
15432}
15433
15434 static char_u *
15435get_tv_string_buf_chk(varp, buf)
15436 typval_T *varp;
15437 char_u *buf;
15438{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015439 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015441 case VAR_NUMBER:
15442 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15443 return buf;
15444 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015445 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015446 break;
15447 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015448 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015449 break;
15450 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015451 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015452 break;
15453 case VAR_STRING:
15454 if (varp->vval.v_string != NULL)
15455 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015456 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015457 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015461 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462}
15463
15464/*
15465 * Find variable "name" in the list of variables.
15466 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015467 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015468 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015469 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015471 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015472find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015474 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015477 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478
Bram Moolenaara7043832005-01-21 11:56:39 +000015479 ht = find_var_ht(name, &varname);
15480 if (htp != NULL)
15481 *htp = ht;
15482 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015484 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485}
15486
15487/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015488 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015489 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015491 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015492find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015493 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015494 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015495 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015496{
Bram Moolenaar33570922005-01-25 22:26:29 +000015497 hashitem_T *hi;
15498
15499 if (*varname == NUL)
15500 {
15501 /* Must be something like "s:", otherwise "ht" would be NULL. */
15502 switch (varname[-2])
15503 {
15504 case 's': return &SCRIPT_SV(current_SID).sv_var;
15505 case 'g': return &globvars_var;
15506 case 'v': return &vimvars_var;
15507 case 'b': return &curbuf->b_bufvar;
15508 case 'w': return &curwin->w_winvar;
15509 case 'l': return &current_funccal->l_vars_var;
15510 case 'a': return &current_funccal->l_avars_var;
15511 }
15512 return NULL;
15513 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015514
15515 hi = hash_find(ht, varname);
15516 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015517 {
15518 /* For global variables we may try auto-loading the script. If it
15519 * worked find the variable again. */
15520 if (ht == &globvarht && !writing
15521 && script_autoload(varname) && !aborting())
15522 hi = hash_find(ht, varname);
15523 if (HASHITEM_EMPTY(hi))
15524 return NULL;
15525 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015526 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015527}
15528
15529/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015531 * Set "varname" to the start of name without ':'.
15532 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015533 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015534find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 char_u *name;
15536 char_u **varname;
15537{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015538 hashitem_T *hi;
15539
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 if (name[1] != ':')
15541 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015542 /* The name must not start with a colon or #. */
15543 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 return NULL;
15545 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015546
15547 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015548 hi = hash_find(&compat_hashtab, name);
15549 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015550 return &compat_hashtab;
15551
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015553 return &globvarht; /* global variable */
15554 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 }
15556 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015557 if (*name == 'g') /* global variable */
15558 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015559 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15560 */
15561 if (vim_strchr(name + 2, ':') != NULL
15562 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015563 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015565 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015568 if (*name == 'v') /* v: variable */
15569 return &vimvarht;
15570 if (*name == 'a' && current_funccal != NULL) /* function argument */
15571 return &current_funccal->l_avars.dv_hashtab;
15572 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15573 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 if (*name == 's' /* script variable */
15575 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15576 return &SCRIPT_VARS(current_SID);
15577 return NULL;
15578}
15579
15580/*
15581 * Get the string value of a (global/local) variable.
15582 * Returns NULL when it doesn't exist.
15583 */
15584 char_u *
15585get_var_value(name)
15586 char_u *name;
15587{
Bram Moolenaar33570922005-01-25 22:26:29 +000015588 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589
Bram Moolenaara7043832005-01-21 11:56:39 +000015590 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 if (v == NULL)
15592 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594}
15595
15596/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 * sourcing this script and when executing functions defined in the script.
15599 */
15600 void
15601new_script_vars(id)
15602 scid_T id;
15603{
Bram Moolenaara7043832005-01-21 11:56:39 +000015604 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 hashtab_T *ht;
15606 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015607
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15609 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015610 /* Re-allocating ga_data means that an ht_array pointing to
15611 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015612 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015613 for (i = 1; i <= ga_scripts.ga_len; ++i)
15614 {
15615 ht = &SCRIPT_VARS(i);
15616 if (ht->ht_mask == HT_INIT_SIZE - 1)
15617 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 sv = &SCRIPT_SV(i);
15619 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015620 }
15621
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 while (ga_scripts.ga_len < id)
15623 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15625 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 }
15628 }
15629}
15630
15631/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015632 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15633 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 */
15635 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015636init_var_dict(dict, dict_var)
15637 dict_T *dict;
15638 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639{
Bram Moolenaar33570922005-01-25 22:26:29 +000015640 hash_init(&dict->dv_hashtab);
15641 dict->dv_refcount = 99999;
15642 dict_var->di_tv.vval.v_dict = dict;
15643 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015644 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15646 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647}
15648
15649/*
15650 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015651 * Frees all allocated variables and the value they contain.
15652 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 */
15654 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015655vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015656 hashtab_T *ht;
15657{
15658 vars_clear_ext(ht, TRUE);
15659}
15660
15661/*
15662 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15663 */
15664 static void
15665vars_clear_ext(ht, free_val)
15666 hashtab_T *ht;
15667 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668{
Bram Moolenaara7043832005-01-21 11:56:39 +000015669 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015670 hashitem_T *hi;
15671 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672
Bram Moolenaar33570922005-01-25 22:26:29 +000015673 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015674 todo = ht->ht_used;
15675 for (hi = ht->ht_array; todo > 0; ++hi)
15676 {
15677 if (!HASHITEM_EMPTY(hi))
15678 {
15679 --todo;
15680
Bram Moolenaar33570922005-01-25 22:26:29 +000015681 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015682 * ht_array might change then. hash_clear() takes care of it
15683 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015684 v = HI2DI(hi);
15685 if (free_val)
15686 clear_tv(&v->di_tv);
15687 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15688 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015689 }
15690 }
15691 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692}
15693
Bram Moolenaara7043832005-01-21 11:56:39 +000015694/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 * Delete a variable from hashtab "ht" at item "hi".
15696 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015699delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015700 hashtab_T *ht;
15701 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702{
Bram Moolenaar33570922005-01-25 22:26:29 +000015703 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015704
15705 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015706 clear_tv(&di->di_tv);
15707 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708}
15709
15710/*
15711 * List the value of one internal variable.
15712 */
15713 static void
15714list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015715 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 char_u *prefix;
15717{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015718 char_u *tofree;
15719 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015720 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015721
Bram Moolenaar33570922005-01-25 22:26:29 +000015722 s = echo_string(&v->di_tv, &tofree, numbuf);
15723 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015724 s == NULL ? (char_u *)"" : s);
15725 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726}
15727
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 static void
15729list_one_var_a(prefix, name, type, string)
15730 char_u *prefix;
15731 char_u *name;
15732 int type;
15733 char_u *string;
15734{
15735 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15736 if (name != NULL) /* "a:" vars don't have a name stored */
15737 msg_puts(name);
15738 msg_putchar(' ');
15739 msg_advance(22);
15740 if (type == VAR_NUMBER)
15741 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015742 else if (type == VAR_FUNC)
15743 msg_putchar('*');
15744 else if (type == VAR_LIST)
15745 {
15746 msg_putchar('[');
15747 if (*string == '[')
15748 ++string;
15749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015750 else if (type == VAR_DICT)
15751 {
15752 msg_putchar('{');
15753 if (*string == '{')
15754 ++string;
15755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 else
15757 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015758
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015760
15761 if (type == VAR_FUNC)
15762 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763}
15764
15765/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015766 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 * If the variable already exists, the value is updated.
15768 * Otherwise the variable is created.
15769 */
15770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015771set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015774 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775{
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015778 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015779 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015781 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015782 {
15783 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15784 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15785 ? name[2] : name[0]))
15786 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015787 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015788 return;
15789 }
15790 if (function_exists(name))
15791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015792 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15793 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015794 return;
15795 }
15796 }
15797
Bram Moolenaara7043832005-01-21 11:56:39 +000015798 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015799 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015800 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015801 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000015802 return;
15803 }
15804
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015805 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015806 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015808 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015809 if (var_check_ro(v->di_flags, name)
15810 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015811 return;
15812 if (v->di_tv.v_type != tv->v_type
15813 && !((v->di_tv.v_type == VAR_STRING
15814 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015815 && (tv->v_type == VAR_STRING
15816 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015817 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015818 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015819 return;
15820 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015821
15822 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015823 * Handle setting internal v: variables separately: we don't change
15824 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 */
15826 if (ht == &vimvarht)
15827 {
15828 if (v->di_tv.v_type == VAR_STRING)
15829 {
15830 vim_free(v->di_tv.vval.v_string);
15831 if (copy || tv->v_type != VAR_STRING)
15832 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15833 else
15834 {
15835 /* Take over the string to avoid an extra alloc/free. */
15836 v->di_tv.vval.v_string = tv->vval.v_string;
15837 tv->vval.v_string = NULL;
15838 }
15839 }
15840 else if (v->di_tv.v_type != VAR_NUMBER)
15841 EMSG2(_(e_intern2), "set_var()");
15842 else
15843 v->di_tv.vval.v_number = get_tv_number(tv);
15844 return;
15845 }
15846
15847 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 }
15849 else /* add a new variable */
15850 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015851 /* Make sure the variable name is valid. */
15852 for (p = varname; *p != NUL; ++p)
15853 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
15854 {
15855 EMSG2(_(e_illvar), varname);
15856 return;
15857 }
15858
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015859 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15860 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015861 if (v == NULL)
15862 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015863 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015864 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015866 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 return;
15868 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015869 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015872 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015874 else
15875 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015877 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015878 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880}
15881
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015882/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015883 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15884 * Also give an error message.
15885 */
15886 static int
15887var_check_ro(flags, name)
15888 int flags;
15889 char_u *name;
15890{
15891 if (flags & DI_FLAGS_RO)
15892 {
15893 EMSG2(_(e_readonlyvar), name);
15894 return TRUE;
15895 }
15896 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15897 {
15898 EMSG2(_(e_readonlysbx), name);
15899 return TRUE;
15900 }
15901 return FALSE;
15902}
15903
15904/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015905 * Return TRUE if typeval "tv" is set to be locked (immutable).
15906 * Also give an error message, using "name".
15907 */
15908 static int
15909tv_check_lock(lock, name)
15910 int lock;
15911 char_u *name;
15912{
15913 if (lock & VAR_LOCKED)
15914 {
15915 EMSG2(_("E741: Value is locked: %s"),
15916 name == NULL ? (char_u *)_("Unknown") : name);
15917 return TRUE;
15918 }
15919 if (lock & VAR_FIXED)
15920 {
15921 EMSG2(_("E742: Cannot change value of %s"),
15922 name == NULL ? (char_u *)_("Unknown") : name);
15923 return TRUE;
15924 }
15925 return FALSE;
15926}
15927
15928/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015931 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015934copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015935 typval_T *from;
15936 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015938 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015939 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015940 switch (from->v_type)
15941 {
15942 case VAR_NUMBER:
15943 to->vval.v_number = from->vval.v_number;
15944 break;
15945 case VAR_STRING:
15946 case VAR_FUNC:
15947 if (from->vval.v_string == NULL)
15948 to->vval.v_string = NULL;
15949 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015951 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015952 if (from->v_type == VAR_FUNC)
15953 func_ref(to->vval.v_string);
15954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015955 break;
15956 case VAR_LIST:
15957 if (from->vval.v_list == NULL)
15958 to->vval.v_list = NULL;
15959 else
15960 {
15961 to->vval.v_list = from->vval.v_list;
15962 ++to->vval.v_list->lv_refcount;
15963 }
15964 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015965 case VAR_DICT:
15966 if (from->vval.v_dict == NULL)
15967 to->vval.v_dict = NULL;
15968 else
15969 {
15970 to->vval.v_dict = from->vval.v_dict;
15971 ++to->vval.v_dict->dv_refcount;
15972 }
15973 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015974 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015975 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015976 break;
15977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978}
15979
15980/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015981 * Make a copy of an item.
15982 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015983 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15984 * reference to an already copied list/dict can be used.
15985 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015986 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015987 static int
15988item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 typval_T *from;
15990 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015991 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015992 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015993{
15994 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015995 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015996
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015998 {
15999 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016000 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016001 }
16002 ++recurse;
16003
16004 switch (from->v_type)
16005 {
16006 case VAR_NUMBER:
16007 case VAR_STRING:
16008 case VAR_FUNC:
16009 copy_tv(from, to);
16010 break;
16011 case VAR_LIST:
16012 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016013 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016014 if (from->vval.v_list == NULL)
16015 to->vval.v_list = NULL;
16016 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16017 {
16018 /* use the copy made earlier */
16019 to->vval.v_list = from->vval.v_list->lv_copylist;
16020 ++to->vval.v_list->lv_refcount;
16021 }
16022 else
16023 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16024 if (to->vval.v_list == NULL)
16025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016026 break;
16027 case VAR_DICT:
16028 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016029 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016030 if (from->vval.v_dict == NULL)
16031 to->vval.v_dict = NULL;
16032 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16033 {
16034 /* use the copy made earlier */
16035 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16036 ++to->vval.v_dict->dv_refcount;
16037 }
16038 else
16039 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16040 if (to->vval.v_dict == NULL)
16041 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016042 break;
16043 default:
16044 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016045 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016046 }
16047 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016048 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016049}
16050
16051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 * ":echo expr1 ..." print each argument separated with a space, add a
16053 * newline at the end.
16054 * ":echon expr1 ..." print each argument plain.
16055 */
16056 void
16057ex_echo(eap)
16058 exarg_T *eap;
16059{
16060 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016062 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 char_u *p;
16064 int needclr = TRUE;
16065 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016066 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067
16068 if (eap->skip)
16069 ++emsg_skip;
16070 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16071 {
16072 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016073 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 {
16075 /*
16076 * Report the invalid expression unless the expression evaluation
16077 * has been cancelled due to an aborting error, an interrupt, or an
16078 * exception.
16079 */
16080 if (!aborting())
16081 EMSG2(_(e_invexpr2), p);
16082 break;
16083 }
16084 if (!eap->skip)
16085 {
16086 if (atstart)
16087 {
16088 atstart = FALSE;
16089 /* Call msg_start() after eval1(), evaluating the expression
16090 * may cause a message to appear. */
16091 if (eap->cmdidx == CMD_echo)
16092 msg_start();
16093 }
16094 else if (eap->cmdidx == CMD_echo)
16095 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016096 p = echo_string(&rettv, &tofree, numbuf);
16097 if (p != NULL)
16098 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016100 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016102 if (*p != TAB && needclr)
16103 {
16104 /* remove any text still there from the command */
16105 msg_clr_eos();
16106 needclr = FALSE;
16107 }
16108 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 }
16110 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016111 {
16112#ifdef FEAT_MBYTE
16113 if (has_mbyte)
16114 {
16115 int i = (*mb_ptr2len_check)(p);
16116
16117 (void)msg_outtrans_len_attr(p, i, echo_attr);
16118 p += i - 1;
16119 }
16120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016122 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016125 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016127 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 arg = skipwhite(arg);
16129 }
16130 eap->nextcmd = check_nextcmd(arg);
16131
16132 if (eap->skip)
16133 --emsg_skip;
16134 else
16135 {
16136 /* remove text that may still be there from the command */
16137 if (needclr)
16138 msg_clr_eos();
16139 if (eap->cmdidx == CMD_echo)
16140 msg_end();
16141 }
16142}
16143
16144/*
16145 * ":echohl {name}".
16146 */
16147 void
16148ex_echohl(eap)
16149 exarg_T *eap;
16150{
16151 int id;
16152
16153 id = syn_name2id(eap->arg);
16154 if (id == 0)
16155 echo_attr = 0;
16156 else
16157 echo_attr = syn_id2attr(id);
16158}
16159
16160/*
16161 * ":execute expr1 ..." execute the result of an expression.
16162 * ":echomsg expr1 ..." Print a message
16163 * ":echoerr expr1 ..." Print an error
16164 * Each gets spaces around each argument and a newline at the end for
16165 * echo commands
16166 */
16167 void
16168ex_execute(eap)
16169 exarg_T *eap;
16170{
16171 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016172 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 int ret = OK;
16174 char_u *p;
16175 garray_T ga;
16176 int len;
16177 int save_did_emsg;
16178
16179 ga_init2(&ga, 1, 80);
16180
16181 if (eap->skip)
16182 ++emsg_skip;
16183 while (*arg != NUL && *arg != '|' && *arg != '\n')
16184 {
16185 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 {
16188 /*
16189 * Report the invalid expression unless the expression evaluation
16190 * has been cancelled due to an aborting error, an interrupt, or an
16191 * exception.
16192 */
16193 if (!aborting())
16194 EMSG2(_(e_invexpr2), p);
16195 ret = FAIL;
16196 break;
16197 }
16198
16199 if (!eap->skip)
16200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 len = (int)STRLEN(p);
16203 if (ga_grow(&ga, len + 2) == FAIL)
16204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 ret = FAIL;
16207 break;
16208 }
16209 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 ga.ga_len += len;
16213 }
16214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 arg = skipwhite(arg);
16217 }
16218
16219 if (ret != FAIL && ga.ga_data != NULL)
16220 {
16221 if (eap->cmdidx == CMD_echomsg)
16222 MSG_ATTR(ga.ga_data, echo_attr);
16223 else if (eap->cmdidx == CMD_echoerr)
16224 {
16225 /* We don't want to abort following commands, restore did_emsg. */
16226 save_did_emsg = did_emsg;
16227 EMSG((char_u *)ga.ga_data);
16228 if (!force_abort)
16229 did_emsg = save_did_emsg;
16230 }
16231 else if (eap->cmdidx == CMD_execute)
16232 do_cmdline((char_u *)ga.ga_data,
16233 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16234 }
16235
16236 ga_clear(&ga);
16237
16238 if (eap->skip)
16239 --emsg_skip;
16240
16241 eap->nextcmd = check_nextcmd(arg);
16242}
16243
16244/*
16245 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16246 * "arg" points to the "&" or '+' when called, to "option" when returning.
16247 * Returns NULL when no option name found. Otherwise pointer to the char
16248 * after the option name.
16249 */
16250 static char_u *
16251find_option_end(arg, opt_flags)
16252 char_u **arg;
16253 int *opt_flags;
16254{
16255 char_u *p = *arg;
16256
16257 ++p;
16258 if (*p == 'g' && p[1] == ':')
16259 {
16260 *opt_flags = OPT_GLOBAL;
16261 p += 2;
16262 }
16263 else if (*p == 'l' && p[1] == ':')
16264 {
16265 *opt_flags = OPT_LOCAL;
16266 p += 2;
16267 }
16268 else
16269 *opt_flags = 0;
16270
16271 if (!ASCII_ISALPHA(*p))
16272 return NULL;
16273 *arg = p;
16274
16275 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16276 p += 4; /* termcap option */
16277 else
16278 while (ASCII_ISALPHA(*p))
16279 ++p;
16280 return p;
16281}
16282
16283/*
16284 * ":function"
16285 */
16286 void
16287ex_function(eap)
16288 exarg_T *eap;
16289{
16290 char_u *theline;
16291 int j;
16292 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 char_u *name = NULL;
16295 char_u *p;
16296 char_u *arg;
16297 garray_T newargs;
16298 garray_T newlines;
16299 int varargs = FALSE;
16300 int mustend = FALSE;
16301 int flags = 0;
16302 ufunc_T *fp;
16303 int indent;
16304 int nesting;
16305 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016306 dictitem_T *v;
16307 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016308 static int func_nr = 0; /* number for nameless function */
16309 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016310 hashtab_T *ht;
16311 int todo;
16312 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313
16314 /*
16315 * ":function" without argument: list functions.
16316 */
16317 if (ends_excmd(*eap->arg))
16318 {
16319 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016320 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016321 todo = func_hashtab.ht_used;
16322 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016323 {
16324 if (!HASHITEM_EMPTY(hi))
16325 {
16326 --todo;
16327 fp = HI2UF(hi);
16328 if (!isdigit(*fp->uf_name))
16329 list_func_head(fp, FALSE);
16330 }
16331 }
16332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 eap->nextcmd = check_nextcmd(eap->arg);
16334 return;
16335 }
16336
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016337 /*
16338 * Get the function name. There are these situations:
16339 * func normal function name
16340 * "name" == func, "fudi.fd_dict" == NULL
16341 * dict.func new dictionary entry
16342 * "name" == NULL, "fudi.fd_dict" set,
16343 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16344 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016345 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016346 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16347 * dict.func existing dict entry that's not a Funcref
16348 * "name" == NULL, "fudi.fd_dict" set,
16349 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016352 name = trans_function_name(&p, eap->skip, 0, &fudi);
16353 paren = (vim_strchr(p, '(') != NULL);
16354 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355 {
16356 /*
16357 * Return on an invalid expression in braces, unless the expression
16358 * evaluation has been cancelled due to an aborting error, an
16359 * interrupt, or an exception.
16360 */
16361 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016363 if (!eap->skip && fudi.fd_newkey != NULL)
16364 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016365 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 else
16369 eap->skip = TRUE;
16370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 /* An error in a function call during evaluation of an expression in magic
16372 * braces should not cause the function not to be defined. */
16373 saved_did_emsg = did_emsg;
16374 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375
16376 /*
16377 * ":function func" with only function name: list function.
16378 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016379 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 {
16381 if (!ends_excmd(*skipwhite(p)))
16382 {
16383 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016384 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 }
16386 eap->nextcmd = check_nextcmd(p);
16387 if (eap->nextcmd != NULL)
16388 *p = NUL;
16389 if (!eap->skip && !got_int)
16390 {
16391 fp = find_func(name);
16392 if (fp != NULL)
16393 {
16394 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016395 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 {
16397 msg_putchar('\n');
16398 msg_outnum((long)(j + 1));
16399 if (j < 9)
16400 msg_putchar(' ');
16401 if (j < 99)
16402 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016403 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 out_flush(); /* show a line at a time */
16405 ui_breakcheck();
16406 }
16407 if (!got_int)
16408 {
16409 msg_putchar('\n');
16410 msg_puts((char_u *)" endfunction");
16411 }
16412 }
16413 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016414 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016416 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 }
16418
16419 /*
16420 * ":function name(arg1, arg2)" Define function.
16421 */
16422 p = skipwhite(p);
16423 if (*p != '(')
16424 {
16425 if (!eap->skip)
16426 {
16427 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016428 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
16430 /* attempt to continue by skipping some text */
16431 if (vim_strchr(p, '(') != NULL)
16432 p = vim_strchr(p, '(');
16433 }
16434 p = skipwhite(p + 1);
16435
16436 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16437 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16438
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016439 if (!eap->skip)
16440 {
16441 /* Check the name of the function. */
16442 if (name != NULL)
16443 arg = name;
16444 else
16445 arg = fudi.fd_newkey;
16446 if (arg != NULL)
16447 {
16448 if (*arg == K_SPECIAL)
16449 j = 3;
16450 else
16451 j = 0;
16452 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16453 : eval_isnamec(arg[j])))
16454 ++j;
16455 if (arg[j] != NUL)
16456 emsg_funcname(_(e_invarg2), arg);
16457 }
16458 }
16459
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 /*
16461 * Isolate the arguments: "arg1, arg2, ...)"
16462 */
16463 while (*p != ')')
16464 {
16465 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16466 {
16467 varargs = TRUE;
16468 p += 3;
16469 mustend = TRUE;
16470 }
16471 else
16472 {
16473 arg = p;
16474 while (ASCII_ISALNUM(*p) || *p == '_')
16475 ++p;
16476 if (arg == p || isdigit(*arg)
16477 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16478 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16479 {
16480 if (!eap->skip)
16481 EMSG2(_("E125: Illegal argument: %s"), arg);
16482 break;
16483 }
16484 if (ga_grow(&newargs, 1) == FAIL)
16485 goto erret;
16486 c = *p;
16487 *p = NUL;
16488 arg = vim_strsave(arg);
16489 if (arg == NULL)
16490 goto erret;
16491 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16492 *p = c;
16493 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 if (*p == ',')
16495 ++p;
16496 else
16497 mustend = TRUE;
16498 }
16499 p = skipwhite(p);
16500 if (mustend && *p != ')')
16501 {
16502 if (!eap->skip)
16503 EMSG2(_(e_invarg2), eap->arg);
16504 break;
16505 }
16506 }
16507 ++p; /* skip the ')' */
16508
Bram Moolenaare9a41262005-01-15 22:18:47 +000016509 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 for (;;)
16511 {
16512 p = skipwhite(p);
16513 if (STRNCMP(p, "range", 5) == 0)
16514 {
16515 flags |= FC_RANGE;
16516 p += 5;
16517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016518 else if (STRNCMP(p, "dict", 4) == 0)
16519 {
16520 flags |= FC_DICT;
16521 p += 4;
16522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 else if (STRNCMP(p, "abort", 5) == 0)
16524 {
16525 flags |= FC_ABORT;
16526 p += 5;
16527 }
16528 else
16529 break;
16530 }
16531
16532 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16533 EMSG(_(e_trailing));
16534
16535 /*
16536 * Read the body of the function, until ":endfunction" is found.
16537 */
16538 if (KeyTyped)
16539 {
16540 /* Check if the function already exists, don't let the user type the
16541 * whole function before telling him it doesn't work! For a script we
16542 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016543 if (!eap->skip && !eap->forceit)
16544 {
16545 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16546 EMSG(_(e_funcdict));
16547 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016548 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016551 if (!eap->skip && did_emsg)
16552 goto erret;
16553
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 msg_putchar('\n'); /* don't overwrite the function name */
16555 cmdline_row = msg_row;
16556 }
16557
16558 indent = 2;
16559 nesting = 0;
16560 for (;;)
16561 {
16562 msg_scroll = TRUE;
16563 need_wait_return = FALSE;
16564 if (eap->getline == NULL)
16565 theline = getcmdline(':', 0L, indent);
16566 else
16567 theline = eap->getline(':', eap->cookie, indent);
16568 if (KeyTyped)
16569 lines_left = Rows - 1;
16570 if (theline == NULL)
16571 {
16572 EMSG(_("E126: Missing :endfunction"));
16573 goto erret;
16574 }
16575
16576 if (skip_until != NULL)
16577 {
16578 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16579 * don't check for ":endfunc". */
16580 if (STRCMP(theline, skip_until) == 0)
16581 {
16582 vim_free(skip_until);
16583 skip_until = NULL;
16584 }
16585 }
16586 else
16587 {
16588 /* skip ':' and blanks*/
16589 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16590 ;
16591
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016592 /* Check for "endfunction". */
16593 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 {
16595 vim_free(theline);
16596 break;
16597 }
16598
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016599 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 * at "end". */
16601 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16602 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016603 else if (STRNCMP(p, "if", 2) == 0
16604 || STRNCMP(p, "wh", 2) == 0
16605 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 || STRNCMP(p, "try", 3) == 0)
16607 indent += 2;
16608
16609 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016610 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016612 if (*p == '!')
16613 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 p += eval_fname_script(p);
16615 if (ASCII_ISALPHA(*p))
16616 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016617 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 if (*skipwhite(p) == '(')
16619 {
16620 ++nesting;
16621 indent += 2;
16622 }
16623 }
16624 }
16625
16626 /* Check for ":append" or ":insert". */
16627 p = skip_range(p, NULL);
16628 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16629 || (p[0] == 'i'
16630 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16631 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16632 skip_until = vim_strsave((char_u *)".");
16633
16634 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16635 arg = skipwhite(skiptowhite(p));
16636 if (arg[0] == '<' && arg[1] =='<'
16637 && ((p[0] == 'p' && p[1] == 'y'
16638 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16639 || (p[0] == 'p' && p[1] == 'e'
16640 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16641 || (p[0] == 't' && p[1] == 'c'
16642 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16643 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16644 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016645 || (p[0] == 'm' && p[1] == 'z'
16646 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 ))
16648 {
16649 /* ":python <<" continues until a dot, like ":append" */
16650 p = skipwhite(arg + 2);
16651 if (*p == NUL)
16652 skip_until = vim_strsave((char_u *)".");
16653 else
16654 skip_until = vim_strsave(p);
16655 }
16656 }
16657
16658 /* Add the line to the function. */
16659 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016660 {
16661 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016663 }
16664
16665 /* Copy the line to newly allocated memory. get_one_sourceline()
16666 * allocates 250 bytes per line, this saves 80% on average. The cost
16667 * is an extra alloc/free. */
16668 p = vim_strsave(theline);
16669 if (p != NULL)
16670 {
16671 vim_free(theline);
16672 theline = p;
16673 }
16674
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16676 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 }
16678
16679 /* Don't define the function when skipping commands or when an error was
16680 * detected. */
16681 if (eap->skip || did_emsg)
16682 goto erret;
16683
16684 /*
16685 * If there are no errors, add the function
16686 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016687 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016688 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016689 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016690 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016691 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016692 emsg_funcname("E707: Function name conflicts with variable: %s",
16693 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016694 goto erret;
16695 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016696
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016697 fp = find_func(name);
16698 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016700 if (!eap->forceit)
16701 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016702 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016703 goto erret;
16704 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016705 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016706 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016707 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016708 name);
16709 goto erret;
16710 }
16711 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016712 ga_clear_strings(&(fp->uf_args));
16713 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016714 vim_free(name);
16715 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 }
16718 else
16719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016720 char numbuf[20];
16721
16722 fp = NULL;
16723 if (fudi.fd_newkey == NULL && !eap->forceit)
16724 {
16725 EMSG(_(e_funcdict));
16726 goto erret;
16727 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016728 if (fudi.fd_di == NULL)
16729 {
16730 /* Can't add a function to a locked dictionary */
16731 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16732 goto erret;
16733 }
16734 /* Can't change an existing function if it is locked */
16735 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16736 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016737
16738 /* Give the function a sequential number. Can only be used with a
16739 * Funcref! */
16740 vim_free(name);
16741 sprintf(numbuf, "%d", ++func_nr);
16742 name = vim_strsave((char_u *)numbuf);
16743 if (name == NULL)
16744 goto erret;
16745 }
16746
16747 if (fp == NULL)
16748 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016749 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016750 {
16751 int slen, plen;
16752 char_u *scriptname;
16753
16754 /* Check that the autoload name matches the script name. */
16755 j = FAIL;
16756 if (sourcing_name != NULL)
16757 {
16758 scriptname = autoload_name(name);
16759 if (scriptname != NULL)
16760 {
16761 p = vim_strchr(scriptname, '/');
16762 plen = STRLEN(p);
16763 slen = STRLEN(sourcing_name);
16764 if (slen > plen && fnamecmp(p,
16765 sourcing_name + slen - plen) == 0)
16766 j = OK;
16767 vim_free(scriptname);
16768 }
16769 }
16770 if (j == FAIL)
16771 {
16772 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16773 goto erret;
16774 }
16775 }
16776
16777 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 if (fp == NULL)
16779 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016780
16781 if (fudi.fd_dict != NULL)
16782 {
16783 if (fudi.fd_di == NULL)
16784 {
16785 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016786 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016787 if (fudi.fd_di == NULL)
16788 {
16789 vim_free(fp);
16790 goto erret;
16791 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016792 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16793 {
16794 vim_free(fudi.fd_di);
16795 goto erret;
16796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016797 }
16798 else
16799 /* overwrite existing dict entry */
16800 clear_tv(&fudi.fd_di->di_tv);
16801 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016802 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016803 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016804 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016805 }
16806
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016808 STRCPY(fp->uf_name, name);
16809 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016811 fp->uf_args = newargs;
16812 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016813#ifdef FEAT_PROFILE
16814 fp->uf_tml_count = NULL;
16815 fp->uf_tml_total = NULL;
16816 fp->uf_tml_self = NULL;
16817 fp->uf_profiling = FALSE;
16818 if (prof_def_func())
16819 func_do_profile(fp);
16820#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016821 fp->uf_varargs = varargs;
16822 fp->uf_flags = flags;
16823 fp->uf_calls = 0;
16824 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016825 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826
16827erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 ga_clear_strings(&newargs);
16829 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016830ret_free:
16831 vim_free(skip_until);
16832 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835}
16836
16837/*
16838 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016839 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016841 * flags:
16842 * TFN_INT: internal function name OK
16843 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 * Advances "pp" to just after the function name (if no error).
16845 */
16846 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016847trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 char_u **pp;
16849 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016850 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016851 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016853 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 char_u *start;
16855 char_u *end;
16856 int lead;
16857 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016859 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016860
16861 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016862 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016864
16865 /* Check for hard coded <SNR>: already translated function ID (from a user
16866 * command). */
16867 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16868 && (*pp)[2] == (int)KE_SNR)
16869 {
16870 *pp += 3;
16871 len = get_id_len(pp) + 3;
16872 return vim_strnsave(start, len);
16873 }
16874
16875 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16876 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016878 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016880
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016881 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16882 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016883 if (end == start)
16884 {
16885 if (!skip)
16886 EMSG(_("E129: Function name required"));
16887 goto theend;
16888 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016889 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016890 {
16891 /*
16892 * Report an invalid expression in braces, unless the expression
16893 * evaluation has been cancelled due to an aborting error, an
16894 * interrupt, or an exception.
16895 */
16896 if (!aborting())
16897 {
16898 if (end != NULL)
16899 EMSG2(_(e_invarg2), start);
16900 }
16901 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016902 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016903 goto theend;
16904 }
16905
16906 if (lv.ll_tv != NULL)
16907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016908 if (fdp != NULL)
16909 {
16910 fdp->fd_dict = lv.ll_dict;
16911 fdp->fd_newkey = lv.ll_newkey;
16912 lv.ll_newkey = NULL;
16913 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016915 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16916 {
16917 name = vim_strsave(lv.ll_tv->vval.v_string);
16918 *pp = end;
16919 }
16920 else
16921 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016922 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16923 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016924 EMSG(_(e_funcref));
16925 else
16926 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016927 name = NULL;
16928 }
16929 goto theend;
16930 }
16931
16932 if (lv.ll_name == NULL)
16933 {
16934 /* Error found, but continue after the function name. */
16935 *pp = end;
16936 goto theend;
16937 }
16938
16939 if (lv.ll_exp_name != NULL)
16940 len = STRLEN(lv.ll_exp_name);
16941 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016942 {
16943 if (lead == 2) /* skip over "s:" */
16944 lv.ll_name += 2;
16945 len = (int)(end - lv.ll_name);
16946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016947
16948 /*
16949 * Copy the function name to allocated memory.
16950 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16951 * Accept <SNR>123_name() outside a script.
16952 */
16953 if (skip)
16954 lead = 0; /* do nothing */
16955 else if (lead > 0)
16956 {
16957 lead = 3;
16958 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16959 {
16960 if (current_SID <= 0)
16961 {
16962 EMSG(_(e_usingsid));
16963 goto theend;
16964 }
16965 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16966 lead += (int)STRLEN(sid_buf);
16967 }
16968 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016969 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016970 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016971 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016972 goto theend;
16973 }
16974 name = alloc((unsigned)(len + lead + 1));
16975 if (name != NULL)
16976 {
16977 if (lead > 0)
16978 {
16979 name[0] = K_SPECIAL;
16980 name[1] = KS_EXTRA;
16981 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016982 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016983 STRCPY(name + 3, sid_buf);
16984 }
16985 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16986 name[len + lead] = NUL;
16987 }
16988 *pp = end;
16989
16990theend:
16991 clear_lval(&lv);
16992 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993}
16994
16995/*
16996 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16997 * Return 2 if "p" starts with "s:".
16998 * Return 0 otherwise.
16999 */
17000 static int
17001eval_fname_script(p)
17002 char_u *p;
17003{
17004 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17005 || STRNICMP(p + 1, "SNR>", 4) == 0))
17006 return 5;
17007 if (p[0] == 's' && p[1] == ':')
17008 return 2;
17009 return 0;
17010}
17011
17012/*
17013 * Return TRUE if "p" starts with "<SID>" or "s:".
17014 * Only works if eval_fname_script() returned non-zero for "p"!
17015 */
17016 static int
17017eval_fname_sid(p)
17018 char_u *p;
17019{
17020 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17021}
17022
17023/*
17024 * List the head of the function: "name(arg1, arg2)".
17025 */
17026 static void
17027list_func_head(fp, indent)
17028 ufunc_T *fp;
17029 int indent;
17030{
17031 int j;
17032
17033 msg_start();
17034 if (indent)
17035 MSG_PUTS(" ");
17036 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017037 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 {
17039 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017040 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 }
17042 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017043 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017045 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 {
17047 if (j)
17048 MSG_PUTS(", ");
17049 msg_puts(FUNCARG(fp, j));
17050 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017051 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 {
17053 if (j)
17054 MSG_PUTS(", ");
17055 MSG_PUTS("...");
17056 }
17057 msg_putchar(')');
17058}
17059
17060/*
17061 * Find a function by name, return pointer to it in ufuncs.
17062 * Return NULL for unknown function.
17063 */
17064 static ufunc_T *
17065find_func(name)
17066 char_u *name;
17067{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017068 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017070 hi = hash_find(&func_hashtab, name);
17071 if (!HASHITEM_EMPTY(hi))
17072 return HI2UF(hi);
17073 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074}
17075
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017076#if defined(EXITFREE) || defined(PROTO)
17077 void
17078free_all_functions()
17079{
17080 hashitem_T *hi;
17081
17082 /* Need to start all over every time, because func_free() may change the
17083 * hash table. */
17084 while (func_hashtab.ht_used > 0)
17085 for (hi = func_hashtab.ht_array; ; ++hi)
17086 if (!HASHITEM_EMPTY(hi))
17087 {
17088 func_free(HI2UF(hi));
17089 break;
17090 }
17091}
17092#endif
17093
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017094/*
17095 * Return TRUE if a function "name" exists.
17096 */
17097 static int
17098function_exists(name)
17099 char_u *name;
17100{
17101 char_u *p = name;
17102 int n = FALSE;
17103
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017104 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105 if (p != NULL)
17106 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017107 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017109 else
17110 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017111 vim_free(p);
17112 }
17113 return n;
17114}
17115
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017116/*
17117 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017118 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017119 */
17120 static int
17121builtin_function(name)
17122 char_u *name;
17123{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017124 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17125 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017126}
17127
Bram Moolenaar05159a02005-02-26 23:04:13 +000017128#if defined(FEAT_PROFILE) || defined(PROTO)
17129/*
17130 * Start profiling function "fp".
17131 */
17132 static void
17133func_do_profile(fp)
17134 ufunc_T *fp;
17135{
17136 fp->uf_tm_count = 0;
17137 profile_zero(&fp->uf_tm_self);
17138 profile_zero(&fp->uf_tm_total);
17139 if (fp->uf_tml_count == NULL)
17140 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17141 (sizeof(int) * fp->uf_lines.ga_len));
17142 if (fp->uf_tml_total == NULL)
17143 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17144 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17145 if (fp->uf_tml_self == NULL)
17146 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17147 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17148 fp->uf_tml_idx = -1;
17149 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17150 || fp->uf_tml_self == NULL)
17151 return; /* out of memory */
17152
17153 fp->uf_profiling = TRUE;
17154}
17155
17156/*
17157 * Dump the profiling results for all functions in file "fd".
17158 */
17159 void
17160func_dump_profile(fd)
17161 FILE *fd;
17162{
17163 hashitem_T *hi;
17164 int todo;
17165 ufunc_T *fp;
17166 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017167 ufunc_T **sorttab;
17168 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017169
17170 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017171 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17172
Bram Moolenaar05159a02005-02-26 23:04:13 +000017173 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17174 {
17175 if (!HASHITEM_EMPTY(hi))
17176 {
17177 --todo;
17178 fp = HI2UF(hi);
17179 if (fp->uf_profiling)
17180 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017181 if (sorttab != NULL)
17182 sorttab[st_len++] = fp;
17183
Bram Moolenaar05159a02005-02-26 23:04:13 +000017184 if (fp->uf_name[0] == K_SPECIAL)
17185 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17186 else
17187 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17188 if (fp->uf_tm_count == 1)
17189 fprintf(fd, "Called 1 time\n");
17190 else
17191 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17192 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17193 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17194 fprintf(fd, "\n");
17195 fprintf(fd, "count total (s) self (s)\n");
17196
17197 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17198 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017199 prof_func_line(fd, fp->uf_tml_count[i],
17200 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017201 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17202 }
17203 fprintf(fd, "\n");
17204 }
17205 }
17206 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017207
17208 if (sorttab != NULL && st_len > 0)
17209 {
17210 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17211 prof_total_cmp);
17212 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17213 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17214 prof_self_cmp);
17215 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17216 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017217}
Bram Moolenaar73830342005-02-28 22:48:19 +000017218
17219 static void
17220prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17221 FILE *fd;
17222 ufunc_T **sorttab;
17223 int st_len;
17224 char *title;
17225 int prefer_self; /* when equal print only self time */
17226{
17227 int i;
17228 ufunc_T *fp;
17229
17230 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17231 fprintf(fd, "count total (s) self (s) function\n");
17232 for (i = 0; i < 20 && i < st_len; ++i)
17233 {
17234 fp = sorttab[i];
17235 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17236 prefer_self);
17237 if (fp->uf_name[0] == K_SPECIAL)
17238 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17239 else
17240 fprintf(fd, " %s()\n", fp->uf_name);
17241 }
17242 fprintf(fd, "\n");
17243}
17244
17245/*
17246 * Print the count and times for one function or function line.
17247 */
17248 static void
17249prof_func_line(fd, count, total, self, prefer_self)
17250 FILE *fd;
17251 int count;
17252 proftime_T *total;
17253 proftime_T *self;
17254 int prefer_self; /* when equal print only self time */
17255{
17256 if (count > 0)
17257 {
17258 fprintf(fd, "%5d ", count);
17259 if (prefer_self && profile_equal(total, self))
17260 fprintf(fd, " ");
17261 else
17262 fprintf(fd, "%s ", profile_msg(total));
17263 if (!prefer_self && profile_equal(total, self))
17264 fprintf(fd, " ");
17265 else
17266 fprintf(fd, "%s ", profile_msg(self));
17267 }
17268 else
17269 fprintf(fd, " ");
17270}
17271
17272/*
17273 * Compare function for total time sorting.
17274 */
17275 static int
17276#ifdef __BORLANDC__
17277_RTLENTRYF
17278#endif
17279prof_total_cmp(s1, s2)
17280 const void *s1;
17281 const void *s2;
17282{
17283 ufunc_T *p1, *p2;
17284
17285 p1 = *(ufunc_T **)s1;
17286 p2 = *(ufunc_T **)s2;
17287 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17288}
17289
17290/*
17291 * Compare function for self time sorting.
17292 */
17293 static int
17294#ifdef __BORLANDC__
17295_RTLENTRYF
17296#endif
17297prof_self_cmp(s1, s2)
17298 const void *s1;
17299 const void *s2;
17300{
17301 ufunc_T *p1, *p2;
17302
17303 p1 = *(ufunc_T **)s1;
17304 p2 = *(ufunc_T **)s2;
17305 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17306}
17307
Bram Moolenaar05159a02005-02-26 23:04:13 +000017308#endif
17309
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017310/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017311 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017312 * Return TRUE if a package was loaded.
17313 */
17314 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017315script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017316 char_u *name;
17317{
17318 char_u *p;
17319 char_u *scriptname;
17320 int ret = FALSE;
17321
17322 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017323 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017324 if (p == NULL || p <= name + 2)
17325 return FALSE;
17326
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017327 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17328 scriptname = autoload_name(name);
17329 if (cmd_runtime(scriptname, FALSE) == OK)
17330 ret = TRUE;
17331
17332 vim_free(scriptname);
17333 return ret;
17334}
17335
17336/*
17337 * Return the autoload script name for a function or variable name.
17338 * Returns NULL when out of memory.
17339 */
17340 static char_u *
17341autoload_name(name)
17342 char_u *name;
17343{
17344 char_u *p;
17345 char_u *scriptname;
17346
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017347 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017348 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17349 if (scriptname == NULL)
17350 return FALSE;
17351 STRCPY(scriptname, "autoload/");
17352 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017353 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017354 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017355 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017356 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017357 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017358}
17359
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17361
17362/*
17363 * Function given to ExpandGeneric() to obtain the list of user defined
17364 * function names.
17365 */
17366 char_u *
17367get_user_func_name(xp, idx)
17368 expand_T *xp;
17369 int idx;
17370{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017371 static long_u done;
17372 static hashitem_T *hi;
17373 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
17375 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017377 done = 0;
17378 hi = func_hashtab.ht_array;
17379 }
17380 if (done < func_hashtab.ht_used)
17381 {
17382 if (done++ > 0)
17383 ++hi;
17384 while (HASHITEM_EMPTY(hi))
17385 ++hi;
17386 fp = HI2UF(hi);
17387
17388 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17389 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390
17391 cat_func_name(IObuff, fp);
17392 if (xp->xp_context != EXPAND_USER_FUNC)
17393 {
17394 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017395 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 STRCAT(IObuff, ")");
17397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 return IObuff;
17399 }
17400 return NULL;
17401}
17402
17403#endif /* FEAT_CMDL_COMPL */
17404
17405/*
17406 * Copy the function name of "fp" to buffer "buf".
17407 * "buf" must be able to hold the function name plus three bytes.
17408 * Takes care of script-local function names.
17409 */
17410 static void
17411cat_func_name(buf, fp)
17412 char_u *buf;
17413 ufunc_T *fp;
17414{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017415 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 {
17417 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017418 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 }
17420 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017421 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422}
17423
17424/*
17425 * ":delfunction {name}"
17426 */
17427 void
17428ex_delfunction(eap)
17429 exarg_T *eap;
17430{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017431 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 char_u *p;
17433 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017434 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435
17436 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017437 name = trans_function_name(&p, eap->skip, 0, &fudi);
17438 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017440 {
17441 if (fudi.fd_dict != NULL && !eap->skip)
17442 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 if (!ends_excmd(*skipwhite(p)))
17446 {
17447 vim_free(name);
17448 EMSG(_(e_trailing));
17449 return;
17450 }
17451 eap->nextcmd = check_nextcmd(p);
17452 if (eap->nextcmd != NULL)
17453 *p = NUL;
17454
17455 if (!eap->skip)
17456 fp = find_func(name);
17457 vim_free(name);
17458
17459 if (!eap->skip)
17460 {
17461 if (fp == NULL)
17462 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017463 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 return;
17465 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017466 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 {
17468 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17469 return;
17470 }
17471
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017472 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017474 /* Delete the dict item that refers to the function, it will
17475 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017476 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017478 else
17479 func_free(fp);
17480 }
17481}
17482
17483/*
17484 * Free a function and remove it from the list of functions.
17485 */
17486 static void
17487func_free(fp)
17488 ufunc_T *fp;
17489{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017490 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017491
17492 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017493 ga_clear_strings(&(fp->uf_args));
17494 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017495#ifdef FEAT_PROFILE
17496 vim_free(fp->uf_tml_count);
17497 vim_free(fp->uf_tml_total);
17498 vim_free(fp->uf_tml_self);
17499#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017500
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017501 /* remove the function from the function hashtable */
17502 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17503 if (HASHITEM_EMPTY(hi))
17504 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017505 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017506 hash_remove(&func_hashtab, hi);
17507
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017508 vim_free(fp);
17509}
17510
17511/*
17512 * Unreference a Function: decrement the reference count and free it when it
17513 * becomes zero. Only for numbered functions.
17514 */
17515 static void
17516func_unref(name)
17517 char_u *name;
17518{
17519 ufunc_T *fp;
17520
17521 if (name != NULL && isdigit(*name))
17522 {
17523 fp = find_func(name);
17524 if (fp == NULL)
17525 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017526 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017527 {
17528 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017529 * when "uf_calls" becomes zero. */
17530 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017531 func_free(fp);
17532 }
17533 }
17534}
17535
17536/*
17537 * Count a reference to a Function.
17538 */
17539 static void
17540func_ref(name)
17541 char_u *name;
17542{
17543 ufunc_T *fp;
17544
17545 if (name != NULL && isdigit(*name))
17546 {
17547 fp = find_func(name);
17548 if (fp == NULL)
17549 EMSG2(_(e_intern2), "func_ref()");
17550 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017551 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 }
17553}
17554
17555/*
17556 * Call a user function.
17557 */
17558 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017559call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 ufunc_T *fp; /* pointer to function */
17561 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017562 typval_T *argvars; /* arguments */
17563 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 linenr_T firstline; /* first line of range */
17565 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017566 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567{
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 char_u *save_sourcing_name;
17569 linenr_T save_sourcing_lnum;
17570 scid_T save_current_SID;
17571 funccall_T fc;
17572 funccall_T *save_fcp = current_funccal;
17573 int save_did_emsg;
17574 static int depth = 0;
17575 dictitem_T *v;
17576 int fixvar_idx = 0; /* index in fixvar[] */
17577 int i;
17578 int ai;
17579 char_u numbuf[NUMBUFLEN];
17580 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017581#ifdef FEAT_PROFILE
17582 proftime_T wait_start;
17583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584
17585 /* If depth of calling is getting too high, don't execute the function */
17586 if (depth >= p_mfd)
17587 {
17588 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 rettv->v_type = VAR_NUMBER;
17590 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 return;
17592 }
17593 ++depth;
17594
17595 line_breakcheck(); /* check for CTRL-C hit */
17596
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 fc.rettv = rettv;
17600 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 fc.linenr = 0;
17602 fc.returned = FALSE;
17603 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017605 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 fc.dbg_tick = debug_tick;
17607
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 /*
17609 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17610 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17611 * each argument variable and saves a lot of time.
17612 */
17613 /*
17614 * Init l: variables.
17615 */
17616 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017617 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017618 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 /* Set l:self to "selfdict". */
17620 v = &fc.fixvar[fixvar_idx++].var;
17621 STRCPY(v->di_key, "self");
17622 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17623 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17624 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017625 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 v->di_tv.vval.v_dict = selfdict;
17627 ++selfdict->dv_refcount;
17628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017629
Bram Moolenaar33570922005-01-25 22:26:29 +000017630 /*
17631 * Init a: variables.
17632 * Set a:0 to "argcount".
17633 * Set a:000 to a list with room for the "..." arguments.
17634 */
17635 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17636 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017637 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017638 v = &fc.fixvar[fixvar_idx++].var;
17639 STRCPY(v->di_key, "000");
17640 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17641 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17642 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017643 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017644 v->di_tv.vval.v_list = &fc.l_varlist;
17645 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17646 fc.l_varlist.lv_refcount = 99999;
17647
17648 /*
17649 * Set a:firstline to "firstline" and a:lastline to "lastline".
17650 * Set a:name to named arguments.
17651 * Set a:N to the "..." arguments.
17652 */
17653 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17654 (varnumber_T)firstline);
17655 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17656 (varnumber_T)lastline);
17657 for (i = 0; i < argcount; ++i)
17658 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017659 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017660 if (ai < 0)
17661 /* named argument a:name */
17662 name = FUNCARG(fp, i);
17663 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 /* "..." argument a:1, a:2, etc. */
17666 sprintf((char *)numbuf, "%d", ai + 1);
17667 name = numbuf;
17668 }
17669 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17670 {
17671 v = &fc.fixvar[fixvar_idx++].var;
17672 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17673 }
17674 else
17675 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017676 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17677 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 if (v == NULL)
17679 break;
17680 v->di_flags = DI_FLAGS_RO;
17681 }
17682 STRCPY(v->di_key, name);
17683 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17684
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017685 /* Note: the values are copied directly to avoid alloc/free.
17686 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017688 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017689
17690 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17691 {
17692 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17693 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017694 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017695 }
17696 }
17697
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 /* Don't redraw while executing the function. */
17699 ++RedrawingDisabled;
17700 save_sourcing_name = sourcing_name;
17701 save_sourcing_lnum = sourcing_lnum;
17702 sourcing_lnum = 1;
17703 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017704 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 if (sourcing_name != NULL)
17706 {
17707 if (save_sourcing_name != NULL
17708 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17709 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17710 else
17711 STRCPY(sourcing_name, "function ");
17712 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17713
17714 if (p_verbose >= 12)
17715 {
17716 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017717 verbose_enter_scroll();
17718
Bram Moolenaar555b2802005-05-19 21:08:39 +000017719 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 if (p_verbose >= 14)
17721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017723 char_u numbuf[NUMBUFLEN];
17724 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725
17726 msg_puts((char_u *)"(");
17727 for (i = 0; i < argcount; ++i)
17728 {
17729 if (i > 0)
17730 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017731 if (argvars[i].v_type == VAR_NUMBER)
17732 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 else
17734 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017735 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017736 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017738 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 }
17740 }
17741 msg_puts((char_u *)")");
17742 }
17743 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017744
17745 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 --no_wait_return;
17747 }
17748 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017749#ifdef FEAT_PROFILE
17750 if (do_profiling)
17751 {
17752 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17753 func_do_profile(fp);
17754 if (fp->uf_profiling
17755 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17756 {
17757 ++fp->uf_tm_count;
17758 profile_start(&fp->uf_tm_start);
17759 profile_zero(&fp->uf_tm_children);
17760 }
17761 script_prof_save(&wait_start);
17762 }
17763#endif
17764
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017766 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 save_did_emsg = did_emsg;
17768 did_emsg = FALSE;
17769
17770 /* call do_cmdline() to execute the lines */
17771 do_cmdline(NULL, get_func_line, (void *)&fc,
17772 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17773
17774 --RedrawingDisabled;
17775
17776 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017777 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779 clear_tv(rettv);
17780 rettv->v_type = VAR_NUMBER;
17781 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 }
17783
Bram Moolenaar05159a02005-02-26 23:04:13 +000017784#ifdef FEAT_PROFILE
17785 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17786 {
17787 profile_end(&fp->uf_tm_start);
17788 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17789 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17790 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17791 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
17792 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
17793 {
17794 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
17795 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
17796 }
17797 }
17798#endif
17799
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 /* when being verbose, mention the return value */
17801 if (p_verbose >= 12)
17802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017804 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017807 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017808 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017809 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17810 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017813 char_u buf[MSG_BUF_LEN];
17814 char_u numbuf[NUMBUFLEN];
17815 char_u *tofree;
17816
Bram Moolenaar555b2802005-05-19 21:08:39 +000017817 /* The value may be very long. Skip the middle part, so that we
17818 * have some idea how it starts and ends. smsg() would always
17819 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017820 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017821 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017822 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017823 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 }
17825 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017826
17827 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 --no_wait_return;
17829 }
17830
17831 vim_free(sourcing_name);
17832 sourcing_name = save_sourcing_name;
17833 sourcing_lnum = save_sourcing_lnum;
17834 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017835#ifdef FEAT_PROFILE
17836 if (do_profiling)
17837 script_prof_restore(&wait_start);
17838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839
17840 if (p_verbose >= 12 && sourcing_name != NULL)
17841 {
17842 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017843 verbose_enter_scroll();
17844
Bram Moolenaar555b2802005-05-19 21:08:39 +000017845 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017847
17848 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 --no_wait_return;
17850 }
17851
17852 did_emsg |= save_did_emsg;
17853 current_funccal = save_fcp;
17854
Bram Moolenaar33570922005-01-25 22:26:29 +000017855 /* The a: variables typevals were not alloced, only free the allocated
17856 * variables. */
17857 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17858
17859 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 --depth;
17861}
17862
17863/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 * Add a number variable "name" to dict "dp" with value "nr".
17865 */
17866 static void
17867add_nr_var(dp, v, name, nr)
17868 dict_T *dp;
17869 dictitem_T *v;
17870 char *name;
17871 varnumber_T nr;
17872{
17873 STRCPY(v->di_key, name);
17874 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17875 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17876 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017877 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017878 v->di_tv.vval.v_number = nr;
17879}
17880
17881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 * ":return [expr]"
17883 */
17884 void
17885ex_return(eap)
17886 exarg_T *eap;
17887{
17888 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017889 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 int returning = FALSE;
17891
17892 if (current_funccal == NULL)
17893 {
17894 EMSG(_("E133: :return not inside a function"));
17895 return;
17896 }
17897
17898 if (eap->skip)
17899 ++emsg_skip;
17900
17901 eap->nextcmd = NULL;
17902 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017903 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 {
17905 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 }
17910 /* It's safer to return also on error. */
17911 else if (!eap->skip)
17912 {
17913 /*
17914 * Return unless the expression evaluation has been cancelled due to an
17915 * aborting error, an interrupt, or an exception.
17916 */
17917 if (!aborting())
17918 returning = do_return(eap, FALSE, TRUE, NULL);
17919 }
17920
17921 /* When skipping or the return gets pending, advance to the next command
17922 * in this line (!returning). Otherwise, ignore the rest of the line.
17923 * Following lines will be ignored by get_func_line(). */
17924 if (returning)
17925 eap->nextcmd = NULL;
17926 else if (eap->nextcmd == NULL) /* no argument */
17927 eap->nextcmd = check_nextcmd(arg);
17928
17929 if (eap->skip)
17930 --emsg_skip;
17931}
17932
17933/*
17934 * Return from a function. Possibly makes the return pending. Also called
17935 * for a pending return at the ":endtry" or after returning from an extra
17936 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017937 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 * FALSE when the return gets pending.
17940 */
17941 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017942do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 exarg_T *eap;
17944 int reanimate;
17945 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017946 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947{
17948 int idx;
17949 struct condstack *cstack = eap->cstack;
17950
17951 if (reanimate)
17952 /* Undo the return. */
17953 current_funccal->returned = FALSE;
17954
17955 /*
17956 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17957 * not in its finally clause (which then is to be executed next) is found.
17958 * In this case, make the ":return" pending for execution at the ":endtry".
17959 * Otherwise, return normally.
17960 */
17961 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17962 if (idx >= 0)
17963 {
17964 cstack->cs_pending[idx] = CSTP_RETURN;
17965
17966 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017967 /* A pending return again gets pending. "rettv" points to an
17968 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017970 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 else
17972 {
17973 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017974 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017978 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 {
17980 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 else
17984 EMSG(_(e_outofmem));
17985 }
17986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988
17989 if (reanimate)
17990 {
17991 /* The pending return value could be overwritten by a ":return"
17992 * without argument in a finally clause; reset the default
17993 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017994 current_funccal->rettv->v_type = VAR_NUMBER;
17995 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 }
17997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017998 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 }
18000 else
18001 {
18002 current_funccal->returned = TRUE;
18003
18004 /* If the return is carried out now, store the return value. For
18005 * a return immediately after reanimation, the value is already
18006 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018007 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018009 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018010 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018012 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 }
18014 }
18015
18016 return idx < 0;
18017}
18018
18019/*
18020 * Free the variable with a pending return value.
18021 */
18022 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018023discard_pending_return(rettv)
18024 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025{
Bram Moolenaar33570922005-01-25 22:26:29 +000018026 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027}
18028
18029/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018030 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 * is an allocated string. Used by report_pending() for verbose messages.
18032 */
18033 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018034get_return_cmd(rettv)
18035 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018037 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018038 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018041 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018042 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018043 if (s == NULL)
18044 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018045
18046 STRCPY(IObuff, ":return ");
18047 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18048 if (STRLEN(s) + 8 >= IOSIZE)
18049 STRCPY(IObuff + IOSIZE - 4, "...");
18050 vim_free(tofree);
18051 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052}
18053
18054/*
18055 * Get next function line.
18056 * Called by do_cmdline() to get the next line.
18057 * Returns allocated string, or NULL for end of function.
18058 */
18059/* ARGSUSED */
18060 char_u *
18061get_func_line(c, cookie, indent)
18062 int c; /* not used */
18063 void *cookie;
18064 int indent; /* not used */
18065{
Bram Moolenaar33570922005-01-25 22:26:29 +000018066 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018067 ufunc_T *fp = fcp->func;
18068 char_u *retval;
18069 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070
18071 /* If breakpoints have been added/deleted need to check for it. */
18072 if (fcp->dbg_tick != debug_tick)
18073 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018074 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 sourcing_lnum);
18076 fcp->dbg_tick = debug_tick;
18077 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018078#ifdef FEAT_PROFILE
18079 if (do_profiling)
18080 func_line_end(cookie);
18081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082
Bram Moolenaar05159a02005-02-26 23:04:13 +000018083 gap = &fp->uf_lines;
18084 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 retval = NULL;
18086 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18087 retval = NULL;
18088 else
18089 {
18090 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18091 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018092#ifdef FEAT_PROFILE
18093 if (do_profiling)
18094 func_line_start(cookie);
18095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 }
18097
18098 /* Did we encounter a breakpoint? */
18099 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18100 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018101 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018103 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 sourcing_lnum);
18105 fcp->dbg_tick = debug_tick;
18106 }
18107
18108 return retval;
18109}
18110
Bram Moolenaar05159a02005-02-26 23:04:13 +000018111#if defined(FEAT_PROFILE) || defined(PROTO)
18112/*
18113 * Called when starting to read a function line.
18114 * "sourcing_lnum" must be correct!
18115 * When skipping lines it may not actually be executed, but we won't find out
18116 * until later and we need to store the time now.
18117 */
18118 void
18119func_line_start(cookie)
18120 void *cookie;
18121{
18122 funccall_T *fcp = (funccall_T *)cookie;
18123 ufunc_T *fp = fcp->func;
18124
18125 if (fp->uf_profiling && sourcing_lnum >= 1
18126 && sourcing_lnum <= fp->uf_lines.ga_len)
18127 {
18128 fp->uf_tml_idx = sourcing_lnum - 1;
18129 fp->uf_tml_execed = FALSE;
18130 profile_start(&fp->uf_tml_start);
18131 profile_zero(&fp->uf_tml_children);
18132 profile_get_wait(&fp->uf_tml_wait);
18133 }
18134}
18135
18136/*
18137 * Called when actually executing a function line.
18138 */
18139 void
18140func_line_exec(cookie)
18141 void *cookie;
18142{
18143 funccall_T *fcp = (funccall_T *)cookie;
18144 ufunc_T *fp = fcp->func;
18145
18146 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18147 fp->uf_tml_execed = TRUE;
18148}
18149
18150/*
18151 * Called when done with a function line.
18152 */
18153 void
18154func_line_end(cookie)
18155 void *cookie;
18156{
18157 funccall_T *fcp = (funccall_T *)cookie;
18158 ufunc_T *fp = fcp->func;
18159
18160 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18161 {
18162 if (fp->uf_tml_execed)
18163 {
18164 ++fp->uf_tml_count[fp->uf_tml_idx];
18165 profile_end(&fp->uf_tml_start);
18166 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18167 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18168 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18169 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18170 }
18171 fp->uf_tml_idx = -1;
18172 }
18173}
18174#endif
18175
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176/*
18177 * Return TRUE if the currently active function should be ended, because a
18178 * return was encountered or an error occured. Used inside a ":while".
18179 */
18180 int
18181func_has_ended(cookie)
18182 void *cookie;
18183{
Bram Moolenaar33570922005-01-25 22:26:29 +000018184 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185
18186 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18187 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018188 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 || fcp->returned);
18190}
18191
18192/*
18193 * return TRUE if cookie indicates a function which "abort"s on errors.
18194 */
18195 int
18196func_has_abort(cookie)
18197 void *cookie;
18198{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018199 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200}
18201
18202#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18203typedef enum
18204{
18205 VAR_FLAVOUR_DEFAULT,
18206 VAR_FLAVOUR_SESSION,
18207 VAR_FLAVOUR_VIMINFO
18208} var_flavour_T;
18209
18210static var_flavour_T var_flavour __ARGS((char_u *varname));
18211
18212 static var_flavour_T
18213var_flavour(varname)
18214 char_u *varname;
18215{
18216 char_u *p = varname;
18217
18218 if (ASCII_ISUPPER(*p))
18219 {
18220 while (*(++p))
18221 if (ASCII_ISLOWER(*p))
18222 return VAR_FLAVOUR_SESSION;
18223 return VAR_FLAVOUR_VIMINFO;
18224 }
18225 else
18226 return VAR_FLAVOUR_DEFAULT;
18227}
18228#endif
18229
18230#if defined(FEAT_VIMINFO) || defined(PROTO)
18231/*
18232 * Restore global vars that start with a capital from the viminfo file
18233 */
18234 int
18235read_viminfo_varlist(virp, writing)
18236 vir_T *virp;
18237 int writing;
18238{
18239 char_u *tab;
18240 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018241 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242
18243 if (!writing && (find_viminfo_parameter('!') != NULL))
18244 {
18245 tab = vim_strchr(virp->vir_line + 1, '\t');
18246 if (tab != NULL)
18247 {
18248 *tab++ = '\0'; /* isolate the variable name */
18249 if (*tab == 'S') /* string var */
18250 is_string = TRUE;
18251
18252 tab = vim_strchr(tab, '\t');
18253 if (tab != NULL)
18254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 if (is_string)
18256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018257 tv.v_type = VAR_STRING;
18258 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 }
18261 else
18262 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018263 tv.v_type = VAR_NUMBER;
18264 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018266 set_var(virp->vir_line + 1, &tv, FALSE);
18267 if (is_string)
18268 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 }
18270 }
18271 }
18272
18273 return viminfo_readline(virp);
18274}
18275
18276/*
18277 * Write global vars that start with a capital to the viminfo file
18278 */
18279 void
18280write_viminfo_varlist(fp)
18281 FILE *fp;
18282{
Bram Moolenaar33570922005-01-25 22:26:29 +000018283 hashitem_T *hi;
18284 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018285 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018286 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018287 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018288 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018289 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290
18291 if (find_viminfo_parameter('!') == NULL)
18292 return;
18293
18294 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018295
Bram Moolenaar33570922005-01-25 22:26:29 +000018296 todo = globvarht.ht_used;
18297 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018299 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018301 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018302 this_var = HI2DI(hi);
18303 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018304 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018305 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018306 {
18307 case VAR_STRING: s = "STR"; break;
18308 case VAR_NUMBER: s = "NUM"; break;
18309 default: continue;
18310 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018311 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018312 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18313 if (p != NULL)
18314 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018315 vim_free(tofree);
18316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 }
18318 }
18319}
18320#endif
18321
18322#if defined(FEAT_SESSION) || defined(PROTO)
18323 int
18324store_session_globals(fd)
18325 FILE *fd;
18326{
Bram Moolenaar33570922005-01-25 22:26:29 +000018327 hashitem_T *hi;
18328 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018329 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 char_u *p, *t;
18331
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 todo = globvarht.ht_used;
18333 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018335 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018337 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018338 this_var = HI2DI(hi);
18339 if ((this_var->di_tv.v_type == VAR_NUMBER
18340 || this_var->di_tv.v_type == VAR_STRING)
18341 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018342 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018343 /* Escape special characters with a backslash. Turn a LF and
18344 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018345 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018346 (char_u *)"\\\"\n\r");
18347 if (p == NULL) /* out of memory */
18348 break;
18349 for (t = p; *t != NUL; ++t)
18350 if (*t == '\n')
18351 *t = 'n';
18352 else if (*t == '\r')
18353 *t = 'r';
18354 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018355 this_var->di_key,
18356 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18357 : ' ',
18358 p,
18359 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18360 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018361 || put_eol(fd) == FAIL)
18362 {
18363 vim_free(p);
18364 return FAIL;
18365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 vim_free(p);
18367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 }
18369 }
18370 return OK;
18371}
18372#endif
18373
18374#endif /* FEAT_EVAL */
18375
18376#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18377
18378
18379#ifdef WIN3264
18380/*
18381 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18382 */
18383static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18384static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18385static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18386
18387/*
18388 * Get the short pathname of a file.
18389 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18390 */
18391 static int
18392get_short_pathname(fnamep, bufp, fnamelen)
18393 char_u **fnamep;
18394 char_u **bufp;
18395 int *fnamelen;
18396{
18397 int l,len;
18398 char_u *newbuf;
18399
18400 len = *fnamelen;
18401
18402 l = GetShortPathName(*fnamep, *fnamep, len);
18403 if (l > len - 1)
18404 {
18405 /* If that doesn't work (not enough space), then save the string
18406 * and try again with a new buffer big enough
18407 */
18408 newbuf = vim_strnsave(*fnamep, l);
18409 if (newbuf == NULL)
18410 return 0;
18411
18412 vim_free(*bufp);
18413 *fnamep = *bufp = newbuf;
18414
18415 l = GetShortPathName(*fnamep,*fnamep,l+1);
18416
18417 /* Really should always succeed, as the buffer is big enough */
18418 }
18419
18420 *fnamelen = l;
18421 return 1;
18422}
18423
18424/*
18425 * Create a short path name. Returns the length of the buffer it needs.
18426 * Doesn't copy over the end of the buffer passed in.
18427 */
18428 static int
18429shortpath_for_invalid_fname(fname, bufp, fnamelen)
18430 char_u **fname;
18431 char_u **bufp;
18432 int *fnamelen;
18433{
18434 char_u *s, *p, *pbuf2, *pbuf3;
18435 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018436 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437
18438 /* Make a copy */
18439 len2 = *fnamelen;
18440 pbuf2 = vim_strnsave(*fname, len2);
18441 pbuf3 = NULL;
18442
18443 s = pbuf2 + len2 - 1; /* Find the end */
18444 slen = 1;
18445 plen = len2;
18446
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018447 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 {
18449 --s;
18450 ++slen;
18451 --plen;
18452 }
18453
18454 do
18455 {
18456 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018457 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 {
18459 --s;
18460 ++slen;
18461 --plen;
18462 }
18463 if (s <= pbuf2)
18464 break;
18465
18466 /* Remeber the character that is about to be blatted */
18467 ch = *s;
18468 *s = 0; /* get_short_pathname requires a null-terminated string */
18469
18470 /* Try it in situ */
18471 p = pbuf2;
18472 if (!get_short_pathname(&p, &pbuf3, &plen))
18473 {
18474 vim_free(pbuf2);
18475 return -1;
18476 }
18477 *s = ch; /* Preserve the string */
18478 } while (plen == 0);
18479
18480 if (plen > 0)
18481 {
18482 /* Remeber the length of the new string. */
18483 *fnamelen = len = plen + slen;
18484 vim_free(*bufp);
18485 if (len > len2)
18486 {
18487 /* If there's not enough space in the currently allocated string,
18488 * then copy it to a buffer big enough.
18489 */
18490 *fname= *bufp = vim_strnsave(p, len);
18491 if (*fname == NULL)
18492 return -1;
18493 }
18494 else
18495 {
18496 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18497 *fname = *bufp = pbuf2;
18498 if (p != pbuf2)
18499 strncpy(*fname, p, plen);
18500 pbuf2 = NULL;
18501 }
18502 /* Concat the next bit */
18503 strncpy(*fname + plen, s, slen);
18504 (*fname)[len] = '\0';
18505 }
18506 vim_free(pbuf3);
18507 vim_free(pbuf2);
18508 return 0;
18509}
18510
18511/*
18512 * Get a pathname for a partial path.
18513 */
18514 static int
18515shortpath_for_partial(fnamep, bufp, fnamelen)
18516 char_u **fnamep;
18517 char_u **bufp;
18518 int *fnamelen;
18519{
18520 int sepcount, len, tflen;
18521 char_u *p;
18522 char_u *pbuf, *tfname;
18523 int hasTilde;
18524
18525 /* Count up the path seperators from the RHS.. so we know which part
18526 * of the path to return.
18527 */
18528 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018529 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 if (vim_ispathsep(*p))
18531 ++sepcount;
18532
18533 /* Need full path first (use expand_env() to remove a "~/") */
18534 hasTilde = (**fnamep == '~');
18535 if (hasTilde)
18536 pbuf = tfname = expand_env_save(*fnamep);
18537 else
18538 pbuf = tfname = FullName_save(*fnamep, FALSE);
18539
18540 len = tflen = STRLEN(tfname);
18541
18542 if (!get_short_pathname(&tfname, &pbuf, &len))
18543 return -1;
18544
18545 if (len == 0)
18546 {
18547 /* Don't have a valid filename, so shorten the rest of the
18548 * path if we can. This CAN give us invalid 8.3 filenames, but
18549 * there's not a lot of point in guessing what it might be.
18550 */
18551 len = tflen;
18552 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18553 return -1;
18554 }
18555
18556 /* Count the paths backward to find the beginning of the desired string. */
18557 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018558 {
18559#ifdef FEAT_MBYTE
18560 if (has_mbyte)
18561 p -= mb_head_off(tfname, p);
18562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 if (vim_ispathsep(*p))
18564 {
18565 if (sepcount == 0 || (hasTilde && sepcount == 1))
18566 break;
18567 else
18568 sepcount --;
18569 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 if (hasTilde)
18572 {
18573 --p;
18574 if (p >= tfname)
18575 *p = '~';
18576 else
18577 return -1;
18578 }
18579 else
18580 ++p;
18581
18582 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18583 vim_free(*bufp);
18584 *fnamelen = (int)STRLEN(p);
18585 *bufp = pbuf;
18586 *fnamep = p;
18587
18588 return 0;
18589}
18590#endif /* WIN3264 */
18591
18592/*
18593 * Adjust a filename, according to a string of modifiers.
18594 * *fnamep must be NUL terminated when called. When returning, the length is
18595 * determined by *fnamelen.
18596 * Returns valid flags.
18597 * When there is an error, *fnamep is set to NULL.
18598 */
18599 int
18600modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18601 char_u *src; /* string with modifiers */
18602 int *usedlen; /* characters after src that are used */
18603 char_u **fnamep; /* file name so far */
18604 char_u **bufp; /* buffer for allocated file name or NULL */
18605 int *fnamelen; /* length of fnamep */
18606{
18607 int valid = 0;
18608 char_u *tail;
18609 char_u *s, *p, *pbuf;
18610 char_u dirname[MAXPATHL];
18611 int c;
18612 int has_fullname = 0;
18613#ifdef WIN3264
18614 int has_shortname = 0;
18615#endif
18616
18617repeat:
18618 /* ":p" - full path/file_name */
18619 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18620 {
18621 has_fullname = 1;
18622
18623 valid |= VALID_PATH;
18624 *usedlen += 2;
18625
18626 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18627 if ((*fnamep)[0] == '~'
18628#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18629 && ((*fnamep)[1] == '/'
18630# ifdef BACKSLASH_IN_FILENAME
18631 || (*fnamep)[1] == '\\'
18632# endif
18633 || (*fnamep)[1] == NUL)
18634
18635#endif
18636 )
18637 {
18638 *fnamep = expand_env_save(*fnamep);
18639 vim_free(*bufp); /* free any allocated file name */
18640 *bufp = *fnamep;
18641 if (*fnamep == NULL)
18642 return -1;
18643 }
18644
18645 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018646 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 {
18648 if (vim_ispathsep(*p)
18649 && p[1] == '.'
18650 && (p[2] == NUL
18651 || vim_ispathsep(p[2])
18652 || (p[2] == '.'
18653 && (p[3] == NUL || vim_ispathsep(p[3])))))
18654 break;
18655 }
18656
18657 /* FullName_save() is slow, don't use it when not needed. */
18658 if (*p != NUL || !vim_isAbsName(*fnamep))
18659 {
18660 *fnamep = FullName_save(*fnamep, *p != NUL);
18661 vim_free(*bufp); /* free any allocated file name */
18662 *bufp = *fnamep;
18663 if (*fnamep == NULL)
18664 return -1;
18665 }
18666
18667 /* Append a path separator to a directory. */
18668 if (mch_isdir(*fnamep))
18669 {
18670 /* Make room for one or two extra characters. */
18671 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18672 vim_free(*bufp); /* free any allocated file name */
18673 *bufp = *fnamep;
18674 if (*fnamep == NULL)
18675 return -1;
18676 add_pathsep(*fnamep);
18677 }
18678 }
18679
18680 /* ":." - path relative to the current directory */
18681 /* ":~" - path relative to the home directory */
18682 /* ":8" - shortname path - postponed till after */
18683 while (src[*usedlen] == ':'
18684 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18685 {
18686 *usedlen += 2;
18687 if (c == '8')
18688 {
18689#ifdef WIN3264
18690 has_shortname = 1; /* Postpone this. */
18691#endif
18692 continue;
18693 }
18694 pbuf = NULL;
18695 /* Need full path first (use expand_env() to remove a "~/") */
18696 if (!has_fullname)
18697 {
18698 if (c == '.' && **fnamep == '~')
18699 p = pbuf = expand_env_save(*fnamep);
18700 else
18701 p = pbuf = FullName_save(*fnamep, FALSE);
18702 }
18703 else
18704 p = *fnamep;
18705
18706 has_fullname = 0;
18707
18708 if (p != NULL)
18709 {
18710 if (c == '.')
18711 {
18712 mch_dirname(dirname, MAXPATHL);
18713 s = shorten_fname(p, dirname);
18714 if (s != NULL)
18715 {
18716 *fnamep = s;
18717 if (pbuf != NULL)
18718 {
18719 vim_free(*bufp); /* free any allocated file name */
18720 *bufp = pbuf;
18721 pbuf = NULL;
18722 }
18723 }
18724 }
18725 else
18726 {
18727 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18728 /* Only replace it when it starts with '~' */
18729 if (*dirname == '~')
18730 {
18731 s = vim_strsave(dirname);
18732 if (s != NULL)
18733 {
18734 *fnamep = s;
18735 vim_free(*bufp);
18736 *bufp = s;
18737 }
18738 }
18739 }
18740 vim_free(pbuf);
18741 }
18742 }
18743
18744 tail = gettail(*fnamep);
18745 *fnamelen = (int)STRLEN(*fnamep);
18746
18747 /* ":h" - head, remove "/file_name", can be repeated */
18748 /* Don't remove the first "/" or "c:\" */
18749 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18750 {
18751 valid |= VALID_HEAD;
18752 *usedlen += 2;
18753 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018754 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 --tail;
18756 *fnamelen = (int)(tail - *fnamep);
18757#ifdef VMS
18758 if (*fnamelen > 0)
18759 *fnamelen += 1; /* the path separator is part of the path */
18760#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018761 while (tail > s && !after_pathsep(s, tail))
18762 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 }
18764
18765 /* ":8" - shortname */
18766 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18767 {
18768 *usedlen += 2;
18769#ifdef WIN3264
18770 has_shortname = 1;
18771#endif
18772 }
18773
18774#ifdef WIN3264
18775 /* Check shortname after we have done 'heads' and before we do 'tails'
18776 */
18777 if (has_shortname)
18778 {
18779 pbuf = NULL;
18780 /* Copy the string if it is shortened by :h */
18781 if (*fnamelen < (int)STRLEN(*fnamep))
18782 {
18783 p = vim_strnsave(*fnamep, *fnamelen);
18784 if (p == 0)
18785 return -1;
18786 vim_free(*bufp);
18787 *bufp = *fnamep = p;
18788 }
18789
18790 /* Split into two implementations - makes it easier. First is where
18791 * there isn't a full name already, second is where there is.
18792 */
18793 if (!has_fullname && !vim_isAbsName(*fnamep))
18794 {
18795 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18796 return -1;
18797 }
18798 else
18799 {
18800 int l;
18801
18802 /* Simple case, already have the full-name
18803 * Nearly always shorter, so try first time. */
18804 l = *fnamelen;
18805 if (!get_short_pathname(fnamep, bufp, &l))
18806 return -1;
18807
18808 if (l == 0)
18809 {
18810 /* Couldn't find the filename.. search the paths.
18811 */
18812 l = *fnamelen;
18813 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18814 return -1;
18815 }
18816 *fnamelen = l;
18817 }
18818 }
18819#endif /* WIN3264 */
18820
18821 /* ":t" - tail, just the basename */
18822 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18823 {
18824 *usedlen += 2;
18825 *fnamelen -= (int)(tail - *fnamep);
18826 *fnamep = tail;
18827 }
18828
18829 /* ":e" - extension, can be repeated */
18830 /* ":r" - root, without extension, can be repeated */
18831 while (src[*usedlen] == ':'
18832 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18833 {
18834 /* find a '.' in the tail:
18835 * - for second :e: before the current fname
18836 * - otherwise: The last '.'
18837 */
18838 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18839 s = *fnamep - 2;
18840 else
18841 s = *fnamep + *fnamelen - 1;
18842 for ( ; s > tail; --s)
18843 if (s[0] == '.')
18844 break;
18845 if (src[*usedlen + 1] == 'e') /* :e */
18846 {
18847 if (s > tail)
18848 {
18849 *fnamelen += (int)(*fnamep - (s + 1));
18850 *fnamep = s + 1;
18851#ifdef VMS
18852 /* cut version from the extension */
18853 s = *fnamep + *fnamelen - 1;
18854 for ( ; s > *fnamep; --s)
18855 if (s[0] == ';')
18856 break;
18857 if (s > *fnamep)
18858 *fnamelen = s - *fnamep;
18859#endif
18860 }
18861 else if (*fnamep <= tail)
18862 *fnamelen = 0;
18863 }
18864 else /* :r */
18865 {
18866 if (s > tail) /* remove one extension */
18867 *fnamelen = (int)(s - *fnamep);
18868 }
18869 *usedlen += 2;
18870 }
18871
18872 /* ":s?pat?foo?" - substitute */
18873 /* ":gs?pat?foo?" - global substitute */
18874 if (src[*usedlen] == ':'
18875 && (src[*usedlen + 1] == 's'
18876 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18877 {
18878 char_u *str;
18879 char_u *pat;
18880 char_u *sub;
18881 int sep;
18882 char_u *flags;
18883 int didit = FALSE;
18884
18885 flags = (char_u *)"";
18886 s = src + *usedlen + 2;
18887 if (src[*usedlen + 1] == 'g')
18888 {
18889 flags = (char_u *)"g";
18890 ++s;
18891 }
18892
18893 sep = *s++;
18894 if (sep)
18895 {
18896 /* find end of pattern */
18897 p = vim_strchr(s, sep);
18898 if (p != NULL)
18899 {
18900 pat = vim_strnsave(s, (int)(p - s));
18901 if (pat != NULL)
18902 {
18903 s = p + 1;
18904 /* find end of substitution */
18905 p = vim_strchr(s, sep);
18906 if (p != NULL)
18907 {
18908 sub = vim_strnsave(s, (int)(p - s));
18909 str = vim_strnsave(*fnamep, *fnamelen);
18910 if (sub != NULL && str != NULL)
18911 {
18912 *usedlen = (int)(p + 1 - src);
18913 s = do_string_sub(str, pat, sub, flags);
18914 if (s != NULL)
18915 {
18916 *fnamep = s;
18917 *fnamelen = (int)STRLEN(s);
18918 vim_free(*bufp);
18919 *bufp = s;
18920 didit = TRUE;
18921 }
18922 }
18923 vim_free(sub);
18924 vim_free(str);
18925 }
18926 vim_free(pat);
18927 }
18928 }
18929 /* after using ":s", repeat all the modifiers */
18930 if (didit)
18931 goto repeat;
18932 }
18933 }
18934
18935 return valid;
18936}
18937
18938/*
18939 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18940 * "flags" can be "g" to do a global substitute.
18941 * Returns an allocated string, NULL for error.
18942 */
18943 char_u *
18944do_string_sub(str, pat, sub, flags)
18945 char_u *str;
18946 char_u *pat;
18947 char_u *sub;
18948 char_u *flags;
18949{
18950 int sublen;
18951 regmatch_T regmatch;
18952 int i;
18953 int do_all;
18954 char_u *tail;
18955 garray_T ga;
18956 char_u *ret;
18957 char_u *save_cpo;
18958
18959 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18960 save_cpo = p_cpo;
18961 p_cpo = (char_u *)"";
18962
18963 ga_init2(&ga, 1, 200);
18964
18965 do_all = (flags[0] == 'g');
18966
18967 regmatch.rm_ic = p_ic;
18968 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18969 if (regmatch.regprog != NULL)
18970 {
18971 tail = str;
18972 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18973 {
18974 /*
18975 * Get some space for a temporary buffer to do the substitution
18976 * into. It will contain:
18977 * - The text up to where the match is.
18978 * - The substituted text.
18979 * - The text after the match.
18980 */
18981 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18982 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18983 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18984 {
18985 ga_clear(&ga);
18986 break;
18987 }
18988
18989 /* copy the text up to where the match is */
18990 i = (int)(regmatch.startp[0] - tail);
18991 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18992 /* add the substituted text */
18993 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18994 + ga.ga_len + i, TRUE, TRUE, FALSE);
18995 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 /* avoid getting stuck on a match with an empty string */
18997 if (tail == regmatch.endp[0])
18998 {
18999 if (*tail == NUL)
19000 break;
19001 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19002 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 }
19004 else
19005 {
19006 tail = regmatch.endp[0];
19007 if (*tail == NUL)
19008 break;
19009 }
19010 if (!do_all)
19011 break;
19012 }
19013
19014 if (ga.ga_data != NULL)
19015 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19016
19017 vim_free(regmatch.regprog);
19018 }
19019
19020 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19021 ga_clear(&ga);
19022 p_cpo = save_cpo;
19023
19024 return ret;
19025}
19026
19027#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */