blob: b33412e9d8d3265ad1a1a404049af74f6adad3e4 [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 Moolenaar49cd9572005-01-03 21:06:01 +0000110
111/*
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 */
123hashtab_T compat_hashtab;
124
125/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000126 * Array to hold the hashtab with variables local to each sourced script.
127 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000129typedef struct
130{
131 dictitem_T sv_var;
132 dict_T sv_dict;
133} scriptvar_T;
134
135static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
136#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
137#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139static int echo_attr = 0; /* attributes used for ":echo" */
140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000141/* Values for trans_function_name() argument: */
142#define TFN_INT 1 /* internal function name OK */
143#define TFN_QUIET 2 /* no error messages */
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
146 * Structure to hold info for a user function.
147 */
148typedef struct ufunc ufunc_T;
149
150struct ufunc
151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000152 int uf_varargs; /* variable nr of arguments */
153 int uf_flags;
154 int uf_calls; /* nr of active calls */
155 garray_T uf_args; /* arguments */
156 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000157#ifdef FEAT_PROFILE
158 int uf_profiling; /* TRUE when func is being profiled */
159 /* profiling the function as a whole */
160 int uf_tm_count; /* nr of calls */
161 proftime_T uf_tm_total; /* time spend in function + children */
162 proftime_T uf_tm_self; /* time spend in function itself */
163 proftime_T uf_tm_start; /* time at function call */
164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
167 proftime_T *uf_tml_total; /* time spend in a line + children */
168 proftime_T *uf_tml_self; /* time spend in a line itself */
169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
267#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
272 int vv_len; /* length of name */
273 dictitem_T vv_di; /* value and name for key */
274 char vv_filler[16]; /* space for LONGEST name below!!! */
275 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
276} vimvars[VV_LEN] =
277{
278 /*
279 * The order here must match the VV_ defines in vim.h!
280 * Initializing a union does not work, leave tv.vval empty to get zero's.
281 */
282 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
283 {VV_NAME("count1", VAR_NUMBER), VV_RO},
284 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
285 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
286 {VV_NAME("warningmsg", VAR_STRING), 0},
287 {VV_NAME("statusmsg", VAR_STRING), 0},
288 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
289 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
290 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
291 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
292 {VV_NAME("termresponse", VAR_STRING), VV_RO},
293 {VV_NAME("fname", VAR_STRING), VV_RO},
294 {VV_NAME("lang", VAR_STRING), VV_RO},
295 {VV_NAME("lc_time", VAR_STRING), VV_RO},
296 {VV_NAME("ctype", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
298 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
299 {VV_NAME("fname_in", VAR_STRING), VV_RO},
300 {VV_NAME("fname_out", VAR_STRING), VV_RO},
301 {VV_NAME("fname_new", VAR_STRING), VV_RO},
302 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
303 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
304 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
307 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
308 {VV_NAME("progname", VAR_STRING), VV_RO},
309 {VV_NAME("servername", VAR_STRING), VV_RO},
310 {VV_NAME("dying", VAR_NUMBER), VV_RO},
311 {VV_NAME("exception", VAR_STRING), VV_RO},
312 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
313 {VV_NAME("register", VAR_STRING), VV_RO},
314 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
315 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000316 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
317 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000318 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000319 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
320 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000321 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
322 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
323 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
324 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
325 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000326};
327
328/* shorthand */
329#define vv_type vv_di.di_tv.v_type
330#define vv_nr vv_di.di_tv.vval.v_number
331#define vv_str vv_di.di_tv.vval.v_string
332#define vv_tv vv_di.di_tv
333
334/*
335 * The v: variables are stored in dictionary "vimvardict".
336 * "vimvars_var" is the variable that is used for the "l:" scope.
337 */
338static dict_T vimvardict;
339static dictitem_T vimvars_var;
340#define vimvarht vimvardict.dv_hashtab
341
342static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
343static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
349static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000350static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000351static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355static list_T *list_alloc __ARGS((void));
356static void list_unref __ARGS((list_T *l));
357static void list_free __ARGS((list_T *l));
358static listitem_T *listitem_alloc __ARGS((void));
359static void listitem_free __ARGS((listitem_T *item));
360static void listitem_remove __ARGS((list_T *l, listitem_T *item));
361static long list_len __ARGS((list_T *l));
362static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
363static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
364static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
365static int string_isa_number __ARGS((char_u *s));
366static listitem_T *list_find __ARGS((list_T *l, long n));
367static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000368static void list_append __ARGS((list_T *l, listitem_T *item));
369static int list_append_tv __ARGS((list_T *l, typval_T *tv));
370static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
371static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
372static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000373static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000374static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
375static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000376static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar33570922005-01-25 22:26:29 +0000378static void dict_unref __ARGS((dict_T *d));
379static void dict_free __ARGS((dict_T *d));
380static dictitem_T *dictitem_alloc __ARGS((char_u *key));
381static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
382static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
383static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000384static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int dict_add __ARGS((dict_T *d, dictitem_T *item));
386static long dict_len __ARGS((dict_T *d));
387static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
388static char_u *dict2string __ARGS((typval_T *tv));
389static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390
391static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
392static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
393static char_u *string_quote __ARGS((char_u *str, int function));
394static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int find_internal_func __ARGS((char_u *name));
396static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
397static 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));
398static 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 +0000399static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400
401static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
425static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000464static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000492static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000509static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513#ifdef vim_mkdir
514static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000521static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000539static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
545#ifdef HAVE_STRFTIME
546static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
547#endif
548static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000560static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577
578static win_T *find_win_by_nr __ARGS((typval_T *vp));
579static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
580static int get_env_len __ARGS((char_u **arg));
581static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
584static int eval_isnamec __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000585static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
586static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static typval_T *alloc_tv __ARGS((void));
588static typval_T *alloc_string_tv __ARGS((char_u *string));
589static void free_tv __ARGS((typval_T *varp));
590static void clear_tv __ARGS((typval_T *varp));
591static void init_tv __ARGS((typval_T *varp));
592static long get_tv_number __ARGS((typval_T *varp));
593static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
594static char_u *get_tv_string __ARGS((typval_T *varp));
595static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
596static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000597static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
599static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
600static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
601static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
602static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
603static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
604static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000605static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000607static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
609static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
610static int eval_fname_script __ARGS((char_u *p));
611static int eval_fname_sid __ARGS((char_u *p));
612static void list_func_head __ARGS((ufunc_T *fp, int indent));
613static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
614static ufunc_T *find_func __ARGS((char_u *name));
615static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000616static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000617#ifdef FEAT_PROFILE
618static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000619static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
620static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
621static int
622# ifdef __BORLANDC__
623 _RTLENTRYF
624# endif
625 prof_total_cmp __ARGS((const void *s1, const void *s2));
626static int
627# ifdef __BORLANDC__
628 _RTLENTRYF
629# endif
630 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000631#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000632static int script_autoload __ARGS((char_u *name));
633static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void func_free __ARGS((ufunc_T *fp));
635static void func_unref __ARGS((char_u *name));
636static void func_ref __ARGS((char_u *name));
637static 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));
638static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
639
640static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
641
642static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
643static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
644static char_u *skip_var_one __ARGS((char_u *arg));
645static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
646static void list_glob_vars __ARGS((void));
647static void list_buf_vars __ARGS((void));
648static void list_win_vars __ARGS((void));
649static void list_vim_vars __ARGS((void));
650static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
651static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
652static int check_changedtick __ARGS((char_u *arg));
653static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
654static void clear_lval __ARGS((lval_T *lp));
655static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
656static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
657static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
658static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
659static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000660static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000662static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
663static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000664static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665
666/*
667 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000668 */
669 void
670eval_init()
671{
Bram Moolenaar33570922005-01-25 22:26:29 +0000672 int i;
673 struct vimvar *p;
674
675 init_var_dict(&globvardict, &globvars_var);
676 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000677 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000679
680 for (i = 0; i < VV_LEN; ++i)
681 {
682 p = &vimvars[i];
683 STRCPY(p->vv_di.di_key, p->vv_name);
684 if (p->vv_flags & VV_RO)
685 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
686 else if (p->vv_flags & VV_RO_SBX)
687 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
688 else
689 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000690
691 /* add to v: scope dict, unless the value is not always available */
692 if (p->vv_type != VAR_UNKNOWN)
693 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000694 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000695 /* add to compat scope dict */
696 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000697 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000698}
699
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 * Return the name of the executed function.
702 */
703 char_u *
704func_name(cookie)
705 void *cookie;
706{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000707 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708}
709
710/*
711 * Return the address holding the next breakpoint line for a funccall cookie.
712 */
713 linenr_T *
714func_breakpoint(cookie)
715 void *cookie;
716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718}
719
720/*
721 * Return the address holding the debug tick for a funccall cookie.
722 */
723 int *
724func_dbg_tick(cookie)
725 void *cookie;
726{
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728}
729
730/*
731 * Return the nesting level for a funccall cookie.
732 */
733 int
734func_level(cookie)
735 void *cookie;
736{
Bram Moolenaar33570922005-01-25 22:26:29 +0000737 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
740/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000741funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
743/*
744 * Return TRUE when a function was ended by a ":return" command.
745 */
746 int
747current_func_returned()
748{
749 return current_funccal->returned;
750}
751
752
753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * Set an internal variable to a string value. Creates the variable if it does
755 * not already exist.
756 */
757 void
758set_internal_string_var(name, value)
759 char_u *name;
760 char_u *value;
761{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000762 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000763 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 val = vim_strsave(value);
766 if (val != NULL)
767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000768 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000769 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000771 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000772 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774 }
775}
776
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000777static lval_T *redir_lval = NULL;
778static char_u *redir_endp = NULL;
779static char_u *redir_varname = NULL;
780
781/*
782 * Start recording command output to a variable
783 * Returns OK if successfully completed the setup. FAIL otherwise.
784 */
785 int
786var_redir_start(name, append)
787 char_u *name;
788 int append; /* append to an existing variable */
789{
790 int save_emsg;
791 int err;
792 typval_T tv;
793
794 /* Make sure a valid variable name is specified */
795 if (!eval_isnamec(*name) || VIM_ISDIGIT(*name))
796 {
797 EMSG(_(e_invarg));
798 return FAIL;
799 }
800
801 redir_varname = vim_strsave(name);
802 if (redir_varname == NULL)
803 return FAIL;
804
805 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
806 if (redir_lval == NULL)
807 {
808 var_redir_stop();
809 return FAIL;
810 }
811
812 /* Parse the variable name (can be a dict or list entry). */
813 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE);
814 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
815 {
816 if (redir_endp != NULL && *redir_endp != NUL)
817 /* Trailing characters are present after the variable name */
818 EMSG(_(e_trailing));
819 else
820 EMSG(_(e_invarg));
821 var_redir_stop();
822 return FAIL;
823 }
824
825 /* check if we can write to the variable: set it to or append an empty
826 * string */
827 save_emsg = did_emsg;
828 did_emsg = FALSE;
829 tv.v_type = VAR_STRING;
830 tv.vval.v_string = (char_u *)"";
831 if (append)
832 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
833 else
834 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
835 err = did_emsg;
836 did_emsg += save_emsg;
837 if (err)
838 {
839 var_redir_stop();
840 return FAIL;
841 }
842 if (redir_lval->ll_newkey != NULL)
843 {
844 /* Dictionary item was created, don't do it again. */
845 vim_free(redir_lval->ll_newkey);
846 redir_lval->ll_newkey = NULL;
847 }
848
849 return OK;
850}
851
852/*
853 * Append "value[len]" to the variable set by var_redir_start().
854 */
855 void
856var_redir_str(value, len)
857 char_u *value;
858 int len;
859{
860 char_u *val;
861 typval_T tv;
862 int save_emsg;
863 int err;
864
865 if (redir_lval == NULL)
866 return;
867
868 if (len == -1)
869 /* Append the entire string */
870 val = vim_strsave(value);
871 else
872 /* Append only the specified number of characters */
873 val = vim_strnsave(value, len);
874 if (val == NULL)
875 return;
876
877 tv.v_type = VAR_STRING;
878 tv.vval.v_string = val;
879
880 save_emsg = did_emsg;
881 did_emsg = FALSE;
882 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
883 err = did_emsg;
884 did_emsg += save_emsg;
885 if (err)
886 var_redir_stop();
887
888 vim_free(tv.vval.v_string);
889}
890
891/*
892 * Stop redirecting command output to a variable.
893 */
894 void
895var_redir_stop()
896{
897 if (redir_lval != NULL)
898 {
899 clear_lval(redir_lval);
900 vim_free(redir_lval);
901 redir_lval = NULL;
902 }
903 vim_free(redir_varname);
904 redir_varname = NULL;
905}
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907# if defined(FEAT_MBYTE) || defined(PROTO)
908 int
909eval_charconvert(enc_from, enc_to, fname_from, fname_to)
910 char_u *enc_from;
911 char_u *enc_to;
912 char_u *fname_from;
913 char_u *fname_to;
914{
915 int err = FALSE;
916
917 set_vim_var_string(VV_CC_FROM, enc_from, -1);
918 set_vim_var_string(VV_CC_TO, enc_to, -1);
919 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
920 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
921 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
922 err = TRUE;
923 set_vim_var_string(VV_CC_FROM, NULL, -1);
924 set_vim_var_string(VV_CC_TO, NULL, -1);
925 set_vim_var_string(VV_FNAME_IN, NULL, -1);
926 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
927
928 if (err)
929 return FAIL;
930 return OK;
931}
932# endif
933
934# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
935 int
936eval_printexpr(fname, args)
937 char_u *fname;
938 char_u *args;
939{
940 int err = FALSE;
941
942 set_vim_var_string(VV_FNAME_IN, fname, -1);
943 set_vim_var_string(VV_CMDARG, args, -1);
944 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
945 err = TRUE;
946 set_vim_var_string(VV_FNAME_IN, NULL, -1);
947 set_vim_var_string(VV_CMDARG, NULL, -1);
948
949 if (err)
950 {
951 mch_remove(fname);
952 return FAIL;
953 }
954 return OK;
955}
956# endif
957
958# if defined(FEAT_DIFF) || defined(PROTO)
959 void
960eval_diff(origfile, newfile, outfile)
961 char_u *origfile;
962 char_u *newfile;
963 char_u *outfile;
964{
965 int err = FALSE;
966
967 set_vim_var_string(VV_FNAME_IN, origfile, -1);
968 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
969 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
970 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
971 set_vim_var_string(VV_FNAME_IN, NULL, -1);
972 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
973 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
974}
975
976 void
977eval_patch(origfile, difffile, outfile)
978 char_u *origfile;
979 char_u *difffile;
980 char_u *outfile;
981{
982 int err;
983
984 set_vim_var_string(VV_FNAME_IN, origfile, -1);
985 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
986 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
987 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
988 set_vim_var_string(VV_FNAME_IN, NULL, -1);
989 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
990 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
991}
992# endif
993
994/*
995 * Top level evaluation function, returning a boolean.
996 * Sets "error" to TRUE if there was an error.
997 * Return TRUE or FALSE.
998 */
999 int
1000eval_to_bool(arg, error, nextcmd, skip)
1001 char_u *arg;
1002 int *error;
1003 char_u **nextcmd;
1004 int skip; /* only parse, don't execute */
1005{
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 int retval = FALSE;
1008
1009 if (skip)
1010 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 else
1014 {
1015 *error = FALSE;
1016 if (!skip)
1017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 retval = (get_tv_number(&tv) != 0);
1019 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021 }
1022 if (skip)
1023 --emsg_skip;
1024
1025 return retval;
1026}
1027
1028/*
1029 * Top level evaluation function, returning a string. If "skip" is TRUE,
1030 * only parsing to "nextcmd" is done, without reporting errors. Return
1031 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1032 */
1033 char_u *
1034eval_to_string_skip(arg, nextcmd, skip)
1035 char_u *arg;
1036 char_u **nextcmd;
1037 int skip; /* only parse, don't execute */
1038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 char_u *retval;
1041
1042 if (skip)
1043 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001044 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 retval = NULL;
1046 else
1047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001048 retval = vim_strsave(get_tv_string(&tv));
1049 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051 if (skip)
1052 --emsg_skip;
1053
1054 return retval;
1055}
1056
1057/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001058 * Skip over an expression at "*pp".
1059 * Return FAIL for an error, OK otherwise.
1060 */
1061 int
1062skip_expr(pp)
1063 char_u **pp;
1064{
Bram Moolenaar33570922005-01-25 22:26:29 +00001065 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001066
1067 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001068 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001069}
1070
1071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 * Top level evaluation function, returning a string.
1073 * Return pointer to allocated memory, or NULL for failure.
1074 */
1075 char_u *
1076eval_to_string(arg, nextcmd)
1077 char_u *arg;
1078 char_u **nextcmd;
1079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 char_u *retval;
1082
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 retval = NULL;
1085 else
1086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 retval = vim_strsave(get_tv_string(&tv));
1088 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090
1091 return retval;
1092}
1093
1094/*
1095 * Call eval_to_string() with "sandbox" set and not using local variables.
1096 */
1097 char_u *
1098eval_to_string_safe(arg, nextcmd)
1099 char_u *arg;
1100 char_u **nextcmd;
1101{
1102 char_u *retval;
1103 void *save_funccalp;
1104
1105 save_funccalp = save_funccal();
1106 ++sandbox;
1107 retval = eval_to_string(arg, nextcmd);
1108 --sandbox;
1109 restore_funccal(save_funccalp);
1110 return retval;
1111}
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113/*
1114 * Top level evaluation function, returning a number.
1115 * Evaluates "expr" silently.
1116 * Returns -1 for an error.
1117 */
1118 int
1119eval_to_number(expr)
1120 char_u *expr;
1121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001124 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126 ++emsg_off;
1127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 retval = -1;
1130 else
1131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 retval = get_tv_number(&rettv);
1133 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 --emsg_off;
1136
1137 return retval;
1138}
1139
1140#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1141/*
1142 * Call some vimL function and return the result as a string
1143 * Uses argv[argc] for the function arguments.
1144 */
1145 char_u *
1146call_vim_function(func, argc, argv, safe)
1147 char_u *func;
1148 int argc;
1149 char_u **argv;
1150 int safe; /* use the sandbox */
1151{
1152 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001153 typval_T rettv;
1154 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 long n;
1156 int len;
1157 int i;
1158 int doesrange;
1159 void *save_funccalp = NULL;
1160
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (argvars == NULL)
1163 return NULL;
1164
1165 for (i = 0; i < argc; i++)
1166 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001167 /* Pass a NULL or empty argument as an empty string */
1168 if (argv[i] == NULL || *argv[i] == NUL)
1169 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001170 argvars[i].v_type = VAR_STRING;
1171 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001172 continue;
1173 }
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 /* Recognize a number argument, the others must be strings. */
1176 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1177 if (len != 0 && len == (int)STRLEN(argv[i]))
1178 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001179 argvars[i].v_type = VAR_NUMBER;
1180 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182 else
1183 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001184 argvars[i].v_type = VAR_STRING;
1185 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 }
1188
1189 if (safe)
1190 {
1191 save_funccalp = save_funccal();
1192 ++sandbox;
1193 }
1194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1196 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001198 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 vim_free(argvars);
1203
1204 if (safe)
1205 {
1206 --sandbox;
1207 restore_funccal(save_funccalp);
1208 }
1209 return retval;
1210}
1211#endif
1212
1213/*
1214 * Save the current function call pointer, and set it to NULL.
1215 * Used when executing autocommands and for ":source".
1216 */
1217 void *
1218save_funccal()
1219{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001220 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 current_funccal = NULL;
1223 return (void *)fc;
1224}
1225
1226 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001227restore_funccal(vfc)
1228 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001230 funccall_T *fc = (funccall_T *)vfc;
1231
1232 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233}
1234
Bram Moolenaar05159a02005-02-26 23:04:13 +00001235#if defined(FEAT_PROFILE) || defined(PROTO)
1236/*
1237 * Prepare profiling for entering a child or something else that is not
1238 * counted for the script/function itself.
1239 * Should always be called in pair with prof_child_exit().
1240 */
1241 void
1242prof_child_enter(tm)
1243 proftime_T *tm; /* place to store waittime */
1244{
1245 funccall_T *fc = current_funccal;
1246
1247 if (fc != NULL && fc->func->uf_profiling)
1248 profile_start(&fc->prof_child);
1249 script_prof_save(tm);
1250}
1251
1252/*
1253 * Take care of time spent in a child.
1254 * Should always be called after prof_child_enter().
1255 */
1256 void
1257prof_child_exit(tm)
1258 proftime_T *tm; /* where waittime was stored */
1259{
1260 funccall_T *fc = current_funccal;
1261
1262 if (fc != NULL && fc->func->uf_profiling)
1263 {
1264 profile_end(&fc->prof_child);
1265 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1266 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1267 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1268 }
1269 script_prof_restore(tm);
1270}
1271#endif
1272
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274#ifdef FEAT_FOLDING
1275/*
1276 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1277 * it in "*cp". Doesn't give error messages.
1278 */
1279 int
1280eval_foldexpr(arg, cp)
1281 char_u *arg;
1282 int *cp;
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval;
1286 char_u *s;
1287
1288 ++emsg_off;
1289 ++sandbox;
1290 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 retval = 0;
1293 else
1294 {
1295 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 if (tv.v_type == VAR_NUMBER)
1297 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001298 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 retval = 0;
1300 else
1301 {
1302 /* If the result is a string, check if there is a non-digit before
1303 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (!VIM_ISDIGIT(*s) && *s != '-')
1306 *cp = *s++;
1307 retval = atol((char *)s);
1308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 --emsg_off;
1312 --sandbox;
1313
1314 return retval;
1315}
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 * ":let" list all variable values
1320 * ":let var1 var2" list variable values
1321 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001322 * ":let var += expr" assignment command.
1323 * ":let var -= expr" assignment command.
1324 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 */
1327 void
1328ex_let(eap)
1329 exarg_T *eap;
1330{
1331 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 int var_count = 0;
1336 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001337 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001339 expr = skip_var_list(arg, &var_count, &semicolon);
1340 if (expr == NULL)
1341 return;
1342 expr = vim_strchr(expr, '=');
1343 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001345 /*
1346 * ":let" without "=": list variables
1347 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001348 if (*arg == '[')
1349 EMSG(_(e_invarg));
1350 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 /* ":let var1 var2" */
1352 arg = list_arg_vars(eap, arg);
1353 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001356 list_glob_vars();
1357 list_buf_vars();
1358 list_win_vars();
1359 list_vim_vars();
1360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 eap->nextcmd = check_nextcmd(arg);
1362 }
1363 else
1364 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 op[0] = '=';
1366 op[1] = NUL;
1367 if (expr > arg)
1368 {
1369 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1370 op[0] = expr[-1]; /* +=, -= or .= */
1371 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (eap->skip)
1375 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001376 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (eap->skip)
1378 {
1379 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 --emsg_skip;
1382 }
1383 else if (i != FAIL)
1384 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001385 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001386 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 }
1390}
1391
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001392/*
1393 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1394 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 * When "nextchars" is not NULL it points to a string with characters that
1396 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1397 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001398 * Returns OK or FAIL;
1399 */
1400 static int
1401ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1402 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001403 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001404 int copy; /* copy values from "tv", don't move */
1405 int semicolon; /* from skip_var_list() */
1406 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001407 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408{
1409 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001410 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001411 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 listitem_T *item;
1413 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414
1415 if (*arg != '[')
1416 {
1417 /*
1418 * ":let var = expr" or ":for var in list"
1419 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001420 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 return FAIL;
1422 return OK;
1423 }
1424
1425 /*
1426 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1427 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001428 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 {
1430 EMSG(_(e_listreq));
1431 return FAIL;
1432 }
1433
1434 i = list_len(l);
1435 if (semicolon == 0 && var_count < i)
1436 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001437 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001438 return FAIL;
1439 }
1440 if (var_count - semicolon > i)
1441 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001442 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001443 return FAIL;
1444 }
1445
1446 item = l->lv_first;
1447 while (*arg != ']')
1448 {
1449 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001450 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001451 item = item->li_next;
1452 if (arg == NULL)
1453 return FAIL;
1454
1455 arg = skipwhite(arg);
1456 if (*arg == ';')
1457 {
1458 /* Put the rest of the list (may be empty) in the var after ';'.
1459 * Create a new list for this. */
1460 l = list_alloc();
1461 if (l == NULL)
1462 return FAIL;
1463 while (item != NULL)
1464 {
1465 list_append_tv(l, &item->li_tv);
1466 item = item->li_next;
1467 }
1468
1469 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001470 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001471 ltv.vval.v_list = l;
1472 l->lv_refcount = 1;
1473
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001474 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1475 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001476 clear_tv(&ltv);
1477 if (arg == NULL)
1478 return FAIL;
1479 break;
1480 }
1481 else if (*arg != ',' && *arg != ']')
1482 {
1483 EMSG2(_(e_intern2), "ex_let_vars()");
1484 return FAIL;
1485 }
1486 }
1487
1488 return OK;
1489}
1490
1491/*
1492 * Skip over assignable variable "var" or list of variables "[var, var]".
1493 * Used for ":let varvar = expr" and ":for varvar in expr".
1494 * For "[var, var]" increment "*var_count" for each variable.
1495 * for "[var, var; var]" set "semicolon".
1496 * Return NULL for an error.
1497 */
1498 static char_u *
1499skip_var_list(arg, var_count, semicolon)
1500 char_u *arg;
1501 int *var_count;
1502 int *semicolon;
1503{
1504 char_u *p, *s;
1505
1506 if (*arg == '[')
1507 {
1508 /* "[var, var]": find the matching ']'. */
1509 p = arg;
1510 while (1)
1511 {
1512 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1513 s = skip_var_one(p);
1514 if (s == p)
1515 {
1516 EMSG2(_(e_invarg2), p);
1517 return NULL;
1518 }
1519 ++*var_count;
1520
1521 p = skipwhite(s);
1522 if (*p == ']')
1523 break;
1524 else if (*p == ';')
1525 {
1526 if (*semicolon == 1)
1527 {
1528 EMSG(_("Double ; in list of variables"));
1529 return NULL;
1530 }
1531 *semicolon = 1;
1532 }
1533 else if (*p != ',')
1534 {
1535 EMSG2(_(e_invarg2), p);
1536 return NULL;
1537 }
1538 }
1539 return p + 1;
1540 }
1541 else
1542 return skip_var_one(arg);
1543}
1544
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001545/*
1546 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1547 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548 static char_u *
1549skip_var_one(arg)
1550 char_u *arg;
1551{
1552 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1553 ++arg;
1554 return find_name_end(arg, NULL, NULL, TRUE);
1555}
1556
Bram Moolenaara7043832005-01-21 11:56:39 +00001557/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001558 * List variables for hashtab "ht" with prefix "prefix".
1559 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001561 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001562list_hashtable_vars(ht, prefix, empty)
1563 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001564 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001565 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001566{
Bram Moolenaar33570922005-01-25 22:26:29 +00001567 hashitem_T *hi;
1568 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001569 int todo;
1570
1571 todo = ht->ht_used;
1572 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1573 {
1574 if (!HASHITEM_EMPTY(hi))
1575 {
1576 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001577 di = HI2DI(hi);
1578 if (empty || di->di_tv.v_type != VAR_STRING
1579 || di->di_tv.vval.v_string != NULL)
1580 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001581 }
1582 }
1583}
1584
1585/*
1586 * List global variables.
1587 */
1588 static void
1589list_glob_vars()
1590{
Bram Moolenaar33570922005-01-25 22:26:29 +00001591 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001592}
1593
1594/*
1595 * List buffer variables.
1596 */
1597 static void
1598list_buf_vars()
1599{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600 char_u numbuf[NUMBUFLEN];
1601
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001603
1604 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1605 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001606}
1607
1608/*
1609 * List window variables.
1610 */
1611 static void
1612list_win_vars()
1613{
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001615}
1616
1617/*
1618 * List Vim variables.
1619 */
1620 static void
1621list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001622{
Bram Moolenaar33570922005-01-25 22:26:29 +00001623 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624}
1625
1626/*
1627 * List variables in "arg".
1628 */
1629 static char_u *
1630list_arg_vars(eap, arg)
1631 exarg_T *eap;
1632 char_u *arg;
1633{
1634 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001635 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001637 char_u *name_start;
1638 char_u *arg_subsc;
1639 char_u *tofree;
1640 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001641
1642 while (!ends_excmd(*arg) && !got_int)
1643 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001644 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001646 arg = find_name_end(arg, NULL, NULL, TRUE);
1647 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1648 {
1649 emsg_severe = TRUE;
1650 EMSG(_(e_trailing));
1651 break;
1652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001654 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001656 /* get_name_len() takes care of expanding curly braces */
1657 name_start = name = arg;
1658 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1659 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001661 /* This is mainly to keep test 49 working: when expanding
1662 * curly braces fails overrule the exception error message. */
1663 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001665 emsg_severe = TRUE;
1666 EMSG2(_(e_invarg2), arg);
1667 break;
1668 }
1669 error = TRUE;
1670 }
1671 else
1672 {
1673 if (tofree != NULL)
1674 name = tofree;
1675 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 else
1678 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001679 /* handle d.key, l[idx], f(expr) */
1680 arg_subsc = arg;
1681 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001682 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001683 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001684 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001685 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001686 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001687 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001688 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001689 case 'g': list_glob_vars(); break;
1690 case 'b': list_buf_vars(); break;
1691 case 'w': list_win_vars(); break;
1692 case 'v': list_vim_vars(); break;
1693 default:
1694 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001695 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001696 }
1697 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001698 {
1699 char_u numbuf[NUMBUFLEN];
1700 char_u *tf;
1701 int c;
1702 char_u *s;
1703
1704 s = echo_string(&tv, &tf, numbuf);
1705 c = *arg;
1706 *arg = NUL;
1707 list_one_var_a((char_u *)"",
1708 arg == arg_subsc ? name : name_start,
1709 tv.v_type, s == NULL ? (char_u *)"" : s);
1710 *arg = c;
1711 vim_free(tf);
1712 }
1713 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001715 }
1716 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001717
1718 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001720
1721 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 }
1723
1724 return arg;
1725}
1726
1727/*
1728 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1729 * Returns a pointer to the char just after the var name.
1730 * Returns NULL if there is an error.
1731 */
1732 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001733ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001735 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001739{
1740 int c1;
1741 char_u *name;
1742 char_u *p;
1743 char_u *arg_end = NULL;
1744 int len;
1745 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747
1748 /*
1749 * ":let $VAR = expr": Set environment variable.
1750 */
1751 if (*arg == '$')
1752 {
1753 /* Find the end of the name. */
1754 ++arg;
1755 name = arg;
1756 len = get_env_len(&arg);
1757 if (len == 0)
1758 EMSG2(_(e_invarg2), name - 1);
1759 else
1760 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 if (op != NULL && (*op == '+' || *op == '-'))
1762 EMSG2(_(e_letwrong), op);
1763 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 EMSG(_(e_letunexp));
1766 else
1767 {
1768 c1 = name[len];
1769 name[len] = NUL;
1770 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 if (op != NULL && *op == '.')
1772 {
1773 int mustfree = FALSE;
1774 char_u *s = vim_getenv(name, &mustfree);
1775
1776 if (s != NULL)
1777 {
1778 p = tofree = concat_str(s, p);
1779 if (mustfree)
1780 vim_free(s);
1781 }
1782 }
1783 if (p != NULL)
1784 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 if (STRICMP(name, "HOME") == 0)
1786 init_homedir();
1787 else if (didset_vim && STRICMP(name, "VIM") == 0)
1788 didset_vim = FALSE;
1789 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1790 didset_vimruntime = FALSE;
1791 name[len] = c1;
1792 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 }
1795 }
1796 }
1797
1798 /*
1799 * ":let &option = expr": Set option value.
1800 * ":let &l:option = expr": Set local option value.
1801 * ":let &g:option = expr": Set global option value.
1802 */
1803 else if (*arg == '&')
1804 {
1805 /* Find the end of the name. */
1806 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807 if (p == NULL || (endchars != NULL
1808 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 EMSG(_(e_letunexp));
1810 else
1811 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 long n;
1813 int opt_type;
1814 long numval;
1815 char_u *stringval = NULL;
1816 char_u *s;
1817
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 c1 = *p;
1819 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820
1821 n = get_tv_number(tv);
1822 s = get_tv_string(tv);
1823 if (op != NULL && *op != '=')
1824 {
1825 opt_type = get_option_value(arg, &numval,
1826 &stringval, opt_flags);
1827 if ((opt_type == 1 && *op == '.')
1828 || (opt_type == 0 && *op != '.'))
1829 EMSG2(_(e_letwrong), op);
1830 else
1831 {
1832 if (opt_type == 1) /* number */
1833 {
1834 if (*op == '+')
1835 n = numval + n;
1836 else
1837 n = numval - n;
1838 }
1839 else if (opt_type == 0 && stringval != NULL) /* string */
1840 {
1841 s = concat_str(stringval, s);
1842 vim_free(stringval);
1843 stringval = s;
1844 }
1845 }
1846 }
1847 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 *p = c1;
1849 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001850 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 }
1852 }
1853
1854 /*
1855 * ":let @r = expr": Set register contents.
1856 */
1857 else if (*arg == '@')
1858 {
1859 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 if (op != NULL && (*op == '+' || *op == '-'))
1861 EMSG2(_(e_letwrong), op);
1862 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 EMSG(_(e_letunexp));
1865 else
1866 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 char_u *tofree = NULL;
1868 char_u *s;
1869
1870 p = get_tv_string(tv);
1871 if (op != NULL && *op == '.')
1872 {
1873 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1874 if (s != NULL)
1875 {
1876 p = tofree = concat_str(s, p);
1877 vim_free(s);
1878 }
1879 }
1880 if (p != NULL)
1881 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 }
1885 }
1886
1887 /*
1888 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001889 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001891 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1896 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001898 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1899 EMSG(_(e_letunexp));
1900 else
1901 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001903 arg_end = p;
1904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 }
1908
1909 else
1910 EMSG2(_(e_invarg2), arg);
1911
1912 return arg_end;
1913}
1914
1915/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001916 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1917 */
1918 static int
1919check_changedtick(arg)
1920 char_u *arg;
1921{
1922 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1923 {
1924 EMSG2(_(e_readonlyvar), arg);
1925 return TRUE;
1926 }
1927 return FALSE;
1928}
1929
1930/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001931 * Get an lval: variable, Dict item or List item that can be assigned a value
1932 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1933 * "name.key", "name.key[expr]" etc.
1934 * Indexing only works if "name" is an existing List or Dictionary.
1935 * "name" points to the start of the name.
1936 * If "rettv" is not NULL it points to the value to be assigned.
1937 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1938 * wrong; must end in space or cmd separator.
1939 *
1940 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001941 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 */
1944 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001945get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 typval_T *rettv;
1948 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001949 int unlet;
1950 int skip;
1951 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 char_u *expr_start, *expr_end;
1955 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 dictitem_T *v;
1957 typval_T var1;
1958 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001959 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001966 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967
1968 if (skip)
1969 {
1970 /* When skipping just find the end of the name. */
1971 lp->ll_name = name;
1972 return find_name_end(name, NULL, NULL, TRUE);
1973 }
1974
1975 /* Find the end of the name. */
1976 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1977 if (expr_start != NULL)
1978 {
1979 /* Don't expand the name when we already know there is an error. */
1980 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1981 && *p != '[' && *p != '.')
1982 {
1983 EMSG(_(e_trailing));
1984 return NULL;
1985 }
1986
1987 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1988 if (lp->ll_exp_name == NULL)
1989 {
1990 /* Report an invalid expression in braces, unless the
1991 * expression evaluation has been cancelled due to an
1992 * aborting error, an interrupt, or an exception. */
1993 if (!aborting() && !quiet)
1994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001995 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001996 EMSG2(_(e_invarg2), name);
1997 return NULL;
1998 }
1999 }
2000 lp->ll_name = lp->ll_exp_name;
2001 }
2002 else
2003 lp->ll_name = name;
2004
2005 /* Without [idx] or .key we are done. */
2006 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2007 return p;
2008
2009 cc = *p;
2010 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002011 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 if (v == NULL && !quiet)
2013 EMSG2(_(e_undefvar), lp->ll_name);
2014 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 if (v == NULL)
2016 return NULL;
2017
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 /*
2019 * Loop until no more [idx] or .key is following.
2020 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002022 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002024 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2025 && !(lp->ll_tv->v_type == VAR_DICT
2026 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (!quiet)
2029 EMSG(_("E689: Can only index a List or Dictionary"));
2030 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002032 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002034 if (!quiet)
2035 EMSG(_("E708: [:] must come last"));
2036 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002038
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 len = -1;
2040 if (*p == '.')
2041 {
2042 key = p + 1;
2043 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2044 ;
2045 if (len == 0)
2046 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 if (!quiet)
2048 EMSG(_(e_emptykey));
2049 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 }
2051 p = key + len;
2052 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002053 else
2054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002056 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002057 if (*p == ':')
2058 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002059 else
2060 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 empty1 = FALSE;
2062 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 }
2065
2066 /* Optionally get the second index [ :expr]. */
2067 if (*p == ':')
2068 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002069 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002071 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002072 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002073 if (!empty1)
2074 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002076 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 if (rettv != NULL && (rettv->v_type != VAR_LIST
2078 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 if (!quiet)
2081 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002082 if (!empty1)
2083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002085 }
2086 p = skipwhite(p + 1);
2087 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002088 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002089 else
2090 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2093 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002094 if (!empty1)
2095 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002096 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002097 }
2098 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002100 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002103
Bram Moolenaar8c711452005-01-14 21:53:12 +00002104 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002105 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 if (!quiet)
2107 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 if (!empty1)
2109 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002111 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 }
2114
2115 /* Skip to past ']'. */
2116 ++p;
2117 }
2118
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 {
2121 if (len == -1)
2122 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002123 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002124 key = get_tv_string(&var1);
2125 if (*key == NUL)
2126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (!quiet)
2128 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002131 }
2132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002133 lp->ll_list = NULL;
2134 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002135 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002138 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002143 if (len == -1)
2144 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 }
2147 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002148 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002151 if (len == -1)
2152 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002154 p = NULL;
2155 break;
2156 }
2157 if (len == -1)
2158 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002160 }
2161 else
2162 {
2163 /*
2164 * Get the number and item for the only or first index of the List.
2165 */
2166 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002168 else
2169 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 clear_tv(&var1);
2172 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 lp->ll_list = lp->ll_tv->vval.v_list;
2175 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2176 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002177 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 if (!quiet)
2179 EMSGN(_(e_listidx), lp->ll_n1);
2180 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002181 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002182 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002183 }
2184
2185 /*
2186 * May need to find the item or absolute index for the second
2187 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 * When no index given: "lp->ll_empty2" is TRUE.
2189 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002196 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002198 if (ni == NULL)
2199 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 if (!quiet)
2201 EMSGN(_(e_listidx), lp->ll_n2);
2202 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 }
2206
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2208 if (lp->ll_n1 < 0)
2209 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2210 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002211 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002212 if (!quiet)
2213 EMSGN(_(e_listidx), lp->ll_n2);
2214 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002215 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002216 }
2217
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 }
2221
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 return p;
2223}
2224
2225/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002226 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 */
2228 static void
2229clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002230 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231{
2232 vim_free(lp->ll_exp_name);
2233 vim_free(lp->ll_newkey);
2234}
2235
2236/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002237 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 */
2241 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002243 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248{
2249 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 listitem_T *ni;
2251 listitem_T *ri;
2252 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253
2254 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 cc = *endp;
2259 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002260 if (op != NULL && *op != '=')
2261 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002262 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002263
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002264 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2266 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 {
2268 if (tv_op(&tv, rettv, op) == OK)
2269 set_var(lp->ll_name, &tv, FALSE);
2270 clear_tv(&tv);
2271 }
2272 }
2273 else
2274 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002278 else if (tv_check_lock(lp->ll_newkey == NULL
2279 ? lp->ll_tv->v_lock
2280 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2281 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 else if (lp->ll_range)
2283 {
2284 /*
2285 * Assign the List values to the list items.
2286 */
2287 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002288 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002289 if (op != NULL && *op != '=')
2290 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2291 else
2292 {
2293 clear_tv(&lp->ll_li->li_tv);
2294 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2295 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 ri = ri->li_next;
2297 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2298 break;
2299 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002300 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 /* Need to add an empty item. */
2302 ni = listitem_alloc();
2303 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002304 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 ri = NULL;
2306 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002307 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002309 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 ni->li_tv.vval.v_number = 0;
2311 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002312 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 lp->ll_li = lp->ll_li->li_next;
2314 ++lp->ll_n1;
2315 }
2316 if (ri != NULL)
2317 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 else if (lp->ll_empty2
2319 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 : lp->ll_n1 != lp->ll_n2)
2321 EMSG(_("E711: List value has not enough items"));
2322 }
2323 else
2324 {
2325 /*
2326 * Assign to a List or Dictionary item.
2327 */
2328 if (lp->ll_newkey != NULL)
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && *op != '=')
2331 {
2332 EMSG2(_(e_letwrong), op);
2333 return;
2334 }
2335
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002337 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (di == NULL)
2339 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002340 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2341 {
2342 vim_free(di);
2343 return;
2344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 else if (op != NULL && *op != '=')
2348 {
2349 tv_op(lp->ll_tv, rettv, op);
2350 return;
2351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 /*
2356 * Assign the value to the variable or list item.
2357 */
2358 if (copy)
2359 copy_tv(rettv, lp->ll_tv);
2360 else
2361 {
2362 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002363 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367}
2368
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2371 * Returns OK or FAIL.
2372 */
2373 static int
2374tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002375 typval_T *tv1;
2376 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 char_u *op;
2378{
2379 long n;
2380 char_u numbuf[NUMBUFLEN];
2381 char_u *s;
2382
2383 /* Can't do anything with a Funcref or a Dict on the right. */
2384 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2385 {
2386 switch (tv1->v_type)
2387 {
2388 case VAR_DICT:
2389 case VAR_FUNC:
2390 break;
2391
2392 case VAR_LIST:
2393 if (*op != '+' || tv2->v_type != VAR_LIST)
2394 break;
2395 /* List += List */
2396 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2397 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2398 return OK;
2399
2400 case VAR_NUMBER:
2401 case VAR_STRING:
2402 if (tv2->v_type == VAR_LIST)
2403 break;
2404 if (*op == '+' || *op == '-')
2405 {
2406 /* nr += nr or nr -= nr*/
2407 n = get_tv_number(tv1);
2408 if (*op == '+')
2409 n += get_tv_number(tv2);
2410 else
2411 n -= get_tv_number(tv2);
2412 clear_tv(tv1);
2413 tv1->v_type = VAR_NUMBER;
2414 tv1->vval.v_number = n;
2415 }
2416 else
2417 {
2418 /* str .= str */
2419 s = get_tv_string(tv1);
2420 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2421 clear_tv(tv1);
2422 tv1->v_type = VAR_STRING;
2423 tv1->vval.v_string = s;
2424 }
2425 return OK;
2426 }
2427 }
2428
2429 EMSG2(_(e_letwrong), op);
2430 return FAIL;
2431}
2432
2433/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434 * Add a watcher to a list.
2435 */
2436 static void
2437list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002438 list_T *l;
2439 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002440{
2441 lw->lw_next = l->lv_watch;
2442 l->lv_watch = lw;
2443}
2444
2445/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002446 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002447 * No warning when it isn't found...
2448 */
2449 static void
2450list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002451 list_T *l;
2452 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002453{
Bram Moolenaar33570922005-01-25 22:26:29 +00002454 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455
2456 lwp = &l->lv_watch;
2457 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2458 {
2459 if (lw == lwrem)
2460 {
2461 *lwp = lw->lw_next;
2462 break;
2463 }
2464 lwp = &lw->lw_next;
2465 }
2466}
2467
2468/*
2469 * Just before removing an item from a list: advance watchers to the next
2470 * item.
2471 */
2472 static void
2473list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002474 list_T *l;
2475 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002476{
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002478
2479 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2480 if (lw->lw_item == item)
2481 lw->lw_item = item->li_next;
2482}
2483
2484/*
2485 * Evaluate the expression used in a ":for var in expr" command.
2486 * "arg" points to "var".
2487 * Set "*errp" to TRUE for an error, FALSE otherwise;
2488 * Return a pointer that holds the info. Null when there is an error.
2489 */
2490 void *
2491eval_for_line(arg, errp, nextcmdp, skip)
2492 char_u *arg;
2493 int *errp;
2494 char_u **nextcmdp;
2495 int skip;
2496{
Bram Moolenaar33570922005-01-25 22:26:29 +00002497 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002498 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002499 typval_T tv;
2500 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002501
2502 *errp = TRUE; /* default: there is an error */
2503
Bram Moolenaar33570922005-01-25 22:26:29 +00002504 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002505 if (fi == NULL)
2506 return NULL;
2507
2508 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2509 if (expr == NULL)
2510 return fi;
2511
2512 expr = skipwhite(expr);
2513 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2514 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002515 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002516 return fi;
2517 }
2518
2519 if (skip)
2520 ++emsg_skip;
2521 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2522 {
2523 *errp = FALSE;
2524 if (!skip)
2525 {
2526 l = tv.vval.v_list;
2527 if (tv.v_type != VAR_LIST || l == NULL)
2528 EMSG(_(e_listreq));
2529 else
2530 {
2531 fi->fi_list = l;
2532 list_add_watch(l, &fi->fi_lw);
2533 fi->fi_lw.lw_item = l->lv_first;
2534 }
2535 }
2536 }
2537 if (skip)
2538 --emsg_skip;
2539
2540 return fi;
2541}
2542
2543/*
2544 * Use the first item in a ":for" list. Advance to the next.
2545 * Assign the values to the variable (list). "arg" points to the first one.
2546 * Return TRUE when a valid item was found, FALSE when at end of list or
2547 * something wrong.
2548 */
2549 int
2550next_for_item(fi_void, arg)
2551 void *fi_void;
2552 char_u *arg;
2553{
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002555 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002557
2558 item = fi->fi_lw.lw_item;
2559 if (item == NULL)
2560 result = FALSE;
2561 else
2562 {
2563 fi->fi_lw.lw_item = item->li_next;
2564 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2565 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2566 }
2567 return result;
2568}
2569
2570/*
2571 * Free the structure used to store info used by ":for".
2572 */
2573 void
2574free_for_info(fi_void)
2575 void *fi_void;
2576{
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002578
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002579 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002580 list_rem_watch(fi->fi_list, &fi->fi_lw);
2581 vim_free(fi);
2582}
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2585
2586 void
2587set_context_for_expression(xp, arg, cmdidx)
2588 expand_T *xp;
2589 char_u *arg;
2590 cmdidx_T cmdidx;
2591{
2592 int got_eq = FALSE;
2593 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002594 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002596 if (cmdidx == CMD_let)
2597 {
2598 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002599 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002600 {
2601 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002602 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002603 {
2604 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002606 if (vim_iswhite(*p))
2607 break;
2608 }
2609 return;
2610 }
2611 }
2612 else
2613 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2614 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 while ((xp->xp_pattern = vim_strpbrk(arg,
2616 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2617 {
2618 c = *xp->xp_pattern;
2619 if (c == '&')
2620 {
2621 c = xp->xp_pattern[1];
2622 if (c == '&')
2623 {
2624 ++xp->xp_pattern;
2625 xp->xp_context = cmdidx != CMD_let || got_eq
2626 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2627 }
2628 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002631 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2632 xp->xp_pattern += 2;
2633
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636 else if (c == '$')
2637 {
2638 /* environment variable */
2639 xp->xp_context = EXPAND_ENV_VARS;
2640 }
2641 else if (c == '=')
2642 {
2643 got_eq = TRUE;
2644 xp->xp_context = EXPAND_EXPRESSION;
2645 }
2646 else if (c == '<'
2647 && xp->xp_context == EXPAND_FUNCTIONS
2648 && vim_strchr(xp->xp_pattern, '(') == NULL)
2649 {
2650 /* Function name can start with "<SNR>" */
2651 break;
2652 }
2653 else if (cmdidx != CMD_let || got_eq)
2654 {
2655 if (c == '"') /* string */
2656 {
2657 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2658 if (c == '\\' && xp->xp_pattern[1] != NUL)
2659 ++xp->xp_pattern;
2660 xp->xp_context = EXPAND_NOTHING;
2661 }
2662 else if (c == '\'') /* literal string */
2663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2666 /* skip */ ;
2667 xp->xp_context = EXPAND_NOTHING;
2668 }
2669 else if (c == '|')
2670 {
2671 if (xp->xp_pattern[1] == '|')
2672 {
2673 ++xp->xp_pattern;
2674 xp->xp_context = EXPAND_EXPRESSION;
2675 }
2676 else
2677 xp->xp_context = EXPAND_COMMANDS;
2678 }
2679 else
2680 xp->xp_context = EXPAND_EXPRESSION;
2681 }
2682 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002683 /* Doesn't look like something valid, expand as an expression
2684 * anyway. */
2685 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 arg = xp->xp_pattern;
2687 if (*arg != NUL)
2688 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2689 /* skip */ ;
2690 }
2691 xp->xp_pattern = arg;
2692}
2693
2694#endif /* FEAT_CMDL_COMPL */
2695
2696/*
2697 * ":1,25call func(arg1, arg2)" function call.
2698 */
2699 void
2700ex_call(eap)
2701 exarg_T *eap;
2702{
2703 char_u *arg = eap->arg;
2704 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002708 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 linenr_T lnum;
2710 int doesrange;
2711 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002712 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002714 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2715 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 if (tofree == NULL)
2717 return;
2718
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002719 /* Increase refcount on dictionary, it could get deleted when evaluating
2720 * the arguments. */
2721 if (fudi.fd_dict != NULL)
2722 ++fudi.fd_dict->dv_refcount;
2723
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2725 len = STRLEN(tofree);
2726 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaar532c7802005-01-27 14:44:31 +00002728 /* Skip white space to allow ":call func ()". Not good, but required for
2729 * backward compatibility. */
2730 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002731 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 if (*startarg != '(')
2734 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002735 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 goto end;
2737 }
2738
2739 /*
2740 * When skipping, evaluate the function once, to find the end of the
2741 * arguments.
2742 * When the function takes a range, this is discovered after the first
2743 * call, and the loop is broken.
2744 */
2745 if (eap->skip)
2746 {
2747 ++emsg_skip;
2748 lnum = eap->line2; /* do it once, also with an invalid range */
2749 }
2750 else
2751 lnum = eap->line1;
2752 for ( ; lnum <= eap->line2; ++lnum)
2753 {
2754 if (!eap->skip && eap->addr_count > 0)
2755 {
2756 curwin->w_cursor.lnum = lnum;
2757 curwin->w_cursor.col = 0;
2758 }
2759 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002761 eap->line1, eap->line2, &doesrange,
2762 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
2764 failed = TRUE;
2765 break;
2766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002767 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (doesrange || eap->skip)
2769 break;
2770 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002771 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002773 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (aborting())
2775 break;
2776 }
2777 if (eap->skip)
2778 --emsg_skip;
2779
2780 if (!failed)
2781 {
2782 /* Check for trailing illegal characters and a following command. */
2783 if (!ends_excmd(*arg))
2784 {
2785 emsg_severe = TRUE;
2786 EMSG(_(e_trailing));
2787 }
2788 else
2789 eap->nextcmd = check_nextcmd(arg);
2790 }
2791
2792end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002793 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795}
2796
2797/*
2798 * ":unlet[!] var1 ... " command.
2799 */
2800 void
2801ex_unlet(eap)
2802 exarg_T *eap;
2803{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002804 ex_unletlock(eap, eap->arg, 0);
2805}
2806
2807/*
2808 * ":lockvar" and ":unlockvar" commands
2809 */
2810 void
2811ex_lockvar(eap)
2812 exarg_T *eap;
2813{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002815 int deep = 2;
2816
2817 if (eap->forceit)
2818 deep = -1;
2819 else if (vim_isdigit(*arg))
2820 {
2821 deep = getdigits(&arg);
2822 arg = skipwhite(arg);
2823 }
2824
2825 ex_unletlock(eap, arg, deep);
2826}
2827
2828/*
2829 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2830 */
2831 static void
2832ex_unletlock(eap, argstart, deep)
2833 exarg_T *eap;
2834 char_u *argstart;
2835 int deep;
2836{
2837 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002840 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 do
2843 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 /* Parse the name and find the end. */
2845 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2846 if (lv.ll_name == NULL)
2847 error = TRUE; /* error but continue parsing */
2848 if (name_end == NULL || (!vim_iswhite(*name_end)
2849 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (name_end != NULL)
2852 {
2853 emsg_severe = TRUE;
2854 EMSG(_(e_trailing));
2855 }
2856 if (!(eap->skip || error))
2857 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 break;
2859 }
2860
2861 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002862 {
2863 if (eap->cmdidx == CMD_unlet)
2864 {
2865 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2866 error = TRUE;
2867 }
2868 else
2869 {
2870 if (do_lock_var(&lv, name_end, deep,
2871 eap->cmdidx == CMD_lockvar) == FAIL)
2872 error = TRUE;
2873 }
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (!eap->skip)
2877 clear_lval(&lv);
2878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 arg = skipwhite(name_end);
2880 } while (!ends_excmd(*arg));
2881
2882 eap->nextcmd = check_nextcmd(arg);
2883}
2884
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 int forceit;
2890{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 int ret = OK;
2892 int cc;
2893
2894 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 cc = *name_end;
2897 *name_end = NUL;
2898
2899 /* Normal name or expanded name. */
2900 if (check_changedtick(lp->ll_name))
2901 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002906 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2907 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 else if (lp->ll_range)
2909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911
2912 /* Delete a range of List items. */
2913 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2914 {
2915 li = lp->ll_li->li_next;
2916 listitem_remove(lp->ll_list, lp->ll_li);
2917 lp->ll_li = li;
2918 ++lp->ll_n1;
2919 }
2920 }
2921 else
2922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 /* unlet a List item. */
2925 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002928 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 }
2930
2931 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932}
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934/*
2935 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002939do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002941 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 hashtab_T *ht;
2944 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002945 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 ht = find_var_ht(name, &varname);
2948 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 hi = hash_find(ht, varname);
2951 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002952 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 if (var_check_ro(HI2DI(hi)->di_flags, name))
2954 return FAIL;
2955 delete_var(ht, hi);
2956 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 if (forceit)
2960 return OK;
2961 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 return FAIL;
2963}
2964
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965/*
2966 * Lock or unlock variable indicated by "lp".
2967 * "deep" is the levels to go (-1 for unlimited);
2968 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2969 */
2970 static int
2971do_lock_var(lp, name_end, deep, lock)
2972 lval_T *lp;
2973 char_u *name_end;
2974 int deep;
2975 int lock;
2976{
2977 int ret = OK;
2978 int cc;
2979 dictitem_T *di;
2980
2981 if (deep == 0) /* nothing to do */
2982 return OK;
2983
2984 if (lp->ll_tv == NULL)
2985 {
2986 cc = *name_end;
2987 *name_end = NUL;
2988
2989 /* Normal name or expanded name. */
2990 if (check_changedtick(lp->ll_name))
2991 ret = FAIL;
2992 else
2993 {
2994 di = find_var(lp->ll_name, NULL);
2995 if (di == NULL)
2996 ret = FAIL;
2997 else
2998 {
2999 if (lock)
3000 di->di_flags |= DI_FLAGS_LOCK;
3001 else
3002 di->di_flags &= ~DI_FLAGS_LOCK;
3003 item_lock(&di->di_tv, deep, lock);
3004 }
3005 }
3006 *name_end = cc;
3007 }
3008 else if (lp->ll_range)
3009 {
3010 listitem_T *li = lp->ll_li;
3011
3012 /* (un)lock a range of List items. */
3013 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3014 {
3015 item_lock(&li->li_tv, deep, lock);
3016 li = li->li_next;
3017 ++lp->ll_n1;
3018 }
3019 }
3020 else if (lp->ll_list != NULL)
3021 /* (un)lock a List item. */
3022 item_lock(&lp->ll_li->li_tv, deep, lock);
3023 else
3024 /* un(lock) a Dictionary item. */
3025 item_lock(&lp->ll_di->di_tv, deep, lock);
3026
3027 return ret;
3028}
3029
3030/*
3031 * Lock or unlock an item. "deep" is nr of levels to go.
3032 */
3033 static void
3034item_lock(tv, deep, lock)
3035 typval_T *tv;
3036 int deep;
3037 int lock;
3038{
3039 static int recurse = 0;
3040 list_T *l;
3041 listitem_T *li;
3042 dict_T *d;
3043 hashitem_T *hi;
3044 int todo;
3045
3046 if (recurse >= DICT_MAXNEST)
3047 {
3048 EMSG(_("E743: variable nested too deep for (un)lock"));
3049 return;
3050 }
3051 if (deep == 0)
3052 return;
3053 ++recurse;
3054
3055 /* lock/unlock the item itself */
3056 if (lock)
3057 tv->v_lock |= VAR_LOCKED;
3058 else
3059 tv->v_lock &= ~VAR_LOCKED;
3060
3061 switch (tv->v_type)
3062 {
3063 case VAR_LIST:
3064 if ((l = tv->vval.v_list) != NULL)
3065 {
3066 if (lock)
3067 l->lv_lock |= VAR_LOCKED;
3068 else
3069 l->lv_lock &= ~VAR_LOCKED;
3070 if (deep < 0 || deep > 1)
3071 /* recursive: lock/unlock the items the List contains */
3072 for (li = l->lv_first; li != NULL; li = li->li_next)
3073 item_lock(&li->li_tv, deep - 1, lock);
3074 }
3075 break;
3076 case VAR_DICT:
3077 if ((d = tv->vval.v_dict) != NULL)
3078 {
3079 if (lock)
3080 d->dv_lock |= VAR_LOCKED;
3081 else
3082 d->dv_lock &= ~VAR_LOCKED;
3083 if (deep < 0 || deep > 1)
3084 {
3085 /* recursive: lock/unlock the items the List contains */
3086 todo = d->dv_hashtab.ht_used;
3087 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3088 {
3089 if (!HASHITEM_EMPTY(hi))
3090 {
3091 --todo;
3092 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3093 }
3094 }
3095 }
3096 }
3097 }
3098 --recurse;
3099}
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3102/*
3103 * Delete all "menutrans_" variables.
3104 */
3105 void
3106del_menutrans_vars()
3107{
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003109 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 hash_lock(&globvarht);
3112 todo = globvarht.ht_used;
3113 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003114 {
3115 if (!HASHITEM_EMPTY(hi))
3116 {
3117 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3119 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003120 }
3121 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124#endif
3125
3126#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3127
3128/*
3129 * Local string buffer for the next two functions to store a variable name
3130 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3131 * get_user_var_name().
3132 */
3133
3134static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3135
3136static char_u *varnamebuf = NULL;
3137static int varnamebuflen = 0;
3138
3139/*
3140 * Function to concatenate a prefix and a variable name.
3141 */
3142 static char_u *
3143cat_prefix_varname(prefix, name)
3144 int prefix;
3145 char_u *name;
3146{
3147 int len;
3148
3149 len = (int)STRLEN(name) + 3;
3150 if (len > varnamebuflen)
3151 {
3152 vim_free(varnamebuf);
3153 len += 10; /* some additional space */
3154 varnamebuf = alloc(len);
3155 if (varnamebuf == NULL)
3156 {
3157 varnamebuflen = 0;
3158 return NULL;
3159 }
3160 varnamebuflen = len;
3161 }
3162 *varnamebuf = prefix;
3163 varnamebuf[1] = ':';
3164 STRCPY(varnamebuf + 2, name);
3165 return varnamebuf;
3166}
3167
3168/*
3169 * Function given to ExpandGeneric() to obtain the list of user defined
3170 * (global/buffer/window/built-in) variable names.
3171 */
3172/*ARGSUSED*/
3173 char_u *
3174get_user_var_name(xp, idx)
3175 expand_T *xp;
3176 int idx;
3177{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003178 static long_u gdone;
3179 static long_u bdone;
3180 static long_u wdone;
3181 static int vidx;
3182 static hashitem_T *hi;
3183 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003186 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003187
3188 /* Global variables */
3189 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003191 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003193 else
3194 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003195 while (HASHITEM_EMPTY(hi))
3196 ++hi;
3197 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3198 return cat_prefix_varname('g', hi->hi_key);
3199 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003201
3202 /* b: variables */
3203 ht = &curbuf->b_vars.dv_hashtab;
3204 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003206 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003208 else
3209 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003210 while (HASHITEM_EMPTY(hi))
3211 ++hi;
3212 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003216 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 return (char_u *)"b:changedtick";
3218 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003219
3220 /* w: variables */
3221 ht = &curwin->w_vars.dv_hashtab;
3222 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003224 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003226 else
3227 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003228 while (HASHITEM_EMPTY(hi))
3229 ++hi;
3230 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003232
3233 /* v: variables */
3234 if (vidx < VV_LEN)
3235 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 vim_free(varnamebuf);
3238 varnamebuf = NULL;
3239 varnamebuflen = 0;
3240 return NULL;
3241}
3242
3243#endif /* FEAT_CMDL_COMPL */
3244
3245/*
3246 * types for expressions.
3247 */
3248typedef enum
3249{
3250 TYPE_UNKNOWN = 0
3251 , TYPE_EQUAL /* == */
3252 , TYPE_NEQUAL /* != */
3253 , TYPE_GREATER /* > */
3254 , TYPE_GEQUAL /* >= */
3255 , TYPE_SMALLER /* < */
3256 , TYPE_SEQUAL /* <= */
3257 , TYPE_MATCH /* =~ */
3258 , TYPE_NOMATCH /* !~ */
3259} exptype_T;
3260
3261/*
3262 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003263 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3265 */
3266
3267/*
3268 * Handle zero level expression.
3269 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003270 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 * Return OK or FAIL.
3272 */
3273 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003274eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u **nextcmd;
3278 int evaluate;
3279{
3280 int ret;
3281 char_u *p;
3282
3283 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003284 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (ret == FAIL || !ends_excmd(*p))
3286 {
3287 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003288 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 /*
3290 * Report the invalid expression unless the expression evaluation has
3291 * been cancelled due to an aborting error, an interrupt, or an
3292 * exception.
3293 */
3294 if (!aborting())
3295 EMSG2(_(e_invexpr2), arg);
3296 ret = FAIL;
3297 }
3298 if (nextcmd != NULL)
3299 *nextcmd = check_nextcmd(p);
3300
3301 return ret;
3302}
3303
3304/*
3305 * Handle top level expression:
3306 * expr1 ? expr0 : expr0
3307 *
3308 * "arg" must point to the first non-white of the expression.
3309 * "arg" is advanced to the next non-white after the recognized expression.
3310 *
3311 * Return OK or FAIL.
3312 */
3313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003314eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 int evaluate;
3318{
3319 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322 /*
3323 * Get the first variable.
3324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003325 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return FAIL;
3327
3328 if ((*arg)[0] == '?')
3329 {
3330 result = FALSE;
3331 if (evaluate)
3332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003333 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337
3338 /*
3339 * Get the second variable.
3340 */
3341 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 return FAIL;
3344
3345 /*
3346 * Check for the ":".
3347 */
3348 if ((*arg)[0] != ':')
3349 {
3350 EMSG(_("E109: Missing ':' after '?'"));
3351 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 return FAIL;
3354 }
3355
3356 /*
3357 * Get the third variable.
3358 */
3359 *arg = skipwhite(*arg + 1);
3360 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3361 {
3362 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return FAIL;
3365 }
3366 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003367 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369
3370 return OK;
3371}
3372
3373/*
3374 * Handle first level expression:
3375 * expr2 || expr2 || expr2 logical OR
3376 *
3377 * "arg" must point to the first non-white of the expression.
3378 * "arg" is advanced to the next non-white after the recognized expression.
3379 *
3380 * Return OK or FAIL.
3381 */
3382 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 int evaluate;
3387{
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 long result;
3390 int first;
3391
3392 /*
3393 * Get the first variable.
3394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return FAIL;
3397
3398 /*
3399 * Repeat until there is no following "||".
3400 */
3401 first = TRUE;
3402 result = FALSE;
3403 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3404 {
3405 if (evaluate && first)
3406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 first = FALSE;
3411 }
3412
3413 /*
3414 * Get the second variable.
3415 */
3416 *arg = skipwhite(*arg + 2);
3417 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3418 return FAIL;
3419
3420 /*
3421 * Compute the result.
3422 */
3423 if (evaluate && !result)
3424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003427 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429 if (evaluate)
3430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 rettv->v_type = VAR_NUMBER;
3432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 }
3435
3436 return OK;
3437}
3438
3439/*
3440 * Handle second level expression:
3441 * expr3 && expr3 && expr3 logical AND
3442 *
3443 * "arg" must point to the first non-white of the expression.
3444 * "arg" is advanced to the next non-white after the recognized expression.
3445 *
3446 * Return OK or FAIL.
3447 */
3448 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int evaluate;
3453{
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 long result;
3456 int first;
3457
3458 /*
3459 * Get the first variable.
3460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003461 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 return FAIL;
3463
3464 /*
3465 * Repeat until there is no following "&&".
3466 */
3467 first = TRUE;
3468 result = TRUE;
3469 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3470 {
3471 if (evaluate && first)
3472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 first = FALSE;
3477 }
3478
3479 /*
3480 * Get the second variable.
3481 */
3482 *arg = skipwhite(*arg + 2);
3483 if (eval4(arg, &var2, evaluate && result) == FAIL)
3484 return FAIL;
3485
3486 /*
3487 * Compute the result.
3488 */
3489 if (evaluate && result)
3490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495 if (evaluate)
3496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 rettv->v_type = VAR_NUMBER;
3498 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500 }
3501
3502 return OK;
3503}
3504
3505/*
3506 * Handle third level expression:
3507 * var1 == var2
3508 * var1 =~ var2
3509 * var1 != var2
3510 * var1 !~ var2
3511 * var1 > var2
3512 * var1 >= var2
3513 * var1 < var2
3514 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003515 * var1 is var2
3516 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 *
3518 * "arg" must point to the first non-white of the expression.
3519 * "arg" is advanced to the next non-white after the recognized expression.
3520 *
3521 * Return OK or FAIL.
3522 */
3523 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 int evaluate;
3528{
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 char_u *p;
3531 int i;
3532 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003533 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 int len = 2;
3535 long n1, n2;
3536 char_u *s1, *s2;
3537 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3538 regmatch_T regmatch;
3539 int ic;
3540 char_u *save_cpo;
3541
3542 /*
3543 * Get the first variable.
3544 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return FAIL;
3547
3548 p = *arg;
3549 switch (p[0])
3550 {
3551 case '=': if (p[1] == '=')
3552 type = TYPE_EQUAL;
3553 else if (p[1] == '~')
3554 type = TYPE_MATCH;
3555 break;
3556 case '!': if (p[1] == '=')
3557 type = TYPE_NEQUAL;
3558 else if (p[1] == '~')
3559 type = TYPE_NOMATCH;
3560 break;
3561 case '>': if (p[1] != '=')
3562 {
3563 type = TYPE_GREATER;
3564 len = 1;
3565 }
3566 else
3567 type = TYPE_GEQUAL;
3568 break;
3569 case '<': if (p[1] != '=')
3570 {
3571 type = TYPE_SMALLER;
3572 len = 1;
3573 }
3574 else
3575 type = TYPE_SEQUAL;
3576 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003577 case 'i': if (p[1] == 's')
3578 {
3579 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3580 len = 5;
3581 if (!vim_isIDc(p[len]))
3582 {
3583 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3584 type_is = TRUE;
3585 }
3586 }
3587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589
3590 /*
3591 * If there is a comparitive operator, use it.
3592 */
3593 if (type != TYPE_UNKNOWN)
3594 {
3595 /* extra question mark appended: ignore case */
3596 if (p[len] == '?')
3597 {
3598 ic = TRUE;
3599 ++len;
3600 }
3601 /* extra '#' appended: match case */
3602 else if (p[len] == '#')
3603 {
3604 ic = FALSE;
3605 ++len;
3606 }
3607 /* nothing appened: use 'ignorecase' */
3608 else
3609 ic = p_ic;
3610
3611 /*
3612 * Get the second variable.
3613 */
3614 *arg = skipwhite(p + len);
3615 if (eval5(arg, &var2, evaluate) == FAIL)
3616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003617 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 return FAIL;
3619 }
3620
3621 if (evaluate)
3622 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003623 if (type_is && rettv->v_type != var2.v_type)
3624 {
3625 /* For "is" a different type always means FALSE, for "notis"
3626 * it means TRUE. */
3627 n1 = (type == TYPE_NEQUAL);
3628 }
3629 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3630 {
3631 if (type_is)
3632 {
3633 n1 = (rettv->v_type == var2.v_type
3634 && rettv->vval.v_list == var2.vval.v_list);
3635 if (type == TYPE_NEQUAL)
3636 n1 = !n1;
3637 }
3638 else if (rettv->v_type != var2.v_type
3639 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3640 {
3641 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003642 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003643 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003644 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003645 clear_tv(rettv);
3646 clear_tv(&var2);
3647 return FAIL;
3648 }
3649 else
3650 {
3651 /* Compare two Lists for being equal or unequal. */
3652 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3653 if (type == TYPE_NEQUAL)
3654 n1 = !n1;
3655 }
3656 }
3657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003658 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3659 {
3660 if (type_is)
3661 {
3662 n1 = (rettv->v_type == var2.v_type
3663 && rettv->vval.v_dict == var2.vval.v_dict);
3664 if (type == TYPE_NEQUAL)
3665 n1 = !n1;
3666 }
3667 else if (rettv->v_type != var2.v_type
3668 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3669 {
3670 if (rettv->v_type != var2.v_type)
3671 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3672 else
3673 EMSG(_("E736: Invalid operation for Dictionary"));
3674 clear_tv(rettv);
3675 clear_tv(&var2);
3676 return FAIL;
3677 }
3678 else
3679 {
3680 /* Compare two Dictionaries for being equal or unequal. */
3681 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3682 if (type == TYPE_NEQUAL)
3683 n1 = !n1;
3684 }
3685 }
3686
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003687 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3688 {
3689 if (rettv->v_type != var2.v_type
3690 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3691 {
3692 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003693 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003694 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003695 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003696 clear_tv(rettv);
3697 clear_tv(&var2);
3698 return FAIL;
3699 }
3700 else
3701 {
3702 /* Compare two Funcrefs for being equal or unequal. */
3703 if (rettv->vval.v_string == NULL
3704 || var2.vval.v_string == NULL)
3705 n1 = FALSE;
3706 else
3707 n1 = STRCMP(rettv->vval.v_string,
3708 var2.vval.v_string) == 0;
3709 if (type == TYPE_NEQUAL)
3710 n1 = !n1;
3711 }
3712 }
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 /*
3715 * If one of the two variables is a number, compare as a number.
3716 * When using "=~" or "!~", always compare as string.
3717 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003718 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 n1 = get_tv_number(rettv);
3722 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 switch (type)
3724 {
3725 case TYPE_EQUAL: n1 = (n1 == n2); break;
3726 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3727 case TYPE_GREATER: n1 = (n1 > n2); break;
3728 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3729 case TYPE_SMALLER: n1 = (n1 < n2); break;
3730 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3731 case TYPE_UNKNOWN:
3732 case TYPE_MATCH:
3733 case TYPE_NOMATCH: break; /* avoid gcc warning */
3734 }
3735 }
3736 else
3737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738 s1 = get_tv_string_buf(rettv, buf1);
3739 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3741 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3742 else
3743 i = 0;
3744 n1 = FALSE;
3745 switch (type)
3746 {
3747 case TYPE_EQUAL: n1 = (i == 0); break;
3748 case TYPE_NEQUAL: n1 = (i != 0); break;
3749 case TYPE_GREATER: n1 = (i > 0); break;
3750 case TYPE_GEQUAL: n1 = (i >= 0); break;
3751 case TYPE_SMALLER: n1 = (i < 0); break;
3752 case TYPE_SEQUAL: n1 = (i <= 0); break;
3753
3754 case TYPE_MATCH:
3755 case TYPE_NOMATCH:
3756 /* avoid 'l' flag in 'cpoptions' */
3757 save_cpo = p_cpo;
3758 p_cpo = (char_u *)"";
3759 regmatch.regprog = vim_regcomp(s2,
3760 RE_MAGIC + RE_STRING);
3761 regmatch.rm_ic = ic;
3762 if (regmatch.regprog != NULL)
3763 {
3764 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3765 vim_free(regmatch.regprog);
3766 if (type == TYPE_NOMATCH)
3767 n1 = !n1;
3768 }
3769 p_cpo = save_cpo;
3770 break;
3771
3772 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3773 }
3774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 clear_tv(rettv);
3776 clear_tv(&var2);
3777 rettv->v_type = VAR_NUMBER;
3778 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
3781
3782 return OK;
3783}
3784
3785/*
3786 * Handle fourth level expression:
3787 * + number addition
3788 * - number subtraction
3789 * . string concatenation
3790 *
3791 * "arg" must point to the first non-white of the expression.
3792 * "arg" is advanced to the next non-white after the recognized expression.
3793 *
3794 * Return OK or FAIL.
3795 */
3796 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 int evaluate;
3801{
Bram Moolenaar33570922005-01-25 22:26:29 +00003802 typval_T var2;
3803 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 int op;
3805 long n1, n2;
3806 char_u *s1, *s2;
3807 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3808 char_u *p;
3809
3810 /*
3811 * Get the first variable.
3812 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return FAIL;
3815
3816 /*
3817 * Repeat computing, until no '+', '-' or '.' is following.
3818 */
3819 for (;;)
3820 {
3821 op = **arg;
3822 if (op != '+' && op != '-' && op != '.')
3823 break;
3824
3825 /*
3826 * Get the second variable.
3827 */
3828 *arg = skipwhite(*arg + 1);
3829 if (eval6(arg, &var2, evaluate) == FAIL)
3830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833 }
3834
3835 if (evaluate)
3836 {
3837 /*
3838 * Compute the result.
3839 */
3840 if (op == '.')
3841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 s1 = get_tv_string_buf(rettv, buf1);
3843 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003844 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 clear_tv(rettv);
3846 rettv->v_type = VAR_STRING;
3847 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003849 else if (op == '+' && rettv->v_type == VAR_LIST
3850 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003851 {
3852 /* concatenate Lists */
3853 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3854 &var3) == FAIL)
3855 {
3856 clear_tv(rettv);
3857 clear_tv(&var2);
3858 return FAIL;
3859 }
3860 clear_tv(rettv);
3861 *rettv = var3;
3862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 else
3864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 n1 = get_tv_number(rettv);
3866 n2 = get_tv_number(&var2);
3867 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (op == '+')
3869 n1 = n1 + n2;
3870 else
3871 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003872 rettv->v_type = VAR_NUMBER;
3873 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877 }
3878 return OK;
3879}
3880
3881/*
3882 * Handle fifth level expression:
3883 * * number multiplication
3884 * / number division
3885 * % number modulo
3886 *
3887 * "arg" must point to the first non-white of the expression.
3888 * "arg" is advanced to the next non-white after the recognized expression.
3889 *
3890 * Return OK or FAIL.
3891 */
3892 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 int evaluate;
3897{
Bram Moolenaar33570922005-01-25 22:26:29 +00003898 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 int op;
3900 long n1, n2;
3901
3902 /*
3903 * Get the first variable.
3904 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return FAIL;
3907
3908 /*
3909 * Repeat computing, until no '*', '/' or '%' is following.
3910 */
3911 for (;;)
3912 {
3913 op = **arg;
3914 if (op != '*' && op != '/' && op != '%')
3915 break;
3916
3917 if (evaluate)
3918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 n1 = get_tv_number(rettv);
3920 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else
3923 n1 = 0;
3924
3925 /*
3926 * Get the second variable.
3927 */
3928 *arg = skipwhite(*arg + 1);
3929 if (eval7(arg, &var2, evaluate) == FAIL)
3930 return FAIL;
3931
3932 if (evaluate)
3933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003934 n2 = get_tv_number(&var2);
3935 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 /*
3938 * Compute the result.
3939 */
3940 if (op == '*')
3941 n1 = n1 * n2;
3942 else if (op == '/')
3943 {
3944 if (n2 == 0) /* give an error message? */
3945 n1 = 0x7fffffffL;
3946 else
3947 n1 = n1 / n2;
3948 }
3949 else
3950 {
3951 if (n2 == 0) /* give an error message? */
3952 n1 = 0;
3953 else
3954 n1 = n1 % n2;
3955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 rettv->v_type = VAR_NUMBER;
3957 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 }
3960
3961 return OK;
3962}
3963
3964/*
3965 * Handle sixth level expression:
3966 * number number constant
3967 * "string" string contstant
3968 * 'string' literal string contstant
3969 * &option-name option value
3970 * @r register contents
3971 * identifier variable value
3972 * function() function call
3973 * $VAR environment variable
3974 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003975 * [expr, expr] List
3976 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 *
3978 * Also handle:
3979 * ! in front logical NOT
3980 * - in front unary minus
3981 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003982 * trailing [] subscript in String or List
3983 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *
3985 * "arg" must point to the first non-white of the expression.
3986 * "arg" is advanced to the next non-white after the recognized expression.
3987 *
3988 * Return OK or FAIL.
3989 */
3990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 int evaluate;
3995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 long n;
3997 int len;
3998 char_u *s;
3999 int val;
4000 char_u *start_leader, *end_leader;
4001 int ret = OK;
4002 char_u *alias;
4003
4004 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004006 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 /*
4011 * Skip '!' and '-' characters. They are handled later.
4012 */
4013 start_leader = *arg;
4014 while (**arg == '!' || **arg == '-' || **arg == '+')
4015 *arg = skipwhite(*arg + 1);
4016 end_leader = *arg;
4017
4018 switch (**arg)
4019 {
4020 /*
4021 * Number constant.
4022 */
4023 case '0':
4024 case '1':
4025 case '2':
4026 case '3':
4027 case '4':
4028 case '5':
4029 case '6':
4030 case '7':
4031 case '8':
4032 case '9':
4033 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4034 *arg += len;
4035 if (evaluate)
4036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 rettv->v_type = VAR_NUMBER;
4038 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040 break;
4041
4042 /*
4043 * String constant: "string".
4044 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 break;
4047
4048 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004052 break;
4053
4054 /*
4055 * List: [expr, expr]
4056 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
4059
4060 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004061 * Dictionary: {key: val, key: val}
4062 */
4063 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4064 break;
4065
4066 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004067 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004069 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 /*
4073 * Environment variable: $VAR.
4074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077
4078 /*
4079 * Register contents: @r.
4080 */
4081 case '@': ++*arg;
4082 if (evaluate)
4083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 rettv->v_type = VAR_STRING;
4085 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087 if (**arg != NUL)
4088 ++*arg;
4089 break;
4090
4091 /*
4092 * nested expression: (expression).
4093 */
4094 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (**arg == ')')
4097 ++*arg;
4098 else if (ret == OK)
4099 {
4100 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 ret = FAIL;
4103 }
4104 break;
4105
Bram Moolenaar8c711452005-01-14 21:53:12 +00004106 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 break;
4108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004109
4110 if (ret == NOTDONE)
4111 {
4112 /*
4113 * Must be a variable or function name.
4114 * Can also be a curly-braces kind of name: {expr}.
4115 */
4116 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004117 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004118 if (alias != NULL)
4119 s = alias;
4120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004121 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004122 ret = FAIL;
4123 else
4124 {
4125 if (**arg == '(') /* recursive! */
4126 {
4127 /* If "s" is the name of a variable of type VAR_FUNC
4128 * use its contents. */
4129 s = deref_func_name(s, &len);
4130
4131 /* Invoke the function. */
4132 ret = get_func_tv(s, len, rettv, arg,
4133 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004134 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004135 /* Stop the expression evaluation when immediately
4136 * aborting on error, or when an interrupt occurred or
4137 * an exception was thrown but not caught. */
4138 if (aborting())
4139 {
4140 if (ret == OK)
4141 clear_tv(rettv);
4142 ret = FAIL;
4143 }
4144 }
4145 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004146 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004147 else
4148 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004149 }
4150
4151 if (alias != NULL)
4152 vim_free(alias);
4153 }
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 *arg = skipwhite(*arg);
4156
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004157 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4158 * expr(expr). */
4159 if (ret == OK)
4160 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 /*
4163 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4164 */
4165 if (ret == OK && evaluate && end_leader > start_leader)
4166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 while (end_leader > start_leader)
4169 {
4170 --end_leader;
4171 if (*end_leader == '!')
4172 val = !val;
4173 else if (*end_leader == '-')
4174 val = -val;
4175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 clear_tv(rettv);
4177 rettv->v_type = VAR_NUMBER;
4178 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 return ret;
4182}
4183
4184/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004185 * Evaluate an "[expr]" or "[expr:expr]" index.
4186 * "*arg" points to the '['.
4187 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4188 */
4189 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004190eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004191 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004192 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004193 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004194 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195{
4196 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004197 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004198 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004199 long len = -1;
4200 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004201 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004202 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004206 if (verbose)
4207 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 return FAIL;
4209 }
4210
Bram Moolenaar8c711452005-01-14 21:53:12 +00004211 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004213 /*
4214 * dict.name
4215 */
4216 key = *arg + 1;
4217 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4218 ;
4219 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004220 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004221 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004222 }
4223 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004225 /*
4226 * something[idx]
4227 *
4228 * Get the (first) variable from inside the [].
4229 */
4230 *arg = skipwhite(*arg + 1);
4231 if (**arg == ':')
4232 empty1 = TRUE;
4233 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4234 return FAIL;
4235
4236 /*
4237 * Get the second variable from inside the [:].
4238 */
4239 if (**arg == ':')
4240 {
4241 range = TRUE;
4242 *arg = skipwhite(*arg + 1);
4243 if (**arg == ']')
4244 empty2 = TRUE;
4245 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4246 {
4247 clear_tv(&var1);
4248 return FAIL;
4249 }
4250 }
4251
4252 /* Check for the ']'. */
4253 if (**arg != ']')
4254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004255 if (verbose)
4256 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004257 clear_tv(&var1);
4258 if (range)
4259 clear_tv(&var2);
4260 return FAIL;
4261 }
4262 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263 }
4264
4265 if (evaluate)
4266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 n1 = 0;
4268 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 n1 = get_tv_number(&var1);
4271 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004272 }
4273 if (range)
4274 {
4275 if (empty2)
4276 n2 = -1;
4277 else
4278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 n2 = get_tv_number(&var2);
4280 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004281 }
4282 }
4283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 {
4286 case VAR_NUMBER:
4287 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004289 len = (long)STRLEN(s);
4290 if (range)
4291 {
4292 /* The resulting variable is a substring. If the indexes
4293 * are out of range the result is empty. */
4294 if (n1 < 0)
4295 {
4296 n1 = len + n1;
4297 if (n1 < 0)
4298 n1 = 0;
4299 }
4300 if (n2 < 0)
4301 n2 = len + n2;
4302 else if (n2 >= len)
4303 n2 = len;
4304 if (n1 >= len || n2 < 0 || n1 > n2)
4305 s = NULL;
4306 else
4307 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4308 }
4309 else
4310 {
4311 /* The resulting variable is a string of a single
4312 * character. If the index is too big or negative the
4313 * result is empty. */
4314 if (n1 >= len || n1 < 0)
4315 s = NULL;
4316 else
4317 s = vim_strnsave(s + n1, 1);
4318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(rettv);
4320 rettv->v_type = VAR_STRING;
4321 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004322 break;
4323
4324 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004326 if (n1 < 0)
4327 n1 = len + n1;
4328 if (!empty1 && (n1 < 0 || n1 >= len))
4329 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004330 if (verbose)
4331 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004332 return FAIL;
4333 }
4334 if (range)
4335 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004336 list_T *l;
4337 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004338
4339 if (n2 < 0)
4340 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004343 if (verbose)
4344 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004345 return FAIL;
4346 }
4347 l = list_alloc();
4348 if (l == NULL)
4349 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004351 n1 <= n2; ++n1)
4352 {
4353 if (list_append_tv(l, &item->li_tv) == FAIL)
4354 {
4355 list_free(l);
4356 return FAIL;
4357 }
4358 item = item->li_next;
4359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(rettv);
4361 rettv->v_type = VAR_LIST;
4362 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004363 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004364 }
4365 else
4366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004368 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 clear_tv(rettv);
4370 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004371 }
4372 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373
4374 case VAR_DICT:
4375 if (range)
4376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004377 if (verbose)
4378 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004379 if (len == -1)
4380 clear_tv(&var1);
4381 return FAIL;
4382 }
4383 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385
4386 if (len == -1)
4387 {
4388 key = get_tv_string(&var1);
4389 if (*key == NUL)
4390 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004391 if (verbose)
4392 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393 clear_tv(&var1);
4394 return FAIL;
4395 }
4396 }
4397
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004398 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004400 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004401 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004402 if (len == -1)
4403 clear_tv(&var1);
4404 if (item == NULL)
4405 return FAIL;
4406
4407 copy_tv(&item->di_tv, &var1);
4408 clear_tv(rettv);
4409 *rettv = var1;
4410 }
4411 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004412 }
4413 }
4414
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004415 return OK;
4416}
4417
4418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 * Get an option value.
4420 * "arg" points to the '&' or '+' before the option name.
4421 * "arg" is advanced to character after the option name.
4422 * Return OK or FAIL.
4423 */
4424 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004427 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 int evaluate;
4429{
4430 char_u *option_end;
4431 long numval;
4432 char_u *stringval;
4433 int opt_type;
4434 int c;
4435 int working = (**arg == '+'); /* has("+option") */
4436 int ret = OK;
4437 int opt_flags;
4438
4439 /*
4440 * Isolate the option name and find its value.
4441 */
4442 option_end = find_option_end(arg, &opt_flags);
4443 if (option_end == NULL)
4444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 EMSG2(_("E112: Option name missing: %s"), *arg);
4447 return FAIL;
4448 }
4449
4450 if (!evaluate)
4451 {
4452 *arg = option_end;
4453 return OK;
4454 }
4455
4456 c = *option_end;
4457 *option_end = NUL;
4458 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 if (opt_type == -3) /* invalid name */
4462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 EMSG2(_("E113: Unknown option: %s"), *arg);
4465 ret = FAIL;
4466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
4469 if (opt_type == -2) /* hidden string option */
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 rettv->v_type = VAR_STRING;
4472 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else if (opt_type == -1) /* hidden number option */
4475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 rettv->v_type = VAR_NUMBER;
4477 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479 else if (opt_type == 1) /* number option */
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 rettv->v_type = VAR_NUMBER;
4482 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484 else /* string option */
4485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 rettv->v_type = VAR_STRING;
4487 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 }
4490 else if (working && (opt_type == -2 || opt_type == -1))
4491 ret = FAIL;
4492
4493 *option_end = c; /* put back for error messages */
4494 *arg = option_end;
4495
4496 return ret;
4497}
4498
4499/*
4500 * Allocate a variable for a string constant.
4501 * Return OK or FAIL.
4502 */
4503 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 int evaluate;
4508{
4509 char_u *p;
4510 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 int extra = 0;
4512
4513 /*
4514 * Find the end of the string, skipping backslashed characters.
4515 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004516 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
4518 if (*p == '\\' && p[1] != NUL)
4519 {
4520 ++p;
4521 /* A "\<x>" form occupies at least 4 characters, and produces up
4522 * to 6 characters: reserve space for 2 extra */
4523 if (*p == '<')
4524 extra += 2;
4525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
4528 if (*p != '"')
4529 {
4530 EMSG2(_("E114: Missing quote: %s"), *arg);
4531 return FAIL;
4532 }
4533
4534 /* If only parsing, set *arg and return here */
4535 if (!evaluate)
4536 {
4537 *arg = p + 1;
4538 return OK;
4539 }
4540
4541 /*
4542 * Copy the string into allocated memory, handling backslashed
4543 * characters.
4544 */
4545 name = alloc((unsigned)(p - *arg + extra));
4546 if (name == NULL)
4547 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 rettv->v_type = VAR_STRING;
4549 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553 if (*p == '\\')
4554 {
4555 switch (*++p)
4556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 case 'b': *name++ = BS; ++p; break;
4558 case 'e': *name++ = ESC; ++p; break;
4559 case 'f': *name++ = FF; ++p; break;
4560 case 'n': *name++ = NL; ++p; break;
4561 case 'r': *name++ = CAR; ++p; break;
4562 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 case 'X': /* hex: "\x1", "\x12" */
4565 case 'x':
4566 case 'u': /* Unicode: "\u0023" */
4567 case 'U':
4568 if (vim_isxdigit(p[1]))
4569 {
4570 int n, nr;
4571 int c = toupper(*p);
4572
4573 if (c == 'X')
4574 n = 2;
4575 else
4576 n = 4;
4577 nr = 0;
4578 while (--n >= 0 && vim_isxdigit(p[1]))
4579 {
4580 ++p;
4581 nr = (nr << 4) + hex2nr(*p);
4582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004583 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_MBYTE
4585 /* For "\u" store the number according to
4586 * 'encoding'. */
4587 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 else
4590#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 break;
4594
4595 /* octal: "\1", "\12", "\123" */
4596 case '0':
4597 case '1':
4598 case '2':
4599 case '3':
4600 case '4':
4601 case '5':
4602 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 case '7': *name = *p++ - '0';
4604 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 *name = (*name << 3) + *p++ - '0';
4607 if (*p >= '0' && *p <= '7')
4608 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (extra != 0)
4616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619 }
4620 /* FALLTHROUGH */
4621
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624 }
4625 }
4626 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 *arg = p + 1;
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return OK;
4634}
4635
4636/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 * Return OK or FAIL.
4639 */
4640 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 int evaluate;
4645{
4646 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004647 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004648 int reduce = 0;
4649
4650 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004652 */
4653 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004655 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658 break;
4659 ++reduce;
4660 ++p;
4661 }
4662 }
4663
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004667 return FAIL;
4668 }
4669
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004671 if (!evaluate)
4672 {
4673 *arg = p + 1;
4674 return OK;
4675 }
4676
4677 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004679 */
4680 str = alloc((unsigned)((p - *arg) - reduce));
4681 if (str == NULL)
4682 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004683 rettv->v_type = VAR_STRING;
4684 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004685
Bram Moolenaar8c711452005-01-14 21:53:12 +00004686 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004691 break;
4692 ++p;
4693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004697 *arg = p + 1;
4698
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004699 return OK;
4700}
4701
4702/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 * Allocate a variable for a List and fill it from "*arg".
4704 * Return OK or FAIL.
4705 */
4706 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004710 int evaluate;
4711{
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 list_T *l = NULL;
4713 typval_T tv;
4714 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715
4716 if (evaluate)
4717 {
4718 l = list_alloc();
4719 if (l == NULL)
4720 return FAIL;
4721 }
4722
4723 *arg = skipwhite(*arg + 1);
4724 while (**arg != ']' && **arg != NUL)
4725 {
4726 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4727 goto failret;
4728 if (evaluate)
4729 {
4730 item = listitem_alloc();
4731 if (item != NULL)
4732 {
4733 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004734 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 list_append(l, item);
4736 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004737 else
4738 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 }
4740
4741 if (**arg == ']')
4742 break;
4743 if (**arg != ',')
4744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 goto failret;
4747 }
4748 *arg = skipwhite(*arg + 1);
4749 }
4750
4751 if (**arg != ']')
4752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754failret:
4755 if (evaluate)
4756 list_free(l);
4757 return FAIL;
4758 }
4759
4760 *arg = skipwhite(*arg + 1);
4761 if (evaluate)
4762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 rettv->v_type = VAR_LIST;
4764 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 ++l->lv_refcount;
4766 }
4767
4768 return OK;
4769}
4770
4771/*
4772 * Allocate an empty header for a list.
4773 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775list_alloc()
4776{
Bram Moolenaar33570922005-01-25 22:26:29 +00004777 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778}
4779
4780/*
4781 * Unreference a list: decrement the reference count and free it when it
4782 * becomes zero.
4783 */
4784 static void
4785list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004786 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787{
4788 if (l != NULL && --l->lv_refcount <= 0)
4789 list_free(l);
4790}
4791
4792/*
4793 * Free a list, including all items it points to.
4794 * Ignores the reference count.
4795 */
4796 static void
4797list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004798 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799{
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 listitem_T *item;
4801 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802
4803 for (item = l->lv_first; item != NULL; item = next)
4804 {
4805 next = item->li_next;
4806 listitem_free(item);
4807 }
4808 vim_free(l);
4809}
4810
4811/*
4812 * Allocate a list item.
4813 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815listitem_alloc()
4816{
Bram Moolenaar33570922005-01-25 22:26:29 +00004817 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818}
4819
4820/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004821 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 */
4823 static void
4824listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004825 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 vim_free(item);
4829}
4830
4831/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004832 * Remove a list item from a List and free it. Also clears the value.
4833 */
4834 static void
4835listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004836 list_T *l;
4837 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004838{
4839 list_remove(l, item, item);
4840 listitem_free(item);
4841}
4842
4843/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844 * Get the number of items in a list.
4845 */
4846 static long
4847list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004848 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 if (l == NULL)
4851 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004852 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004853}
4854
4855/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004856 * Return TRUE when two lists have exactly the same values.
4857 */
4858 static int
4859list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004860 list_T *l1;
4861 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004862 int ic; /* ignore case for strings */
4863{
Bram Moolenaar33570922005-01-25 22:26:29 +00004864 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004865
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004866 if (list_len(l1) != list_len(l2))
4867 return FALSE;
4868
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004869 for (item1 = l1->lv_first, item2 = l2->lv_first;
4870 item1 != NULL && item2 != NULL;
4871 item1 = item1->li_next, item2 = item2->li_next)
4872 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4873 return FALSE;
4874 return item1 == NULL && item2 == NULL;
4875}
4876
4877/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004878 * Return TRUE when two dictionaries have exactly the same key/values.
4879 */
4880 static int
4881dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004882 dict_T *d1;
4883 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004884 int ic; /* ignore case for strings */
4885{
Bram Moolenaar33570922005-01-25 22:26:29 +00004886 hashitem_T *hi;
4887 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004888 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004889
4890 if (dict_len(d1) != dict_len(d2))
4891 return FALSE;
4892
Bram Moolenaar33570922005-01-25 22:26:29 +00004893 todo = d1->dv_hashtab.ht_used;
4894 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004895 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004896 if (!HASHITEM_EMPTY(hi))
4897 {
4898 item2 = dict_find(d2, hi->hi_key, -1);
4899 if (item2 == NULL)
4900 return FALSE;
4901 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4902 return FALSE;
4903 --todo;
4904 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004905 }
4906 return TRUE;
4907}
4908
4909/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004910 * Return TRUE if "tv1" and "tv2" have the same value.
4911 * Compares the items just like "==" would compare them.
4912 */
4913 static int
4914tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004915 typval_T *tv1;
4916 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004917 int ic; /* ignore case */
4918{
4919 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4920
4921 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4922 {
4923 /* recursive! */
4924 if (tv1->v_type != tv2->v_type
4925 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4926 return FALSE;
4927 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004928 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4929 {
4930 /* recursive! */
4931 if (tv1->v_type != tv2->v_type
4932 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4933 return FALSE;
4934 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004935 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4936 {
4937 if (tv1->v_type != tv2->v_type
4938 || tv1->vval.v_string == NULL
4939 || tv2->vval.v_string == NULL
4940 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4941 return FALSE;
4942 }
4943 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4944 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004945 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4946 * Don't consider "4x" to be equal to 4. */
4947 if ((tv1->v_type == VAR_STRING
4948 && !string_isa_number(tv1->vval.v_string))
4949 || (tv2->v_type == VAR_STRING
4950 && !string_isa_number(tv2->vval.v_string)))
4951 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004952 if (get_tv_number(tv1) != get_tv_number(tv2))
4953 return FALSE;
4954 }
4955 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4956 get_tv_string_buf(tv2, buf2)) != 0)
4957 return FALSE;
4958 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4959 get_tv_string_buf(tv2, buf2)) != 0)
4960 return FALSE;
4961 return TRUE;
4962}
4963
4964/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004965 * Return TRUE if "tv" is a number without other non-white characters.
4966 */
4967 static int
4968string_isa_number(s)
4969 char_u *s;
4970{
4971 int len;
4972
4973 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4974 return len > 0 && *skipwhite(s + len) == NUL;
4975}
4976
4977/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978 * Locate item with index "n" in list "l" and return it.
4979 * A negative index is counted from the end; -1 is the last item.
4980 * Returns NULL when "n" is out of range.
4981 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004982 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004984 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 long n;
4986{
Bram Moolenaar33570922005-01-25 22:26:29 +00004987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 long idx;
4989
4990 if (l == NULL)
4991 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004992
4993 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004995 n = l->lv_len + n;
4996
4997 /* Check for index out of range. */
4998 if (n < 0 || n >= l->lv_len)
4999 return NULL;
5000
5001 /* When there is a cached index may start search from there. */
5002 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005004 if (n < l->lv_idx / 2)
5005 {
5006 /* closest to the start of the list */
5007 item = l->lv_first;
5008 idx = 0;
5009 }
5010 else if (n > (l->lv_idx + l->lv_len) / 2)
5011 {
5012 /* closest to the end of the list */
5013 item = l->lv_last;
5014 idx = l->lv_len - 1;
5015 }
5016 else
5017 {
5018 /* closest to the cached index */
5019 item = l->lv_idx_item;
5020 idx = l->lv_idx;
5021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005022 }
5023 else
5024 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005025 if (n < l->lv_len / 2)
5026 {
5027 /* closest to the start of the list */
5028 item = l->lv_first;
5029 idx = 0;
5030 }
5031 else
5032 {
5033 /* closest to the end of the list */
5034 item = l->lv_last;
5035 idx = l->lv_len - 1;
5036 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005037 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005038
5039 while (n > idx)
5040 {
5041 /* search forward */
5042 item = item->li_next;
5043 ++idx;
5044 }
5045 while (n < idx)
5046 {
5047 /* search backward */
5048 item = item->li_prev;
5049 --idx;
5050 }
5051
5052 /* cache the used index */
5053 l->lv_idx = idx;
5054 l->lv_idx_item = item;
5055
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005056 return item;
5057}
5058
5059/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005060 * Locate "item" list "l" and return its index.
5061 * Returns -1 when "item" is not in the list.
5062 */
5063 static long
5064list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005065 list_T *l;
5066 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005067{
5068 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005069 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005070
5071 if (l == NULL)
5072 return -1;
5073 idx = 0;
5074 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5075 ++idx;
5076 if (li == NULL)
5077 return -1;
5078 return idx;;
5079}
5080
5081/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005082 * Append item "item" to the end of list "l".
5083 */
5084 static void
5085list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005086 list_T *l;
5087 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088{
5089 if (l->lv_last == NULL)
5090 {
5091 /* empty list */
5092 l->lv_first = item;
5093 l->lv_last = item;
5094 item->li_prev = NULL;
5095 }
5096 else
5097 {
5098 l->lv_last->li_next = item;
5099 item->li_prev = l->lv_last;
5100 l->lv_last = item;
5101 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005102 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 item->li_next = NULL;
5104}
5105
5106/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005107 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005108 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 */
5110 static int
5111list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005112 list_T *l;
5113 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005115 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116
Bram Moolenaar05159a02005-02-26 23:04:13 +00005117 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005119 copy_tv(tv, &li->li_tv);
5120 list_append(l, li);
5121 return OK;
5122}
5123
5124/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005125 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005126 * Return FAIL when out of memory.
5127 */
5128 int
5129list_append_dict(list, dict)
5130 list_T *list;
5131 dict_T *dict;
5132{
5133 listitem_T *li = listitem_alloc();
5134
5135 if (li == NULL)
5136 return FAIL;
5137 li->li_tv.v_type = VAR_DICT;
5138 li->li_tv.v_lock = 0;
5139 li->li_tv.vval.v_dict = dict;
5140 list_append(list, li);
5141 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 return OK;
5143}
5144
5145/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005146 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005147 * If "item" is NULL append at the end.
5148 * Return FAIL when out of memory.
5149 */
5150 static int
5151list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005152 list_T *l;
5153 typval_T *tv;
5154 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005155{
Bram Moolenaar33570922005-01-25 22:26:29 +00005156 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005157
5158 if (ni == NULL)
5159 return FAIL;
5160 copy_tv(tv, &ni->li_tv);
5161 if (item == NULL)
5162 /* Append new item at end of list. */
5163 list_append(l, ni);
5164 else
5165 {
5166 /* Insert new item before existing item. */
5167 ni->li_prev = item->li_prev;
5168 ni->li_next = item;
5169 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005170 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005171 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005172 ++l->lv_idx;
5173 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005174 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005175 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005176 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005177 l->lv_idx_item = NULL;
5178 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005179 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005180 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005181 }
5182 return OK;
5183}
5184
5185/*
5186 * Extend "l1" with "l2".
5187 * If "bef" is NULL append at the end, otherwise insert before this item.
5188 * Returns FAIL when out of memory.
5189 */
5190 static int
5191list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 list_T *l1;
5193 list_T *l2;
5194 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005195{
Bram Moolenaar33570922005-01-25 22:26:29 +00005196 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005197
5198 for (item = l2->lv_first; item != NULL; item = item->li_next)
5199 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5200 return FAIL;
5201 return OK;
5202}
5203
5204/*
5205 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5206 * Return FAIL when out of memory.
5207 */
5208 static int
5209list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005210 list_T *l1;
5211 list_T *l2;
5212 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005213{
Bram Moolenaar33570922005-01-25 22:26:29 +00005214 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005215
5216 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005217 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005218 if (l == NULL)
5219 return FAIL;
5220 tv->v_type = VAR_LIST;
5221 tv->vval.v_list = l;
5222
5223 /* append all items from the second list */
5224 return list_extend(l, l2, NULL);
5225}
5226
5227/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005228 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005230 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 * Returns NULL when out of memory.
5232 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005233 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005234list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005235 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005237 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238{
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 list_T *copy;
5240 listitem_T *item;
5241 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242
5243 if (orig == NULL)
5244 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245
5246 copy = list_alloc();
5247 if (copy != NULL)
5248 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005249 if (copyID != 0)
5250 {
5251 /* Do this before adding the items, because one of the items may
5252 * refer back to this list. */
5253 orig->lv_copyID = copyID;
5254 orig->lv_copylist = copy;
5255 }
5256 for (item = orig->lv_first; item != NULL && !got_int;
5257 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {
5259 ni = listitem_alloc();
5260 if (ni == NULL)
5261 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005262 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005263 {
5264 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5265 {
5266 vim_free(ni);
5267 break;
5268 }
5269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005271 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 list_append(copy, ni);
5273 }
5274 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005275 if (item != NULL)
5276 {
5277 list_unref(copy);
5278 copy = NULL;
5279 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 }
5281
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 return copy;
5283}
5284
5285/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005286 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005287 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005289 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005290list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 list_T *l;
5292 listitem_T *item;
5293 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005294{
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 /* notify watchers */
5298 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005300 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005301 list_fix_watch(l, ip);
5302 if (ip == item2)
5303 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305
5306 if (item2->li_next == NULL)
5307 l->lv_last = item->li_prev;
5308 else
5309 item2->li_next->li_prev = item->li_prev;
5310 if (item->li_prev == NULL)
5311 l->lv_first = item2->li_next;
5312 else
5313 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005314 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315}
5316
5317/*
5318 * Return an allocated string with the string representation of a list.
5319 * May return NULL.
5320 */
5321 static char_u *
5322list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005323 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324{
5325 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326
5327 if (tv->vval.v_list == NULL)
5328 return NULL;
5329 ga_init2(&ga, (int)sizeof(char), 80);
5330 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005331 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5332 {
5333 vim_free(ga.ga_data);
5334 return NULL;
5335 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 ga_append(&ga, ']');
5337 ga_append(&ga, NUL);
5338 return (char_u *)ga.ga_data;
5339}
5340
5341/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005342 * Join list "l" into a string in "*gap", using separator "sep".
5343 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005344 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005345 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005346 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005347list_join(gap, l, sep, echo)
5348 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005349 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005350 char_u *sep;
5351 int echo;
5352{
5353 int first = TRUE;
5354 char_u *tofree;
5355 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005356 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005357 char_u *s;
5358
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005359 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005360 {
5361 if (first)
5362 first = FALSE;
5363 else
5364 ga_concat(gap, sep);
5365
5366 if (echo)
5367 s = echo_string(&item->li_tv, &tofree, numbuf);
5368 else
5369 s = tv2string(&item->li_tv, &tofree, numbuf);
5370 if (s != NULL)
5371 ga_concat(gap, s);
5372 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005373 if (s == NULL)
5374 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005375 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005376 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005377}
5378
5379/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 * Allocate an empty header for a dictionary.
5381 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005382 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383dict_alloc()
5384{
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005386
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005388 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005389 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005390 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005391 d->dv_lock = 0;
5392 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005393 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005395 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396}
5397
5398/*
5399 * Unreference a Dictionary: decrement the reference count and free it when it
5400 * becomes zero.
5401 */
5402 static void
5403dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005404 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405{
5406 if (d != NULL && --d->dv_refcount <= 0)
5407 dict_free(d);
5408}
5409
5410/*
5411 * Free a Dictionary, including all items it contains.
5412 * Ignores the reference count.
5413 */
5414 static void
5415dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005418 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005421 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005422 * hashtab. Must not try to resize the hashtab! */
5423 todo = d->dv_hashtab.ht_used;
5424 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005426 if (!HASHITEM_EMPTY(hi))
5427 {
5428 dictitem_free(HI2DI(hi));
5429 --todo;
5430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 vim_free(d);
5434}
5435
5436/*
5437 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005438 * The "key" is copied to the new item.
5439 * Note that the value of the item "di_tv" still needs to be initialized!
5440 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005443dictitem_alloc(key)
5444 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445{
Bram Moolenaar33570922005-01-25 22:26:29 +00005446 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005447
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005449 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005451 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005452 di->di_flags = 0;
5453 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005454 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455}
5456
5457/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005458 * Make a copy of a Dictionary item.
5459 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005460 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005461dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005463{
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005465
Bram Moolenaar33570922005-01-25 22:26:29 +00005466 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005467 if (di != NULL)
5468 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005469 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005470 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005471 copy_tv(&org->di_tv, &di->di_tv);
5472 }
5473 return di;
5474}
5475
5476/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005477 * Remove item "item" from Dictionary "dict" and free it.
5478 */
5479 static void
5480dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005481 dict_T *dict;
5482 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005483{
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 if (HASHITEM_EMPTY(hi))
5488 EMSG2(_(e_intern2), "dictitem_remove()");
5489 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005490 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005491 dictitem_free(item);
5492}
5493
5494/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495 * Free a dict item. Also clears the value.
5496 */
5497 static void
5498dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 clear_tv(&item->di_tv);
5502 vim_free(item);
5503}
5504
5505/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005506 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5507 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005508 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005509 * Returns NULL when out of memory.
5510 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005511 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005512dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005513 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005514 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005515 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005516{
Bram Moolenaar33570922005-01-25 22:26:29 +00005517 dict_T *copy;
5518 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005519 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005521
5522 if (orig == NULL)
5523 return NULL;
5524
5525 copy = dict_alloc();
5526 if (copy != NULL)
5527 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005528 if (copyID != 0)
5529 {
5530 orig->dv_copyID = copyID;
5531 orig->dv_copydict = copy;
5532 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005533 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005534 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005536 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005538 --todo;
5539
5540 di = dictitem_alloc(hi->hi_key);
5541 if (di == NULL)
5542 break;
5543 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005544 {
5545 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5546 copyID) == FAIL)
5547 {
5548 vim_free(di);
5549 break;
5550 }
5551 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005552 else
5553 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5554 if (dict_add(copy, di) == FAIL)
5555 {
5556 dictitem_free(di);
5557 break;
5558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005559 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005560 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005561
Bram Moolenaare9a41262005-01-15 22:18:47 +00005562 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005563 if (todo > 0)
5564 {
5565 dict_unref(copy);
5566 copy = NULL;
5567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005568 }
5569
5570 return copy;
5571}
5572
5573/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005575 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005577 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005579 dict_T *d;
5580 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581{
Bram Moolenaar33570922005-01-25 22:26:29 +00005582 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583}
5584
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005586 * Add a number or string entry to dictionary "d".
5587 * When "str" is NULL use number "nr", otherwise use "str".
5588 * Returns FAIL when out of memory and when key already exists.
5589 */
5590 int
5591dict_add_nr_str(d, key, nr, str)
5592 dict_T *d;
5593 char *key;
5594 long nr;
5595 char_u *str;
5596{
5597 dictitem_T *item;
5598
5599 item = dictitem_alloc((char_u *)key);
5600 if (item == NULL)
5601 return FAIL;
5602 item->di_tv.v_lock = 0;
5603 if (str == NULL)
5604 {
5605 item->di_tv.v_type = VAR_NUMBER;
5606 item->di_tv.vval.v_number = nr;
5607 }
5608 else
5609 {
5610 item->di_tv.v_type = VAR_STRING;
5611 item->di_tv.vval.v_string = vim_strsave(str);
5612 }
5613 if (dict_add(d, item) == FAIL)
5614 {
5615 dictitem_free(item);
5616 return FAIL;
5617 }
5618 return OK;
5619}
5620
5621/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005622 * Get the number of items in a Dictionary.
5623 */
5624 static long
5625dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005627{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005628 if (d == NULL)
5629 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005631}
5632
5633/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634 * Find item "key[len]" in Dictionary "d".
5635 * If "len" is negative use strlen(key).
5636 * Returns NULL when not found.
5637 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005638 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005639dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005640 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 char_u *key;
5642 int len;
5643{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005644#define AKEYLEN 200
5645 char_u buf[AKEYLEN];
5646 char_u *akey;
5647 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005650 if (len < 0)
5651 akey = key;
5652 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005654 tofree = akey = vim_strnsave(key, len);
5655 if (akey == NULL)
5656 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005657 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005658 else
5659 {
5660 /* Avoid a malloc/free by using buf[]. */
5661 STRNCPY(buf, key, len);
5662 buf[len] = NUL;
5663 akey = buf;
5664 }
5665
Bram Moolenaar33570922005-01-25 22:26:29 +00005666 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005667 vim_free(tofree);
5668 if (HASHITEM_EMPTY(hi))
5669 return NULL;
5670 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671}
5672
5673/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005674 * Get a string item from a dictionary in allocated memory.
5675 * Returns NULL if the entry doesn't exist or out of memory.
5676 */
5677 char_u *
5678get_dict_string(d, key)
5679 dict_T *d;
5680 char_u *key;
5681{
5682 dictitem_T *di;
5683
5684 di = dict_find(d, key, -1);
5685 if (di == NULL)
5686 return NULL;
5687 return vim_strsave(get_tv_string(&di->di_tv));
5688}
5689
5690/*
5691 * Get a number item from a dictionary.
5692 * Returns 0 if the entry doesn't exist or out of memory.
5693 */
5694 long
5695get_dict_number(d, key)
5696 dict_T *d;
5697 char_u *key;
5698{
5699 dictitem_T *di;
5700
5701 di = dict_find(d, key, -1);
5702 if (di == NULL)
5703 return 0;
5704 return get_tv_number(&di->di_tv);
5705}
5706
5707/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 * Return an allocated string with the string representation of a Dictionary.
5709 * May return NULL.
5710 */
5711 static char_u *
5712dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005713 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714{
5715 garray_T ga;
5716 int first = TRUE;
5717 char_u *tofree;
5718 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005719 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005721 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005722 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005724 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 return NULL;
5726 ga_init2(&ga, (int)sizeof(char), 80);
5727 ga_append(&ga, '{');
5728
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005730 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005732 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005734 --todo;
5735
5736 if (first)
5737 first = FALSE;
5738 else
5739 ga_concat(&ga, (char_u *)", ");
5740
5741 tofree = string_quote(hi->hi_key, FALSE);
5742 if (tofree != NULL)
5743 {
5744 ga_concat(&ga, tofree);
5745 vim_free(tofree);
5746 }
5747 ga_concat(&ga, (char_u *)": ");
5748 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5749 if (s != NULL)
5750 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005752 if (s == NULL)
5753 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005756 if (todo > 0)
5757 {
5758 vim_free(ga.ga_data);
5759 return NULL;
5760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761
5762 ga_append(&ga, '}');
5763 ga_append(&ga, NUL);
5764 return (char_u *)ga.ga_data;
5765}
5766
5767/*
5768 * Allocate a variable for a Dictionary and fill it from "*arg".
5769 * Return OK or FAIL. Returns NOTDONE for {expr}.
5770 */
5771 static int
5772get_dict_tv(arg, rettv, evaluate)
5773 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 int evaluate;
5776{
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 dict_T *d = NULL;
5778 typval_T tvkey;
5779 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005783 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784
5785 /*
5786 * First check if it's not a curly-braces thing: {expr}.
5787 * Must do this without evaluating, otherwise a function may be called
5788 * twice. Unfortunately this means we need to call eval1() twice for the
5789 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005792 if (*start != '}')
5793 {
5794 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5795 return FAIL;
5796 if (*start == '}')
5797 return NOTDONE;
5798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799
5800 if (evaluate)
5801 {
5802 d = dict_alloc();
5803 if (d == NULL)
5804 return FAIL;
5805 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005806 tvkey.v_type = VAR_UNKNOWN;
5807 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808
5809 *arg = skipwhite(*arg + 1);
5810 while (**arg != '}' && **arg != NUL)
5811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005812 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 goto failret;
5814 if (**arg != ':')
5815 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005816 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005817 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 goto failret;
5819 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005820 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 if (*key == NUL)
5822 {
5823 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005824 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 goto failret;
5826 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827
5828 *arg = skipwhite(*arg + 1);
5829 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5830 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005831 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 goto failret;
5833 }
5834 if (evaluate)
5835 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005836 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 if (item != NULL)
5838 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00005839 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005840 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 clear_tv(&tv);
5842 goto failret;
5843 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005844 item = dictitem_alloc(key);
5845 clear_tv(&tvkey);
5846 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005849 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005850 if (dict_add(d, item) == FAIL)
5851 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 }
5853 }
5854
5855 if (**arg == '}')
5856 break;
5857 if (**arg != ',')
5858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005859 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 goto failret;
5861 }
5862 *arg = skipwhite(*arg + 1);
5863 }
5864
5865 if (**arg != '}')
5866 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005867 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868failret:
5869 if (evaluate)
5870 dict_free(d);
5871 return FAIL;
5872 }
5873
5874 *arg = skipwhite(*arg + 1);
5875 if (evaluate)
5876 {
5877 rettv->v_type = VAR_DICT;
5878 rettv->vval.v_dict = d;
5879 ++d->dv_refcount;
5880 }
5881
5882 return OK;
5883}
5884
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 * Return a string with the string representation of a variable.
5887 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 * May return NULL;
5891 */
5892 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005896 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 static int recurse = 0;
5899 char_u *r = NULL;
5900
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005902 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005903 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005904 *tofree = NULL;
5905 return NULL;
5906 }
5907 ++recurse;
5908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 switch (tv->v_type)
5910 {
5911 case VAR_FUNC:
5912 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 r = tv->vval.v_string;
5914 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 case VAR_LIST:
5916 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917 r = *tofree;
5918 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 case VAR_DICT:
5920 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 r = *tofree;
5922 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005923 case VAR_STRING:
5924 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925 *tofree = NULL;
5926 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005928 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005930 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932
5933 --recurse;
5934 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935}
5936
5937/*
5938 * Return a string with the string representation of a variable.
5939 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5940 * "numbuf" is used for a number.
5941 * Puts quotes around strings, so that they can be parsed back by eval().
5942 * May return NULL;
5943 */
5944 static char_u *
5945tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 char_u **tofree;
5948 char_u *numbuf;
5949{
5950 switch (tv->v_type)
5951 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 case VAR_FUNC:
5953 *tofree = string_quote(tv->vval.v_string, TRUE);
5954 return *tofree;
5955 case VAR_STRING:
5956 *tofree = string_quote(tv->vval.v_string, FALSE);
5957 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005958 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005959 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005961 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005963 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966}
5967
5968/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 * Return string "str" in ' quotes, doubling ' characters.
5970 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 */
5973 static char_u *
5974string_quote(str, function)
5975 char_u *str;
5976 int function;
5977{
Bram Moolenaar33570922005-01-25 22:26:29 +00005978 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005979 char_u *p, *r, *s;
5980
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 len = (function ? 13 : 3);
5982 if (str != NULL)
5983 {
5984 len += STRLEN(str);
5985 for (p = str; *p != NUL; mb_ptr_adv(p))
5986 if (*p == '\'')
5987 ++len;
5988 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 s = r = alloc(len);
5990 if (r != NULL)
5991 {
5992 if (function)
5993 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 r += 10;
5996 }
5997 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005998 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 if (str != NULL)
6000 for (p = str; *p != NUL; )
6001 {
6002 if (*p == '\'')
6003 *r++ = '\'';
6004 MB_COPY_CHAR(p, r);
6005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006006 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006007 if (function)
6008 *r++ = ')';
6009 *r++ = NUL;
6010 }
6011 return s;
6012}
6013
6014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 * Get the value of an environment variable.
6016 * "arg" is pointing to the '$'. It is advanced to after the name.
6017 * If the environment variable was not set, silently assume it is empty.
6018 * Always return OK.
6019 */
6020 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006021get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 int evaluate;
6025{
6026 char_u *string = NULL;
6027 int len;
6028 int cc;
6029 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006030 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031
6032 ++*arg;
6033 name = *arg;
6034 len = get_env_len(arg);
6035 if (evaluate)
6036 {
6037 if (len != 0)
6038 {
6039 cc = name[len];
6040 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006041 /* first try vim_getenv(), fast for normal environment vars */
6042 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006044 {
6045 if (!mustfree)
6046 string = vim_strsave(string);
6047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 else
6049 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006050 if (mustfree)
6051 vim_free(string);
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 /* next try expanding things like $VIM and ${HOME} */
6054 string = expand_env_save(name - 1);
6055 if (string != NULL && *string == '$')
6056 {
6057 vim_free(string);
6058 string = NULL;
6059 }
6060 }
6061 name[len] = cc;
6062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063 rettv->v_type = VAR_STRING;
6064 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 }
6066
6067 return OK;
6068}
6069
6070/*
6071 * Array with names and number of arguments of all internal functions
6072 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6073 */
6074static struct fst
6075{
6076 char *f_name; /* function name */
6077 char f_min_argc; /* minimal number of arguments */
6078 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081} functions[] =
6082{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006083 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {"append", 2, 2, f_append},
6085 {"argc", 0, 0, f_argc},
6086 {"argidx", 0, 0, f_argidx},
6087 {"argv", 1, 1, f_argv},
6088 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006089 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {"bufexists", 1, 1, f_bufexists},
6091 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6092 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6093 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6094 {"buflisted", 1, 1, f_buflisted},
6095 {"bufloaded", 1, 1, f_bufloaded},
6096 {"bufname", 1, 1, f_bufname},
6097 {"bufnr", 1, 1, f_bufnr},
6098 {"bufwinnr", 1, 1, f_bufwinnr},
6099 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006100 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {"char2nr", 1, 1, f_char2nr},
6103 {"cindent", 1, 1, f_cindent},
6104 {"col", 1, 1, f_col},
6105 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006107 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {"cscope_connection",0,3, f_cscope_connection},
6109 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006110 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {"delete", 1, 1, f_delete},
6112 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006113 {"diff_filler", 1, 1, f_diff_filler},
6114 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006115 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006117 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {"eventhandler", 0, 0, f_eventhandler},
6119 {"executable", 1, 1, f_executable},
6120 {"exists", 1, 1, f_exists},
6121 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006122 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6124 {"filereadable", 1, 1, f_filereadable},
6125 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006126 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006127 {"finddir", 1, 3, f_finddir},
6128 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {"fnamemodify", 2, 2, f_fnamemodify},
6130 {"foldclosed", 1, 1, f_foldclosed},
6131 {"foldclosedend", 1, 1, f_foldclosedend},
6132 {"foldlevel", 1, 1, f_foldlevel},
6133 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006134 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006137 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {"getbufvar", 2, 2, f_getbufvar},
6139 {"getchar", 0, 1, f_getchar},
6140 {"getcharmod", 0, 0, f_getcharmod},
6141 {"getcmdline", 0, 0, f_getcmdline},
6142 {"getcmdpos", 0, 0, f_getcmdpos},
6143 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006144 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006145 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {"getfsize", 1, 1, f_getfsize},
6147 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006148 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006149 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006150 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {"getreg", 0, 1, f_getreg},
6152 {"getregtype", 0, 1, f_getregtype},
6153 {"getwinposx", 0, 0, f_getwinposx},
6154 {"getwinposy", 0, 0, f_getwinposy},
6155 {"getwinvar", 2, 2, f_getwinvar},
6156 {"glob", 1, 1, f_glob},
6157 {"globpath", 2, 2, f_globpath},
6158 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {"hasmapto", 1, 2, f_hasmapto},
6161 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6162 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6163 {"histadd", 2, 2, f_histadd},
6164 {"histdel", 1, 2, f_histdel},
6165 {"histget", 1, 2, f_histget},
6166 {"histnr", 1, 1, f_histnr},
6167 {"hlID", 1, 1, f_hlID},
6168 {"hlexists", 1, 1, f_hlexists},
6169 {"hostname", 0, 0, f_hostname},
6170 {"iconv", 3, 3, f_iconv},
6171 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006172 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {"input", 1, 2, f_input},
6174 {"inputdialog", 1, 3, f_inputdialog},
6175 {"inputrestore", 0, 0, f_inputrestore},
6176 {"inputsave", 0, 0, f_inputsave},
6177 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006180 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006181 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006182 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006183 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {"libcall", 3, 3, f_libcall},
6187 {"libcallnr", 3, 3, f_libcallnr},
6188 {"line", 1, 1, f_line},
6189 {"line2byte", 1, 1, f_line2byte},
6190 {"lispindent", 1, 1, f_lispindent},
6191 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006192 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {"maparg", 1, 2, f_maparg},
6194 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006195 {"match", 2, 4, f_match},
6196 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006197 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006198 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006199 {"max", 1, 1, f_max},
6200 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006201#ifdef vim_mkdir
6202 {"mkdir", 1, 3, f_mkdir},
6203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {"mode", 0, 0, f_mode},
6205 {"nextnonblank", 1, 1, f_nextnonblank},
6206 {"nr2char", 1, 1, f_nr2char},
6207 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006209 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {"remote_expr", 2, 3, f_remote_expr},
6211 {"remote_foreground", 1, 1, f_remote_foreground},
6212 {"remote_peek", 1, 2, f_remote_peek},
6213 {"remote_read", 1, 1, f_remote_read},
6214 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006217 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006219 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 {"search", 1, 2, f_search},
6221 {"searchpair", 3, 5, f_searchpair},
6222 {"server2client", 2, 2, f_server2client},
6223 {"serverlist", 0, 0, f_serverlist},
6224 {"setbufvar", 3, 3, f_setbufvar},
6225 {"setcmdpos", 1, 1, f_setcmdpos},
6226 {"setline", 2, 2, f_setline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006227 {"setqflist", 1, 1, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {"setreg", 2, 3, f_setreg},
6229 {"setwinvar", 3, 3, f_setwinvar},
6230 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006231 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006232 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233#ifdef HAVE_STRFTIME
6234 {"strftime", 1, 2, f_strftime},
6235#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {"strlen", 1, 1, f_strlen},
6239 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006240 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {"strtrans", 1, 1, f_strtrans},
6242 {"submatch", 1, 1, f_submatch},
6243 {"substitute", 4, 4, f_substitute},
6244 {"synID", 3, 3, f_synID},
6245 {"synIDattr", 2, 3, f_synIDattr},
6246 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006247 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006248 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {"tempname", 0, 0, f_tempname},
6250 {"tolower", 1, 1, f_tolower},
6251 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006252 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006254 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {"virtcol", 1, 1, f_virtcol},
6256 {"visualmode", 0, 1, f_visualmode},
6257 {"winbufnr", 1, 1, f_winbufnr},
6258 {"wincol", 0, 0, f_wincol},
6259 {"winheight", 1, 1, f_winheight},
6260 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006261 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {"winrestcmd", 0, 0, f_winrestcmd},
6263 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006264 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265};
6266
6267#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6268
6269/*
6270 * Function given to ExpandGeneric() to obtain the list of internal
6271 * or user defined function names.
6272 */
6273 char_u *
6274get_function_name(xp, idx)
6275 expand_T *xp;
6276 int idx;
6277{
6278 static int intidx = -1;
6279 char_u *name;
6280
6281 if (idx == 0)
6282 intidx = -1;
6283 if (intidx < 0)
6284 {
6285 name = get_user_func_name(xp, idx);
6286 if (name != NULL)
6287 return name;
6288 }
6289 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6290 {
6291 STRCPY(IObuff, functions[intidx].f_name);
6292 STRCAT(IObuff, "(");
6293 if (functions[intidx].f_max_argc == 0)
6294 STRCAT(IObuff, ")");
6295 return IObuff;
6296 }
6297
6298 return NULL;
6299}
6300
6301/*
6302 * Function given to ExpandGeneric() to obtain the list of internal or
6303 * user defined variable or function names.
6304 */
6305/*ARGSUSED*/
6306 char_u *
6307get_expr_name(xp, idx)
6308 expand_T *xp;
6309 int idx;
6310{
6311 static int intidx = -1;
6312 char_u *name;
6313
6314 if (idx == 0)
6315 intidx = -1;
6316 if (intidx < 0)
6317 {
6318 name = get_function_name(xp, idx);
6319 if (name != NULL)
6320 return name;
6321 }
6322 return get_user_var_name(xp, ++intidx);
6323}
6324
6325#endif /* FEAT_CMDL_COMPL */
6326
6327/*
6328 * Find internal function in table above.
6329 * Return index, or -1 if not found
6330 */
6331 static int
6332find_internal_func(name)
6333 char_u *name; /* name of the function */
6334{
6335 int first = 0;
6336 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6337 int cmp;
6338 int x;
6339
6340 /*
6341 * Find the function name in the table. Binary search.
6342 */
6343 while (first <= last)
6344 {
6345 x = first + ((unsigned)(last - first) >> 1);
6346 cmp = STRCMP(name, functions[x].f_name);
6347 if (cmp < 0)
6348 last = x - 1;
6349 else if (cmp > 0)
6350 first = x + 1;
6351 else
6352 return x;
6353 }
6354 return -1;
6355}
6356
6357/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6359 * name it contains, otherwise return "name".
6360 */
6361 static char_u *
6362deref_func_name(name, lenp)
6363 char_u *name;
6364 int *lenp;
6365{
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006367 int cc;
6368
6369 cc = name[*lenp];
6370 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006371 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 {
6377 *lenp = 0;
6378 return (char_u *)""; /* just in case */
6379 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 *lenp = STRLEN(v->di_tv.vval.v_string);
6381 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 }
6383
6384 return name;
6385}
6386
6387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 * Allocate a variable for the result of a function.
6389 * Return OK or FAIL.
6390 */
6391 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6393 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 char_u *name; /* name of the function */
6395 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 char_u **arg; /* argument, pointing to the '(' */
6398 linenr_T firstline; /* first line of range */
6399 linenr_T lastline; /* last line of range */
6400 int *doesrange; /* return: function handled range */
6401 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 char_u *argp;
6405 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 int argcount = 0; /* number of arguments found */
6408
6409 /*
6410 * Get the arguments.
6411 */
6412 argp = *arg;
6413 while (argcount < MAX_FUNC_ARGS)
6414 {
6415 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6416 if (*argp == ')' || *argp == ',' || *argp == NUL)
6417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6419 {
6420 ret = FAIL;
6421 break;
6422 }
6423 ++argcount;
6424 if (*argp != ',')
6425 break;
6426 }
6427 if (*argp == ')')
6428 ++argp;
6429 else
6430 ret = FAIL;
6431
6432 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006433 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006434 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 {
6437 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006444 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446 *arg = skipwhite(argp);
6447 return ret;
6448}
6449
6450
6451/*
6452 * Call a function with its resolved parameters
6453 * Return OK or FAIL.
6454 */
6455 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 char_u *name; /* name of the function */
6459 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 linenr_T firstline; /* first line of range */
6464 linenr_T lastline; /* last line of range */
6465 int *doesrange; /* return: function handled range */
6466 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470#define ERROR_UNKNOWN 0
6471#define ERROR_TOOMANY 1
6472#define ERROR_TOOFEW 2
6473#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006474#define ERROR_DICT 4
6475#define ERROR_NONE 5
6476#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 int error = ERROR_NONE;
6478 int i;
6479 int llen;
6480 ufunc_T *fp;
6481 int cc;
6482#define FLEN_FIXED 40
6483 char_u fname_buf[FLEN_FIXED + 1];
6484 char_u *fname;
6485
6486 /*
6487 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6488 * Change <SNR>123_name() to K_SNR 123_name().
6489 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6490 */
6491 cc = name[len];
6492 name[len] = NUL;
6493 llen = eval_fname_script(name);
6494 if (llen > 0)
6495 {
6496 fname_buf[0] = K_SPECIAL;
6497 fname_buf[1] = KS_EXTRA;
6498 fname_buf[2] = (int)KE_SNR;
6499 i = 3;
6500 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6501 {
6502 if (current_SID <= 0)
6503 error = ERROR_SCRIPT;
6504 else
6505 {
6506 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6507 i = (int)STRLEN(fname_buf);
6508 }
6509 }
6510 if (i + STRLEN(name + llen) < FLEN_FIXED)
6511 {
6512 STRCPY(fname_buf + i, name + llen);
6513 fname = fname_buf;
6514 }
6515 else
6516 {
6517 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6518 if (fname == NULL)
6519 error = ERROR_OTHER;
6520 else
6521 {
6522 mch_memmove(fname, fname_buf, (size_t)i);
6523 STRCPY(fname + i, name + llen);
6524 }
6525 }
6526 }
6527 else
6528 fname = name;
6529
6530 *doesrange = FALSE;
6531
6532
6533 /* execute the function if no errors detected and executing */
6534 if (evaluate && error == ERROR_NONE)
6535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006536 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 error = ERROR_UNKNOWN;
6538
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006539 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
6541 /*
6542 * User defined function.
6543 */
6544 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006545
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006547 /* Trigger FuncUndefined event, may load the function. */
6548 if (fp == NULL
6549 && apply_autocmds(EVENT_FUNCUNDEFINED,
6550 fname, fname, TRUE, NULL)
6551 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006553 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 fp = find_func(fname);
6555 }
6556#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006557 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006558 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006559 {
6560 /* loaded a package, search for the function again */
6561 fp = find_func(fname);
6562 }
6563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 if (fp != NULL)
6565 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006566 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006568 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006570 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006572 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 else
6575 {
6576 /*
6577 * Call the user function.
6578 * Save and restore search patterns, script variables and
6579 * redo buffer.
6580 */
6581 save_search_patterns();
6582 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006583 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006584 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006585 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006586 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6587 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6588 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006589 /* Function was unreferenced while being used, free it
6590 * now. */
6591 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 restoreRedobuff();
6593 restore_search_patterns();
6594 error = ERROR_NONE;
6595 }
6596 }
6597 }
6598 else
6599 {
6600 /*
6601 * Find the function name in the table, call its implementation.
6602 */
6603 i = find_internal_func(fname);
6604 if (i >= 0)
6605 {
6606 if (argcount < functions[i].f_min_argc)
6607 error = ERROR_TOOFEW;
6608 else if (argcount > functions[i].f_max_argc)
6609 error = ERROR_TOOMANY;
6610 else
6611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 error = ERROR_NONE;
6615 }
6616 }
6617 }
6618 /*
6619 * The function call (or "FuncUndefined" autocommand sequence) might
6620 * have been aborted by an error, an interrupt, or an explicitly thrown
6621 * exception that has not been caught so far. This situation can be
6622 * tested for by calling aborting(). For an error in an internal
6623 * function or for the "E132" error in call_user_func(), however, the
6624 * throw point at which the "force_abort" flag (temporarily reset by
6625 * emsg()) is normally updated has not been reached yet. We need to
6626 * update that flag first to make aborting() reliable.
6627 */
6628 update_force_abort();
6629 }
6630 if (error == ERROR_NONE)
6631 ret = OK;
6632
6633 /*
6634 * Report an error unless the argument evaluation or function call has been
6635 * cancelled due to an aborting error, an interrupt, or an exception.
6636 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 if (!aborting())
6638 {
6639 switch (error)
6640 {
6641 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006642 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643 break;
6644 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006645 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646 break;
6647 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006648 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649 name);
6650 break;
6651 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 name);
6654 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006655 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657 name);
6658 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006659 }
6660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 name[len] = cc;
6663 if (fname != name && fname != fname_buf)
6664 vim_free(fname);
6665
6666 return ret;
6667}
6668
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006669/*
6670 * Give an error message with a function name. Handle <SNR> things.
6671 */
6672 static void
6673emsg_funcname(msg, name)
6674 char *msg;
6675 char_u *name;
6676{
6677 char_u *p;
6678
6679 if (*name == K_SPECIAL)
6680 p = concat_str((char_u *)"<SNR>", name + 3);
6681 else
6682 p = name;
6683 EMSG2(_(msg), p);
6684 if (p != name)
6685 vim_free(p);
6686}
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688/*********************************************
6689 * Implementation of the built-in functions
6690 */
6691
6692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006693 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 */
6695 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006696f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006697 typval_T *argvars;
6698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
Bram Moolenaar33570922005-01-25 22:26:29 +00006700 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006702 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006705 if ((l = argvars[0].vval.v_list) != NULL
6706 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6707 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006708 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 }
6710 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006711 EMSG(_(e_listreq));
6712}
6713
6714/*
6715 * "append(lnum, string/list)" function
6716 */
6717 static void
6718f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006719 typval_T *argvars;
6720 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006721{
6722 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 list_T *l = NULL;
6724 listitem_T *li = NULL;
6725 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006726 long added = 0;
6727
6728 rettv->vval.v_number = 1; /* Default: Failed */
6729 lnum = get_tv_lnum(argvars);
6730 if (lnum >= 0
6731 && lnum <= curbuf->b_ml.ml_line_count
6732 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006734 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006736 l = argvars[1].vval.v_list;
6737 if (l == NULL)
6738 return;
6739 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006741 for (;;)
6742 {
6743 if (l == NULL)
6744 tv = &argvars[1]; /* append a string */
6745 else if (li == NULL)
6746 break; /* end of list */
6747 else
6748 tv = &li->li_tv; /* append item from list */
6749 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6750 ++added;
6751 if (l == NULL)
6752 break;
6753 li = li->li_next;
6754 }
6755
6756 appended_lines_mark(lnum, added);
6757 if (curwin->w_cursor.lnum > lnum)
6758 curwin->w_cursor.lnum += added;
6759 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 }
6761}
6762
6763/*
6764 * "argc()" function
6765 */
6766/* ARGSUSED */
6767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006768f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006769 typval_T *argvars;
6770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006772 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773}
6774
6775/*
6776 * "argidx()" function
6777 */
6778/* ARGSUSED */
6779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006780f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 typval_T *argvars;
6782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006784 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785}
6786
6787/*
6788 * "argv(nr)" function
6789 */
6790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 typval_T *argvars;
6793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 int idx;
6796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006797 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006799 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801 rettv->vval.v_string = NULL;
6802 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803}
6804
6805/*
6806 * "browse(save, title, initdir, default)" function
6807 */
6808/* ARGSUSED */
6809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006811 typval_T *argvars;
6812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814#ifdef FEAT_BROWSE
6815 int save;
6816 char_u *title;
6817 char_u *initdir;
6818 char_u *defname;
6819 char_u buf[NUMBUFLEN];
6820 char_u buf2[NUMBUFLEN];
6821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006822 save = get_tv_number(&argvars[0]);
6823 title = get_tv_string(&argvars[1]);
6824 initdir = get_tv_string_buf(&argvars[2], buf);
6825 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006827 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006828 do_browse(save ? BROWSE_SAVE : 0,
6829 title, defname, NULL, initdir, NULL, curbuf);
6830#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006831 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006832#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006834}
6835
6836/*
6837 * "browsedir(title, initdir)" function
6838 */
6839/* ARGSUSED */
6840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006842 typval_T *argvars;
6843 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006844{
6845#ifdef FEAT_BROWSE
6846 char_u *title;
6847 char_u *initdir;
6848 char_u buf[NUMBUFLEN];
6849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006850 title = get_tv_string(&argvars[0]);
6851 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006852
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006853 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006854 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006856 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859}
6860
Bram Moolenaar33570922005-01-25 22:26:29 +00006861static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863/*
6864 * Find a buffer by number or exact name.
6865 */
6866 static buf_T *
6867find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006872 if (avar->v_type == VAR_NUMBER)
6873 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006874 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006876 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006877 if (buf == NULL)
6878 {
6879 /* No full path name match, try a match with a URL or a "nofile"
6880 * buffer, these don't use the full path. */
6881 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6882 if (buf->b_fname != NULL
6883 && (path_with_url(buf->b_fname)
6884#ifdef FEAT_QUICKFIX
6885 || bt_nofile(buf)
6886#endif
6887 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006888 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006889 break;
6890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 return buf;
6893}
6894
6895/*
6896 * "bufexists(expr)" function
6897 */
6898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 typval_T *argvars;
6901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006903 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
6906/*
6907 * "buflisted(expr)" function
6908 */
6909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 typval_T *argvars;
6912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 buf_T *buf;
6915
6916 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006917 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919
6920/*
6921 * "bufloaded(expr)" function
6922 */
6923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006924f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006925 typval_T *argvars;
6926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
6928 buf_T *buf;
6929
6930 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006931 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932}
6933
Bram Moolenaar33570922005-01-25 22:26:29 +00006934static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936/*
6937 * Get buffer by number or pattern.
6938 */
6939 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006940get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006943 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 int save_magic;
6945 char_u *save_cpo;
6946 buf_T *buf;
6947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006948 if (tv->v_type == VAR_NUMBER)
6949 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006950 if (tv->v_type != VAR_STRING)
6951 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (name == NULL || *name == NUL)
6953 return curbuf;
6954 if (name[0] == '$' && name[1] == NUL)
6955 return lastbuf;
6956
6957 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6958 save_magic = p_magic;
6959 p_magic = TRUE;
6960 save_cpo = p_cpo;
6961 p_cpo = (char_u *)"";
6962
6963 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6964 TRUE, FALSE));
6965
6966 p_magic = save_magic;
6967 p_cpo = save_cpo;
6968
6969 /* If not found, try expanding the name, like done for bufexists(). */
6970 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 return buf;
6974}
6975
6976/*
6977 * "bufname(expr)" function
6978 */
6979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006980f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 typval_T *argvars;
6982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 buf_T *buf;
6985
6986 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006987 buf = get_buf_tv(&argvars[0]);
6988 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 --emsg_off;
6994}
6995
6996/*
6997 * "bufnr(expr)" function
6998 */
6999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007000f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007001 typval_T *argvars;
7002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 buf_T *buf;
7005
7006 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007007 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 --emsg_off;
7013}
7014
7015/*
7016 * "bufwinnr(nr)" function
7017 */
7018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007019f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 typval_T *argvars;
7021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023#ifdef FEAT_WINDOWS
7024 win_T *wp;
7025 int winnr = 0;
7026#endif
7027 buf_T *buf;
7028
7029 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031#ifdef FEAT_WINDOWS
7032 for (wp = firstwin; wp; wp = wp->w_next)
7033 {
7034 ++winnr;
7035 if (wp->w_buffer == buf)
7036 break;
7037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007040 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041#endif
7042 --emsg_off;
7043}
7044
7045/*
7046 * "byte2line(byte)" function
7047 */
7048/*ARGSUSED*/
7049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007050f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 typval_T *argvars;
7052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#else
7057 long boff = 0;
7058
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007059 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007061 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007063 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 (linenr_T)0, &boff);
7065#endif
7066}
7067
7068/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007069 * "byteidx()" function
7070 */
7071/*ARGSUSED*/
7072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 typval_T *argvars;
7075 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007076{
7077#ifdef FEAT_MBYTE
7078 char_u *t;
7079#endif
7080 char_u *str;
7081 long idx;
7082
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083 str = get_tv_string(&argvars[0]);
7084 idx = get_tv_number(&argvars[1]);
7085 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007086 if (idx < 0)
7087 return;
7088
7089#ifdef FEAT_MBYTE
7090 t = str;
7091 for ( ; idx > 0; idx--)
7092 {
7093 if (*t == NUL) /* EOL reached */
7094 return;
7095 t += mb_ptr2len_check(t);
7096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007097 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007098#else
7099 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007101#endif
7102}
7103
7104/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007105 * "call(func, arglist)" function
7106 */
7107 static void
7108f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 typval_T *argvars;
7110 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007111{
7112 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007114 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007116 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007118
7119 rettv->vval.v_number = 0;
7120 if (argvars[1].v_type != VAR_LIST)
7121 {
7122 EMSG(_(e_listreq));
7123 return;
7124 }
7125 if (argvars[1].vval.v_list == NULL)
7126 return;
7127
7128 if (argvars[0].v_type == VAR_FUNC)
7129 func = argvars[0].vval.v_string;
7130 else
7131 func = get_tv_string(&argvars[0]);
7132
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 if (argvars[2].v_type != VAR_UNKNOWN)
7134 {
7135 if (argvars[2].v_type != VAR_DICT)
7136 {
7137 EMSG(_(e_dictreq));
7138 return;
7139 }
7140 selfdict = argvars[2].vval.v_dict;
7141 }
7142
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007143 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7144 item = item->li_next)
7145 {
7146 if (argc == MAX_FUNC_ARGS)
7147 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007148 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007149 break;
7150 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007151 /* Make a copy of each argument. This is needed to be able to set
7152 * v_lock to VAR_FIXED in the copy without changing the original list.
7153 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007154 copy_tv(&item->li_tv, &argv[argc++]);
7155 }
7156
7157 if (item == NULL)
7158 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7160 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007161
7162 /* Free the arguments. */
7163 while (argc > 0)
7164 clear_tv(&argv[--argc]);
7165}
7166
7167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * "char2nr(string)" function
7169 */
7170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007171f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 typval_T *argvars;
7173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175#ifdef FEAT_MBYTE
7176 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007177 rettv->vval.v_number =
7178 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 else
7180#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007181 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182}
7183
7184/*
7185 * "cindent(lnum)" function
7186 */
7187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 typval_T *argvars;
7190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192#ifdef FEAT_CINDENT
7193 pos_T pos;
7194 linenr_T lnum;
7195
7196 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007197 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7199 {
7200 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007201 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 curwin->w_cursor = pos;
7203 }
7204 else
7205#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
7209/*
7210 * "col(string)" function
7211 */
7212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007213f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 typval_T *argvars;
7215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 colnr_T col = 0;
7218 pos_T *fp;
7219
7220 fp = var2fpos(&argvars[0], FALSE);
7221 if (fp != NULL)
7222 {
7223 if (fp->col == MAXCOL)
7224 {
7225 /* '> can be MAXCOL, get the length of the line then */
7226 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7227 col = STRLEN(ml_get(fp->lnum)) + 1;
7228 else
7229 col = MAXCOL;
7230 }
7231 else
7232 {
7233 col = fp->col + 1;
7234#ifdef FEAT_VIRTUALEDIT
7235 /* col(".") when the cursor is on the NUL at the end of the line
7236 * because of "coladd" can be seen as an extra column. */
7237 if (virtual_active() && fp == &curwin->w_cursor)
7238 {
7239 char_u *p = ml_get_cursor();
7240
7241 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7242 curwin->w_virtcol - curwin->w_cursor.coladd))
7243 {
7244# ifdef FEAT_MBYTE
7245 int l;
7246
7247 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7248 col += l;
7249# else
7250 if (*p != NUL && p[1] == NUL)
7251 ++col;
7252# endif
7253 }
7254 }
7255#endif
7256 }
7257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007258 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259}
7260
7261/*
7262 * "confirm(message, buttons[, default [, type]])" function
7263 */
7264/*ARGSUSED*/
7265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007266f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 typval_T *argvars;
7268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7271 char_u *message;
7272 char_u *buttons = NULL;
7273 char_u buf[NUMBUFLEN];
7274 char_u buf2[NUMBUFLEN];
7275 int def = 1;
7276 int type = VIM_GENERIC;
7277 int c;
7278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007279 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007282 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007283 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007285 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007286 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007288 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007289 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 switch (TOUPPER_ASC(c))
7291 {
7292 case 'E': type = VIM_ERROR; break;
7293 case 'Q': type = VIM_QUESTION; break;
7294 case 'I': type = VIM_INFO; break;
7295 case 'W': type = VIM_WARNING; break;
7296 case 'G': type = VIM_GENERIC; break;
7297 }
7298 }
7299 }
7300 }
7301
7302 if (buttons == NULL || *buttons == NUL)
7303 buttons = (char_u *)_("&Ok");
7304
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 def, NULL);
7307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007308 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309#endif
7310}
7311
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007312/*
7313 * "copy()" function
7314 */
7315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007316f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 typval_T *argvars;
7318 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007320 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
7323/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007324 * "count()" function
7325 */
7326 static void
7327f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 typval_T *argvars;
7329 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007330{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007331 long n = 0;
7332 int ic = FALSE;
7333
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007335 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 listitem_T *li;
7337 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007339
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 if ((l = argvars[0].vval.v_list) != NULL)
7341 {
7342 li = l->lv_first;
7343 if (argvars[2].v_type != VAR_UNKNOWN)
7344 {
7345 ic = get_tv_number(&argvars[2]);
7346 if (argvars[3].v_type != VAR_UNKNOWN)
7347 {
7348 idx = get_tv_number(&argvars[3]);
7349 li = list_find(l, idx);
7350 if (li == NULL)
7351 EMSGN(_(e_listidx), idx);
7352 }
7353 }
7354
7355 for ( ; li != NULL; li = li->li_next)
7356 if (tv_equal(&li->li_tv, &argvars[1], ic))
7357 ++n;
7358 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 else if (argvars[0].v_type == VAR_DICT)
7361 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 int todo;
7363 dict_T *d;
7364 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 if ((d = argvars[0].vval.v_dict) != NULL)
7367 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 if (argvars[2].v_type != VAR_UNKNOWN)
7369 {
7370 ic = get_tv_number(&argvars[2]);
7371 if (argvars[3].v_type != VAR_UNKNOWN)
7372 EMSG(_(e_invarg));
7373 }
7374
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 todo = d->dv_hashtab.ht_used;
7376 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 {
7378 if (!HASHITEM_EMPTY(hi))
7379 {
7380 --todo;
7381 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7382 ++n;
7383 }
7384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 }
7386 }
7387 else
7388 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007389 rettv->vval.v_number = n;
7390}
7391
7392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7394 *
7395 * Checks the existence of a cscope connection.
7396 */
7397/*ARGSUSED*/
7398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 typval_T *argvars;
7401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403#ifdef FEAT_CSCOPE
7404 int num = 0;
7405 char_u *dbpath = NULL;
7406 char_u *prepend = NULL;
7407 char_u buf[NUMBUFLEN];
7408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007409 if (argvars[0].v_type != VAR_UNKNOWN
7410 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412 num = (int)get_tv_number(&argvars[0]);
7413 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007414 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 }
7417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007420 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421#endif
7422}
7423
7424/*
7425 * "cursor(lnum, col)" function
7426 *
7427 * Moves the cursor to the specified line and column
7428 */
7429/*ARGSUSED*/
7430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 typval_T *argvars;
7433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
7435 long line, col;
7436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (line > 0)
7439 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (col > 0)
7442 curwin->w_cursor.col = col - 1;
7443#ifdef FEAT_VIRTUALEDIT
7444 curwin->w_cursor.coladd = 0;
7445#endif
7446
7447 /* Make sure the cursor is in a valid position. */
7448 check_cursor();
7449#ifdef FEAT_MBYTE
7450 /* Correct cursor for multi-byte character. */
7451 if (has_mbyte)
7452 mb_adjust_cursor();
7453#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007454
7455 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456}
7457
7458/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007459 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 */
7461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007463 typval_T *argvars;
7464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007466 static int copyID = 0;
7467 int noref = 0;
7468
7469 if (argvars[1].v_type != VAR_UNKNOWN)
7470 noref = get_tv_number(&argvars[1]);
7471 if (noref < 0 || noref > 1)
7472 EMSG(_(e_invarg));
7473 else
7474 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475}
7476
7477/*
7478 * "delete()" function
7479 */
7480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 typval_T *argvars;
7483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491/*
7492 * "did_filetype()" function
7493 */
7494/*ARGSUSED*/
7495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 typval_T *argvars;
7498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499{
7500#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007503 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504#endif
7505}
7506
7507/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007508 * "diff_filler()" function
7509 */
7510/*ARGSUSED*/
7511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 typval_T *argvars;
7514 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007515{
7516#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007518#endif
7519}
7520
7521/*
7522 * "diff_hlID()" function
7523 */
7524/*ARGSUSED*/
7525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007526f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 typval_T *argvars;
7528 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007529{
7530#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007532 static linenr_T prev_lnum = 0;
7533 static int changedtick = 0;
7534 static int fnum = 0;
7535 static int change_start = 0;
7536 static int change_end = 0;
7537 static enum hlf_value hlID = 0;
7538 int filler_lines;
7539 int col;
7540
7541 if (lnum != prev_lnum
7542 || changedtick != curbuf->b_changedtick
7543 || fnum != curbuf->b_fnum)
7544 {
7545 /* New line, buffer, change: need to get the values. */
7546 filler_lines = diff_check(curwin, lnum);
7547 if (filler_lines < 0)
7548 {
7549 if (filler_lines == -1)
7550 {
7551 change_start = MAXCOL;
7552 change_end = -1;
7553 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7554 hlID = HLF_ADD; /* added line */
7555 else
7556 hlID = HLF_CHD; /* changed line */
7557 }
7558 else
7559 hlID = HLF_ADD; /* added line */
7560 }
7561 else
7562 hlID = (enum hlf_value)0;
7563 prev_lnum = lnum;
7564 changedtick = curbuf->b_changedtick;
7565 fnum = curbuf->b_fnum;
7566 }
7567
7568 if (hlID == HLF_CHD || hlID == HLF_TXD)
7569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007570 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007571 if (col >= change_start && col <= change_end)
7572 hlID = HLF_TXD; /* changed text */
7573 else
7574 hlID = HLF_CHD; /* changed line */
7575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007577#endif
7578}
7579
7580/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007581 * "empty({expr})" function
7582 */
7583 static void
7584f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 typval_T *argvars;
7586 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007587{
7588 int n;
7589
7590 switch (argvars[0].v_type)
7591 {
7592 case VAR_STRING:
7593 case VAR_FUNC:
7594 n = argvars[0].vval.v_string == NULL
7595 || *argvars[0].vval.v_string == NUL;
7596 break;
7597 case VAR_NUMBER:
7598 n = argvars[0].vval.v_number == 0;
7599 break;
7600 case VAR_LIST:
7601 n = argvars[0].vval.v_list == NULL
7602 || argvars[0].vval.v_list->lv_first == NULL;
7603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604 case VAR_DICT:
7605 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007608 default:
7609 EMSG2(_(e_intern2), "f_empty()");
7610 n = 0;
7611 }
7612
7613 rettv->vval.v_number = n;
7614}
7615
7616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 * "escape({string}, {chars})" function
7618 */
7619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *argvars;
7622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 char_u buf[NUMBUFLEN];
7625
Bram Moolenaar758711c2005-02-02 23:11:38 +00007626 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7627 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629}
7630
7631/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007632 * "eval()" function
7633 */
7634/*ARGSUSED*/
7635 static void
7636f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 typval_T *argvars;
7638 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007639{
7640 char_u *s;
7641
7642 s = get_tv_string(&argvars[0]);
7643 s = skipwhite(s);
7644
7645 if (eval1(&s, rettv, TRUE) == FAIL)
7646 rettv->vval.v_number = 0;
7647 else if (*s != NUL)
7648 EMSG(_(e_trailing));
7649}
7650
7651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 * "eventhandler()" function
7653 */
7654/*ARGSUSED*/
7655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 typval_T *argvars;
7658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661}
7662
7663/*
7664 * "executable()" function
7665 */
7666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672}
7673
7674/*
7675 * "exists()" function
7676 */
7677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
7682 char_u *p;
7683 char_u *name;
7684 int n = FALSE;
7685 int len = 0;
7686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (*p == '$') /* environment variable */
7689 {
7690 /* first try "normal" environment variables (fast) */
7691 if (mch_getenv(p + 1) != NULL)
7692 n = TRUE;
7693 else
7694 {
7695 /* try expanding things like $VIM and ${HOME} */
7696 p = expand_env_save(p);
7697 if (p != NULL && *p != '$')
7698 n = TRUE;
7699 vim_free(p);
7700 }
7701 }
7702 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 else if (*p == '*') /* internal or user defined function */
7705 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007706 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708 else if (*p == ':')
7709 {
7710 n = cmd_exists(p + 1);
7711 }
7712 else if (*p == '#')
7713 {
7714#ifdef FEAT_AUTOCMD
7715 name = p + 1;
7716 p = vim_strchr(name, '#');
7717 if (p != NULL)
7718 n = au_exists(name, p, p + 1);
7719 else
7720 n = au_exists(name, name + STRLEN(name), NULL);
7721#endif
7722 }
7723 else /* internal variable */
7724 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007725 char_u *tofree;
7726 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007728 /* get_name_len() takes care of expanding curly braces */
7729 name = p;
7730 len = get_name_len(&p, &tofree, TRUE, FALSE);
7731 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007733 if (tofree != NULL)
7734 name = tofree;
7735 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7736 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007738 /* handle d.key, l[idx], f(expr) */
7739 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7740 if (n)
7741 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007745 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007748 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749}
7750
7751/*
7752 * "expand()" function
7753 */
7754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007756 typval_T *argvars;
7757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
7759 char_u *s;
7760 int len;
7761 char_u *errormsg;
7762 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7763 expand_T xpc;
7764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 rettv->v_type = VAR_STRING;
7766 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (*s == '%' || *s == '#' || *s == '<')
7768 {
7769 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 --emsg_off;
7772 }
7773 else
7774 {
7775 /* When the optional second argument is non-zero, don't remove matches
7776 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 flags |= WILD_KEEP_ALL;
7779 ExpandInit(&xpc);
7780 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 ExpandCleanup(&xpc);
7783 }
7784}
7785
7786/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007787 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007789 */
7790 static void
7791f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 typval_T *argvars;
7793 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007794{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007796 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 list_T *l1, *l2;
7799 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007801
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 l1 = argvars[0].vval.v_list;
7803 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007804 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7805 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 {
7807 if (argvars[2].v_type != VAR_UNKNOWN)
7808 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007809 before = get_tv_number(&argvars[2]);
7810 if (before == l1->lv_len)
7811 item = NULL;
7812 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007814 item = list_find(l1, before);
7815 if (item == NULL)
7816 {
7817 EMSGN(_(e_listidx), before);
7818 return;
7819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 }
7821 }
7822 else
7823 item = NULL;
7824 list_extend(l1, l2, item);
7825
7826 ++l1->lv_refcount;
7827 copy_tv(&argvars[0], rettv);
7828 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7831 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 dict_T *d1, *d2;
7833 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007834 char_u *action;
7835 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007837 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007838
7839 d1 = argvars[0].vval.v_dict;
7840 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007841 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7842 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 {
7844 /* Check the third argument. */
7845 if (argvars[2].v_type != VAR_UNKNOWN)
7846 {
7847 static char *(av[]) = {"keep", "force", "error"};
7848
7849 action = get_tv_string(&argvars[2]);
7850 for (i = 0; i < 3; ++i)
7851 if (STRCMP(action, av[i]) == 0)
7852 break;
7853 if (i == 3)
7854 {
7855 EMSGN(_(e_invarg2), action);
7856 return;
7857 }
7858 }
7859 else
7860 action = (char_u *)"force";
7861
7862 /* Go over all entries in the second dict and add them to the
7863 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 todo = d2->dv_hashtab.ht_used;
7865 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007867 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007869 --todo;
7870 di1 = dict_find(d1, hi2->hi_key, -1);
7871 if (di1 == NULL)
7872 {
7873 di1 = dictitem_copy(HI2DI(hi2));
7874 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7875 dictitem_free(di1);
7876 }
7877 else if (*action == 'e')
7878 {
7879 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7880 break;
7881 }
7882 else if (*action == 'f')
7883 {
7884 clear_tv(&di1->di_tv);
7885 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887 }
7888 }
7889
7890 ++d1->dv_refcount;
7891 copy_tv(&argvars[0], rettv);
7892 }
7893 }
7894 else
7895 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007896}
7897
7898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 * "filereadable()" function
7900 */
7901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 typval_T *argvars;
7904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 FILE *fd;
7907 char_u *p;
7908 int n;
7909
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7912 {
7913 n = TRUE;
7914 fclose(fd);
7915 }
7916 else
7917 n = FALSE;
7918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007919 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920}
7921
7922/*
7923 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7924 * rights to write into.
7925 */
7926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 typval_T *argvars;
7929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 char_u *p;
7932 int retval = 0;
7933#if defined(UNIX) || defined(VMS)
7934 int perm = 0;
7935#endif
7936
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938#if defined(UNIX) || defined(VMS)
7939 perm = mch_getperm(p);
7940#endif
7941#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7942 if (
7943# ifdef WIN3264
7944 mch_writable(p) &&
7945# else
7946# if defined(UNIX) || defined(VMS)
7947 (perm & 0222) &&
7948# endif
7949# endif
7950 mch_access((char *)p, W_OK) == 0
7951 )
7952#endif
7953 {
7954 ++retval;
7955 if (mch_isdir(p))
7956 ++retval;
7957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
Bram Moolenaar33570922005-01-25 22:26:29 +00007961static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007962
7963 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007964findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 typval_T *argvars;
7966 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007967 int dir;
7968{
7969#ifdef FEAT_SEARCHPATH
7970 char_u *fname;
7971 char_u *fresult = NULL;
7972 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7973 char_u *p;
7974 char_u pathbuf[NUMBUFLEN];
7975 int count = 1;
7976 int first = TRUE;
7977
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007979
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007980 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007983 if (*p != NUL)
7984 path = p;
7985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007988 }
7989
7990 do
7991 {
7992 vim_free(fresult);
7993 fresult = find_file_in_path_option(first ? fname : NULL,
7994 first ? (int)STRLEN(fname) : 0,
7995 0, first, path, dir, NULL);
7996 first = FALSE;
7997 } while (--count > 0 && fresult != NULL);
7998
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007999 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008000#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004}
8005
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008006static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8007static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008008static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8009static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010
8011/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008012 * Prepare v: variable "idx" to be used.
8013 * Save the current typeval in "save_tv".
8014 * When not used yet add the variable to the v: hashtable.
8015 */
8016 static void
8017prepare_vimvar(idx, save_tv)
8018 int idx;
8019 typval_T *save_tv;
8020{
8021 *save_tv = vimvars[idx].vv_tv;
8022 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8023 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8024}
8025
8026/*
8027 * Restore v: variable "idx" to typeval "save_tv".
8028 * When no longer defined, remove the variable from the v: hashtable.
8029 */
8030 static void
8031restore_vimvar(idx, save_tv)
8032 int idx;
8033 typval_T *save_tv;
8034{
8035 hashitem_T *hi;
8036
8037 clear_tv(&vimvars[idx].vv_tv);
8038 vimvars[idx].vv_tv = *save_tv;
8039 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8040 {
8041 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8042 if (HASHITEM_EMPTY(hi))
8043 EMSG2(_(e_intern2), "restore_vimvar()");
8044 else
8045 hash_remove(&vimvarht, hi);
8046 }
8047}
8048
8049/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008050 * Implementation of map() and filter().
8051 */
8052 static void
8053filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 typval_T *argvars;
8055 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008056 int map;
8057{
8058 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008059 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008060 listitem_T *li, *nli;
8061 list_T *l = NULL;
8062 dictitem_T *di;
8063 hashtab_T *ht;
8064 hashitem_T *hi;
8065 dict_T *d = NULL;
8066 typval_T save_val;
8067 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008069 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008070 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8071
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008072
8073 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008074 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008075 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008076 if ((l = argvars[0].vval.v_list) == NULL
8077 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 return;
8079 }
8080 else if (argvars[0].v_type == VAR_DICT)
8081 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008082 if ((d = argvars[0].vval.v_dict) == NULL
8083 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008084 return;
8085 }
8086 else
8087 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008088 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008089 return;
8090 }
8091
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008092 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008093 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008094
8095 if (argvars[0].v_type == VAR_DICT)
8096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008097 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008099
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008101 hash_lock(ht);
8102 todo = ht->ht_used;
8103 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008105 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008106 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008107 --todo;
8108 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008109 if (tv_check_lock(di->di_tv.v_lock, msg))
8110 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008112 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8113 break;
8114 if (!map && rem)
8115 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008119 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008121 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 else
8124 {
8125 for (li = l->lv_first; li != NULL; li = nli)
8126 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008127 if (tv_check_lock(li->li_tv.v_lock, msg))
8128 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008129 nli = li->li_next;
8130 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8131 break;
8132 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008133 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 }
8135 }
8136
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008137 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008138
8139 copy_tv(&argvars[0], rettv);
8140}
8141
8142 static int
8143filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 char_u *expr;
8146 int map;
8147 int *remp;
8148{
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008150 char_u *s;
8151
Bram Moolenaar33570922005-01-25 22:26:29 +00008152 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 s = expr;
8154 if (eval1(&s, &rettv, TRUE) == FAIL)
8155 return FAIL;
8156 if (*s != NUL) /* check for trailing chars after expr */
8157 {
8158 EMSG2(_(e_invexpr2), s);
8159 return FAIL;
8160 }
8161 if (map)
8162 {
8163 /* map(): replace the list item value */
8164 clear_tv(tv);
8165 *tv = rettv;
8166 }
8167 else
8168 {
8169 /* filter(): when expr is zero remove the item */
8170 *remp = (get_tv_number(&rettv) == 0);
8171 clear_tv(&rettv);
8172 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008173 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008174 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175}
8176
8177/*
8178 * "filter()" function
8179 */
8180 static void
8181f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 typval_T *argvars;
8183 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008184{
8185 filter_map(argvars, rettv, FALSE);
8186}
8187
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008189 * "finddir({fname}[, {path}[, {count}]])" function
8190 */
8191 static void
8192f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 typval_T *argvars;
8194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008195{
8196 findfilendir(argvars, rettv, TRUE);
8197}
8198
8199/*
8200 * "findfile({fname}[, {path}[, {count}]])" function
8201 */
8202 static void
8203f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008206{
8207 findfilendir(argvars, rettv, FALSE);
8208}
8209
8210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 * "fnamemodify({fname}, {mods})" function
8212 */
8213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 typval_T *argvars;
8216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 char_u *fname;
8219 char_u *mods;
8220 int usedlen = 0;
8221 int len;
8222 char_u *fbuf = NULL;
8223 char_u buf[NUMBUFLEN];
8224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225 fname = get_tv_string(&argvars[0]);
8226 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 len = (int)STRLEN(fname);
8228
8229 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8230
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008231 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 vim_free(fbuf);
8237}
8238
Bram Moolenaar33570922005-01-25 22:26:29 +00008239static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240
8241/*
8242 * "foldclosed()" function
8243 */
8244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008246 typval_T *argvars;
8247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 int end;
8249{
8250#ifdef FEAT_FOLDING
8251 linenr_T lnum;
8252 linenr_T first, last;
8253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8256 {
8257 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8258 {
8259 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 return;
8264 }
8265 }
8266#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268}
8269
8270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008271 * "foldclosed()" function
8272 */
8273 static void
8274f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008275 typval_T *argvars;
8276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008277{
8278 foldclosed_both(argvars, rettv, FALSE);
8279}
8280
8281/*
8282 * "foldclosedend()" function
8283 */
8284 static void
8285f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 typval_T *argvars;
8287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008288{
8289 foldclosed_both(argvars, rettv, TRUE);
8290}
8291
8292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 * "foldlevel()" function
8294 */
8295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008296f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 typval_T *argvars;
8298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299{
8300#ifdef FEAT_FOLDING
8301 linenr_T lnum;
8302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008303 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008305 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 else
8307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309}
8310
8311/*
8312 * "foldtext()" function
8313 */
8314/*ARGSUSED*/
8315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 typval_T *argvars;
8318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320#ifdef FEAT_FOLDING
8321 linenr_T lnum;
8322 char_u *s;
8323 char_u *r;
8324 int len;
8325 char *txt;
8326#endif
8327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008328 rettv->v_type = VAR_STRING;
8329 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8332 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8333 <= curbuf->b_ml.ml_line_count
8334 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
8336 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008337 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8338 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
8340 if (!linewhite(lnum))
8341 break;
8342 ++lnum;
8343 }
8344
8345 /* Find interesting text in this line. */
8346 s = skipwhite(ml_get(lnum));
8347 /* skip C comment-start */
8348 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008351 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008352 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008353 {
8354 s = skipwhite(ml_get(lnum + 1));
8355 if (*s == '*')
8356 s = skipwhite(s + 1);
8357 }
8358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 txt = _("+-%s%3ld lines: ");
8360 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008361 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 + 20 /* for %3ld */
8363 + STRLEN(s))); /* concatenated */
8364 if (r != NULL)
8365 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008366 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8367 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8368 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 len = (int)STRLEN(r);
8370 STRCAT(r, s);
8371 /* remove 'foldmarker' and 'commentstring' */
8372 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 }
8375 }
8376#endif
8377}
8378
8379/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008380 * "foldtextresult(lnum)" function
8381 */
8382/*ARGSUSED*/
8383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 typval_T *argvars;
8386 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008387{
8388#ifdef FEAT_FOLDING
8389 linenr_T lnum;
8390 char_u *text;
8391 char_u buf[51];
8392 foldinfo_T foldinfo;
8393 int fold_count;
8394#endif
8395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008396 rettv->v_type = VAR_STRING;
8397 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008398#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008400 fold_count = foldedCount(curwin, lnum, &foldinfo);
8401 if (fold_count > 0)
8402 {
8403 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8404 &foldinfo, buf);
8405 if (text == buf)
8406 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008407 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008408 }
8409#endif
8410}
8411
8412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 * "foreground()" function
8414 */
8415/*ARGSUSED*/
8416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 typval_T *argvars;
8419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422#ifdef FEAT_GUI
8423 if (gui.in_use)
8424 gui_mch_set_foreground();
8425#else
8426# ifdef WIN32
8427 win32_set_foreground();
8428# endif
8429#endif
8430}
8431
8432/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008433 * "function()" function
8434 */
8435/*ARGSUSED*/
8436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008437f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008438 typval_T *argvars;
8439 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440{
8441 char_u *s;
8442
Bram Moolenaara7043832005-01-21 11:56:39 +00008443 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008445 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008446 EMSG2(_(e_invarg2), s);
8447 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008448 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008449 else
8450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451 rettv->vval.v_string = vim_strsave(s);
8452 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008453 }
8454}
8455
8456/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008457 * "get()" function
8458 */
8459 static void
8460f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008461 typval_T *argvars;
8462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008463{
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 listitem_T *li;
8465 list_T *l;
8466 dictitem_T *di;
8467 dict_T *d;
8468 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008469
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008471 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008473 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008474 li = list_find(l, get_tv_number(&argvars[1]));
8475 if (li != NULL)
8476 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008477 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008478 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 else if (argvars[0].v_type == VAR_DICT)
8480 {
8481 if ((d = argvars[0].vval.v_dict) != NULL)
8482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008483 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008484 if (di != NULL)
8485 tv = &di->di_tv;
8486 }
8487 }
8488 else
8489 EMSG2(_(e_listdictarg), "get()");
8490
8491 if (tv == NULL)
8492 {
8493 if (argvars[2].v_type == VAR_UNKNOWN)
8494 rettv->vval.v_number = 0;
8495 else
8496 copy_tv(&argvars[2], rettv);
8497 }
8498 else
8499 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008500}
8501
8502/*
8503 * "getbufvar()" function
8504 */
8505 static void
8506f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 typval_T *argvars;
8508 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008509{
8510 buf_T *buf;
8511 buf_T *save_curbuf;
8512 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008514
8515 ++emsg_off;
8516 buf = get_buf_tv(&argvars[0]);
8517 varname = get_tv_string(&argvars[1]);
8518
8519 rettv->v_type = VAR_STRING;
8520 rettv->vval.v_string = NULL;
8521
8522 if (buf != NULL && varname != NULL)
8523 {
8524 if (*varname == '&') /* buffer-local-option */
8525 {
8526 /* set curbuf to be our buf, temporarily */
8527 save_curbuf = curbuf;
8528 curbuf = buf;
8529
8530 get_option_tv(&varname, rettv, TRUE);
8531
8532 /* restore previous notion of curbuf */
8533 curbuf = save_curbuf;
8534 }
8535 else
8536 {
8537 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008538 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008539 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008541 }
8542 }
8543
8544 --emsg_off;
8545}
8546
8547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 * "getchar()" function
8549 */
8550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008552 typval_T *argvars;
8553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 varnumber_T n;
8556
8557 ++no_mapping;
8558 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008559 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 /* getchar(): blocking wait. */
8561 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 /* getchar(1): only check if char avail */
8564 n = vpeekc();
8565 else if (vpeekc() == NUL)
8566 /* getchar(0) and no char avail: return zero */
8567 n = 0;
8568 else
8569 /* getchar(0) and char avail: return char */
8570 n = safe_vgetc();
8571 --no_mapping;
8572 --allow_keys;
8573
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008574 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (IS_SPECIAL(n) || mod_mask != 0)
8576 {
8577 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8578 int i = 0;
8579
8580 /* Turn a special key into three bytes, plus modifier. */
8581 if (mod_mask != 0)
8582 {
8583 temp[i++] = K_SPECIAL;
8584 temp[i++] = KS_MODIFIER;
8585 temp[i++] = mod_mask;
8586 }
8587 if (IS_SPECIAL(n))
8588 {
8589 temp[i++] = K_SPECIAL;
8590 temp[i++] = K_SECOND(n);
8591 temp[i++] = K_THIRD(n);
8592 }
8593#ifdef FEAT_MBYTE
8594 else if (has_mbyte)
8595 i += (*mb_char2bytes)(n, temp + i);
8596#endif
8597 else
8598 temp[i++] = n;
8599 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600 rettv->v_type = VAR_STRING;
8601 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603}
8604
8605/*
8606 * "getcharmod()" function
8607 */
8608/*ARGSUSED*/
8609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 typval_T *argvars;
8612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
8618 * "getcmdline()" function
8619 */
8620/*ARGSUSED*/
8621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626 rettv->v_type = VAR_STRING;
8627 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628}
8629
8630/*
8631 * "getcmdpos()" function
8632 */
8633/*ARGSUSED*/
8634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *argvars;
8637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641
8642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 * "getcwd()" function
8644 */
8645/*ARGSUSED*/
8646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 typval_T *argvars;
8649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
8651 char_u cwd[MAXPATHL];
8652
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008655 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 else
8657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661#endif
8662 }
8663}
8664
8665/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008666 * "getfontname()" function
8667 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008668/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008670f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 typval_T *argvars;
8672 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->v_type = VAR_STRING;
8675 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008676#ifdef FEAT_GUI
8677 if (gui.in_use)
8678 {
8679 GuiFont font;
8680 char_u *name = NULL;
8681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008682 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008683 {
8684 /* Get the "Normal" font. Either the name saved by
8685 * hl_set_font_name() or from the font ID. */
8686 font = gui.norm_font;
8687 name = hl_get_font_name();
8688 }
8689 else
8690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008692 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8693 return;
8694 font = gui_mch_get_font(name, FALSE);
8695 if (font == NOFONT)
8696 return; /* Invalid font name, return empty string. */
8697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008699 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008700 gui_mch_free_font(font);
8701 }
8702#endif
8703}
8704
8705/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008706 * "getfperm({fname})" function
8707 */
8708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 typval_T *argvars;
8711 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008712{
8713 char_u *fname;
8714 struct stat st;
8715 char_u *perm = NULL;
8716 char_u flags[] = "rwx";
8717 int i;
8718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008720
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008722 if (mch_stat((char *)fname, &st) >= 0)
8723 {
8724 perm = vim_strsave((char_u *)"---------");
8725 if (perm != NULL)
8726 {
8727 for (i = 0; i < 9; i++)
8728 {
8729 if (st.st_mode & (1 << (8 - i)))
8730 perm[i] = flags[i % 3];
8731 }
8732 }
8733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008735}
8736
8737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 * "getfsize({fname})" function
8739 */
8740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008742 typval_T *argvars;
8743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
8745 char_u *fname;
8746 struct stat st;
8747
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 if (mch_stat((char *)fname, &st) >= 0)
8753 {
8754 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
8759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761}
8762
8763/*
8764 * "getftime({fname})" function
8765 */
8766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T *argvars;
8769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
8771 char_u *fname;
8772 struct stat st;
8773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
8776 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780}
8781
8782/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008783 * "getftype({fname})" function
8784 */
8785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *argvars;
8788 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008789{
8790 char_u *fname;
8791 struct stat st;
8792 char_u *type = NULL;
8793 char *t;
8794
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008798 if (mch_lstat((char *)fname, &st) >= 0)
8799 {
8800#ifdef S_ISREG
8801 if (S_ISREG(st.st_mode))
8802 t = "file";
8803 else if (S_ISDIR(st.st_mode))
8804 t = "dir";
8805# ifdef S_ISLNK
8806 else if (S_ISLNK(st.st_mode))
8807 t = "link";
8808# endif
8809# ifdef S_ISBLK
8810 else if (S_ISBLK(st.st_mode))
8811 t = "bdev";
8812# endif
8813# ifdef S_ISCHR
8814 else if (S_ISCHR(st.st_mode))
8815 t = "cdev";
8816# endif
8817# ifdef S_ISFIFO
8818 else if (S_ISFIFO(st.st_mode))
8819 t = "fifo";
8820# endif
8821# ifdef S_ISSOCK
8822 else if (S_ISSOCK(st.st_mode))
8823 t = "fifo";
8824# endif
8825 else
8826 t = "other";
8827#else
8828# ifdef S_IFMT
8829 switch (st.st_mode & S_IFMT)
8830 {
8831 case S_IFREG: t = "file"; break;
8832 case S_IFDIR: t = "dir"; break;
8833# ifdef S_IFLNK
8834 case S_IFLNK: t = "link"; break;
8835# endif
8836# ifdef S_IFBLK
8837 case S_IFBLK: t = "bdev"; break;
8838# endif
8839# ifdef S_IFCHR
8840 case S_IFCHR: t = "cdev"; break;
8841# endif
8842# ifdef S_IFIFO
8843 case S_IFIFO: t = "fifo"; break;
8844# endif
8845# ifdef S_IFSOCK
8846 case S_IFSOCK: t = "socket"; break;
8847# endif
8848 default: t = "other";
8849 }
8850# else
8851 if (mch_isdir(fname))
8852 t = "dir";
8853 else
8854 t = "file";
8855# endif
8856#endif
8857 type = vim_strsave((char_u *)t);
8858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008860}
8861
8862/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008863 * "getline(lnum)" function
8864 */
8865 static void
8866f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 typval_T *argvars;
8868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008869{
8870 linenr_T lnum;
8871 linenr_T end;
8872 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 list_T *l;
8874 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008875
8876 lnum = get_tv_lnum(argvars);
8877
8878 if (argvars[1].v_type == VAR_UNKNOWN)
8879 {
8880 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8881 p = ml_get(lnum);
8882 else
8883 p = (char_u *)"";
8884
8885 rettv->v_type = VAR_STRING;
8886 rettv->vval.v_string = vim_strsave(p);
8887 }
8888 else
8889 {
8890 end = get_tv_lnum(&argvars[1]);
8891 if (end < lnum)
8892 {
8893 EMSG(_(e_invrange));
8894 rettv->vval.v_number = 0;
8895 }
8896 else
8897 {
8898 l = list_alloc();
8899 if (l != NULL)
8900 {
8901 if (lnum < 1)
8902 lnum = 1;
8903 if (end > curbuf->b_ml.ml_line_count)
8904 end = curbuf->b_ml.ml_line_count;
8905 while (lnum <= end)
8906 {
8907 li = listitem_alloc();
8908 if (li == NULL)
8909 break;
8910 list_append(l, li);
8911 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008912 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008913 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8914 }
8915 rettv->vval.v_list = l;
8916 rettv->v_type = VAR_LIST;
8917 ++l->lv_refcount;
8918 }
8919 }
8920 }
8921}
8922
8923/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00008924 * "getqflist()" function
8925 */
8926/*ARGSUSED*/
8927 static void
8928f_getqflist(argvars, rettv)
8929 typval_T *argvars;
8930 typval_T *rettv;
8931{
8932#ifdef FEAT_QUICKFIX
8933 list_T *l;
8934#endif
8935
8936 rettv->vval.v_number = FALSE;
8937#ifdef FEAT_QUICKFIX
8938 l = list_alloc();
8939 if (l != NULL)
8940 {
8941 if (get_errorlist(l) != FAIL)
8942 {
8943 rettv->vval.v_list = l;
8944 rettv->v_type = VAR_LIST;
8945 ++l->lv_refcount;
8946 }
8947 else
8948 list_free(l);
8949 }
8950#endif
8951}
8952
8953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 * "getreg()" function
8955 */
8956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *argvars;
8959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
8961 char_u *strregname;
8962 int regname;
8963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 regname = (strregname == NULL ? '"' : *strregname);
8969 if (regname == 0)
8970 regname = '"';
8971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 rettv->v_type = VAR_STRING;
8973 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974}
8975
8976/*
8977 * "getregtype()" function
8978 */
8979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 char_u *strregname;
8985 int regname;
8986 char_u buf[NUMBUFLEN + 2];
8987 long reglen = 0;
8988
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 else
8992 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008993 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994
8995 regname = (strregname == NULL ? '"' : *strregname);
8996 if (regname == 0)
8997 regname = '"';
8998
8999 buf[0] = NUL;
9000 buf[1] = NUL;
9001 switch (get_reg_type(regname, &reglen))
9002 {
9003 case MLINE: buf[0] = 'V'; break;
9004 case MCHAR: buf[0] = 'v'; break;
9005#ifdef FEAT_VISUAL
9006 case MBLOCK:
9007 buf[0] = Ctrl_V;
9008 sprintf((char *)buf + 1, "%ld", reglen + 1);
9009 break;
9010#endif
9011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->v_type = VAR_STRING;
9013 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014}
9015
9016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 * "getwinposx()" function
9018 */
9019/*ARGSUSED*/
9020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009022 typval_T *argvars;
9023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026#ifdef FEAT_GUI
9027 if (gui.in_use)
9028 {
9029 int x, y;
9030
9031 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
9034#endif
9035}
9036
9037/*
9038 * "getwinposy()" function
9039 */
9040/*ARGSUSED*/
9041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009043 typval_T *argvars;
9044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047#ifdef FEAT_GUI
9048 if (gui.in_use)
9049 {
9050 int x, y;
9051
9052 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
9055#endif
9056}
9057
9058/*
9059 * "getwinvar()" function
9060 */
9061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 typval_T *argvars;
9064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 win_T *win, *oldcurwin;
9067 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
9070 ++emsg_off;
9071 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->v_type = VAR_STRING;
9075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076
9077 if (win != NULL && varname != NULL)
9078 {
9079 if (*varname == '&') /* window-local-option */
9080 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009081 /* Set curwin to be our win, temporarily. Also set curbuf, so
9082 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 oldcurwin = curwin;
9084 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009085 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088
9089 /* restore previous notion of curwin */
9090 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009091 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 }
9093 else
9094 {
9095 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009096 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 }
9100 }
9101
9102 --emsg_off;
9103}
9104
9105/*
9106 * "glob()" function
9107 */
9108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *argvars;
9111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 expand_T xpc;
9114
9115 ExpandInit(&xpc);
9116 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->v_type = VAR_STRING;
9118 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9120 ExpandCleanup(&xpc);
9121}
9122
9123/*
9124 * "globpath()" function
9125 */
9126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009128 typval_T *argvars;
9129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
9131 char_u buf1[NUMBUFLEN];
9132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->v_type = VAR_STRING;
9134 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9135 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136}
9137
9138/*
9139 * "has()" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 int i;
9147 char_u *name;
9148 int n = FALSE;
9149 static char *(has_list[]) =
9150 {
9151#ifdef AMIGA
9152 "amiga",
9153# ifdef FEAT_ARP
9154 "arp",
9155# endif
9156#endif
9157#ifdef __BEOS__
9158 "beos",
9159#endif
9160#ifdef MSDOS
9161# ifdef DJGPP
9162 "dos32",
9163# else
9164 "dos16",
9165# endif
9166#endif
9167#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9168 "mac",
9169#endif
9170#if defined(MACOS_X_UNIX)
9171 "macunix",
9172#endif
9173#ifdef OS2
9174 "os2",
9175#endif
9176#ifdef __QNX__
9177 "qnx",
9178#endif
9179#ifdef RISCOS
9180 "riscos",
9181#endif
9182#ifdef UNIX
9183 "unix",
9184#endif
9185#ifdef VMS
9186 "vms",
9187#endif
9188#ifdef WIN16
9189 "win16",
9190#endif
9191#ifdef WIN32
9192 "win32",
9193#endif
9194#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9195 "win32unix",
9196#endif
9197#ifdef WIN64
9198 "win64",
9199#endif
9200#ifdef EBCDIC
9201 "ebcdic",
9202#endif
9203#ifndef CASE_INSENSITIVE_FILENAME
9204 "fname_case",
9205#endif
9206#ifdef FEAT_ARABIC
9207 "arabic",
9208#endif
9209#ifdef FEAT_AUTOCMD
9210 "autocmd",
9211#endif
9212#ifdef FEAT_BEVAL
9213 "balloon_eval",
9214#endif
9215#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9216 "builtin_terms",
9217# ifdef ALL_BUILTIN_TCAPS
9218 "all_builtin_terms",
9219# endif
9220#endif
9221#ifdef FEAT_BYTEOFF
9222 "byte_offset",
9223#endif
9224#ifdef FEAT_CINDENT
9225 "cindent",
9226#endif
9227#ifdef FEAT_CLIENTSERVER
9228 "clientserver",
9229#endif
9230#ifdef FEAT_CLIPBOARD
9231 "clipboard",
9232#endif
9233#ifdef FEAT_CMDL_COMPL
9234 "cmdline_compl",
9235#endif
9236#ifdef FEAT_CMDHIST
9237 "cmdline_hist",
9238#endif
9239#ifdef FEAT_COMMENTS
9240 "comments",
9241#endif
9242#ifdef FEAT_CRYPT
9243 "cryptv",
9244#endif
9245#ifdef FEAT_CSCOPE
9246 "cscope",
9247#endif
9248#ifdef DEBUG
9249 "debug",
9250#endif
9251#ifdef FEAT_CON_DIALOG
9252 "dialog_con",
9253#endif
9254#ifdef FEAT_GUI_DIALOG
9255 "dialog_gui",
9256#endif
9257#ifdef FEAT_DIFF
9258 "diff",
9259#endif
9260#ifdef FEAT_DIGRAPHS
9261 "digraphs",
9262#endif
9263#ifdef FEAT_DND
9264 "dnd",
9265#endif
9266#ifdef FEAT_EMACS_TAGS
9267 "emacs_tags",
9268#endif
9269 "eval", /* always present, of course! */
9270#ifdef FEAT_EX_EXTRA
9271 "ex_extra",
9272#endif
9273#ifdef FEAT_SEARCH_EXTRA
9274 "extra_search",
9275#endif
9276#ifdef FEAT_FKMAP
9277 "farsi",
9278#endif
9279#ifdef FEAT_SEARCHPATH
9280 "file_in_path",
9281#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009282#if defined(UNIX) && !defined(USE_SYSTEM)
9283 "filterpipe",
9284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#ifdef FEAT_FIND_ID
9286 "find_in_path",
9287#endif
9288#ifdef FEAT_FOLDING
9289 "folding",
9290#endif
9291#ifdef FEAT_FOOTER
9292 "footer",
9293#endif
9294#if !defined(USE_SYSTEM) && defined(UNIX)
9295 "fork",
9296#endif
9297#ifdef FEAT_GETTEXT
9298 "gettext",
9299#endif
9300#ifdef FEAT_GUI
9301 "gui",
9302#endif
9303#ifdef FEAT_GUI_ATHENA
9304# ifdef FEAT_GUI_NEXTAW
9305 "gui_neXtaw",
9306# else
9307 "gui_athena",
9308# endif
9309#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009310#ifdef FEAT_GUI_KDE
9311 "gui_kde",
9312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313#ifdef FEAT_GUI_GTK
9314 "gui_gtk",
9315# ifdef HAVE_GTK2
9316 "gui_gtk2",
9317# endif
9318#endif
9319#ifdef FEAT_GUI_MAC
9320 "gui_mac",
9321#endif
9322#ifdef FEAT_GUI_MOTIF
9323 "gui_motif",
9324#endif
9325#ifdef FEAT_GUI_PHOTON
9326 "gui_photon",
9327#endif
9328#ifdef FEAT_GUI_W16
9329 "gui_win16",
9330#endif
9331#ifdef FEAT_GUI_W32
9332 "gui_win32",
9333#endif
9334#ifdef FEAT_HANGULIN
9335 "hangul_input",
9336#endif
9337#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9338 "iconv",
9339#endif
9340#ifdef FEAT_INS_EXPAND
9341 "insert_expand",
9342#endif
9343#ifdef FEAT_JUMPLIST
9344 "jumplist",
9345#endif
9346#ifdef FEAT_KEYMAP
9347 "keymap",
9348#endif
9349#ifdef FEAT_LANGMAP
9350 "langmap",
9351#endif
9352#ifdef FEAT_LIBCALL
9353 "libcall",
9354#endif
9355#ifdef FEAT_LINEBREAK
9356 "linebreak",
9357#endif
9358#ifdef FEAT_LISP
9359 "lispindent",
9360#endif
9361#ifdef FEAT_LISTCMDS
9362 "listcmds",
9363#endif
9364#ifdef FEAT_LOCALMAP
9365 "localmap",
9366#endif
9367#ifdef FEAT_MENU
9368 "menu",
9369#endif
9370#ifdef FEAT_SESSION
9371 "mksession",
9372#endif
9373#ifdef FEAT_MODIFY_FNAME
9374 "modify_fname",
9375#endif
9376#ifdef FEAT_MOUSE
9377 "mouse",
9378#endif
9379#ifdef FEAT_MOUSESHAPE
9380 "mouseshape",
9381#endif
9382#if defined(UNIX) || defined(VMS)
9383# ifdef FEAT_MOUSE_DEC
9384 "mouse_dec",
9385# endif
9386# ifdef FEAT_MOUSE_GPM
9387 "mouse_gpm",
9388# endif
9389# ifdef FEAT_MOUSE_JSB
9390 "mouse_jsbterm",
9391# endif
9392# ifdef FEAT_MOUSE_NET
9393 "mouse_netterm",
9394# endif
9395# ifdef FEAT_MOUSE_PTERM
9396 "mouse_pterm",
9397# endif
9398# ifdef FEAT_MOUSE_XTERM
9399 "mouse_xterm",
9400# endif
9401#endif
9402#ifdef FEAT_MBYTE
9403 "multi_byte",
9404#endif
9405#ifdef FEAT_MBYTE_IME
9406 "multi_byte_ime",
9407#endif
9408#ifdef FEAT_MULTI_LANG
9409 "multi_lang",
9410#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009411#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009412#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009413 "mzscheme",
9414#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#ifdef FEAT_OLE
9417 "ole",
9418#endif
9419#ifdef FEAT_OSFILETYPE
9420 "osfiletype",
9421#endif
9422#ifdef FEAT_PATH_EXTRA
9423 "path_extra",
9424#endif
9425#ifdef FEAT_PERL
9426#ifndef DYNAMIC_PERL
9427 "perl",
9428#endif
9429#endif
9430#ifdef FEAT_PYTHON
9431#ifndef DYNAMIC_PYTHON
9432 "python",
9433#endif
9434#endif
9435#ifdef FEAT_POSTSCRIPT
9436 "postscript",
9437#endif
9438#ifdef FEAT_PRINTER
9439 "printer",
9440#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009441#ifdef FEAT_PROFILE
9442 "profile",
9443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444#ifdef FEAT_QUICKFIX
9445 "quickfix",
9446#endif
9447#ifdef FEAT_RIGHTLEFT
9448 "rightleft",
9449#endif
9450#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9451 "ruby",
9452#endif
9453#ifdef FEAT_SCROLLBIND
9454 "scrollbind",
9455#endif
9456#ifdef FEAT_CMDL_INFO
9457 "showcmd",
9458 "cmdline_info",
9459#endif
9460#ifdef FEAT_SIGNS
9461 "signs",
9462#endif
9463#ifdef FEAT_SMARTINDENT
9464 "smartindent",
9465#endif
9466#ifdef FEAT_SNIFF
9467 "sniff",
9468#endif
9469#ifdef FEAT_STL_OPT
9470 "statusline",
9471#endif
9472#ifdef FEAT_SUN_WORKSHOP
9473 "sun_workshop",
9474#endif
9475#ifdef FEAT_NETBEANS_INTG
9476 "netbeans_intg",
9477#endif
9478#ifdef FEAT_SYN_HL
9479 "syntax",
9480#endif
9481#if defined(USE_SYSTEM) || !defined(UNIX)
9482 "system",
9483#endif
9484#ifdef FEAT_TAG_BINS
9485 "tag_binary",
9486#endif
9487#ifdef FEAT_TAG_OLDSTATIC
9488 "tag_old_static",
9489#endif
9490#ifdef FEAT_TAG_ANYWHITE
9491 "tag_any_white",
9492#endif
9493#ifdef FEAT_TCL
9494# ifndef DYNAMIC_TCL
9495 "tcl",
9496# endif
9497#endif
9498#ifdef TERMINFO
9499 "terminfo",
9500#endif
9501#ifdef FEAT_TERMRESPONSE
9502 "termresponse",
9503#endif
9504#ifdef FEAT_TEXTOBJ
9505 "textobjects",
9506#endif
9507#ifdef HAVE_TGETENT
9508 "tgetent",
9509#endif
9510#ifdef FEAT_TITLE
9511 "title",
9512#endif
9513#ifdef FEAT_TOOLBAR
9514 "toolbar",
9515#endif
9516#ifdef FEAT_USR_CMDS
9517 "user-commands", /* was accidentally included in 5.4 */
9518 "user_commands",
9519#endif
9520#ifdef FEAT_VIMINFO
9521 "viminfo",
9522#endif
9523#ifdef FEAT_VERTSPLIT
9524 "vertsplit",
9525#endif
9526#ifdef FEAT_VIRTUALEDIT
9527 "virtualedit",
9528#endif
9529#ifdef FEAT_VISUAL
9530 "visual",
9531#endif
9532#ifdef FEAT_VISUALEXTRA
9533 "visualextra",
9534#endif
9535#ifdef FEAT_VREPLACE
9536 "vreplace",
9537#endif
9538#ifdef FEAT_WILDIGN
9539 "wildignore",
9540#endif
9541#ifdef FEAT_WILDMENU
9542 "wildmenu",
9543#endif
9544#ifdef FEAT_WINDOWS
9545 "windows",
9546#endif
9547#ifdef FEAT_WAK
9548 "winaltkeys",
9549#endif
9550#ifdef FEAT_WRITEBACKUP
9551 "writebackup",
9552#endif
9553#ifdef FEAT_XIM
9554 "xim",
9555#endif
9556#ifdef FEAT_XFONTSET
9557 "xfontset",
9558#endif
9559#ifdef USE_XSMP
9560 "xsmp",
9561#endif
9562#ifdef USE_XSMP_INTERACT
9563 "xsmp_interact",
9564#endif
9565#ifdef FEAT_XCLIPBOARD
9566 "xterm_clipboard",
9567#endif
9568#ifdef FEAT_XTERM_SAVE
9569 "xterm_save",
9570#endif
9571#if defined(UNIX) && defined(FEAT_X11)
9572 "X11",
9573#endif
9574 NULL
9575 };
9576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 for (i = 0; has_list[i] != NULL; ++i)
9579 if (STRICMP(name, has_list[i]) == 0)
9580 {
9581 n = TRUE;
9582 break;
9583 }
9584
9585 if (n == FALSE)
9586 {
9587 if (STRNICMP(name, "patch", 5) == 0)
9588 n = has_patch(atoi((char *)name + 5));
9589 else if (STRICMP(name, "vim_starting") == 0)
9590 n = (starting != 0);
9591#ifdef DYNAMIC_TCL
9592 else if (STRICMP(name, "tcl") == 0)
9593 n = tcl_enabled(FALSE);
9594#endif
9595#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9596 else if (STRICMP(name, "iconv") == 0)
9597 n = iconv_enabled(FALSE);
9598#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009599#ifdef DYNAMIC_MZSCHEME
9600 else if (STRICMP(name, "mzscheme") == 0)
9601 n = mzscheme_enabled(FALSE);
9602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#ifdef DYNAMIC_RUBY
9604 else if (STRICMP(name, "ruby") == 0)
9605 n = ruby_enabled(FALSE);
9606#endif
9607#ifdef DYNAMIC_PYTHON
9608 else if (STRICMP(name, "python") == 0)
9609 n = python_enabled(FALSE);
9610#endif
9611#ifdef DYNAMIC_PERL
9612 else if (STRICMP(name, "perl") == 0)
9613 n = perl_enabled(FALSE);
9614#endif
9615#ifdef FEAT_GUI
9616 else if (STRICMP(name, "gui_running") == 0)
9617 n = (gui.in_use || gui.starting);
9618# ifdef FEAT_GUI_W32
9619 else if (STRICMP(name, "gui_win32s") == 0)
9620 n = gui_is_win32s();
9621# endif
9622# ifdef FEAT_BROWSE
9623 else if (STRICMP(name, "browse") == 0)
9624 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9625# endif
9626#endif
9627#ifdef FEAT_SYN_HL
9628 else if (STRICMP(name, "syntax_items") == 0)
9629 n = syntax_present(curbuf);
9630#endif
9631#if defined(WIN3264)
9632 else if (STRICMP(name, "win95") == 0)
9633 n = mch_windows95();
9634#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009635#ifdef FEAT_NETBEANS_INTG
9636 else if (STRICMP(name, "netbeans_enabled") == 0)
9637 n = usingNetbeans;
9638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 }
9640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 * "has_key()" function
9646 */
9647 static void
9648f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009649 typval_T *argvars;
9650 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651{
9652 rettv->vval.v_number = 0;
9653 if (argvars[0].v_type != VAR_DICT)
9654 {
9655 EMSG(_(e_dictreq));
9656 return;
9657 }
9658 if (argvars[0].vval.v_dict == NULL)
9659 return;
9660
9661 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009662 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663}
9664
9665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 * "hasmapto()" function
9667 */
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673 char_u *name;
9674 char_u *mode;
9675 char_u buf[NUMBUFLEN];
9676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009678 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 mode = (char_u *)"nvo";
9680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687}
9688
9689/*
9690 * "histadd()" function
9691 */
9692/*ARGSUSED*/
9693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009695 typval_T *argvars;
9696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
9698#ifdef FEAT_CMDHIST
9699 int histype;
9700 char_u *str;
9701 char_u buf[NUMBUFLEN];
9702#endif
9703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (check_restricted() || check_secure())
9706 return;
9707#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 if (histype >= 0)
9710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 if (*str != NUL)
9713 {
9714 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 return;
9717 }
9718 }
9719#endif
9720}
9721
9722/*
9723 * "histdel()" function
9724 */
9725/*ARGSUSED*/
9726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731#ifdef FEAT_CMDHIST
9732 int n;
9733 char_u buf[NUMBUFLEN];
9734
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009735 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009738 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9741 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 else
9743 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9745 get_tv_string_buf(&argvars[1], buf));
9746 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
9750}
9751
9752/*
9753 * "histget()" function
9754 */
9755/*ARGSUSED*/
9756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009758 typval_T *argvars;
9759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761#ifdef FEAT_CMDHIST
9762 int type;
9763 int idx;
9764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009766 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 idx = get_history_idx(type);
9768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 idx = (int)get_tv_number(&argvars[1]);
9770 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775}
9776
9777/*
9778 * "histnr()" function
9779 */
9780/*ARGSUSED*/
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786 int i;
9787
9788#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 if (i >= HIST_CMD && i < HIST_COUNT)
9791 i = get_history_idx(i);
9792 else
9793#endif
9794 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796}
9797
9798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 * "highlightID(name)" function
9800 */
9801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009803 typval_T *argvars;
9804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807}
9808
9809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009810 * "highlight_exists()" function
9811 */
9812 static void
9813f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009814 typval_T *argvars;
9815 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009816{
9817 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9818}
9819
9820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 * "hostname()" function
9822 */
9823/*ARGSUSED*/
9824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 typval_T *argvars;
9827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 char_u hostname[256];
9830
9831 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->v_type = VAR_STRING;
9833 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834}
9835
9836/*
9837 * iconv() function
9838 */
9839/*ARGSUSED*/
9840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 typval_T *argvars;
9843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
9845#ifdef FEAT_MBYTE
9846 char_u buf1[NUMBUFLEN];
9847 char_u buf2[NUMBUFLEN];
9848 char_u *from, *to, *str;
9849 vimconv_T vimconv;
9850#endif
9851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->v_type = VAR_STRING;
9853 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
9855#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 str = get_tv_string(&argvars[0]);
9857 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9858 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 vimconv.vc_type = CONV_NONE;
9860 convert_setup(&vimconv, from, to);
9861
9862 /* If the encodings are equal, no conversion needed. */
9863 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867
9868 convert_setup(&vimconv, NULL, NULL);
9869 vim_free(from);
9870 vim_free(to);
9871#endif
9872}
9873
9874/*
9875 * "indent()" function
9876 */
9877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009879 typval_T *argvars;
9880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 linenr_T lnum;
9883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889}
9890
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891/*
9892 * "index()" function
9893 */
9894 static void
9895f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009898{
Bram Moolenaar33570922005-01-25 22:26:29 +00009899 list_T *l;
9900 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901 long idx = 0;
9902 int ic = FALSE;
9903
9904 rettv->vval.v_number = -1;
9905 if (argvars[0].v_type != VAR_LIST)
9906 {
9907 EMSG(_(e_listreq));
9908 return;
9909 }
9910 l = argvars[0].vval.v_list;
9911 if (l != NULL)
9912 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009913 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009914 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009915 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009916 /* Start at specified item. Use the cached index that list_find()
9917 * sets, so that a negative number also works. */
9918 item = list_find(l, get_tv_number(&argvars[2]));
9919 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009920 if (argvars[3].v_type != VAR_UNKNOWN)
9921 ic = get_tv_number(&argvars[3]);
9922 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009923
Bram Moolenaar758711c2005-02-02 23:11:38 +00009924 for ( ; item != NULL; item = item->li_next, ++idx)
9925 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009926 {
9927 rettv->vval.v_number = idx;
9928 break;
9929 }
9930 }
9931}
9932
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933static int inputsecret_flag = 0;
9934
9935/*
9936 * "input()" function
9937 * Also handles inputsecret() when inputsecret is set.
9938 */
9939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009941 typval_T *argvars;
9942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 char_u *p = NULL;
9946 int c;
9947 char_u buf[NUMBUFLEN];
9948 int cmd_silent_save = cmd_silent;
9949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951
9952#ifdef NO_CONSOLE_INPUT
9953 /* While starting up, there is no place to enter text. */
9954 if (no_console_input())
9955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 return;
9958 }
9959#endif
9960
9961 cmd_silent = FALSE; /* Want to see the prompt. */
9962 if (prompt != NULL)
9963 {
9964 /* Only the part of the message after the last NL is considered as
9965 * prompt for the command line */
9966 p = vim_strrchr(prompt, '\n');
9967 if (p == NULL)
9968 p = prompt;
9969 else
9970 {
9971 ++p;
9972 c = *p;
9973 *p = NUL;
9974 msg_start();
9975 msg_clr_eos();
9976 msg_puts_attr(prompt, echo_attr);
9977 msg_didout = FALSE;
9978 msg_starthere();
9979 *p = c;
9980 }
9981 cmdline_row = msg_row;
9982 }
9983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009984 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9989
9990 /* since the user typed this, no need to wait for return */
9991 need_wait_return = FALSE;
9992 msg_didout = FALSE;
9993 cmd_silent = cmd_silent_save;
9994}
9995
9996/*
9997 * "inputdialog()" function
9998 */
9999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 typval_T *argvars;
10002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004#if defined(FEAT_GUI_TEXTDIALOG)
10005 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10006 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10007 {
10008 char_u *message;
10009 char_u buf[NUMBUFLEN];
10010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 IObuff[IOSIZE - 1] = NUL;
10016 }
10017 else
10018 IObuff[0] = NUL;
10019 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
10020 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
10023 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010024 if (argvars[1].v_type != VAR_UNKNOWN
10025 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->vval.v_string = vim_strsave(
10027 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 }
10033 else
10034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036}
10037
10038static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10039
10040/*
10041 * "inputrestore()" function
10042 */
10043/*ARGSUSED*/
10044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 typval_T *argvars;
10047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
10049 if (ga_userinput.ga_len > 0)
10050 {
10051 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10053 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056 else if (p_verbose > 1)
10057 {
10058 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061}
10062
10063/*
10064 * "inputsave()" function
10065 */
10066/*ARGSUSED*/
10067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 typval_T *argvars;
10070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071{
10072 /* Add an entry to the stack of typehead storage. */
10073 if (ga_grow(&ga_userinput, 1) == OK)
10074 {
10075 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10076 + ga_userinput.ga_len);
10077 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082}
10083
10084/*
10085 * "inputsecret()" function
10086 */
10087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091{
10092 ++cmdline_star;
10093 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 --cmdline_star;
10096 --inputsecret_flag;
10097}
10098
10099/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010100 * "insert()" function
10101 */
10102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010104 typval_T *argvars;
10105 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010106{
10107 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010108 listitem_T *item;
10109 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010110
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010111 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010112 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010113 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010114 else if ((l = argvars[0].vval.v_list) != NULL
10115 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010116 {
10117 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119
Bram Moolenaar758711c2005-02-02 23:11:38 +000010120 if (before == l->lv_len)
10121 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010122 else
10123 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010124 item = list_find(l, before);
10125 if (item == NULL)
10126 {
10127 EMSGN(_(e_listidx), before);
10128 l = NULL;
10129 }
10130 }
10131 if (l != NULL)
10132 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010133 list_insert_tv(l, &argvars[1], item);
10134 ++l->lv_refcount;
10135 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010136 }
10137 }
10138}
10139
10140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 * "isdirectory()" function
10142 */
10143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 typval_T *argvars;
10146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010151/*
10152 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10153 * it refers to a List or Dictionary that is locked.
10154 */
10155 static int
10156tv_islocked(tv)
10157 typval_T *tv;
10158{
10159 return (tv->v_lock & VAR_LOCKED)
10160 || (tv->v_type == VAR_LIST
10161 && tv->vval.v_list != NULL
10162 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10163 || (tv->v_type == VAR_DICT
10164 && tv->vval.v_dict != NULL
10165 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10166}
10167
10168/*
10169 * "islocked()" function
10170 */
10171 static void
10172f_islocked(argvars, rettv)
10173 typval_T *argvars;
10174 typval_T *rettv;
10175{
10176 lval_T lv;
10177 char_u *end;
10178 dictitem_T *di;
10179
10180 rettv->vval.v_number = -1;
10181 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
10182 if (end != NULL && lv.ll_name != NULL)
10183 {
10184 if (*end != NUL)
10185 EMSG(_(e_trailing));
10186 else
10187 {
10188 if (lv.ll_tv == NULL)
10189 {
10190 if (check_changedtick(lv.ll_name))
10191 rettv->vval.v_number = 1; /* always locked */
10192 else
10193 {
10194 di = find_var(lv.ll_name, NULL);
10195 if (di != NULL)
10196 {
10197 /* Consider a variable locked when:
10198 * 1. the variable itself is locked
10199 * 2. the value of the variable is locked.
10200 * 3. the List or Dict value is locked.
10201 */
10202 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10203 || tv_islocked(&di->di_tv));
10204 }
10205 }
10206 }
10207 else if (lv.ll_range)
10208 EMSG(_("E745: Range not allowed"));
10209 else if (lv.ll_newkey != NULL)
10210 EMSG2(_(e_dictkey), lv.ll_newkey);
10211 else if (lv.ll_list != NULL)
10212 /* List item. */
10213 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10214 else
10215 /* Dictionary item. */
10216 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10217 }
10218 }
10219
10220 clear_lval(&lv);
10221}
10222
Bram Moolenaar33570922005-01-25 22:26:29 +000010223static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010224
10225/*
10226 * Turn a dict into a list:
10227 * "what" == 0: list of keys
10228 * "what" == 1: list of values
10229 * "what" == 2: list of items
10230 */
10231 static void
10232dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 typval_T *argvars;
10234 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010235 int what;
10236{
Bram Moolenaar33570922005-01-25 22:26:29 +000010237 list_T *l;
10238 list_T *l2;
10239 dictitem_T *di;
10240 hashitem_T *hi;
10241 listitem_T *li;
10242 listitem_T *li2;
10243 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010245
10246 rettv->vval.v_number = 0;
10247 if (argvars[0].v_type != VAR_DICT)
10248 {
10249 EMSG(_(e_dictreq));
10250 return;
10251 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010252 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010253 return;
10254
10255 l = list_alloc();
10256 if (l == NULL)
10257 return;
10258 rettv->v_type = VAR_LIST;
10259 rettv->vval.v_list = l;
10260 ++l->lv_refcount;
10261
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 todo = d->dv_hashtab.ht_used;
10263 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010264 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010265 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010266 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010267 --todo;
10268 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010269
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010270 li = listitem_alloc();
10271 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010272 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010273 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010274
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010275 if (what == 0)
10276 {
10277 /* keys() */
10278 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010279 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010280 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10281 }
10282 else if (what == 1)
10283 {
10284 /* values() */
10285 copy_tv(&di->di_tv, &li->li_tv);
10286 }
10287 else
10288 {
10289 /* items() */
10290 l2 = list_alloc();
10291 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010292 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010293 li->li_tv.vval.v_list = l2;
10294 if (l2 == NULL)
10295 break;
10296 ++l2->lv_refcount;
10297
10298 li2 = listitem_alloc();
10299 if (li2 == NULL)
10300 break;
10301 list_append(l2, li2);
10302 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010303 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010304 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10305
10306 li2 = listitem_alloc();
10307 if (li2 == NULL)
10308 break;
10309 list_append(l2, li2);
10310 copy_tv(&di->di_tv, &li2->li_tv);
10311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010312 }
10313 }
10314}
10315
10316/*
10317 * "items(dict)" function
10318 */
10319 static void
10320f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010323{
10324 dict_list(argvars, rettv, 2);
10325}
10326
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010328 * "join()" function
10329 */
10330 static void
10331f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010334{
10335 garray_T ga;
10336 char_u *sep;
10337
10338 rettv->vval.v_number = 0;
10339 if (argvars[0].v_type != VAR_LIST)
10340 {
10341 EMSG(_(e_listreq));
10342 return;
10343 }
10344 if (argvars[0].vval.v_list == NULL)
10345 return;
10346 if (argvars[1].v_type == VAR_UNKNOWN)
10347 sep = (char_u *)" ";
10348 else
10349 sep = get_tv_string(&argvars[1]);
10350
10351 ga_init2(&ga, (int)sizeof(char), 80);
10352 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10353 ga_append(&ga, NUL);
10354
10355 rettv->v_type = VAR_STRING;
10356 rettv->vval.v_string = (char_u *)ga.ga_data;
10357}
10358
10359/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010360 * "keys()" function
10361 */
10362 static void
10363f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010364 typval_T *argvars;
10365 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010366{
10367 dict_list(argvars, rettv, 0);
10368}
10369
10370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 * "last_buffer_nr()" function.
10372 */
10373/*ARGSUSED*/
10374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010375f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010376 typval_T *argvars;
10377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 int n = 0;
10380 buf_T *buf;
10381
10382 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10383 if (n < buf->b_fnum)
10384 n = buf->b_fnum;
10385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387}
10388
10389/*
10390 * "len()" function
10391 */
10392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010394 typval_T *argvars;
10395 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396{
10397 switch (argvars[0].v_type)
10398 {
10399 case VAR_STRING:
10400 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 rettv->vval.v_number = (varnumber_T)STRLEN(
10402 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010403 break;
10404 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010406 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010407 case VAR_DICT:
10408 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10409 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010410 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010411 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010412 break;
10413 }
10414}
10415
Bram Moolenaar33570922005-01-25 22:26:29 +000010416static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010417
10418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010420 typval_T *argvars;
10421 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010422 int type;
10423{
10424#ifdef FEAT_LIBCALL
10425 char_u *string_in;
10426 char_u **string_result;
10427 int nr_result;
10428#endif
10429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010431 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010435
10436 if (check_restricted() || check_secure())
10437 return;
10438
10439#ifdef FEAT_LIBCALL
10440 /* The first two args must be strings, otherwise its meaningless */
10441 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10442 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010443 string_in = NULL;
10444 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010445 string_in = argvars[2].vval.v_string;
10446 if (type == VAR_NUMBER)
10447 string_result = NULL;
10448 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010450 if (mch_libcall(argvars[0].vval.v_string,
10451 argvars[1].vval.v_string,
10452 string_in,
10453 argvars[2].vval.v_number,
10454 string_result,
10455 &nr_result) == OK
10456 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010458 }
10459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460}
10461
10462/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010463 * "libcall()" function
10464 */
10465 static void
10466f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010467 typval_T *argvars;
10468 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010469{
10470 libcall_common(argvars, rettv, VAR_STRING);
10471}
10472
10473/*
10474 * "libcallnr()" function
10475 */
10476 static void
10477f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *argvars;
10479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010480{
10481 libcall_common(argvars, rettv, VAR_NUMBER);
10482}
10483
10484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 * "line(string)" function
10486 */
10487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010488f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 linenr_T lnum = 0;
10493 pos_T *fp;
10494
10495 fp = var2fpos(&argvars[0], TRUE);
10496 if (fp != NULL)
10497 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501/*
10502 * "line2byte(lnum)" function
10503 */
10504/*ARGSUSED*/
10505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512#else
10513 linenr_T lnum;
10514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10520 if (rettv->vval.v_number >= 0)
10521 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522#endif
10523}
10524
10525/*
10526 * "lispindent(lnum)" function
10527 */
10528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533#ifdef FEAT_LISP
10534 pos_T pos;
10535 linenr_T lnum;
10536
10537 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10540 {
10541 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 curwin->w_cursor = pos;
10544 }
10545 else
10546#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548}
10549
10550/*
10551 * "localtime()" function
10552 */
10553/*ARGSUSED*/
10554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 typval_T *argvars;
10557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560}
10561
Bram Moolenaar33570922005-01-25 22:26:29 +000010562static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 int exact;
10569{
10570 char_u *keys;
10571 char_u *which;
10572 char_u buf[NUMBUFLEN];
10573 char_u *keys_buf = NULL;
10574 char_u *rhs;
10575 int mode;
10576 garray_T ga;
10577
10578 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 rettv->v_type = VAR_STRING;
10580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (*keys == NUL)
10584 return;
10585
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010586 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 else
10589 which = (char_u *)"";
10590 mode = get_map_mode(&which, 0);
10591
10592 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10593 rhs = check_map(keys, mode, exact);
10594 vim_free(keys_buf);
10595 if (rhs != NULL)
10596 {
10597 ga_init(&ga);
10598 ga.ga_itemsize = 1;
10599 ga.ga_growsize = 40;
10600
10601 while (*rhs != NUL)
10602 ga_concat(&ga, str2special(&rhs, FALSE));
10603
10604 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 }
10607}
10608
10609/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010610 * "map()" function
10611 */
10612 static void
10613f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010614 typval_T *argvars;
10615 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010616{
10617 filter_map(argvars, rettv, TRUE);
10618}
10619
10620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010621 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 */
10623 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010624f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 typval_T *argvars;
10626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010628 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629}
10630
10631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 */
10634 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010636 typval_T *argvars;
10637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010639 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640}
10641
Bram Moolenaar33570922005-01-25 22:26:29 +000010642static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643
10644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010646 typval_T *argvars;
10647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 int type;
10649{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010650 char_u *str = NULL;
10651 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 char_u *pat;
10653 regmatch_T regmatch;
10654 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010655 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 char_u *save_cpo;
10657 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010658 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010659 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 list_T *l = NULL;
10661 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010662 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010663 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664
10665 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10666 save_cpo = p_cpo;
10667 p_cpo = (char_u *)"";
10668
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010669 rettv->vval.v_number = -1;
10670 if (type == 3)
10671 {
10672 /* return empty list when there are no matches */
10673 if ((rettv->vval.v_list = list_alloc()) == NULL)
10674 goto theend;
10675 rettv->v_type = VAR_LIST;
10676 ++rettv->vval.v_list->lv_refcount;
10677 }
10678 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680 rettv->v_type = VAR_STRING;
10681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684 if (argvars[0].v_type == VAR_LIST)
10685 {
10686 if ((l = argvars[0].vval.v_list) == NULL)
10687 goto theend;
10688 li = l->lv_first;
10689 }
10690 else
10691 expr = str = get_tv_string(&argvars[0]);
10692
10693 pat = get_tv_string_buf(&argvars[1], patbuf);
10694
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010695 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010698 if (l != NULL)
10699 {
10700 li = list_find(l, start);
10701 if (li == NULL)
10702 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010703 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010704 }
10705 else
10706 {
10707 if (start < 0)
10708 start = 0;
10709 if (start > (long)STRLEN(str))
10710 goto theend;
10711 str += start;
10712 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010714 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 }
10717
10718 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10719 if (regmatch.regprog != NULL)
10720 {
10721 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010722
10723 while (1)
10724 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010725 if (l != NULL)
10726 {
10727 if (li == NULL)
10728 {
10729 match = FALSE;
10730 break;
10731 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010732 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010733 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010734 if (str == NULL)
10735 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010736 }
10737
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010738 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010739
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010740 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010741 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010742 if (l == NULL && !match)
10743 break;
10744
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010745 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010746 if (l != NULL)
10747 {
10748 li = li->li_next;
10749 ++idx;
10750 }
10751 else
10752 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010753#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010754 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010755#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010756 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010757#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010758 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010759 }
10760
10761 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010763 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010764 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010765 int i;
10766
10767 /* return list with matched string and submatches */
10768 for (i = 0; i < NSUBEXP; ++i)
10769 {
10770 if (regmatch.endp[i] == NULL)
10771 break;
10772 li = listitem_alloc();
10773 if (li == NULL)
10774 break;
10775 li->li_tv.v_type = VAR_STRING;
10776 li->li_tv.v_lock = 0;
10777 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10778 (int)(regmatch.endp[i] - regmatch.startp[i]));
10779 list_append(rettv->vval.v_list, li);
10780 }
10781 }
10782 else if (type == 2)
10783 {
10784 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010785 if (l != NULL)
10786 copy_tv(&li->li_tv, rettv);
10787 else
10788 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010790 }
10791 else if (l != NULL)
10792 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 else
10794 {
10795 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 (varnumber_T)(regmatch.startp[0] - str);
10798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 }
10803 }
10804 vim_free(regmatch.regprog);
10805 }
10806
10807theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010808 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 p_cpo = save_cpo;
10810}
10811
10812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813 * "match()" function
10814 */
10815 static void
10816f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 typval_T *argvars;
10818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010819{
10820 find_some_match(argvars, rettv, 1);
10821}
10822
10823/*
10824 * "matchend()" function
10825 */
10826 static void
10827f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 typval_T *argvars;
10829 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830{
10831 find_some_match(argvars, rettv, 0);
10832}
10833
10834/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010835 * "matchlist()" function
10836 */
10837 static void
10838f_matchlist(argvars, rettv)
10839 typval_T *argvars;
10840 typval_T *rettv;
10841{
10842 find_some_match(argvars, rettv, 3);
10843}
10844
10845/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010846 * "matchstr()" function
10847 */
10848 static void
10849f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010850 typval_T *argvars;
10851 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010852{
10853 find_some_match(argvars, rettv, 2);
10854}
10855
Bram Moolenaar33570922005-01-25 22:26:29 +000010856static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010857
10858 static void
10859max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010860 typval_T *argvars;
10861 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010862 int domax;
10863{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010864 long n = 0;
10865 long i;
10866
10867 if (argvars[0].v_type == VAR_LIST)
10868 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010869 list_T *l;
10870 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010872 l = argvars[0].vval.v_list;
10873 if (l != NULL)
10874 {
10875 li = l->lv_first;
10876 if (li != NULL)
10877 {
10878 n = get_tv_number(&li->li_tv);
10879 while (1)
10880 {
10881 li = li->li_next;
10882 if (li == NULL)
10883 break;
10884 i = get_tv_number(&li->li_tv);
10885 if (domax ? i > n : i < n)
10886 n = i;
10887 }
10888 }
10889 }
10890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 else if (argvars[0].v_type == VAR_DICT)
10892 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010894 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010895 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010896 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897
10898 d = argvars[0].vval.v_dict;
10899 if (d != NULL)
10900 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010901 todo = d->dv_hashtab.ht_used;
10902 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010904 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010906 --todo;
10907 i = get_tv_number(&HI2DI(hi)->di_tv);
10908 if (first)
10909 {
10910 n = i;
10911 first = FALSE;
10912 }
10913 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914 n = i;
10915 }
10916 }
10917 }
10918 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010919 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010920 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010921 rettv->vval.v_number = n;
10922}
10923
10924/*
10925 * "max()" function
10926 */
10927 static void
10928f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010929 typval_T *argvars;
10930 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010931{
10932 max_min(argvars, rettv, TRUE);
10933}
10934
10935/*
10936 * "min()" function
10937 */
10938 static void
10939f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010940 typval_T *argvars;
10941 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010942{
10943 max_min(argvars, rettv, FALSE);
10944}
10945
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010946static int mkdir_recurse __ARGS((char_u *dir, int prot));
10947
10948/*
10949 * Create the directory in which "dir" is located, and higher levels when
10950 * needed.
10951 */
10952 static int
10953mkdir_recurse(dir, prot)
10954 char_u *dir;
10955 int prot;
10956{
10957 char_u *p;
10958 char_u *updir;
10959 int r = FAIL;
10960
10961 /* Get end of directory name in "dir".
10962 * We're done when it's "/" or "c:/". */
10963 p = gettail_sep(dir);
10964 if (p <= get_past_head(dir))
10965 return OK;
10966
10967 /* If the directory exists we're done. Otherwise: create it.*/
10968 updir = vim_strnsave(dir, (int)(p - dir));
10969 if (updir == NULL)
10970 return FAIL;
10971 if (mch_isdir(updir))
10972 r = OK;
10973 else if (mkdir_recurse(updir, prot) == OK)
10974 r = vim_mkdir_emsg(updir, prot);
10975 vim_free(updir);
10976 return r;
10977}
10978
10979#ifdef vim_mkdir
10980/*
10981 * "mkdir()" function
10982 */
10983 static void
10984f_mkdir(argvars, rettv)
10985 typval_T *argvars;
10986 typval_T *rettv;
10987{
10988 char_u *dir;
10989 char_u buf[NUMBUFLEN];
10990 int prot = 0755;
10991
10992 rettv->vval.v_number = FAIL;
10993 if (check_restricted() || check_secure())
10994 return;
10995
10996 dir = get_tv_string_buf(&argvars[0], buf);
10997 if (argvars[1].v_type != VAR_UNKNOWN)
10998 {
10999 if (argvars[2].v_type != VAR_UNKNOWN)
11000 prot = get_tv_number(&argvars[2]);
11001 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
11002 mkdir_recurse(dir, prot);
11003 }
11004 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
11005}
11006#endif
11007
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 * "mode()" function
11010 */
11011/*ARGSUSED*/
11012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013f_mode(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 char_u buf[2];
11018
11019#ifdef FEAT_VISUAL
11020 if (VIsual_active)
11021 {
11022 if (VIsual_select)
11023 buf[0] = VIsual_mode + 's' - 'v';
11024 else
11025 buf[0] = VIsual_mode;
11026 }
11027 else
11028#endif
11029 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11030 buf[0] = 'r';
11031 else if (State & INSERT)
11032 {
11033 if (State & REPLACE_FLAG)
11034 buf[0] = 'R';
11035 else
11036 buf[0] = 'i';
11037 }
11038 else if (State & CMDLINE)
11039 buf[0] = 'c';
11040 else
11041 buf[0] = 'n';
11042
11043 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_string = vim_strsave(buf);
11045 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046}
11047
11048/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011049 * "nextnonblank()" function
11050 */
11051 static void
11052f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011055{
11056 linenr_T lnum;
11057
11058 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11059 {
11060 if (lnum > curbuf->b_ml.ml_line_count)
11061 {
11062 lnum = 0;
11063 break;
11064 }
11065 if (*skipwhite(ml_get(lnum)) != NUL)
11066 break;
11067 }
11068 rettv->vval.v_number = lnum;
11069}
11070
11071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 * "nr2char()" function
11073 */
11074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *argvars;
11077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u buf[NUMBUFLEN];
11080
11081#ifdef FEAT_MBYTE
11082 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 else
11085#endif
11086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 buf[1] = NUL;
11089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->v_type = VAR_STRING;
11091 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011092}
11093
11094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095 * "prevnonblank()" function
11096 */
11097 static void
11098f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011101{
11102 linenr_T lnum;
11103
11104 lnum = get_tv_lnum(argvars);
11105 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11106 lnum = 0;
11107 else
11108 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11109 --lnum;
11110 rettv->vval.v_number = lnum;
11111}
11112
Bram Moolenaar8c711452005-01-14 21:53:12 +000011113/*
11114 * "range()" function
11115 */
11116 static void
11117f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 typval_T *argvars;
11119 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011120{
11121 long start;
11122 long end;
11123 long stride = 1;
11124 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011125 list_T *l;
11126 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011127
11128 start = get_tv_number(&argvars[0]);
11129 if (argvars[1].v_type == VAR_UNKNOWN)
11130 {
11131 end = start - 1;
11132 start = 0;
11133 }
11134 else
11135 {
11136 end = get_tv_number(&argvars[1]);
11137 if (argvars[2].v_type != VAR_UNKNOWN)
11138 stride = get_tv_number(&argvars[2]);
11139 }
11140
11141 rettv->vval.v_number = 0;
11142 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011143 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011144 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011145 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011146 else
11147 {
11148 l = list_alloc();
11149 if (l != NULL)
11150 {
11151 rettv->v_type = VAR_LIST;
11152 rettv->vval.v_list = l;
11153 ++l->lv_refcount;
11154
11155 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11156 {
11157 li = listitem_alloc();
11158 if (li == NULL)
11159 break;
11160 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011161 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011162 li->li_tv.vval.v_number = i;
11163 list_append(l, li);
11164 }
11165 }
11166 }
11167}
11168
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011169/*
11170 * "readfile()" function
11171 */
11172 static void
11173f_readfile(argvars, rettv)
11174 typval_T *argvars;
11175 typval_T *rettv;
11176{
11177 int binary = FALSE;
11178 char_u *fname;
11179 FILE *fd;
11180 list_T *l;
11181 listitem_T *li;
11182#define FREAD_SIZE 200 /* optimized for text lines */
11183 char_u buf[FREAD_SIZE];
11184 int readlen; /* size of last fread() */
11185 int buflen; /* nr of valid chars in buf[] */
11186 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11187 int tolist; /* first byte in buf[] still to be put in list */
11188 int chop; /* how many CR to chop off */
11189 char_u *prev = NULL; /* previously read bytes, if any */
11190 int prevlen = 0; /* length of "prev" if not NULL */
11191 char_u *s;
11192 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011193 long maxline = MAXLNUM;
11194 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011195
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011196 if (argvars[1].v_type != VAR_UNKNOWN)
11197 {
11198 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11199 binary = TRUE;
11200 if (argvars[2].v_type != VAR_UNKNOWN)
11201 maxline = get_tv_number(&argvars[2]);
11202 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011203
11204 l = list_alloc();
11205 if (l == NULL)
11206 return;
11207 rettv->v_type = VAR_LIST;
11208 rettv->vval.v_list = l;
11209 l->lv_refcount = 1;
11210
11211 /* Always open the file in binary mode, library functions have a mind of
11212 * their own about CR-LF conversion. */
11213 fname = get_tv_string(&argvars[0]);
11214 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11215 {
11216 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11217 return;
11218 }
11219
11220 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011221 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011222 {
11223 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11224 buflen = filtd + readlen;
11225 tolist = 0;
11226 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11227 {
11228 if (buf[filtd] == '\n' || readlen <= 0)
11229 {
11230 /* Only when in binary mode add an empty list item when the
11231 * last line ends in a '\n'. */
11232 if (!binary && readlen == 0 && filtd == 0)
11233 break;
11234
11235 /* Found end-of-line or end-of-file: add a text line to the
11236 * list. */
11237 chop = 0;
11238 if (!binary)
11239 while (filtd - chop - 1 >= tolist
11240 && buf[filtd - chop - 1] == '\r')
11241 ++chop;
11242 len = filtd - tolist - chop;
11243 if (prev == NULL)
11244 s = vim_strnsave(buf + tolist, len);
11245 else
11246 {
11247 s = alloc((unsigned)(prevlen + len + 1));
11248 if (s != NULL)
11249 {
11250 mch_memmove(s, prev, prevlen);
11251 vim_free(prev);
11252 prev = NULL;
11253 mch_memmove(s + prevlen, buf + tolist, len);
11254 s[prevlen + len] = NUL;
11255 }
11256 }
11257 tolist = filtd + 1;
11258
11259 li = listitem_alloc();
11260 if (li == NULL)
11261 {
11262 vim_free(s);
11263 break;
11264 }
11265 li->li_tv.v_type = VAR_STRING;
11266 li->li_tv.v_lock = 0;
11267 li->li_tv.vval.v_string = s;
11268 list_append(l, li);
11269
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011270 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011271 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011272 if (readlen <= 0)
11273 break;
11274 }
11275 else if (buf[filtd] == NUL)
11276 buf[filtd] = '\n';
11277 }
11278 if (readlen <= 0)
11279 break;
11280
11281 if (tolist == 0)
11282 {
11283 /* "buf" is full, need to move text to an allocated buffer */
11284 if (prev == NULL)
11285 {
11286 prev = vim_strnsave(buf, buflen);
11287 prevlen = buflen;
11288 }
11289 else
11290 {
11291 s = alloc((unsigned)(prevlen + buflen));
11292 if (s != NULL)
11293 {
11294 mch_memmove(s, prev, prevlen);
11295 mch_memmove(s + prevlen, buf, buflen);
11296 vim_free(prev);
11297 prev = s;
11298 prevlen += buflen;
11299 }
11300 }
11301 filtd = 0;
11302 }
11303 else
11304 {
11305 mch_memmove(buf, buf + tolist, buflen - tolist);
11306 filtd -= tolist;
11307 }
11308 }
11309
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011310 /*
11311 * For a negative line count use only the lines at the end of the file,
11312 * free the rest.
11313 */
11314 if (maxline < 0)
11315 while (cnt > -maxline)
11316 {
11317 listitem_remove(l, l->lv_first);
11318 --cnt;
11319 }
11320
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011321 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011322 fclose(fd);
11323}
11324
11325
Bram Moolenaar0d660222005-01-07 21:51:51 +000011326#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11327static void make_connection __ARGS((void));
11328static int check_connection __ARGS((void));
11329
11330 static void
11331make_connection()
11332{
11333 if (X_DISPLAY == NULL
11334# ifdef FEAT_GUI
11335 && !gui.in_use
11336# endif
11337 )
11338 {
11339 x_force_connect = TRUE;
11340 setup_term_clip();
11341 x_force_connect = FALSE;
11342 }
11343}
11344
11345 static int
11346check_connection()
11347{
11348 make_connection();
11349 if (X_DISPLAY == NULL)
11350 {
11351 EMSG(_("E240: No connection to Vim server"));
11352 return FAIL;
11353 }
11354 return OK;
11355}
11356#endif
11357
11358#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011359static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360
11361 static void
11362remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011365 int expr;
11366{
11367 char_u *server_name;
11368 char_u *keys;
11369 char_u *r = NULL;
11370 char_u buf[NUMBUFLEN];
11371# ifdef WIN32
11372 HWND w;
11373# else
11374 Window w;
11375# endif
11376
11377 if (check_restricted() || check_secure())
11378 return;
11379
11380# ifdef FEAT_X11
11381 if (check_connection() == FAIL)
11382 return;
11383# endif
11384
11385 server_name = get_tv_string(&argvars[0]);
11386 keys = get_tv_string_buf(&argvars[1], buf);
11387# ifdef WIN32
11388 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11389# else
11390 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11391 < 0)
11392# endif
11393 {
11394 if (r != NULL)
11395 EMSG(r); /* sending worked but evaluation failed */
11396 else
11397 EMSG2(_("E241: Unable to send to %s"), server_name);
11398 return;
11399 }
11400
11401 rettv->vval.v_string = r;
11402
11403 if (argvars[2].v_type != VAR_UNKNOWN)
11404 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011405 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011406 char_u str[30];
11407
11408 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 v.di_tv.v_type = VAR_STRING;
11410 v.di_tv.vval.v_string = vim_strsave(str);
11411 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11412 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011413 }
11414}
11415#endif
11416
11417/*
11418 * "remote_expr()" function
11419 */
11420/*ARGSUSED*/
11421 static void
11422f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011425{
11426 rettv->v_type = VAR_STRING;
11427 rettv->vval.v_string = NULL;
11428#ifdef FEAT_CLIENTSERVER
11429 remote_common(argvars, rettv, TRUE);
11430#endif
11431}
11432
11433/*
11434 * "remote_foreground()" function
11435 */
11436/*ARGSUSED*/
11437 static void
11438f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011439 typval_T *argvars;
11440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011441{
11442 rettv->vval.v_number = 0;
11443#ifdef FEAT_CLIENTSERVER
11444# ifdef WIN32
11445 /* On Win32 it's done in this application. */
11446 serverForeground(get_tv_string(&argvars[0]));
11447# else
11448 /* Send a foreground() expression to the server. */
11449 argvars[1].v_type = VAR_STRING;
11450 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11451 argvars[2].v_type = VAR_UNKNOWN;
11452 remote_common(argvars, rettv, TRUE);
11453 vim_free(argvars[1].vval.v_string);
11454# endif
11455#endif
11456}
11457
11458/*ARGSUSED*/
11459 static void
11460f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T *argvars;
11462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011463{
11464#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466 char_u *s = NULL;
11467# ifdef WIN32
11468 int n = 0;
11469# endif
11470
11471 if (check_restricted() || check_secure())
11472 {
11473 rettv->vval.v_number = -1;
11474 return;
11475 }
11476# ifdef WIN32
11477 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11478 if (n == 0)
11479 rettv->vval.v_number = -1;
11480 else
11481 {
11482 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11483 rettv->vval.v_number = (s != NULL);
11484 }
11485# else
11486 rettv->vval.v_number = 0;
11487 if (check_connection() == FAIL)
11488 return;
11489
11490 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11491 serverStrToWin(get_tv_string(&argvars[0])), &s);
11492# endif
11493
11494 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11495 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 v.di_tv.v_type = VAR_STRING;
11497 v.di_tv.vval.v_string = vim_strsave(s);
11498 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11499 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500 }
11501#else
11502 rettv->vval.v_number = -1;
11503#endif
11504}
11505
11506/*ARGSUSED*/
11507 static void
11508f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 typval_T *argvars;
11510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511{
11512 char_u *r = NULL;
11513
11514#ifdef FEAT_CLIENTSERVER
11515 if (!check_restricted() && !check_secure())
11516 {
11517# ifdef WIN32
11518 /* The server's HWND is encoded in the 'id' parameter */
11519 int n = 0;
11520
11521 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11522 if (n != 0)
11523 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11524 if (r == NULL)
11525# else
11526 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11527 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11528# endif
11529 EMSG(_("E277: Unable to read a server reply"));
11530 }
11531#endif
11532 rettv->v_type = VAR_STRING;
11533 rettv->vval.v_string = r;
11534}
11535
11536/*
11537 * "remote_send()" function
11538 */
11539/*ARGSUSED*/
11540 static void
11541f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544{
11545 rettv->v_type = VAR_STRING;
11546 rettv->vval.v_string = NULL;
11547#ifdef FEAT_CLIENTSERVER
11548 remote_common(argvars, rettv, FALSE);
11549#endif
11550}
11551
11552/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011553 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011554 */
11555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011559{
Bram Moolenaar33570922005-01-25 22:26:29 +000011560 list_T *l;
11561 listitem_T *item, *item2;
11562 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011563 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011565 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 dict_T *d;
11567 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011568
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011569 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011570 if (argvars[0].v_type == VAR_DICT)
11571 {
11572 if (argvars[2].v_type != VAR_UNKNOWN)
11573 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011574 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011575 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011576 {
11577 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011578 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011579 if (di == NULL)
11580 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011581 else
11582 {
11583 *rettv = di->di_tv;
11584 init_tv(&di->di_tv);
11585 dictitem_remove(d, di);
11586 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011587 }
11588 }
11589 else if (argvars[0].v_type != VAR_LIST)
11590 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011591 else if ((l = argvars[0].vval.v_list) != NULL
11592 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011595 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011596 if (item == NULL)
11597 EMSGN(_(e_listidx), idx);
11598 else
11599 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011600 if (argvars[2].v_type == VAR_UNKNOWN)
11601 {
11602 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011603 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011604 *rettv = item->li_tv;
11605 vim_free(item);
11606 }
11607 else
11608 {
11609 /* Remove range of items, return list with values. */
11610 end = get_tv_number(&argvars[2]);
11611 item2 = list_find(l, end);
11612 if (item2 == NULL)
11613 EMSGN(_(e_listidx), end);
11614 else
11615 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011616 int cnt = 0;
11617
11618 for (li = item; li != NULL; li = li->li_next)
11619 {
11620 ++cnt;
11621 if (li == item2)
11622 break;
11623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011624 if (li == NULL) /* didn't find "item2" after "item" */
11625 EMSG(_(e_invrange));
11626 else
11627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011628 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011629 l = list_alloc();
11630 if (l != NULL)
11631 {
11632 rettv->v_type = VAR_LIST;
11633 rettv->vval.v_list = l;
11634 l->lv_first = item;
11635 l->lv_last = item2;
11636 l->lv_refcount = 1;
11637 item->li_prev = NULL;
11638 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011639 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011640 }
11641 }
11642 }
11643 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011644 }
11645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646}
11647
11648/*
11649 * "rename({from}, {to})" function
11650 */
11651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 typval_T *argvars;
11654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655{
11656 char_u buf[NUMBUFLEN];
11657
11658 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11662 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663}
11664
11665/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011666 * "repeat()" function
11667 */
11668/*ARGSUSED*/
11669 static void
11670f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 typval_T *argvars;
11672 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011673{
11674 char_u *p;
11675 int n;
11676 int slen;
11677 int len;
11678 char_u *r;
11679 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011681
11682 n = get_tv_number(&argvars[1]);
11683 if (argvars[0].v_type == VAR_LIST)
11684 {
11685 l = list_alloc();
11686 if (l != NULL && argvars[0].vval.v_list != NULL)
11687 {
11688 l->lv_refcount = 1;
11689 while (n-- > 0)
11690 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11691 break;
11692 }
11693 rettv->v_type = VAR_LIST;
11694 rettv->vval.v_list = l;
11695 }
11696 else
11697 {
11698 p = get_tv_string(&argvars[0]);
11699 rettv->v_type = VAR_STRING;
11700 rettv->vval.v_string = NULL;
11701
11702 slen = (int)STRLEN(p);
11703 len = slen * n;
11704 if (len <= 0)
11705 return;
11706
11707 r = alloc(len + 1);
11708 if (r != NULL)
11709 {
11710 for (i = 0; i < n; i++)
11711 mch_memmove(r + i * slen, p, (size_t)slen);
11712 r[len] = NUL;
11713 }
11714
11715 rettv->vval.v_string = r;
11716 }
11717}
11718
11719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 * "resolve()" function
11721 */
11722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011724 typval_T *argvars;
11725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726{
11727 char_u *p;
11728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730#ifdef FEAT_SHORTCUT
11731 {
11732 char_u *v = NULL;
11733
11734 v = mch_resolve_shortcut(p);
11735 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 }
11740#else
11741# ifdef HAVE_READLINK
11742 {
11743 char_u buf[MAXPATHL + 1];
11744 char_u *cpy;
11745 int len;
11746 char_u *remain = NULL;
11747 char_u *q;
11748 int is_relative_to_current = FALSE;
11749 int has_trailing_pathsep = FALSE;
11750 int limit = 100;
11751
11752 p = vim_strsave(p);
11753
11754 if (p[0] == '.' && (vim_ispathsep(p[1])
11755 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11756 is_relative_to_current = TRUE;
11757
11758 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011759 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 has_trailing_pathsep = TRUE;
11761
11762 q = getnextcomp(p);
11763 if (*q != NUL)
11764 {
11765 /* Separate the first path component in "p", and keep the
11766 * remainder (beginning with the path separator). */
11767 remain = vim_strsave(q - 1);
11768 q[-1] = NUL;
11769 }
11770
11771 for (;;)
11772 {
11773 for (;;)
11774 {
11775 len = readlink((char *)p, (char *)buf, MAXPATHL);
11776 if (len <= 0)
11777 break;
11778 buf[len] = NUL;
11779
11780 if (limit-- == 0)
11781 {
11782 vim_free(p);
11783 vim_free(remain);
11784 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 goto fail;
11787 }
11788
11789 /* Ensure that the result will have a trailing path separator
11790 * if the argument has one. */
11791 if (remain == NULL && has_trailing_pathsep)
11792 add_pathsep(buf);
11793
11794 /* Separate the first path component in the link value and
11795 * concatenate the remainders. */
11796 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11797 if (*q != NUL)
11798 {
11799 if (remain == NULL)
11800 remain = vim_strsave(q - 1);
11801 else
11802 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011803 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 if (cpy != NULL)
11805 {
11806 STRCAT(cpy, remain);
11807 vim_free(remain);
11808 remain = cpy;
11809 }
11810 }
11811 q[-1] = NUL;
11812 }
11813
11814 q = gettail(p);
11815 if (q > p && *q == NUL)
11816 {
11817 /* Ignore trailing path separator. */
11818 q[-1] = NUL;
11819 q = gettail(p);
11820 }
11821 if (q > p && !mch_isFullName(buf))
11822 {
11823 /* symlink is relative to directory of argument */
11824 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11825 if (cpy != NULL)
11826 {
11827 STRCPY(cpy, p);
11828 STRCPY(gettail(cpy), buf);
11829 vim_free(p);
11830 p = cpy;
11831 }
11832 }
11833 else
11834 {
11835 vim_free(p);
11836 p = vim_strsave(buf);
11837 }
11838 }
11839
11840 if (remain == NULL)
11841 break;
11842
11843 /* Append the first path component of "remain" to "p". */
11844 q = getnextcomp(remain + 1);
11845 len = q - remain - (*q != NUL);
11846 cpy = vim_strnsave(p, STRLEN(p) + len);
11847 if (cpy != NULL)
11848 {
11849 STRNCAT(cpy, remain, len);
11850 vim_free(p);
11851 p = cpy;
11852 }
11853 /* Shorten "remain". */
11854 if (*q != NUL)
11855 STRCPY(remain, q - 1);
11856 else
11857 {
11858 vim_free(remain);
11859 remain = NULL;
11860 }
11861 }
11862
11863 /* If the result is a relative path name, make it explicitly relative to
11864 * the current directory if and only if the argument had this form. */
11865 if (!vim_ispathsep(*p))
11866 {
11867 if (is_relative_to_current
11868 && *p != NUL
11869 && !(p[0] == '.'
11870 && (p[1] == NUL
11871 || vim_ispathsep(p[1])
11872 || (p[1] == '.'
11873 && (p[2] == NUL
11874 || vim_ispathsep(p[2]))))))
11875 {
11876 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011877 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 if (cpy != NULL)
11879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 vim_free(p);
11881 p = cpy;
11882 }
11883 }
11884 else if (!is_relative_to_current)
11885 {
11886 /* Strip leading "./". */
11887 q = p;
11888 while (q[0] == '.' && vim_ispathsep(q[1]))
11889 q += 2;
11890 if (q > p)
11891 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11892 }
11893 }
11894
11895 /* Ensure that the result will have no trailing path separator
11896 * if the argument had none. But keep "/" or "//". */
11897 if (!has_trailing_pathsep)
11898 {
11899 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011900 if (after_pathsep(p, q))
11901 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 }
11903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 }
11906# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011907 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908# endif
11909#endif
11910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912
11913#ifdef HAVE_READLINK
11914fail:
11915#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917}
11918
11919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 */
11922 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011923f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011924 typval_T *argvars;
11925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926{
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 list_T *l;
11928 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930 rettv->vval.v_number = 0;
11931 if (argvars[0].v_type != VAR_LIST)
11932 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011933 else if ((l = argvars[0].vval.v_list) != NULL
11934 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011935 {
11936 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011937 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011938 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011939 while (li != NULL)
11940 {
11941 ni = li->li_prev;
11942 list_append(l, li);
11943 li = ni;
11944 }
11945 rettv->vval.v_list = l;
11946 rettv->v_type = VAR_LIST;
11947 ++l->lv_refcount;
11948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949}
11950
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011951#define SP_NOMOVE 1 /* don't move cursor */
11952#define SP_REPEAT 2 /* repeat to find outer pair */
11953#define SP_RETCOUNT 4 /* return matchcount */
11954
Bram Moolenaar33570922005-01-25 22:26:29 +000011955static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956
11957/*
11958 * Get flags for a search function.
11959 * Possibly sets "p_ws".
11960 * Returns BACKWARD, FORWARD or zero (for an error).
11961 */
11962 static int
11963get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011964 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011965 int *flagsp;
11966{
11967 int dir = FORWARD;
11968 char_u *flags;
11969 char_u nbuf[NUMBUFLEN];
11970 int mask;
11971
11972 if (varp->v_type != VAR_UNKNOWN)
11973 {
11974 flags = get_tv_string_buf(varp, nbuf);
11975 while (*flags != NUL)
11976 {
11977 switch (*flags)
11978 {
11979 case 'b': dir = BACKWARD; break;
11980 case 'w': p_ws = TRUE; break;
11981 case 'W': p_ws = FALSE; break;
11982 default: mask = 0;
11983 if (flagsp != NULL)
11984 switch (*flags)
11985 {
11986 case 'n': mask = SP_NOMOVE; break;
11987 case 'r': mask = SP_REPEAT; break;
11988 case 'm': mask = SP_RETCOUNT; break;
11989 }
11990 if (mask == 0)
11991 {
11992 EMSG2(_(e_invarg2), flags);
11993 dir = 0;
11994 }
11995 else
11996 *flagsp |= mask;
11997 }
11998 if (dir == 0)
11999 break;
12000 ++flags;
12001 }
12002 }
12003 return dir;
12004}
12005
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006/*
12007 * "search()" function
12008 */
12009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *argvars;
12012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013{
12014 char_u *pat;
12015 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012016 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 int save_p_ws = p_ws;
12018 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012019 int flags = 0;
12020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012024 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12025 if (dir == 0)
12026 goto theend;
12027 if ((flags & ~SP_NOMOVE) != 0)
12028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012030 goto theend;
12031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012033 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12035 SEARCH_KEEP, RE_SEARCH) != FAIL)
12036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 curwin->w_cursor = pos;
12039 /* "/$" will put the cursor after the end of the line, may need to
12040 * correct that here */
12041 check_cursor();
12042 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012043
12044 /* If 'n' flag is used: restore cursor position. */
12045 if (flags & SP_NOMOVE)
12046 curwin->w_cursor = save_cursor;
12047theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 p_ws = save_p_ws;
12049}
12050
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051/*
12052 * "searchpair()" function
12053 */
12054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012056 typval_T *argvars;
12057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058{
12059 char_u *spat, *mpat, *epat;
12060 char_u *skip;
12061 char_u *pat, *pat2, *pat3;
12062 pos_T pos;
12063 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012064 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 pos_T save_cursor;
12066 pos_T save_pos;
12067 int save_p_ws = p_ws;
12068 char_u *save_cpo;
12069 int dir;
12070 int flags = 0;
12071 char_u nbuf1[NUMBUFLEN];
12072 char_u nbuf2[NUMBUFLEN];
12073 char_u nbuf3[NUMBUFLEN];
12074 int n;
12075 int r;
12076 int nest = 1;
12077 int err;
12078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080
12081 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12082 save_cpo = p_cpo;
12083 p_cpo = (char_u *)"";
12084
12085 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086 spat = get_tv_string(&argvars[0]);
12087 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12088 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089
12090 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12091 * start/middle/end (pat3, for the top pair). */
12092 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12093 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12094 if (pat2 == NULL || pat3 == NULL)
12095 goto theend;
12096 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12097 if (*mpat == NUL)
12098 STRCPY(pat3, pat2);
12099 else
12100 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12101 spat, epat, mpat);
12102
12103 /* Handle the optional fourth argument: flags */
12104 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012105 if (dir == 0)
12106 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107
12108 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012109 if (argvars[3].v_type == VAR_UNKNOWN
12110 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 skip = (char_u *)"";
12112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114
12115 save_cursor = curwin->w_cursor;
12116 pos = curwin->w_cursor;
12117 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012118 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 pat = pat3;
12120 for (;;)
12121 {
12122 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12123 SEARCH_KEEP, RE_SEARCH);
12124 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12125 /* didn't find it or found the first match again: FAIL */
12126 break;
12127
12128 if (firstpos.lnum == 0)
12129 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012130 if (equalpos(pos, foundpos))
12131 {
12132 /* Found the same position again. Can happen with a pattern that
12133 * has "\zs" at the end and searching backwards. Advance one
12134 * character and try again. */
12135 if (dir == BACKWARD)
12136 decl(&pos);
12137 else
12138 incl(&pos);
12139 }
12140 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141
12142 /* If the skip pattern matches, ignore this match. */
12143 if (*skip != NUL)
12144 {
12145 save_pos = curwin->w_cursor;
12146 curwin->w_cursor = pos;
12147 r = eval_to_bool(skip, &err, NULL, FALSE);
12148 curwin->w_cursor = save_pos;
12149 if (err)
12150 {
12151 /* Evaluating {skip} caused an error, break here. */
12152 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 break;
12155 }
12156 if (r)
12157 continue;
12158 }
12159
12160 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12161 {
12162 /* Found end when searching backwards or start when searching
12163 * forward: nested pair. */
12164 ++nest;
12165 pat = pat2; /* nested, don't search for middle */
12166 }
12167 else
12168 {
12169 /* Found end when searching forward or start when searching
12170 * backward: end of (nested) pair; or found middle in outer pair. */
12171 if (--nest == 1)
12172 pat = pat3; /* outer level, search for middle */
12173 }
12174
12175 if (nest == 0)
12176 {
12177 /* Found the match: return matchcount or line number. */
12178 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 curwin->w_cursor = pos;
12183 if (!(flags & SP_REPEAT))
12184 break;
12185 nest = 1; /* search for next unmatched */
12186 }
12187 }
12188
12189 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 curwin->w_cursor = save_cursor;
12192
12193theend:
12194 vim_free(pat2);
12195 vim_free(pat3);
12196 p_ws = save_p_ws;
12197 p_cpo = save_cpo;
12198}
12199
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200/*ARGSUSED*/
12201 static void
12202f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012203 typval_T *argvars;
12204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206#ifdef FEAT_CLIENTSERVER
12207 char_u buf[NUMBUFLEN];
12208 char_u *server = get_tv_string(&argvars[0]);
12209 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211 rettv->vval.v_number = -1;
12212 if (check_restricted() || check_secure())
12213 return;
12214# ifdef FEAT_X11
12215 if (check_connection() == FAIL)
12216 return;
12217# endif
12218
12219 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012221 EMSG(_("E258: Unable to send to client"));
12222 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 rettv->vval.v_number = 0;
12225#else
12226 rettv->vval.v_number = -1;
12227#endif
12228}
12229
12230/*ARGSUSED*/
12231 static void
12232f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 typval_T *argvars;
12234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012235{
12236 char_u *r = NULL;
12237
12238#ifdef FEAT_CLIENTSERVER
12239# ifdef WIN32
12240 r = serverGetVimNames();
12241# else
12242 make_connection();
12243 if (X_DISPLAY != NULL)
12244 r = serverGetVimNames(X_DISPLAY);
12245# endif
12246#endif
12247 rettv->v_type = VAR_STRING;
12248 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249}
12250
12251/*
12252 * "setbufvar()" function
12253 */
12254/*ARGSUSED*/
12255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 typval_T *argvars;
12258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259{
12260 buf_T *buf;
12261#ifdef FEAT_AUTOCMD
12262 aco_save_T aco;
12263#else
12264 buf_T *save_curbuf;
12265#endif
12266 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012267 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 char_u nbuf[NUMBUFLEN];
12269
12270 if (check_restricted() || check_secure())
12271 return;
12272 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 buf = get_buf_tv(&argvars[0]);
12274 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 varp = &argvars[2];
12276
12277 if (buf != NULL && varname != NULL && varp != NULL)
12278 {
12279 /* set curbuf to be our buf, temporarily */
12280#ifdef FEAT_AUTOCMD
12281 aucmd_prepbuf(&aco, buf);
12282#else
12283 save_curbuf = curbuf;
12284 curbuf = buf;
12285#endif
12286
12287 if (*varname == '&')
12288 {
12289 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 set_option_value(varname, get_tv_number(varp),
12291 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 }
12293 else
12294 {
12295 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12296 if (bufvarname != NULL)
12297 {
12298 STRCPY(bufvarname, "b:");
12299 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012300 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 vim_free(bufvarname);
12302 }
12303 }
12304
12305 /* reset notion of buffer */
12306#ifdef FEAT_AUTOCMD
12307 aucmd_restbuf(&aco);
12308#else
12309 curbuf = save_curbuf;
12310#endif
12311 }
12312 --emsg_off;
12313}
12314
12315/*
12316 * "setcmdpos()" function
12317 */
12318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012320 typval_T *argvars;
12321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_number = set_cmdline_pos(
12324 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325}
12326
12327/*
12328 * "setline()" function
12329 */
12330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 typval_T *argvars;
12333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
12335 linenr_T lnum;
12336 char_u *line;
12337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 lnum = get_tv_lnum(argvars);
12339 line = get_tv_string(&argvars[1]);
12340 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341
12342 if (lnum >= 1
12343 && lnum <= curbuf->b_ml.ml_line_count
12344 && u_savesub(lnum) == OK
12345 && ml_replace(lnum, line, TRUE) == OK)
12346 {
12347 changed_bytes(lnum, 0);
12348 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 }
12351}
12352
12353/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012354 * "setqflist()" function
12355 */
12356/*ARGSUSED*/
12357 static void
12358f_setqflist(argvars, rettv)
12359 typval_T *argvars;
12360 typval_T *rettv;
12361{
12362 rettv->vval.v_number = -1;
12363
12364#ifdef FEAT_QUICKFIX
12365 if (argvars[0].v_type != VAR_LIST)
12366 EMSG(_(e_listreq));
12367 else
12368 {
12369 list_T *l = argvars[0].vval.v_list;
12370
12371 if (l != NULL && set_errorlist(l) == OK)
12372 rettv->vval.v_number = 0;
12373 }
12374#endif
12375}
12376
12377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 * "setreg()" function
12379 */
12380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012382 typval_T *argvars;
12383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384{
12385 int regname;
12386 char_u *strregname;
12387 char_u *stropt;
12388 int append;
12389 char_u yank_type;
12390 long block_len;
12391
12392 block_len = -1;
12393 yank_type = MAUTO;
12394 append = FALSE;
12395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 strregname = get_tv_string(argvars);
12397 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398
12399 regname = (strregname == NULL ? '"' : *strregname);
12400 if (regname == 0 || regname == '@')
12401 regname = '"';
12402 else if (regname == '=')
12403 return;
12404
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 switch (*stropt)
12409 {
12410 case 'a': case 'A': /* append */
12411 append = TRUE;
12412 break;
12413 case 'v': case 'c': /* character-wise selection */
12414 yank_type = MCHAR;
12415 break;
12416 case 'V': case 'l': /* line-wise selection */
12417 yank_type = MLINE;
12418 break;
12419#ifdef FEAT_VISUAL
12420 case 'b': case Ctrl_V: /* block-wise selection */
12421 yank_type = MBLOCK;
12422 if (VIM_ISDIGIT(stropt[1]))
12423 {
12424 ++stropt;
12425 block_len = getdigits(&stropt) - 1;
12426 --stropt;
12427 }
12428 break;
12429#endif
12430 }
12431 }
12432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436}
12437
12438
12439/*
12440 * "setwinvar(expr)" function
12441 */
12442/*ARGSUSED*/
12443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012445 typval_T *argvars;
12446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447{
12448 win_T *win;
12449#ifdef FEAT_WINDOWS
12450 win_T *save_curwin;
12451#endif
12452 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012453 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454 char_u nbuf[NUMBUFLEN];
12455
12456 if (check_restricted() || check_secure())
12457 return;
12458 ++emsg_off;
12459 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 varp = &argvars[2];
12462
12463 if (win != NULL && varname != NULL && varp != NULL)
12464 {
12465#ifdef FEAT_WINDOWS
12466 /* set curwin to be our win, temporarily */
12467 save_curwin = curwin;
12468 curwin = win;
12469 curbuf = curwin->w_buffer;
12470#endif
12471
12472 if (*varname == '&')
12473 {
12474 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 set_option_value(varname, get_tv_number(varp),
12476 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 }
12478 else
12479 {
12480 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12481 if (winvarname != NULL)
12482 {
12483 STRCPY(winvarname, "w:");
12484 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012485 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 vim_free(winvarname);
12487 }
12488 }
12489
12490#ifdef FEAT_WINDOWS
12491 /* Restore current window, if it's still valid (autocomands can make
12492 * it invalid). */
12493 if (win_valid(save_curwin))
12494 {
12495 curwin = save_curwin;
12496 curbuf = curwin->w_buffer;
12497 }
12498#endif
12499 }
12500 --emsg_off;
12501}
12502
12503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012504 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 */
12506 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012507f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *argvars;
12509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513 p = get_tv_string(&argvars[0]);
12514 rettv->vval.v_string = vim_strsave(p);
12515 simplify_filename(rettv->vval.v_string); /* simplify in place */
12516 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517}
12518
Bram Moolenaar0d660222005-01-07 21:51:51 +000012519static int
12520#ifdef __BORLANDC__
12521 _RTLENTRYF
12522#endif
12523 item_compare __ARGS((const void *s1, const void *s2));
12524static int
12525#ifdef __BORLANDC__
12526 _RTLENTRYF
12527#endif
12528 item_compare2 __ARGS((const void *s1, const void *s2));
12529
12530static int item_compare_ic;
12531static char_u *item_compare_func;
12532#define ITEM_COMPARE_FAIL 999
12533
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012535 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 static int
12538#ifdef __BORLANDC__
12539_RTLENTRYF
12540#endif
12541item_compare(s1, s2)
12542 const void *s1;
12543 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545 char_u *p1, *p2;
12546 char_u *tofree1, *tofree2;
12547 int res;
12548 char_u numbuf1[NUMBUFLEN];
12549 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550
Bram Moolenaar33570922005-01-25 22:26:29 +000012551 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12552 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553 if (item_compare_ic)
12554 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556 res = STRCMP(p1, p2);
12557 vim_free(tofree1);
12558 vim_free(tofree2);
12559 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560}
12561
12562 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563#ifdef __BORLANDC__
12564_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566item_compare2(s1, s2)
12567 const void *s1;
12568 const void *s2;
12569{
12570 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T rettv;
12572 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012575 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12576 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012577 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12578 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579
12580 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12581 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012582 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012583 clear_tv(&argv[0]);
12584 clear_tv(&argv[1]);
12585
12586 if (res == FAIL)
12587 res = ITEM_COMPARE_FAIL;
12588 else
12589 res = get_tv_number(&rettv);
12590 clear_tv(&rettv);
12591 return res;
12592}
12593
12594/*
12595 * "sort({list})" function
12596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 typval_T *argvars;
12600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601{
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 list_T *l;
12603 listitem_T *li;
12604 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605 long len;
12606 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 rettv->vval.v_number = 0;
12609 if (argvars[0].v_type != VAR_LIST)
12610 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 else
12612 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012614 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615 return;
12616 rettv->vval.v_list = l;
12617 rettv->v_type = VAR_LIST;
12618 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619
Bram Moolenaar0d660222005-01-07 21:51:51 +000012620 len = list_len(l);
12621 if (len <= 1)
12622 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 item_compare_ic = FALSE;
12625 item_compare_func = NULL;
12626 if (argvars[1].v_type != VAR_UNKNOWN)
12627 {
12628 if (argvars[1].v_type == VAR_FUNC)
12629 item_compare_func = argvars[0].vval.v_string;
12630 else
12631 {
12632 i = get_tv_number(&argvars[1]);
12633 if (i == 1)
12634 item_compare_ic = TRUE;
12635 else
12636 item_compare_func = get_tv_string(&argvars[1]);
12637 }
12638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639
Bram Moolenaar0d660222005-01-07 21:51:51 +000012640 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012641 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012642 if (ptrs == NULL)
12643 return;
12644 i = 0;
12645 for (li = l->lv_first; li != NULL; li = li->li_next)
12646 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
Bram Moolenaar0d660222005-01-07 21:51:51 +000012648 /* test the compare function */
12649 if (item_compare_func != NULL
12650 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12651 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012652 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654 {
12655 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012656 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657 item_compare_func == NULL ? item_compare : item_compare2);
12658
12659 /* Clear the List and append the items in the sorted order. */
12660 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012661 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012662 for (i = 0; i < len; ++i)
12663 list_append(l, ptrs[i]);
12664 }
12665
12666 vim_free(ptrs);
12667 }
12668}
12669
12670 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012671f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 typval_T *argvars;
12673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012674{
12675 char_u *str;
12676 char_u *end;
12677 char_u *pat;
12678 regmatch_T regmatch;
12679 char_u patbuf[NUMBUFLEN];
12680 char_u *save_cpo;
12681 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012682 listitem_T *ni;
12683 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012684 colnr_T col = 0;
12685
12686 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12687 save_cpo = p_cpo;
12688 p_cpo = (char_u *)"";
12689
12690 str = get_tv_string(&argvars[0]);
12691 if (argvars[1].v_type == VAR_UNKNOWN)
12692 pat = (char_u *)"[\\x01- ]\\+";
12693 else
12694 pat = get_tv_string_buf(&argvars[1], patbuf);
12695
12696 l = list_alloc();
12697 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699 rettv->v_type = VAR_LIST;
12700 rettv->vval.v_list = l;
12701 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12704 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012706 regmatch.rm_ic = FALSE;
12707 while (*str != NUL)
12708 {
12709 match = vim_regexec_nl(&regmatch, str, col);
12710 if (match)
12711 end = regmatch.startp[0];
12712 else
12713 end = str + STRLEN(str);
12714 if (end > str)
12715 {
12716 ni = listitem_alloc();
12717 if (ni == NULL)
12718 break;
12719 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012720 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12722 list_append(l, ni);
12723 }
12724 if (!match)
12725 break;
12726 /* Advance to just after the match. */
12727 if (regmatch.endp[0] > str)
12728 col = 0;
12729 else
12730 {
12731 /* Don't get stuck at the same match. */
12732#ifdef FEAT_MBYTE
12733 col = mb_ptr2len_check(regmatch.endp[0]);
12734#else
12735 col = 1;
12736#endif
12737 }
12738 str = regmatch.endp[0];
12739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740
Bram Moolenaar0d660222005-01-07 21:51:51 +000012741 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743
Bram Moolenaar0d660222005-01-07 21:51:51 +000012744 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745}
12746
12747#ifdef HAVE_STRFTIME
12748/*
12749 * "strftime({format}[, {time}])" function
12750 */
12751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *argvars;
12754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755{
12756 char_u result_buf[256];
12757 struct tm *curtime;
12758 time_t seconds;
12759 char_u *p;
12760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012764 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 seconds = time(NULL);
12766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 curtime = localtime(&seconds);
12769 /* MSVC returns NULL for an invalid value of seconds. */
12770 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 else
12773 {
12774# ifdef FEAT_MBYTE
12775 vimconv_T conv;
12776 char_u *enc;
12777
12778 conv.vc_type = CONV_NONE;
12779 enc = enc_locale();
12780 convert_setup(&conv, p_enc, enc);
12781 if (conv.vc_type != CONV_NONE)
12782 p = string_convert(&conv, p, NULL);
12783# endif
12784 if (p != NULL)
12785 (void)strftime((char *)result_buf, sizeof(result_buf),
12786 (char *)p, curtime);
12787 else
12788 result_buf[0] = NUL;
12789
12790# ifdef FEAT_MBYTE
12791 if (conv.vc_type != CONV_NONE)
12792 vim_free(p);
12793 convert_setup(&conv, enc, p_enc);
12794 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 else
12797# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799
12800# ifdef FEAT_MBYTE
12801 /* Release conversion descriptors */
12802 convert_setup(&conv, NULL, NULL);
12803 vim_free(enc);
12804# endif
12805 }
12806}
12807#endif
12808
12809/*
12810 * "stridx()" function
12811 */
12812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012814 typval_T *argvars;
12815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816{
12817 char_u buf[NUMBUFLEN];
12818 char_u *needle;
12819 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012822 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 if (argvars[2].v_type != VAR_UNKNOWN)
12829 {
12830 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012831 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012833 if (start_idx >= 0)
12834 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 }
12836
12837 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12838 if (pos != NULL)
12839 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840}
12841
12842/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012843 * "string()" function
12844 */
12845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012847 typval_T *argvars;
12848 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012849{
12850 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012851 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012854 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012855 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857}
12858
12859/*
12860 * "strlen()" function
12861 */
12862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *argvars;
12865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = (varnumber_T)(STRLEN(
12868 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869}
12870
12871/*
12872 * "strpart()" function
12873 */
12874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878{
12879 char_u *p;
12880 int n;
12881 int len;
12882 int slen;
12883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 slen = (int)STRLEN(p);
12886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012888 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 else
12891 len = slen - n; /* default len: all bytes that are available. */
12892
12893 /*
12894 * Only return the overlap between the specified part and the actual
12895 * string.
12896 */
12897 if (n < 0)
12898 {
12899 len += n;
12900 n = 0;
12901 }
12902 else if (n > slen)
12903 n = slen;
12904 if (len < 0)
12905 len = 0;
12906 else if (n + len > slen)
12907 len = slen - n;
12908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->v_type = VAR_STRING;
12910 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911}
12912
12913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914 * "strridx()" function
12915 */
12916 static void
12917f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 typval_T *argvars;
12919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012920{
12921 char_u buf[NUMBUFLEN];
12922 char_u *needle;
12923 char_u *haystack;
12924 char_u *rest;
12925 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012926 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012927
12928 needle = get_tv_string(&argvars[1]);
12929 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012930 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012931 if (argvars[2].v_type != VAR_UNKNOWN)
12932 {
12933 /* Third argument: upper limit for index */
12934 end_idx = get_tv_number(&argvars[2]);
12935 if (end_idx < 0)
12936 {
12937 /* can never find a match */
12938 rettv->vval.v_number = -1;
12939 return;
12940 }
12941 }
12942 else
12943 end_idx = haystack_len;
12944
Bram Moolenaar0d660222005-01-07 21:51:51 +000012945 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012946 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012947 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012948 lastmatch = haystack + end_idx;
12949 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012951 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012952 for (rest = haystack; *rest != '\0'; ++rest)
12953 {
12954 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012955 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 break;
12957 lastmatch = rest;
12958 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960
12961 if (lastmatch == NULL)
12962 rettv->vval.v_number = -1;
12963 else
12964 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12965}
12966
12967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 * "strtrans()" function
12969 */
12970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 typval_T *argvars;
12973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 rettv->v_type = VAR_STRING;
12976 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977}
12978
12979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012980 * "submatch()" function
12981 */
12982 static void
12983f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012984 typval_T *argvars;
12985 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012986{
12987 rettv->v_type = VAR_STRING;
12988 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12989}
12990
12991/*
12992 * "substitute()" function
12993 */
12994 static void
12995f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012996 typval_T *argvars;
12997 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012998{
12999 char_u patbuf[NUMBUFLEN];
13000 char_u subbuf[NUMBUFLEN];
13001 char_u flagsbuf[NUMBUFLEN];
13002
13003 rettv->v_type = VAR_STRING;
13004 rettv->vval.v_string = do_string_sub(
13005 get_tv_string(&argvars[0]),
13006 get_tv_string_buf(&argvars[1], patbuf),
13007 get_tv_string_buf(&argvars[2], subbuf),
13008 get_tv_string_buf(&argvars[3], flagsbuf));
13009}
13010
13011/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013012 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 */
13014/*ARGSUSED*/
13015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013016f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013017 typval_T *argvars;
13018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019{
13020 int id = 0;
13021#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013022 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 long col;
13024 int trans;
13025
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013026 lnum = get_tv_lnum(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013027 col = get_tv_number(&argvars[1]) - 1;
13028 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013030 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13031 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13032 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#endif
13034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036}
13037
13038/*
13039 * "synIDattr(id, what [, mode])" function
13040 */
13041/*ARGSUSED*/
13042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013044 typval_T *argvars;
13045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046{
13047 char_u *p = NULL;
13048#ifdef FEAT_SYN_HL
13049 int id;
13050 char_u *what;
13051 char_u *mode;
13052 char_u modebuf[NUMBUFLEN];
13053 int modec;
13054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 id = get_tv_number(&argvars[0]);
13056 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013057 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 modec = TOLOWER_ASC(mode[0]);
13061 if (modec != 't' && modec != 'c'
13062#ifdef FEAT_GUI
13063 && modec != 'g'
13064#endif
13065 )
13066 modec = 0; /* replace invalid with current */
13067 }
13068 else
13069 {
13070#ifdef FEAT_GUI
13071 if (gui.in_use)
13072 modec = 'g';
13073 else
13074#endif
13075 if (t_colors > 1)
13076 modec = 'c';
13077 else
13078 modec = 't';
13079 }
13080
13081
13082 switch (TOLOWER_ASC(what[0]))
13083 {
13084 case 'b':
13085 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13086 p = highlight_color(id, what, modec);
13087 else /* bold */
13088 p = highlight_has_attr(id, HL_BOLD, modec);
13089 break;
13090
13091 case 'f': /* fg[#] */
13092 p = highlight_color(id, what, modec);
13093 break;
13094
13095 case 'i':
13096 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13097 p = highlight_has_attr(id, HL_INVERSE, modec);
13098 else /* italic */
13099 p = highlight_has_attr(id, HL_ITALIC, modec);
13100 break;
13101
13102 case 'n': /* name */
13103 p = get_highlight_name(NULL, id - 1);
13104 break;
13105
13106 case 'r': /* reverse */
13107 p = highlight_has_attr(id, HL_INVERSE, modec);
13108 break;
13109
13110 case 's': /* standout */
13111 p = highlight_has_attr(id, HL_STANDOUT, modec);
13112 break;
13113
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013114 case 'u':
13115 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13116 /* underline */
13117 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13118 else
13119 /* undercurl */
13120 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 break;
13122 }
13123
13124 if (p != NULL)
13125 p = vim_strsave(p);
13126#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127 rettv->v_type = VAR_STRING;
13128 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129}
13130
13131/*
13132 * "synIDtrans(id)" function
13133 */
13134/*ARGSUSED*/
13135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013137 typval_T *argvars;
13138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139{
13140 int id;
13141
13142#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
13145 if (id > 0)
13146 id = syn_get_final_id(id);
13147 else
13148#endif
13149 id = 0;
13150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013151 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152}
13153
13154/*
13155 * "system()" function
13156 */
13157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *argvars;
13160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013162 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013164 char_u *infile = NULL;
13165 char_u buf[NUMBUFLEN];
13166 int err = FALSE;
13167 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013170 {
13171 /*
13172 * Write the string to a temp file, to be used for input of the shell
13173 * command.
13174 */
13175 if ((infile = vim_tempname('i')) == NULL)
13176 {
13177 EMSG(_(e_notmp));
13178 return;
13179 }
13180
13181 fd = mch_fopen((char *)infile, WRITEBIN);
13182 if (fd == NULL)
13183 {
13184 EMSG2(_(e_notopen), infile);
13185 goto done;
13186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013188 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13189 err = TRUE;
13190 if (fclose(fd) != 0)
13191 err = TRUE;
13192 if (err)
13193 {
13194 EMSG(_("E677: Error writing temp file"));
13195 goto done;
13196 }
13197 }
13198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013200
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef USE_CR
13202 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013203 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 {
13205 char_u *s;
13206
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013207 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 {
13209 if (*s == CAR)
13210 *s = NL;
13211 }
13212 }
13213#else
13214# ifdef USE_CRNL
13215 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013216 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 {
13218 char_u *s, *d;
13219
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013220 d = res;
13221 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 {
13223 if (s[0] == CAR && s[1] == NL)
13224 ++s;
13225 *d++ = *s;
13226 }
13227 *d = NUL;
13228 }
13229# endif
13230#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013231
13232done:
13233 if (infile != NULL)
13234 {
13235 mch_remove(infile);
13236 vim_free(infile);
13237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->v_type = VAR_STRING;
13239 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240}
13241
13242/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013243 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013244 */
13245 static void
13246f_taglist(argvars, rettv)
13247 typval_T *argvars;
13248 typval_T *rettv;
13249{
13250 char_u *tag_pattern;
13251 list_T *l;
13252
13253 tag_pattern = get_tv_string(&argvars[0]);
13254
13255 rettv->vval.v_number = FALSE;
13256
13257 l = list_alloc();
13258 if (l != NULL)
13259 {
13260 if (get_tags(l, tag_pattern) != FAIL)
13261 {
13262 rettv->vval.v_list = l;
13263 rettv->v_type = VAR_LIST;
13264 ++l->lv_refcount;
13265 }
13266 else
13267 list_free(l);
13268 }
13269}
13270
13271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 * "tempname()" function
13273 */
13274/*ARGSUSED*/
13275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 typval_T *argvars;
13278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279{
13280 static int x = 'A';
13281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 rettv->v_type = VAR_STRING;
13283 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
13285 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13286 * names. Skip 'I' and 'O', they are used for shell redirection. */
13287 do
13288 {
13289 if (x == 'Z')
13290 x = '0';
13291 else if (x == '9')
13292 x = 'A';
13293 else
13294 {
13295#ifdef EBCDIC
13296 if (x == 'I')
13297 x = 'J';
13298 else if (x == 'R')
13299 x = 'S';
13300 else
13301#endif
13302 ++x;
13303 }
13304 } while (x == 'I' || x == 'O');
13305}
13306
13307/*
13308 * "tolower(string)" function
13309 */
13310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 typval_T *argvars;
13313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314{
13315 char_u *p;
13316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317 p = vim_strsave(get_tv_string(&argvars[0]));
13318 rettv->v_type = VAR_STRING;
13319 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320
13321 if (p != NULL)
13322 while (*p != NUL)
13323 {
13324#ifdef FEAT_MBYTE
13325 int l;
13326
13327 if (enc_utf8)
13328 {
13329 int c, lc;
13330
13331 c = utf_ptr2char(p);
13332 lc = utf_tolower(c);
13333 l = utf_ptr2len_check(p);
13334 /* TODO: reallocate string when byte count changes. */
13335 if (utf_char2len(lc) == l)
13336 utf_char2bytes(lc, p);
13337 p += l;
13338 }
13339 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13340 p += l; /* skip multi-byte character */
13341 else
13342#endif
13343 {
13344 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13345 ++p;
13346 }
13347 }
13348}
13349
13350/*
13351 * "toupper(string)" function
13352 */
13353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 typval_T *argvars;
13356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357{
13358 char_u *p;
13359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 p = vim_strsave(get_tv_string(&argvars[0]));
13361 rettv->v_type = VAR_STRING;
13362 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363
13364 if (p != NULL)
13365 while (*p != NUL)
13366 {
13367#ifdef FEAT_MBYTE
13368 int l;
13369
13370 if (enc_utf8)
13371 {
13372 int c, uc;
13373
13374 c = utf_ptr2char(p);
13375 uc = utf_toupper(c);
13376 l = utf_ptr2len_check(p);
13377 /* TODO: reallocate string when byte count changes. */
13378 if (utf_char2len(uc) == l)
13379 utf_char2bytes(uc, p);
13380 p += l;
13381 }
13382 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13383 p += l; /* skip multi-byte character */
13384 else
13385#endif
13386 {
13387 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13388 p++;
13389 }
13390 }
13391}
13392
13393/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013394 * "tr(string, fromstr, tostr)" function
13395 */
13396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013400{
13401 char_u *instr;
13402 char_u *fromstr;
13403 char_u *tostr;
13404 char_u *p;
13405#ifdef FEAT_MBYTE
13406 int inlen;
13407 int fromlen;
13408 int tolen;
13409 int idx;
13410 char_u *cpstr;
13411 int cplen;
13412 int first = TRUE;
13413#endif
13414 char_u buf[NUMBUFLEN];
13415 char_u buf2[NUMBUFLEN];
13416 garray_T ga;
13417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 instr = get_tv_string(&argvars[0]);
13419 fromstr = get_tv_string_buf(&argvars[1], buf);
13420 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013421
13422 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 rettv->v_type = VAR_STRING;
13424 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013425 ga_init2(&ga, (int)sizeof(char), 80);
13426
13427#ifdef FEAT_MBYTE
13428 if (!has_mbyte)
13429#endif
13430 /* not multi-byte: fromstr and tostr must be the same length */
13431 if (STRLEN(fromstr) != STRLEN(tostr))
13432 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013433#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013434error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013435#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013436 EMSG2(_(e_invarg2), fromstr);
13437 ga_clear(&ga);
13438 return;
13439 }
13440
13441 /* fromstr and tostr have to contain the same number of chars */
13442 while (*instr != NUL)
13443 {
13444#ifdef FEAT_MBYTE
13445 if (has_mbyte)
13446 {
13447 inlen = mb_ptr2len_check(instr);
13448 cpstr = instr;
13449 cplen = inlen;
13450 idx = 0;
13451 for (p = fromstr; *p != NUL; p += fromlen)
13452 {
13453 fromlen = mb_ptr2len_check(p);
13454 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13455 {
13456 for (p = tostr; *p != NUL; p += tolen)
13457 {
13458 tolen = mb_ptr2len_check(p);
13459 if (idx-- == 0)
13460 {
13461 cplen = tolen;
13462 cpstr = p;
13463 break;
13464 }
13465 }
13466 if (*p == NUL) /* tostr is shorter than fromstr */
13467 goto error;
13468 break;
13469 }
13470 ++idx;
13471 }
13472
13473 if (first && cpstr == instr)
13474 {
13475 /* Check that fromstr and tostr have the same number of
13476 * (multi-byte) characters. Done only once when a character
13477 * of instr doesn't appear in fromstr. */
13478 first = FALSE;
13479 for (p = tostr; *p != NUL; p += tolen)
13480 {
13481 tolen = mb_ptr2len_check(p);
13482 --idx;
13483 }
13484 if (idx != 0)
13485 goto error;
13486 }
13487
13488 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013489 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013490 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013491
13492 instr += inlen;
13493 }
13494 else
13495#endif
13496 {
13497 /* When not using multi-byte chars we can do it faster. */
13498 p = vim_strchr(fromstr, *instr);
13499 if (p != NULL)
13500 ga_append(&ga, tostr[p - fromstr]);
13501 else
13502 ga_append(&ga, *instr);
13503 ++instr;
13504 }
13505 }
13506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013508}
13509
13510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 * "type(expr)" function
13512 */
13513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013515 typval_T *argvars;
13516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013518 int n;
13519
13520 switch (argvars[0].v_type)
13521 {
13522 case VAR_NUMBER: n = 0; break;
13523 case VAR_STRING: n = 1; break;
13524 case VAR_FUNC: n = 2; break;
13525 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013526 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013527 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13528 }
13529 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530}
13531
13532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013533 * "values(dict)" function
13534 */
13535 static void
13536f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *argvars;
13538 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013539{
13540 dict_list(argvars, rettv, 1);
13541}
13542
13543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 * "virtcol(string)" function
13545 */
13546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 typval_T *argvars;
13549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550{
13551 colnr_T vcol = 0;
13552 pos_T *fp;
13553
13554 fp = var2fpos(&argvars[0], FALSE);
13555 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13556 {
13557 getvvcol(curwin, fp, NULL, NULL, &vcol);
13558 ++vcol;
13559 }
13560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562}
13563
13564/*
13565 * "visualmode()" function
13566 */
13567/*ARGSUSED*/
13568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013570 typval_T *argvars;
13571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572{
13573#ifdef FEAT_VISUAL
13574 char_u str[2];
13575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 str[0] = curbuf->b_visual_mode_eval;
13578 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580
13581 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013582 if ((argvars[0].v_type == VAR_NUMBER
13583 && argvars[0].vval.v_number != 0)
13584 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 curbuf->b_visual_mode_eval = NUL;
13587#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589#endif
13590}
13591
13592/*
13593 * "winbufnr(nr)" function
13594 */
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600 win_T *wp;
13601
13602 wp = find_win_by_nr(&argvars[0]);
13603 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607}
13608
13609/*
13610 * "wincol()" function
13611 */
13612/*ARGSUSED*/
13613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *argvars;
13616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620}
13621
13622/*
13623 * "winheight(nr)" function
13624 */
13625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013627 typval_T *argvars;
13628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629{
13630 win_T *wp;
13631
13632 wp = find_win_by_nr(&argvars[0]);
13633 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637}
13638
13639/*
13640 * "winline()" function
13641 */
13642/*ARGSUSED*/
13643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013645 typval_T *argvars;
13646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
13648 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650}
13651
13652/*
13653 * "winnr()" function
13654 */
13655/* ARGSUSED */
13656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660{
13661 int nr = 1;
13662#ifdef FEAT_WINDOWS
13663 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013664 win_T *twin = curwin;
13665 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013667 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013670 if (STRCMP(arg, "$") == 0)
13671 twin = lastwin;
13672 else if (STRCMP(arg, "#") == 0)
13673 {
13674 twin = prevwin;
13675 if (prevwin == NULL)
13676 nr = 0;
13677 }
13678 else
13679 {
13680 EMSG2(_(e_invexpr2), arg);
13681 nr = 0;
13682 }
13683 }
13684
13685 if (nr > 0)
13686 for (wp = firstwin; wp != twin; wp = wp->w_next)
13687 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690}
13691
13692/*
13693 * "winrestcmd()" function
13694 */
13695/* ARGSUSED */
13696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 typval_T *argvars;
13699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700{
13701#ifdef FEAT_WINDOWS
13702 win_T *wp;
13703 int winnr = 1;
13704 garray_T ga;
13705 char_u buf[50];
13706
13707 ga_init2(&ga, (int)sizeof(char), 70);
13708 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13709 {
13710 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13711 ga_concat(&ga, buf);
13712# ifdef FEAT_VERTSPLIT
13713 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13714 ga_concat(&ga, buf);
13715# endif
13716 ++winnr;
13717 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013718 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725}
13726
13727/*
13728 * "winwidth(nr)" function
13729 */
13730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *argvars;
13733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
13735 win_T *wp;
13736
13737 wp = find_win_by_nr(&argvars[0]);
13738 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 else
13741#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#endif
13746}
13747
13748 static win_T *
13749find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751{
13752#ifdef FEAT_WINDOWS
13753 win_T *wp;
13754#endif
13755 int nr;
13756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013757 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758
13759#ifdef FEAT_WINDOWS
13760 if (nr == 0)
13761 return curwin;
13762
13763 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13764 if (--nr <= 0)
13765 break;
13766 return wp;
13767#else
13768 if (nr == 0 || nr == 1)
13769 return curwin;
13770 return NULL;
13771#endif
13772}
13773
13774/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013775 * "writefile()" function
13776 */
13777 static void
13778f_writefile(argvars, rettv)
13779 typval_T *argvars;
13780 typval_T *rettv;
13781{
13782 int binary = FALSE;
13783 char_u *fname;
13784 FILE *fd;
13785 listitem_T *li;
13786 char_u *s;
13787 int ret = 0;
13788 int c;
13789
13790 if (argvars[0].v_type != VAR_LIST)
13791 {
13792 EMSG2(_(e_listarg), "writefile()");
13793 return;
13794 }
13795 if (argvars[0].vval.v_list == NULL)
13796 return;
13797
13798 if (argvars[2].v_type != VAR_UNKNOWN
13799 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13800 binary = TRUE;
13801
13802 /* Always open the file in binary mode, library functions have a mind of
13803 * their own about CR-LF conversion. */
13804 fname = get_tv_string(&argvars[1]);
13805 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13806 {
13807 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13808 ret = -1;
13809 }
13810 else
13811 {
13812 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13813 li = li->li_next)
13814 {
13815 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13816 {
13817 if (*s == '\n')
13818 c = putc(NUL, fd);
13819 else
13820 c = putc(*s, fd);
13821 if (c == EOF)
13822 {
13823 ret = -1;
13824 break;
13825 }
13826 }
13827 if (!binary || li->li_next != NULL)
13828 if (putc('\n', fd) == EOF)
13829 {
13830 ret = -1;
13831 break;
13832 }
13833 if (ret < 0)
13834 {
13835 EMSG(_(e_write));
13836 break;
13837 }
13838 }
13839 fclose(fd);
13840 }
13841
13842 rettv->vval.v_number = ret;
13843}
13844
13845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 * Translate a String variable into a position.
13847 */
13848 static pos_T *
13849var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 int lnum; /* TRUE when $ is last line */
13852{
13853 char_u *name;
13854 static pos_T pos;
13855 pos_T *pp;
13856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013857 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 if (name[0] == '.') /* cursor */
13859 return &curwin->w_cursor;
13860 if (name[0] == '\'') /* mark */
13861 {
13862 pp = getmark(name[1], FALSE);
13863 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13864 return NULL;
13865 return pp;
13866 }
13867 if (name[0] == '$') /* last column or line */
13868 {
13869 if (lnum)
13870 {
13871 pos.lnum = curbuf->b_ml.ml_line_count;
13872 pos.col = 0;
13873 }
13874 else
13875 {
13876 pos.lnum = curwin->w_cursor.lnum;
13877 pos.col = (colnr_T)STRLEN(ml_get_curline());
13878 }
13879 return &pos;
13880 }
13881 return NULL;
13882}
13883
13884/*
13885 * Get the length of an environment variable name.
13886 * Advance "arg" to the first character after the name.
13887 * Return 0 for error.
13888 */
13889 static int
13890get_env_len(arg)
13891 char_u **arg;
13892{
13893 char_u *p;
13894 int len;
13895
13896 for (p = *arg; vim_isIDc(*p); ++p)
13897 ;
13898 if (p == *arg) /* no name found */
13899 return 0;
13900
13901 len = (int)(p - *arg);
13902 *arg = p;
13903 return len;
13904}
13905
13906/*
13907 * Get the length of the name of a function or internal variable.
13908 * "arg" is advanced to the first non-white character after the name.
13909 * Return 0 if something is wrong.
13910 */
13911 static int
13912get_id_len(arg)
13913 char_u **arg;
13914{
13915 char_u *p;
13916 int len;
13917
13918 /* Find the end of the name. */
13919 for (p = *arg; eval_isnamec(*p); ++p)
13920 ;
13921 if (p == *arg) /* no name found */
13922 return 0;
13923
13924 len = (int)(p - *arg);
13925 *arg = skipwhite(p);
13926
13927 return len;
13928}
13929
13930/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013931 * Get the length of the name of a variable or function.
13932 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013934 * Return -1 if curly braces expansion failed.
13935 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 * If the name contains 'magic' {}'s, expand them and return the
13937 * expanded name in an allocated string via 'alias' - caller must free.
13938 */
13939 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013940get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 char_u **arg;
13942 char_u **alias;
13943 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013944 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945{
13946 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 char_u *p;
13948 char_u *expr_start;
13949 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950
13951 *alias = NULL; /* default to no alias */
13952
13953 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13954 && (*arg)[2] == (int)KE_SNR)
13955 {
13956 /* hard coded <SNR>, already translated */
13957 *arg += 3;
13958 return get_id_len(arg) + 3;
13959 }
13960 len = eval_fname_script(*arg);
13961 if (len > 0)
13962 {
13963 /* literal "<SID>", "s:" or "<SNR>" */
13964 *arg += len;
13965 }
13966
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 if (expr_start != NULL)
13972 {
13973 char_u *temp_string;
13974
13975 if (!evaluate)
13976 {
13977 len += (int)(p - *arg);
13978 *arg = skipwhite(p);
13979 return len;
13980 }
13981
13982 /*
13983 * Include any <SID> etc in the expanded string:
13984 * Thus the -len here.
13985 */
13986 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13987 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013988 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 *alias = temp_string;
13990 *arg = skipwhite(p);
13991 return (int)STRLEN(temp_string);
13992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993
13994 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013995 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 EMSG2(_(e_invexpr2), *arg);
13997
13998 return len;
13999}
14000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001/*
14002 * Find the end of a variable or function name, taking care of magic braces.
14003 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14004 * start and end of the first magic braces item.
14005 * Return a pointer to just after the name. Equal to "arg" if there is no
14006 * valid name.
14007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 char_u *arg;
14011 char_u **expr_start;
14012 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014013 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015 int mb_nest = 0;
14016 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 char_u *p;
14018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 *expr_start = NULL;
14022 *expr_end = NULL;
14023 }
14024
14025 for (p = arg; *p != NUL
14026 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014027 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000014028 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029 || mb_nest != 0
14030 || br_nest != 0); ++p)
14031 {
14032 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 if (*p == '[')
14035 ++br_nest;
14036 else if (*p == ']')
14037 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 if (*p == '{')
14042 {
14043 mb_nest++;
14044 if (expr_start != NULL && *expr_start == NULL)
14045 *expr_start = p;
14046 }
14047 else if (*p == '}')
14048 {
14049 mb_nest--;
14050 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14051 *expr_end = p;
14052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 }
14055
14056 return p;
14057}
14058
14059/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014060 * Expands out the 'magic' {}'s in a variable/function name.
14061 * Note that this can call itself recursively, to deal with
14062 * constructs like foo{bar}{baz}{bam}
14063 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14064 * "in_start" ^
14065 * "expr_start" ^
14066 * "expr_end" ^
14067 * "in_end" ^
14068 *
14069 * Returns a new allocated string, which the caller must free.
14070 * Returns NULL for failure.
14071 */
14072 static char_u *
14073make_expanded_name(in_start, expr_start, expr_end, in_end)
14074 char_u *in_start;
14075 char_u *expr_start;
14076 char_u *expr_end;
14077 char_u *in_end;
14078{
14079 char_u c1;
14080 char_u *retval = NULL;
14081 char_u *temp_result;
14082 char_u *nextcmd = NULL;
14083
14084 if (expr_end == NULL || in_end == NULL)
14085 return NULL;
14086 *expr_start = NUL;
14087 *expr_end = NUL;
14088 c1 = *in_end;
14089 *in_end = NUL;
14090
14091 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14092 if (temp_result != NULL && nextcmd == NULL)
14093 {
14094 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14095 + (in_end - expr_end) + 1));
14096 if (retval != NULL)
14097 {
14098 STRCPY(retval, in_start);
14099 STRCAT(retval, temp_result);
14100 STRCAT(retval, expr_end + 1);
14101 }
14102 }
14103 vim_free(temp_result);
14104
14105 *in_end = c1; /* put char back for error messages */
14106 *expr_start = '{';
14107 *expr_end = '}';
14108
14109 if (retval != NULL)
14110 {
14111 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
14112 if (expr_start != NULL)
14113 {
14114 /* Further expansion! */
14115 temp_result = make_expanded_name(retval, expr_start,
14116 expr_end, temp_result);
14117 vim_free(retval);
14118 retval = temp_result;
14119 }
14120 }
14121
14122 return retval;
14123}
14124
14125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014127 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 */
14129 static int
14130eval_isnamec(c)
14131 int c;
14132{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014133 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134}
14135
14136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 * Set number v: variable to "val".
14138 */
14139 void
14140set_vim_var_nr(idx, val)
14141 int idx;
14142 long val;
14143{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014144 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145}
14146
14147/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014148 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 */
14150 long
14151get_vim_var_nr(idx)
14152 int idx;
14153{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014154 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155}
14156
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014157#if defined(FEAT_AUTOCMD) || defined(PROTO)
14158/*
14159 * Get string v: variable value. Uses a static buffer, can only be used once.
14160 */
14161 char_u *
14162get_vim_var_str(idx)
14163 int idx;
14164{
14165 return get_tv_string(&vimvars[idx].vv_tv);
14166}
14167#endif
14168
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169/*
14170 * Set v:count, v:count1 and v:prevcount.
14171 */
14172 void
14173set_vcount(count, count1)
14174 long count;
14175 long count1;
14176{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014177 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14178 vimvars[VV_COUNT].vv_nr = count;
14179 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180}
14181
14182/*
14183 * Set string v: variable to a copy of "val".
14184 */
14185 void
14186set_vim_var_string(idx, val, len)
14187 int idx;
14188 char_u *val;
14189 int len; /* length of "val" to use or -1 (whole string) */
14190{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014191 /* Need to do this (at least) once, since we can't initialize a union.
14192 * Will always be invoked when "v:progname" is set. */
14193 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14194
Bram Moolenaare9a41262005-01-15 22:18:47 +000014195 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014197 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014199 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014201 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202}
14203
14204/*
14205 * Set v:register if needed.
14206 */
14207 void
14208set_reg_var(c)
14209 int c;
14210{
14211 char_u regname;
14212
14213 if (c == 0 || c == ' ')
14214 regname = '"';
14215 else
14216 regname = c;
14217 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014218 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219 set_vim_var_string(VV_REG, &regname, 1);
14220}
14221
14222/*
14223 * Get or set v:exception. If "oldval" == NULL, return the current value.
14224 * Otherwise, restore the value to "oldval" and return NULL.
14225 * Must always be called in pairs to save and restore v:exception! Does not
14226 * take care of memory allocations.
14227 */
14228 char_u *
14229v_exception(oldval)
14230 char_u *oldval;
14231{
14232 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014233 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaare9a41262005-01-15 22:18:47 +000014235 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 return NULL;
14237}
14238
14239/*
14240 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14241 * Otherwise, restore the value to "oldval" and return NULL.
14242 * Must always be called in pairs to save and restore v:throwpoint! Does not
14243 * take care of memory allocations.
14244 */
14245 char_u *
14246v_throwpoint(oldval)
14247 char_u *oldval;
14248{
14249 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014250 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251
Bram Moolenaare9a41262005-01-15 22:18:47 +000014252 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 return NULL;
14254}
14255
14256#if defined(FEAT_AUTOCMD) || defined(PROTO)
14257/*
14258 * Set v:cmdarg.
14259 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14260 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14261 * Must always be called in pairs!
14262 */
14263 char_u *
14264set_cmdarg(eap, oldarg)
14265 exarg_T *eap;
14266 char_u *oldarg;
14267{
14268 char_u *oldval;
14269 char_u *newval;
14270 unsigned len;
14271
Bram Moolenaare9a41262005-01-15 22:18:47 +000014272 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014273 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014275 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014276 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014277 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 }
14279
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014280 if (eap->force_bin == FORCE_BIN)
14281 len = 6;
14282 else if (eap->force_bin == FORCE_NOBIN)
14283 len = 8;
14284 else
14285 len = 0;
14286 if (eap->force_ff != 0)
14287 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14288# ifdef FEAT_MBYTE
14289 if (eap->force_enc != 0)
14290 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14291# endif
14292
14293 newval = alloc(len + 1);
14294 if (newval == NULL)
14295 return NULL;
14296
14297 if (eap->force_bin == FORCE_BIN)
14298 sprintf((char *)newval, " ++bin");
14299 else if (eap->force_bin == FORCE_NOBIN)
14300 sprintf((char *)newval, " ++nobin");
14301 else
14302 *newval = NUL;
14303 if (eap->force_ff != 0)
14304 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14305 eap->cmd + eap->force_ff);
14306# ifdef FEAT_MBYTE
14307 if (eap->force_enc != 0)
14308 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14309 eap->cmd + eap->force_enc);
14310# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014311 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014312 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313}
14314#endif
14315
14316/*
14317 * Get the value of internal variable "name".
14318 * Return OK or FAIL.
14319 */
14320 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014321get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 char_u *name;
14323 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014324 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014325 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326{
14327 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *tv = NULL;
14329 typval_T atv;
14330 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332
14333 /* truncate the name, so that we can use strcmp() */
14334 cc = name[len];
14335 name[len] = NUL;
14336
14337 /*
14338 * Check for "b:changedtick".
14339 */
14340 if (STRCMP(name, "b:changedtick") == 0)
14341 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014342 atv.v_type = VAR_NUMBER;
14343 atv.vval.v_number = curbuf->b_changedtick;
14344 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 }
14346
14347 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 * Check for user-defined variables.
14349 */
14350 else
14351 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014352 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014354 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 }
14356
Bram Moolenaare9a41262005-01-15 22:18:47 +000014357 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014359 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 ret = FAIL;
14362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014364 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365
14366 name[len] = cc;
14367
14368 return ret;
14369}
14370
14371/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014372 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14373 * Also handle function call with Funcref variable: func(expr)
14374 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14375 */
14376 static int
14377handle_subscript(arg, rettv, evaluate, verbose)
14378 char_u **arg;
14379 typval_T *rettv;
14380 int evaluate; /* do more than finding the end */
14381 int verbose; /* give error messages */
14382{
14383 int ret = OK;
14384 dict_T *selfdict = NULL;
14385 char_u *s;
14386 int len;
14387
14388 while (ret == OK
14389 && (**arg == '['
14390 || (**arg == '.' && rettv->v_type == VAR_DICT)
14391 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14392 && !vim_iswhite(*(*arg - 1)))
14393 {
14394 if (**arg == '(')
14395 {
14396 s = rettv->vval.v_string;
14397
14398 /* Invoke the function. Recursive! */
14399 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14400 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14401 &len, evaluate, selfdict);
14402
14403 /* Stop the expression evaluation when immediately aborting on
14404 * error, or when an interrupt occurred or an exception was thrown
14405 * but not caught. */
14406 if (aborting())
14407 {
14408 if (ret == OK)
14409 clear_tv(rettv);
14410 ret = FAIL;
14411 }
14412 dict_unref(selfdict);
14413 selfdict = NULL;
14414 }
14415 else /* **arg == '[' || **arg == '.' */
14416 {
14417 dict_unref(selfdict);
14418 if (rettv->v_type == VAR_DICT)
14419 {
14420 selfdict = rettv->vval.v_dict;
14421 if (selfdict != NULL)
14422 ++selfdict->dv_refcount;
14423 }
14424 else
14425 selfdict = NULL;
14426 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14427 {
14428 clear_tv(rettv);
14429 ret = FAIL;
14430 }
14431 }
14432 }
14433 dict_unref(selfdict);
14434 return ret;
14435}
14436
14437/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014438 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14439 * value).
14440 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014441 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014443{
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014445}
14446
14447/*
14448 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 * The string "s" must have been allocated, it is consumed.
14450 * Return NULL for out of memory, the variable otherwise.
14451 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014452 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 char_u *s;
14455{
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv = alloc_tv();
14459 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->v_type = VAR_STRING;
14462 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 }
14464 else
14465 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467}
14468
14469/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014470 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 */
14472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014474 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 if (varp != NULL)
14477 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014478 switch (varp->v_type)
14479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014480 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014481 func_unref(varp->vval.v_string);
14482 /*FALLTHROUGH*/
14483 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484 vim_free(varp->vval.v_string);
14485 break;
14486 case VAR_LIST:
14487 list_unref(varp->vval.v_list);
14488 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014489 case VAR_DICT:
14490 dict_unref(varp->vval.v_dict);
14491 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014492 case VAR_NUMBER:
14493 case VAR_UNKNOWN:
14494 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014495 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014496 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014497 break;
14498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 vim_free(varp);
14500 }
14501}
14502
14503/*
14504 * Free the memory for a variable value and set the value to NULL or 0.
14505 */
14506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509{
14510 if (varp != NULL)
14511 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014512 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014514 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014515 func_unref(varp->vval.v_string);
14516 /*FALLTHROUGH*/
14517 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014518 vim_free(varp->vval.v_string);
14519 varp->vval.v_string = NULL;
14520 break;
14521 case VAR_LIST:
14522 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014523 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014524 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014525 case VAR_DICT:
14526 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014527 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014528 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014530 varp->vval.v_number = 0;
14531 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532 case VAR_UNKNOWN:
14533 break;
14534 default:
14535 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014537 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 }
14539}
14540
14541/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 * Set the value of a variable to NULL without freeing items.
14543 */
14544 static void
14545init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547{
14548 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014549 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550}
14551
14552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 * Get the number value of a variable.
14554 * If it is a String variable, uses vim_str2nr().
14555 */
14556 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014558 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014560 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014562 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014564 case VAR_NUMBER:
14565 n = (long)(varp->vval.v_number);
14566 break;
14567 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014568 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014569 break;
14570 case VAR_STRING:
14571 if (varp->vval.v_string != NULL)
14572 vim_str2nr(varp->vval.v_string, NULL, NULL,
14573 TRUE, TRUE, &n, NULL);
14574 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014575 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014576 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014577 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014578 case VAR_DICT:
14579 EMSG(_("E728: Using a Dictionary as a number"));
14580 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014581 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014582 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014583 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014585 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586}
14587
14588/*
14589 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14590 */
14591 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594{
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 linenr_T lnum;
14597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 if (lnum == 0) /* no valid number, try using line() */
14600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 rettv.v_type = VAR_NUMBER;
14602 f_line(argvars, &rettv);
14603 lnum = rettv.vval.v_number;
14604 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 }
14606 return lnum;
14607}
14608
14609/*
14610 * Get the string value of a variable.
14611 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014612 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14613 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 * If the String variable has never been set, return an empty string.
14615 * Never returns NULL;
14616 */
14617 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620{
14621 static char_u mybuf[NUMBUFLEN];
14622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624}
14625
14626 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014629 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014633 case VAR_NUMBER:
14634 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14635 return buf;
14636 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014637 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014638 break;
14639 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014640 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641 break;
14642 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014643 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014644 break;
14645 case VAR_STRING:
14646 if (varp->vval.v_string != NULL)
14647 return varp->vval.v_string;
14648 break;
14649 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014653 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654}
14655
14656/*
14657 * Find variable "name" in the list of variables.
14658 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014659 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014660 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014661 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014664find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaara7043832005-01-21 11:56:39 +000014671 ht = find_var_ht(name, &varname);
14672 if (htp != NULL)
14673 *htp = ht;
14674 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014676 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677}
14678
14679/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014681 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014684find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014686 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014687 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014688{
Bram Moolenaar33570922005-01-25 22:26:29 +000014689 hashitem_T *hi;
14690
14691 if (*varname == NUL)
14692 {
14693 /* Must be something like "s:", otherwise "ht" would be NULL. */
14694 switch (varname[-2])
14695 {
14696 case 's': return &SCRIPT_SV(current_SID).sv_var;
14697 case 'g': return &globvars_var;
14698 case 'v': return &vimvars_var;
14699 case 'b': return &curbuf->b_bufvar;
14700 case 'w': return &curwin->w_winvar;
14701 case 'l': return &current_funccal->l_vars_var;
14702 case 'a': return &current_funccal->l_avars_var;
14703 }
14704 return NULL;
14705 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014706
14707 hi = hash_find(ht, varname);
14708 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014709 {
14710 /* For global variables we may try auto-loading the script. If it
14711 * worked find the variable again. */
14712 if (ht == &globvarht && !writing
14713 && script_autoload(varname) && !aborting())
14714 hi = hash_find(ht, varname);
14715 if (HASHITEM_EMPTY(hi))
14716 return NULL;
14717 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014718 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014719}
14720
14721/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014723 * Set "varname" to the start of name without ':'.
14724 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014725 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014726find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 char_u *name;
14728 char_u **varname;
14729{
14730 if (name[1] != ':')
14731 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014732 /* The name must not start with a colon. */
14733 if (name[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 return NULL;
14735 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014736
14737 /* "version" is "v:version" in all scopes */
14738 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14739 return &compat_hashtab;
14740
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014742 return &globvarht; /* global variable */
14743 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 }
14745 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014746 if (*name == 'g') /* global variable */
14747 return &globvarht;
14748 /* There must be no ':' in the rest of the name, unless g: is used */
14749 if (vim_strchr(name + 2, ':') != NULL)
14750 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014755 if (*name == 'v') /* v: variable */
14756 return &vimvarht;
14757 if (*name == 'a' && current_funccal != NULL) /* function argument */
14758 return &current_funccal->l_avars.dv_hashtab;
14759 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14760 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 if (*name == 's' /* script variable */
14762 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14763 return &SCRIPT_VARS(current_SID);
14764 return NULL;
14765}
14766
14767/*
14768 * Get the string value of a (global/local) variable.
14769 * Returns NULL when it doesn't exist.
14770 */
14771 char_u *
14772get_var_value(name)
14773 char_u *name;
14774{
Bram Moolenaar33570922005-01-25 22:26:29 +000014775 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776
Bram Moolenaara7043832005-01-21 11:56:39 +000014777 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 if (v == NULL)
14779 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781}
14782
14783/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 * sourcing this script and when executing functions defined in the script.
14786 */
14787 void
14788new_script_vars(id)
14789 scid_T id;
14790{
Bram Moolenaara7043832005-01-21 11:56:39 +000014791 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 hashtab_T *ht;
14793 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014794
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14796 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014797 /* Re-allocating ga_data means that an ht_array pointing to
14798 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014799 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014800 for (i = 1; i <= ga_scripts.ga_len; ++i)
14801 {
14802 ht = &SCRIPT_VARS(i);
14803 if (ht->ht_mask == HT_INIT_SIZE - 1)
14804 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 sv = &SCRIPT_SV(i);
14806 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014807 }
14808
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 while (ga_scripts.ga_len < id)
14810 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014811 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14812 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 }
14815 }
14816}
14817
14818/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014819 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14820 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 */
14822 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014823init_var_dict(dict, dict_var)
14824 dict_T *dict;
14825 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826{
Bram Moolenaar33570922005-01-25 22:26:29 +000014827 hash_init(&dict->dv_hashtab);
14828 dict->dv_refcount = 99999;
14829 dict_var->di_tv.vval.v_dict = dict;
14830 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014831 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014832 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14833 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834}
14835
14836/*
14837 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014838 * Frees all allocated variables and the value they contain.
14839 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 */
14841 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014842vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014843 hashtab_T *ht;
14844{
14845 vars_clear_ext(ht, TRUE);
14846}
14847
14848/*
14849 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14850 */
14851 static void
14852vars_clear_ext(ht, free_val)
14853 hashtab_T *ht;
14854 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855{
Bram Moolenaara7043832005-01-21 11:56:39 +000014856 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 hashitem_T *hi;
14858 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859
Bram Moolenaar33570922005-01-25 22:26:29 +000014860 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014861 todo = ht->ht_used;
14862 for (hi = ht->ht_array; todo > 0; ++hi)
14863 {
14864 if (!HASHITEM_EMPTY(hi))
14865 {
14866 --todo;
14867
Bram Moolenaar33570922005-01-25 22:26:29 +000014868 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014869 * ht_array might change then. hash_clear() takes care of it
14870 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014871 v = HI2DI(hi);
14872 if (free_val)
14873 clear_tv(&v->di_tv);
14874 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14875 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014876 }
14877 }
14878 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879}
14880
Bram Moolenaara7043832005-01-21 11:56:39 +000014881/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 * Delete a variable from hashtab "ht" at item "hi".
14883 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014886delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014887 hashtab_T *ht;
14888 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889{
Bram Moolenaar33570922005-01-25 22:26:29 +000014890 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014891
14892 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 clear_tv(&di->di_tv);
14894 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895}
14896
14897/*
14898 * List the value of one internal variable.
14899 */
14900 static void
14901list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014902 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 char_u *prefix;
14904{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014905 char_u *tofree;
14906 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014907 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014908
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 s = echo_string(&v->di_tv, &tofree, numbuf);
14910 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014911 s == NULL ? (char_u *)"" : s);
14912 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913}
14914
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 static void
14916list_one_var_a(prefix, name, type, string)
14917 char_u *prefix;
14918 char_u *name;
14919 int type;
14920 char_u *string;
14921{
14922 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14923 if (name != NULL) /* "a:" vars don't have a name stored */
14924 msg_puts(name);
14925 msg_putchar(' ');
14926 msg_advance(22);
14927 if (type == VAR_NUMBER)
14928 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014929 else if (type == VAR_FUNC)
14930 msg_putchar('*');
14931 else if (type == VAR_LIST)
14932 {
14933 msg_putchar('[');
14934 if (*string == '[')
14935 ++string;
14936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014937 else if (type == VAR_DICT)
14938 {
14939 msg_putchar('{');
14940 if (*string == '{')
14941 ++string;
14942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 else
14944 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014945
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014947
14948 if (type == VAR_FUNC)
14949 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950}
14951
14952/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 * If the variable already exists, the value is updated.
14955 * Otherwise the variable is created.
14956 */
14957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014960 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962{
Bram Moolenaar33570922005-01-25 22:26:29 +000014963 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014965 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014968 {
14969 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14970 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14971 ? name[2] : name[0]))
14972 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014973 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014974 return;
14975 }
14976 if (function_exists(name))
14977 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014978 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14979 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014980 return;
14981 }
14982 }
14983
Bram Moolenaara7043832005-01-21 11:56:39 +000014984 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014986 {
14987 EMSG2(_("E461: Illegal variable name: %s"), name);
14988 return;
14989 }
14990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014991 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014992 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014994 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014995 if (var_check_ro(v->di_flags, name)
14996 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 return;
14998 if (v->di_tv.v_type != tv->v_type
14999 && !((v->di_tv.v_type == VAR_STRING
15000 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001 && (tv->v_type == VAR_STRING
15002 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015003 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015004 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015005 return;
15006 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015007
15008 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015009 * Handle setting internal v: variables separately: we don't change
15010 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015011 */
15012 if (ht == &vimvarht)
15013 {
15014 if (v->di_tv.v_type == VAR_STRING)
15015 {
15016 vim_free(v->di_tv.vval.v_string);
15017 if (copy || tv->v_type != VAR_STRING)
15018 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15019 else
15020 {
15021 /* Take over the string to avoid an extra alloc/free. */
15022 v->di_tv.vval.v_string = tv->vval.v_string;
15023 tv->vval.v_string = NULL;
15024 }
15025 }
15026 else if (v->di_tv.v_type != VAR_NUMBER)
15027 EMSG2(_(e_intern2), "set_var()");
15028 else
15029 v->di_tv.vval.v_number = get_tv_number(tv);
15030 return;
15031 }
15032
15033 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 }
15035 else /* add a new variable */
15036 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015037 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15038 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015039 if (v == NULL)
15040 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015042 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015044 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 return;
15046 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015047 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015052 else
15053 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015055 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015061 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15062 * Also give an error message.
15063 */
15064 static int
15065var_check_ro(flags, name)
15066 int flags;
15067 char_u *name;
15068{
15069 if (flags & DI_FLAGS_RO)
15070 {
15071 EMSG2(_(e_readonlyvar), name);
15072 return TRUE;
15073 }
15074 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15075 {
15076 EMSG2(_(e_readonlysbx), name);
15077 return TRUE;
15078 }
15079 return FALSE;
15080}
15081
15082/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015083 * Return TRUE if typeval "tv" is set to be locked (immutable).
15084 * Also give an error message, using "name".
15085 */
15086 static int
15087tv_check_lock(lock, name)
15088 int lock;
15089 char_u *name;
15090{
15091 if (lock & VAR_LOCKED)
15092 {
15093 EMSG2(_("E741: Value is locked: %s"),
15094 name == NULL ? (char_u *)_("Unknown") : name);
15095 return TRUE;
15096 }
15097 if (lock & VAR_FIXED)
15098 {
15099 EMSG2(_("E742: Cannot change value of %s"),
15100 name == NULL ? (char_u *)_("Unknown") : name);
15101 return TRUE;
15102 }
15103 return FALSE;
15104}
15105
15106/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015107 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015108 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015109 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015112copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015113 typval_T *from;
15114 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015116 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015117 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015118 switch (from->v_type)
15119 {
15120 case VAR_NUMBER:
15121 to->vval.v_number = from->vval.v_number;
15122 break;
15123 case VAR_STRING:
15124 case VAR_FUNC:
15125 if (from->vval.v_string == NULL)
15126 to->vval.v_string = NULL;
15127 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015128 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015129 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015130 if (from->v_type == VAR_FUNC)
15131 func_ref(to->vval.v_string);
15132 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015133 break;
15134 case VAR_LIST:
15135 if (from->vval.v_list == NULL)
15136 to->vval.v_list = NULL;
15137 else
15138 {
15139 to->vval.v_list = from->vval.v_list;
15140 ++to->vval.v_list->lv_refcount;
15141 }
15142 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015143 case VAR_DICT:
15144 if (from->vval.v_dict == NULL)
15145 to->vval.v_dict = NULL;
15146 else
15147 {
15148 to->vval.v_dict = from->vval.v_dict;
15149 ++to->vval.v_dict->dv_refcount;
15150 }
15151 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015152 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015153 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015154 break;
15155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156}
15157
15158/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015159 * Make a copy of an item.
15160 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015161 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15162 * reference to an already copied list/dict can be used.
15163 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015164 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015165 static int
15166item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015167 typval_T *from;
15168 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015169 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015170 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015171{
15172 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015173 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015174
Bram Moolenaar33570922005-01-25 22:26:29 +000015175 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015176 {
15177 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015178 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015179 }
15180 ++recurse;
15181
15182 switch (from->v_type)
15183 {
15184 case VAR_NUMBER:
15185 case VAR_STRING:
15186 case VAR_FUNC:
15187 copy_tv(from, to);
15188 break;
15189 case VAR_LIST:
15190 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015191 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015192 if (from->vval.v_list == NULL)
15193 to->vval.v_list = NULL;
15194 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15195 {
15196 /* use the copy made earlier */
15197 to->vval.v_list = from->vval.v_list->lv_copylist;
15198 ++to->vval.v_list->lv_refcount;
15199 }
15200 else
15201 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15202 if (to->vval.v_list == NULL)
15203 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015204 break;
15205 case VAR_DICT:
15206 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015207 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015208 if (from->vval.v_dict == NULL)
15209 to->vval.v_dict = NULL;
15210 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15211 {
15212 /* use the copy made earlier */
15213 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15214 ++to->vval.v_dict->dv_refcount;
15215 }
15216 else
15217 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15218 if (to->vval.v_dict == NULL)
15219 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015220 break;
15221 default:
15222 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015223 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015224 }
15225 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015226 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015227}
15228
15229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 * ":echo expr1 ..." print each argument separated with a space, add a
15231 * newline at the end.
15232 * ":echon expr1 ..." print each argument plain.
15233 */
15234 void
15235ex_echo(eap)
15236 exarg_T *eap;
15237{
15238 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015239 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 char_u *p;
15242 int needclr = TRUE;
15243 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015244 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245
15246 if (eap->skip)
15247 ++emsg_skip;
15248 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15249 {
15250 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 {
15253 /*
15254 * Report the invalid expression unless the expression evaluation
15255 * has been cancelled due to an aborting error, an interrupt, or an
15256 * exception.
15257 */
15258 if (!aborting())
15259 EMSG2(_(e_invexpr2), p);
15260 break;
15261 }
15262 if (!eap->skip)
15263 {
15264 if (atstart)
15265 {
15266 atstart = FALSE;
15267 /* Call msg_start() after eval1(), evaluating the expression
15268 * may cause a message to appear. */
15269 if (eap->cmdidx == CMD_echo)
15270 msg_start();
15271 }
15272 else if (eap->cmdidx == CMD_echo)
15273 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015274 p = echo_string(&rettv, &tofree, numbuf);
15275 if (p != NULL)
15276 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015278 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015280 if (*p != TAB && needclr)
15281 {
15282 /* remove any text still there from the command */
15283 msg_clr_eos();
15284 needclr = FALSE;
15285 }
15286 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 }
15288 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015289 {
15290#ifdef FEAT_MBYTE
15291 if (has_mbyte)
15292 {
15293 int i = (*mb_ptr2len_check)(p);
15294
15295 (void)msg_outtrans_len_attr(p, i, echo_attr);
15296 p += i - 1;
15297 }
15298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015300 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 arg = skipwhite(arg);
15307 }
15308 eap->nextcmd = check_nextcmd(arg);
15309
15310 if (eap->skip)
15311 --emsg_skip;
15312 else
15313 {
15314 /* remove text that may still be there from the command */
15315 if (needclr)
15316 msg_clr_eos();
15317 if (eap->cmdidx == CMD_echo)
15318 msg_end();
15319 }
15320}
15321
15322/*
15323 * ":echohl {name}".
15324 */
15325 void
15326ex_echohl(eap)
15327 exarg_T *eap;
15328{
15329 int id;
15330
15331 id = syn_name2id(eap->arg);
15332 if (id == 0)
15333 echo_attr = 0;
15334 else
15335 echo_attr = syn_id2attr(id);
15336}
15337
15338/*
15339 * ":execute expr1 ..." execute the result of an expression.
15340 * ":echomsg expr1 ..." Print a message
15341 * ":echoerr expr1 ..." Print an error
15342 * Each gets spaces around each argument and a newline at the end for
15343 * echo commands
15344 */
15345 void
15346ex_execute(eap)
15347 exarg_T *eap;
15348{
15349 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 int ret = OK;
15352 char_u *p;
15353 garray_T ga;
15354 int len;
15355 int save_did_emsg;
15356
15357 ga_init2(&ga, 1, 80);
15358
15359 if (eap->skip)
15360 ++emsg_skip;
15361 while (*arg != NUL && *arg != '|' && *arg != '\n')
15362 {
15363 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 {
15366 /*
15367 * Report the invalid expression unless the expression evaluation
15368 * has been cancelled due to an aborting error, an interrupt, or an
15369 * exception.
15370 */
15371 if (!aborting())
15372 EMSG2(_(e_invexpr2), p);
15373 ret = FAIL;
15374 break;
15375 }
15376
15377 if (!eap->skip)
15378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015379 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 len = (int)STRLEN(p);
15381 if (ga_grow(&ga, len + 2) == FAIL)
15382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 ret = FAIL;
15385 break;
15386 }
15387 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 ga.ga_len += len;
15391 }
15392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 arg = skipwhite(arg);
15395 }
15396
15397 if (ret != FAIL && ga.ga_data != NULL)
15398 {
15399 if (eap->cmdidx == CMD_echomsg)
15400 MSG_ATTR(ga.ga_data, echo_attr);
15401 else if (eap->cmdidx == CMD_echoerr)
15402 {
15403 /* We don't want to abort following commands, restore did_emsg. */
15404 save_did_emsg = did_emsg;
15405 EMSG((char_u *)ga.ga_data);
15406 if (!force_abort)
15407 did_emsg = save_did_emsg;
15408 }
15409 else if (eap->cmdidx == CMD_execute)
15410 do_cmdline((char_u *)ga.ga_data,
15411 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15412 }
15413
15414 ga_clear(&ga);
15415
15416 if (eap->skip)
15417 --emsg_skip;
15418
15419 eap->nextcmd = check_nextcmd(arg);
15420}
15421
15422/*
15423 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15424 * "arg" points to the "&" or '+' when called, to "option" when returning.
15425 * Returns NULL when no option name found. Otherwise pointer to the char
15426 * after the option name.
15427 */
15428 static char_u *
15429find_option_end(arg, opt_flags)
15430 char_u **arg;
15431 int *opt_flags;
15432{
15433 char_u *p = *arg;
15434
15435 ++p;
15436 if (*p == 'g' && p[1] == ':')
15437 {
15438 *opt_flags = OPT_GLOBAL;
15439 p += 2;
15440 }
15441 else if (*p == 'l' && p[1] == ':')
15442 {
15443 *opt_flags = OPT_LOCAL;
15444 p += 2;
15445 }
15446 else
15447 *opt_flags = 0;
15448
15449 if (!ASCII_ISALPHA(*p))
15450 return NULL;
15451 *arg = p;
15452
15453 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15454 p += 4; /* termcap option */
15455 else
15456 while (ASCII_ISALPHA(*p))
15457 ++p;
15458 return p;
15459}
15460
15461/*
15462 * ":function"
15463 */
15464 void
15465ex_function(eap)
15466 exarg_T *eap;
15467{
15468 char_u *theline;
15469 int j;
15470 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 char_u *name = NULL;
15473 char_u *p;
15474 char_u *arg;
15475 garray_T newargs;
15476 garray_T newlines;
15477 int varargs = FALSE;
15478 int mustend = FALSE;
15479 int flags = 0;
15480 ufunc_T *fp;
15481 int indent;
15482 int nesting;
15483 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 dictitem_T *v;
15485 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015486 static int func_nr = 0; /* number for nameless function */
15487 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015488 hashtab_T *ht;
15489 int todo;
15490 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491
15492 /*
15493 * ":function" without argument: list functions.
15494 */
15495 if (ends_excmd(*eap->arg))
15496 {
15497 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015498 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015499 todo = func_hashtab.ht_used;
15500 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015501 {
15502 if (!HASHITEM_EMPTY(hi))
15503 {
15504 --todo;
15505 fp = HI2UF(hi);
15506 if (!isdigit(*fp->uf_name))
15507 list_func_head(fp, FALSE);
15508 }
15509 }
15510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 eap->nextcmd = check_nextcmd(eap->arg);
15512 return;
15513 }
15514
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015515 /*
15516 * Get the function name. There are these situations:
15517 * func normal function name
15518 * "name" == func, "fudi.fd_dict" == NULL
15519 * dict.func new dictionary entry
15520 * "name" == NULL, "fudi.fd_dict" set,
15521 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15522 * dict.func existing dict entry with a Funcref
15523 * "name" == fname, "fudi.fd_dict" set,
15524 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15525 * dict.func existing dict entry that's not a Funcref
15526 * "name" == NULL, "fudi.fd_dict" set,
15527 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015530 name = trans_function_name(&p, eap->skip, 0, &fudi);
15531 paren = (vim_strchr(p, '(') != NULL);
15532 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 {
15534 /*
15535 * Return on an invalid expression in braces, unless the expression
15536 * evaluation has been cancelled due to an aborting error, an
15537 * interrupt, or an exception.
15538 */
15539 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015540 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015541 if (!eap->skip && fudi.fd_newkey != NULL)
15542 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015543 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 else
15547 eap->skip = TRUE;
15548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 /* An error in a function call during evaluation of an expression in magic
15550 * braces should not cause the function not to be defined. */
15551 saved_did_emsg = did_emsg;
15552 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553
15554 /*
15555 * ":function func" with only function name: list function.
15556 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015557 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 {
15559 if (!ends_excmd(*skipwhite(p)))
15560 {
15561 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015562 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 }
15564 eap->nextcmd = check_nextcmd(p);
15565 if (eap->nextcmd != NULL)
15566 *p = NUL;
15567 if (!eap->skip && !got_int)
15568 {
15569 fp = find_func(name);
15570 if (fp != NULL)
15571 {
15572 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015573 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 {
15575 msg_putchar('\n');
15576 msg_outnum((long)(j + 1));
15577 if (j < 9)
15578 msg_putchar(' ');
15579 if (j < 99)
15580 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015581 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 out_flush(); /* show a line at a time */
15583 ui_breakcheck();
15584 }
15585 if (!got_int)
15586 {
15587 msg_putchar('\n');
15588 msg_puts((char_u *)" endfunction");
15589 }
15590 }
15591 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015592 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015594 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 }
15596
15597 /*
15598 * ":function name(arg1, arg2)" Define function.
15599 */
15600 p = skipwhite(p);
15601 if (*p != '(')
15602 {
15603 if (!eap->skip)
15604 {
15605 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015606 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 }
15608 /* attempt to continue by skipping some text */
15609 if (vim_strchr(p, '(') != NULL)
15610 p = vim_strchr(p, '(');
15611 }
15612 p = skipwhite(p + 1);
15613
15614 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15615 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15616
15617 /*
15618 * Isolate the arguments: "arg1, arg2, ...)"
15619 */
15620 while (*p != ')')
15621 {
15622 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15623 {
15624 varargs = TRUE;
15625 p += 3;
15626 mustend = TRUE;
15627 }
15628 else
15629 {
15630 arg = p;
15631 while (ASCII_ISALNUM(*p) || *p == '_')
15632 ++p;
15633 if (arg == p || isdigit(*arg)
15634 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15635 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15636 {
15637 if (!eap->skip)
15638 EMSG2(_("E125: Illegal argument: %s"), arg);
15639 break;
15640 }
15641 if (ga_grow(&newargs, 1) == FAIL)
15642 goto erret;
15643 c = *p;
15644 *p = NUL;
15645 arg = vim_strsave(arg);
15646 if (arg == NULL)
15647 goto erret;
15648 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15649 *p = c;
15650 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 if (*p == ',')
15652 ++p;
15653 else
15654 mustend = TRUE;
15655 }
15656 p = skipwhite(p);
15657 if (mustend && *p != ')')
15658 {
15659 if (!eap->skip)
15660 EMSG2(_(e_invarg2), eap->arg);
15661 break;
15662 }
15663 }
15664 ++p; /* skip the ')' */
15665
Bram Moolenaare9a41262005-01-15 22:18:47 +000015666 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 for (;;)
15668 {
15669 p = skipwhite(p);
15670 if (STRNCMP(p, "range", 5) == 0)
15671 {
15672 flags |= FC_RANGE;
15673 p += 5;
15674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015675 else if (STRNCMP(p, "dict", 4) == 0)
15676 {
15677 flags |= FC_DICT;
15678 p += 4;
15679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 else if (STRNCMP(p, "abort", 5) == 0)
15681 {
15682 flags |= FC_ABORT;
15683 p += 5;
15684 }
15685 else
15686 break;
15687 }
15688
15689 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15690 EMSG(_(e_trailing));
15691
15692 /*
15693 * Read the body of the function, until ":endfunction" is found.
15694 */
15695 if (KeyTyped)
15696 {
15697 /* Check if the function already exists, don't let the user type the
15698 * whole function before telling him it doesn't work! For a script we
15699 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015700 if (!eap->skip && !eap->forceit)
15701 {
15702 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15703 EMSG(_(e_funcdict));
15704 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015705 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707
15708 msg_putchar('\n'); /* don't overwrite the function name */
15709 cmdline_row = msg_row;
15710 }
15711
15712 indent = 2;
15713 nesting = 0;
15714 for (;;)
15715 {
15716 msg_scroll = TRUE;
15717 need_wait_return = FALSE;
15718 if (eap->getline == NULL)
15719 theline = getcmdline(':', 0L, indent);
15720 else
15721 theline = eap->getline(':', eap->cookie, indent);
15722 if (KeyTyped)
15723 lines_left = Rows - 1;
15724 if (theline == NULL)
15725 {
15726 EMSG(_("E126: Missing :endfunction"));
15727 goto erret;
15728 }
15729
15730 if (skip_until != NULL)
15731 {
15732 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15733 * don't check for ":endfunc". */
15734 if (STRCMP(theline, skip_until) == 0)
15735 {
15736 vim_free(skip_until);
15737 skip_until = NULL;
15738 }
15739 }
15740 else
15741 {
15742 /* skip ':' and blanks*/
15743 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15744 ;
15745
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015746 /* Check for "endfunction". */
15747 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 {
15749 vim_free(theline);
15750 break;
15751 }
15752
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015753 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 * at "end". */
15755 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15756 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015757 else if (STRNCMP(p, "if", 2) == 0
15758 || STRNCMP(p, "wh", 2) == 0
15759 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 || STRNCMP(p, "try", 3) == 0)
15761 indent += 2;
15762
15763 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015766 if (*p == '!')
15767 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 p += eval_fname_script(p);
15769 if (ASCII_ISALPHA(*p))
15770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015771 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 if (*skipwhite(p) == '(')
15773 {
15774 ++nesting;
15775 indent += 2;
15776 }
15777 }
15778 }
15779
15780 /* Check for ":append" or ":insert". */
15781 p = skip_range(p, NULL);
15782 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15783 || (p[0] == 'i'
15784 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15785 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15786 skip_until = vim_strsave((char_u *)".");
15787
15788 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15789 arg = skipwhite(skiptowhite(p));
15790 if (arg[0] == '<' && arg[1] =='<'
15791 && ((p[0] == 'p' && p[1] == 'y'
15792 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15793 || (p[0] == 'p' && p[1] == 'e'
15794 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15795 || (p[0] == 't' && p[1] == 'c'
15796 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15797 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15798 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015799 || (p[0] == 'm' && p[1] == 'z'
15800 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 ))
15802 {
15803 /* ":python <<" continues until a dot, like ":append" */
15804 p = skipwhite(arg + 2);
15805 if (*p == NUL)
15806 skip_until = vim_strsave((char_u *)".");
15807 else
15808 skip_until = vim_strsave(p);
15809 }
15810 }
15811
15812 /* Add the line to the function. */
15813 if (ga_grow(&newlines, 1) == FAIL)
15814 goto erret;
15815 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15816 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 }
15818
15819 /* Don't define the function when skipping commands or when an error was
15820 * detected. */
15821 if (eap->skip || did_emsg)
15822 goto erret;
15823
15824 /*
15825 * If there are no errors, add the function
15826 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015827 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015828 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015829 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015831 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015832 emsg_funcname("E707: Function name conflicts with variable: %s",
15833 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015834 goto erret;
15835 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015837 fp = find_func(name);
15838 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015840 if (!eap->forceit)
15841 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015842 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015843 goto erret;
15844 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015845 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015846 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015847 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015848 name);
15849 goto erret;
15850 }
15851 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015852 ga_clear_strings(&(fp->uf_args));
15853 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015854 vim_free(name);
15855 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 }
15858 else
15859 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015860 char numbuf[20];
15861
15862 fp = NULL;
15863 if (fudi.fd_newkey == NULL && !eap->forceit)
15864 {
15865 EMSG(_(e_funcdict));
15866 goto erret;
15867 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015868 if (fudi.fd_di == NULL)
15869 {
15870 /* Can't add a function to a locked dictionary */
15871 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15872 goto erret;
15873 }
15874 /* Can't change an existing function if it is locked */
15875 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15876 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015877
15878 /* Give the function a sequential number. Can only be used with a
15879 * Funcref! */
15880 vim_free(name);
15881 sprintf(numbuf, "%d", ++func_nr);
15882 name = vim_strsave((char_u *)numbuf);
15883 if (name == NULL)
15884 goto erret;
15885 }
15886
15887 if (fp == NULL)
15888 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015889 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL)
15890 {
15891 int slen, plen;
15892 char_u *scriptname;
15893
15894 /* Check that the autoload name matches the script name. */
15895 j = FAIL;
15896 if (sourcing_name != NULL)
15897 {
15898 scriptname = autoload_name(name);
15899 if (scriptname != NULL)
15900 {
15901 p = vim_strchr(scriptname, '/');
15902 plen = STRLEN(p);
15903 slen = STRLEN(sourcing_name);
15904 if (slen > plen && fnamecmp(p,
15905 sourcing_name + slen - plen) == 0)
15906 j = OK;
15907 vim_free(scriptname);
15908 }
15909 }
15910 if (j == FAIL)
15911 {
15912 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15913 goto erret;
15914 }
15915 }
15916
15917 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 if (fp == NULL)
15919 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015920
15921 if (fudi.fd_dict != NULL)
15922 {
15923 if (fudi.fd_di == NULL)
15924 {
15925 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015926 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015927 if (fudi.fd_di == NULL)
15928 {
15929 vim_free(fp);
15930 goto erret;
15931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015932 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15933 {
15934 vim_free(fudi.fd_di);
15935 goto erret;
15936 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015937 }
15938 else
15939 /* overwrite existing dict entry */
15940 clear_tv(&fudi.fd_di->di_tv);
15941 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015942 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015943 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015944 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015945 }
15946
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015948 STRCPY(fp->uf_name, name);
15949 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015951 fp->uf_args = newargs;
15952 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015953#ifdef FEAT_PROFILE
15954 fp->uf_tml_count = NULL;
15955 fp->uf_tml_total = NULL;
15956 fp->uf_tml_self = NULL;
15957 fp->uf_profiling = FALSE;
15958 if (prof_def_func())
15959 func_do_profile(fp);
15960#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015961 fp->uf_varargs = varargs;
15962 fp->uf_flags = flags;
15963 fp->uf_calls = 0;
15964 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015965 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966
15967erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 ga_clear_strings(&newargs);
15969 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015970ret_free:
15971 vim_free(skip_until);
15972 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975}
15976
15977/*
15978 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015979 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015981 * flags:
15982 * TFN_INT: internal function name OK
15983 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 * Advances "pp" to just after the function name (if no error).
15985 */
15986 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015987trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 char_u **pp;
15989 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015990 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015993 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 char_u *start;
15995 char_u *end;
15996 int lead;
15997 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015999 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016000
16001 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016004
16005 /* Check for hard coded <SNR>: already translated function ID (from a user
16006 * command). */
16007 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16008 && (*pp)[2] == (int)KE_SNR)
16009 {
16010 *pp += 3;
16011 len = get_id_len(pp) + 3;
16012 return vim_strnsave(start, len);
16013 }
16014
16015 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16016 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016018 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016020
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016021 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016022 if (end == start)
16023 {
16024 if (!skip)
16025 EMSG(_("E129: Function name required"));
16026 goto theend;
16027 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016028 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016029 {
16030 /*
16031 * Report an invalid expression in braces, unless the expression
16032 * evaluation has been cancelled due to an aborting error, an
16033 * interrupt, or an exception.
16034 */
16035 if (!aborting())
16036 {
16037 if (end != NULL)
16038 EMSG2(_(e_invarg2), start);
16039 }
16040 else
16041 *pp = find_name_end(start, NULL, NULL, TRUE);
16042 goto theend;
16043 }
16044
16045 if (lv.ll_tv != NULL)
16046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016047 if (fdp != NULL)
16048 {
16049 fdp->fd_dict = lv.ll_dict;
16050 fdp->fd_newkey = lv.ll_newkey;
16051 lv.ll_newkey = NULL;
16052 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016054 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16055 {
16056 name = vim_strsave(lv.ll_tv->vval.v_string);
16057 *pp = end;
16058 }
16059 else
16060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016061 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16062 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016063 EMSG(_(e_funcref));
16064 else
16065 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016066 name = NULL;
16067 }
16068 goto theend;
16069 }
16070
16071 if (lv.ll_name == NULL)
16072 {
16073 /* Error found, but continue after the function name. */
16074 *pp = end;
16075 goto theend;
16076 }
16077
16078 if (lv.ll_exp_name != NULL)
16079 len = STRLEN(lv.ll_exp_name);
16080 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016081 {
16082 if (lead == 2) /* skip over "s:" */
16083 lv.ll_name += 2;
16084 len = (int)(end - lv.ll_name);
16085 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016086
16087 /*
16088 * Copy the function name to allocated memory.
16089 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16090 * Accept <SNR>123_name() outside a script.
16091 */
16092 if (skip)
16093 lead = 0; /* do nothing */
16094 else if (lead > 0)
16095 {
16096 lead = 3;
16097 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16098 {
16099 if (current_SID <= 0)
16100 {
16101 EMSG(_(e_usingsid));
16102 goto theend;
16103 }
16104 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16105 lead += (int)STRLEN(sid_buf);
16106 }
16107 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016108 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016110 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016111 goto theend;
16112 }
16113 name = alloc((unsigned)(len + lead + 1));
16114 if (name != NULL)
16115 {
16116 if (lead > 0)
16117 {
16118 name[0] = K_SPECIAL;
16119 name[1] = KS_EXTRA;
16120 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016121 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016122 STRCPY(name + 3, sid_buf);
16123 }
16124 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16125 name[len + lead] = NUL;
16126 }
16127 *pp = end;
16128
16129theend:
16130 clear_lval(&lv);
16131 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132}
16133
16134/*
16135 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16136 * Return 2 if "p" starts with "s:".
16137 * Return 0 otherwise.
16138 */
16139 static int
16140eval_fname_script(p)
16141 char_u *p;
16142{
16143 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16144 || STRNICMP(p + 1, "SNR>", 4) == 0))
16145 return 5;
16146 if (p[0] == 's' && p[1] == ':')
16147 return 2;
16148 return 0;
16149}
16150
16151/*
16152 * Return TRUE if "p" starts with "<SID>" or "s:".
16153 * Only works if eval_fname_script() returned non-zero for "p"!
16154 */
16155 static int
16156eval_fname_sid(p)
16157 char_u *p;
16158{
16159 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16160}
16161
16162/*
16163 * List the head of the function: "name(arg1, arg2)".
16164 */
16165 static void
16166list_func_head(fp, indent)
16167 ufunc_T *fp;
16168 int indent;
16169{
16170 int j;
16171
16172 msg_start();
16173 if (indent)
16174 MSG_PUTS(" ");
16175 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016176 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 {
16178 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016179 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 }
16181 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016182 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016184 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 {
16186 if (j)
16187 MSG_PUTS(", ");
16188 msg_puts(FUNCARG(fp, j));
16189 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016190 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 {
16192 if (j)
16193 MSG_PUTS(", ");
16194 MSG_PUTS("...");
16195 }
16196 msg_putchar(')');
16197}
16198
16199/*
16200 * Find a function by name, return pointer to it in ufuncs.
16201 * Return NULL for unknown function.
16202 */
16203 static ufunc_T *
16204find_func(name)
16205 char_u *name;
16206{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016207 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016209 hi = hash_find(&func_hashtab, name);
16210 if (!HASHITEM_EMPTY(hi))
16211 return HI2UF(hi);
16212 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213}
16214
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016215/*
16216 * Return TRUE if a function "name" exists.
16217 */
16218 static int
16219function_exists(name)
16220 char_u *name;
16221{
16222 char_u *p = name;
16223 int n = FALSE;
16224
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016225 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016226 if (p != NULL)
16227 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016228 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016229 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016230 else
16231 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016232 vim_free(p);
16233 }
16234 return n;
16235}
16236
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016237/*
16238 * Return TRUE if "name" looks like a builtin function name: starts with a
16239 * lower case letter and doesn't contain a ':'.
16240 */
16241 static int
16242builtin_function(name)
16243 char_u *name;
16244{
16245 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
16246}
16247
Bram Moolenaar05159a02005-02-26 23:04:13 +000016248#if defined(FEAT_PROFILE) || defined(PROTO)
16249/*
16250 * Start profiling function "fp".
16251 */
16252 static void
16253func_do_profile(fp)
16254 ufunc_T *fp;
16255{
16256 fp->uf_tm_count = 0;
16257 profile_zero(&fp->uf_tm_self);
16258 profile_zero(&fp->uf_tm_total);
16259 if (fp->uf_tml_count == NULL)
16260 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16261 (sizeof(int) * fp->uf_lines.ga_len));
16262 if (fp->uf_tml_total == NULL)
16263 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16264 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16265 if (fp->uf_tml_self == NULL)
16266 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16267 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16268 fp->uf_tml_idx = -1;
16269 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16270 || fp->uf_tml_self == NULL)
16271 return; /* out of memory */
16272
16273 fp->uf_profiling = TRUE;
16274}
16275
16276/*
16277 * Dump the profiling results for all functions in file "fd".
16278 */
16279 void
16280func_dump_profile(fd)
16281 FILE *fd;
16282{
16283 hashitem_T *hi;
16284 int todo;
16285 ufunc_T *fp;
16286 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016287 ufunc_T **sorttab;
16288 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016289
16290 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016291 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16292
Bram Moolenaar05159a02005-02-26 23:04:13 +000016293 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16294 {
16295 if (!HASHITEM_EMPTY(hi))
16296 {
16297 --todo;
16298 fp = HI2UF(hi);
16299 if (fp->uf_profiling)
16300 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016301 if (sorttab != NULL)
16302 sorttab[st_len++] = fp;
16303
Bram Moolenaar05159a02005-02-26 23:04:13 +000016304 if (fp->uf_name[0] == K_SPECIAL)
16305 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16306 else
16307 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16308 if (fp->uf_tm_count == 1)
16309 fprintf(fd, "Called 1 time\n");
16310 else
16311 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16312 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16313 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16314 fprintf(fd, "\n");
16315 fprintf(fd, "count total (s) self (s)\n");
16316
16317 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16318 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016319 prof_func_line(fd, fp->uf_tml_count[i],
16320 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016321 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16322 }
16323 fprintf(fd, "\n");
16324 }
16325 }
16326 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016327
16328 if (sorttab != NULL && st_len > 0)
16329 {
16330 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16331 prof_total_cmp);
16332 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16333 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16334 prof_self_cmp);
16335 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16336 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016337}
Bram Moolenaar73830342005-02-28 22:48:19 +000016338
16339 static void
16340prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16341 FILE *fd;
16342 ufunc_T **sorttab;
16343 int st_len;
16344 char *title;
16345 int prefer_self; /* when equal print only self time */
16346{
16347 int i;
16348 ufunc_T *fp;
16349
16350 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16351 fprintf(fd, "count total (s) self (s) function\n");
16352 for (i = 0; i < 20 && i < st_len; ++i)
16353 {
16354 fp = sorttab[i];
16355 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16356 prefer_self);
16357 if (fp->uf_name[0] == K_SPECIAL)
16358 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16359 else
16360 fprintf(fd, " %s()\n", fp->uf_name);
16361 }
16362 fprintf(fd, "\n");
16363}
16364
16365/*
16366 * Print the count and times for one function or function line.
16367 */
16368 static void
16369prof_func_line(fd, count, total, self, prefer_self)
16370 FILE *fd;
16371 int count;
16372 proftime_T *total;
16373 proftime_T *self;
16374 int prefer_self; /* when equal print only self time */
16375{
16376 if (count > 0)
16377 {
16378 fprintf(fd, "%5d ", count);
16379 if (prefer_self && profile_equal(total, self))
16380 fprintf(fd, " ");
16381 else
16382 fprintf(fd, "%s ", profile_msg(total));
16383 if (!prefer_self && profile_equal(total, self))
16384 fprintf(fd, " ");
16385 else
16386 fprintf(fd, "%s ", profile_msg(self));
16387 }
16388 else
16389 fprintf(fd, " ");
16390}
16391
16392/*
16393 * Compare function for total time sorting.
16394 */
16395 static int
16396#ifdef __BORLANDC__
16397_RTLENTRYF
16398#endif
16399prof_total_cmp(s1, s2)
16400 const void *s1;
16401 const void *s2;
16402{
16403 ufunc_T *p1, *p2;
16404
16405 p1 = *(ufunc_T **)s1;
16406 p2 = *(ufunc_T **)s2;
16407 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16408}
16409
16410/*
16411 * Compare function for self time sorting.
16412 */
16413 static int
16414#ifdef __BORLANDC__
16415_RTLENTRYF
16416#endif
16417prof_self_cmp(s1, s2)
16418 const void *s1;
16419 const void *s2;
16420{
16421 ufunc_T *p1, *p2;
16422
16423 p1 = *(ufunc_T **)s1;
16424 p2 = *(ufunc_T **)s2;
16425 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16426}
16427
Bram Moolenaar05159a02005-02-26 23:04:13 +000016428#endif
16429
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016430/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016431 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016432 * Return TRUE if a package was loaded.
16433 */
16434 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016435script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016436 char_u *name;
16437{
16438 char_u *p;
16439 char_u *scriptname;
16440 int ret = FALSE;
16441
16442 /* If there is no colon after name[1] there is no package name. */
16443 p = vim_strchr(name, ':');
16444 if (p == NULL || p <= name + 2)
16445 return FALSE;
16446
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016447 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16448 scriptname = autoload_name(name);
16449 if (cmd_runtime(scriptname, FALSE) == OK)
16450 ret = TRUE;
16451
16452 vim_free(scriptname);
16453 return ret;
16454}
16455
16456/*
16457 * Return the autoload script name for a function or variable name.
16458 * Returns NULL when out of memory.
16459 */
16460 static char_u *
16461autoload_name(name)
16462 char_u *name;
16463{
16464 char_u *p;
16465 char_u *scriptname;
16466
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016467 /* Get the script file name: replace ':' with '/', append ".vim". */
16468 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16469 if (scriptname == NULL)
16470 return FALSE;
16471 STRCPY(scriptname, "autoload/");
16472 STRCAT(scriptname, name);
16473 *vim_strrchr(scriptname, ':') = NUL;
16474 STRCAT(scriptname, ".vim");
16475 while ((p = vim_strchr(scriptname, ':')) != NULL)
16476 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016477 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016478}
16479
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16481
16482/*
16483 * Function given to ExpandGeneric() to obtain the list of user defined
16484 * function names.
16485 */
16486 char_u *
16487get_user_func_name(xp, idx)
16488 expand_T *xp;
16489 int idx;
16490{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016491 static long_u done;
16492 static hashitem_T *hi;
16493 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494
16495 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016497 done = 0;
16498 hi = func_hashtab.ht_array;
16499 }
16500 if (done < func_hashtab.ht_used)
16501 {
16502 if (done++ > 0)
16503 ++hi;
16504 while (HASHITEM_EMPTY(hi))
16505 ++hi;
16506 fp = HI2UF(hi);
16507
16508 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16509 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510
16511 cat_func_name(IObuff, fp);
16512 if (xp->xp_context != EXPAND_USER_FUNC)
16513 {
16514 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016515 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 STRCAT(IObuff, ")");
16517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 return IObuff;
16519 }
16520 return NULL;
16521}
16522
16523#endif /* FEAT_CMDL_COMPL */
16524
16525/*
16526 * Copy the function name of "fp" to buffer "buf".
16527 * "buf" must be able to hold the function name plus three bytes.
16528 * Takes care of script-local function names.
16529 */
16530 static void
16531cat_func_name(buf, fp)
16532 char_u *buf;
16533 ufunc_T *fp;
16534{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016535 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 {
16537 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016538 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 }
16540 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016541 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542}
16543
16544/*
16545 * ":delfunction {name}"
16546 */
16547 void
16548ex_delfunction(eap)
16549 exarg_T *eap;
16550{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016551 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 char_u *p;
16553 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555
16556 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016557 name = trans_function_name(&p, eap->skip, 0, &fudi);
16558 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016560 {
16561 if (fudi.fd_dict != NULL && !eap->skip)
16562 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 if (!ends_excmd(*skipwhite(p)))
16566 {
16567 vim_free(name);
16568 EMSG(_(e_trailing));
16569 return;
16570 }
16571 eap->nextcmd = check_nextcmd(p);
16572 if (eap->nextcmd != NULL)
16573 *p = NUL;
16574
16575 if (!eap->skip)
16576 fp = find_func(name);
16577 vim_free(name);
16578
16579 if (!eap->skip)
16580 {
16581 if (fp == NULL)
16582 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016583 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 return;
16585 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016586 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 {
16588 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16589 return;
16590 }
16591
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016592 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016594 /* Delete the dict item that refers to the function, it will
16595 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016596 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016598 else
16599 func_free(fp);
16600 }
16601}
16602
16603/*
16604 * Free a function and remove it from the list of functions.
16605 */
16606 static void
16607func_free(fp)
16608 ufunc_T *fp;
16609{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016610 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016611
16612 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016613 ga_clear_strings(&(fp->uf_args));
16614 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016615#ifdef FEAT_PROFILE
16616 vim_free(fp->uf_tml_count);
16617 vim_free(fp->uf_tml_total);
16618 vim_free(fp->uf_tml_self);
16619#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016620
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016621 /* remove the function from the function hashtable */
16622 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16623 if (HASHITEM_EMPTY(hi))
16624 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016625 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016626 hash_remove(&func_hashtab, hi);
16627
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016628 vim_free(fp);
16629}
16630
16631/*
16632 * Unreference a Function: decrement the reference count and free it when it
16633 * becomes zero. Only for numbered functions.
16634 */
16635 static void
16636func_unref(name)
16637 char_u *name;
16638{
16639 ufunc_T *fp;
16640
16641 if (name != NULL && isdigit(*name))
16642 {
16643 fp = find_func(name);
16644 if (fp == NULL)
16645 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016646 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016647 {
16648 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016649 * when "uf_calls" becomes zero. */
16650 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016651 func_free(fp);
16652 }
16653 }
16654}
16655
16656/*
16657 * Count a reference to a Function.
16658 */
16659 static void
16660func_ref(name)
16661 char_u *name;
16662{
16663 ufunc_T *fp;
16664
16665 if (name != NULL && isdigit(*name))
16666 {
16667 fp = find_func(name);
16668 if (fp == NULL)
16669 EMSG2(_(e_intern2), "func_ref()");
16670 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016671 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 }
16673}
16674
16675/*
16676 * Call a user function.
16677 */
16678 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 ufunc_T *fp; /* pointer to function */
16681 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016682 typval_T *argvars; /* arguments */
16683 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 linenr_T firstline; /* first line of range */
16685 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016686 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687{
Bram Moolenaar33570922005-01-25 22:26:29 +000016688 char_u *save_sourcing_name;
16689 linenr_T save_sourcing_lnum;
16690 scid_T save_current_SID;
16691 funccall_T fc;
16692 funccall_T *save_fcp = current_funccal;
16693 int save_did_emsg;
16694 static int depth = 0;
16695 dictitem_T *v;
16696 int fixvar_idx = 0; /* index in fixvar[] */
16697 int i;
16698 int ai;
16699 char_u numbuf[NUMBUFLEN];
16700 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016701#ifdef FEAT_PROFILE
16702 proftime_T wait_start;
16703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704
16705 /* If depth of calling is getting too high, don't execute the function */
16706 if (depth >= p_mfd)
16707 {
16708 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 rettv->v_type = VAR_NUMBER;
16710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 return;
16712 }
16713 ++depth;
16714
16715 line_breakcheck(); /* check for CTRL-C hit */
16716
Bram Moolenaar33570922005-01-25 22:26:29 +000016717 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 fc.rettv = rettv;
16720 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 fc.linenr = 0;
16722 fc.returned = FALSE;
16723 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016725 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 fc.dbg_tick = debug_tick;
16727
Bram Moolenaar33570922005-01-25 22:26:29 +000016728 /*
16729 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16730 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16731 * each argument variable and saves a lot of time.
16732 */
16733 /*
16734 * Init l: variables.
16735 */
16736 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016737 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016738 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016739 /* Set l:self to "selfdict". */
16740 v = &fc.fixvar[fixvar_idx++].var;
16741 STRCPY(v->di_key, "self");
16742 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16743 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16744 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016745 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016746 v->di_tv.vval.v_dict = selfdict;
16747 ++selfdict->dv_refcount;
16748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016749
Bram Moolenaar33570922005-01-25 22:26:29 +000016750 /*
16751 * Init a: variables.
16752 * Set a:0 to "argcount".
16753 * Set a:000 to a list with room for the "..." arguments.
16754 */
16755 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16756 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016757 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 v = &fc.fixvar[fixvar_idx++].var;
16759 STRCPY(v->di_key, "000");
16760 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16761 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16762 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016763 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016764 v->di_tv.vval.v_list = &fc.l_varlist;
16765 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16766 fc.l_varlist.lv_refcount = 99999;
16767
16768 /*
16769 * Set a:firstline to "firstline" and a:lastline to "lastline".
16770 * Set a:name to named arguments.
16771 * Set a:N to the "..." arguments.
16772 */
16773 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16774 (varnumber_T)firstline);
16775 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16776 (varnumber_T)lastline);
16777 for (i = 0; i < argcount; ++i)
16778 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016779 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016780 if (ai < 0)
16781 /* named argument a:name */
16782 name = FUNCARG(fp, i);
16783 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 /* "..." argument a:1, a:2, etc. */
16786 sprintf((char *)numbuf, "%d", ai + 1);
16787 name = numbuf;
16788 }
16789 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16790 {
16791 v = &fc.fixvar[fixvar_idx++].var;
16792 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16793 }
16794 else
16795 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016796 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16797 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016798 if (v == NULL)
16799 break;
16800 v->di_flags = DI_FLAGS_RO;
16801 }
16802 STRCPY(v->di_key, name);
16803 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16804
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016805 /* Note: the values are copied directly to avoid alloc/free.
16806 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016807 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016808 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016809
16810 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16811 {
16812 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16813 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016814 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016815 }
16816 }
16817
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 /* Don't redraw while executing the function. */
16819 ++RedrawingDisabled;
16820 save_sourcing_name = sourcing_name;
16821 save_sourcing_lnum = sourcing_lnum;
16822 sourcing_lnum = 1;
16823 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016824 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 if (sourcing_name != NULL)
16826 {
16827 if (save_sourcing_name != NULL
16828 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16829 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16830 else
16831 STRCPY(sourcing_name, "function ");
16832 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16833
16834 if (p_verbose >= 12)
16835 {
16836 ++no_wait_return;
16837 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16838 msg_str((char_u *)_("calling %s"), sourcing_name);
16839 if (p_verbose >= 14)
16840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016842 char_u numbuf[NUMBUFLEN];
16843 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844
16845 msg_puts((char_u *)"(");
16846 for (i = 0; i < argcount; ++i)
16847 {
16848 if (i > 0)
16849 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016850 if (argvars[i].v_type == VAR_NUMBER)
16851 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 else
16853 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016854 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016857 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 }
16859 }
16860 msg_puts((char_u *)")");
16861 }
16862 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16863 cmdline_row = msg_row;
16864 --no_wait_return;
16865 }
16866 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016867#ifdef FEAT_PROFILE
16868 if (do_profiling)
16869 {
16870 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16871 func_do_profile(fp);
16872 if (fp->uf_profiling
16873 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16874 {
16875 ++fp->uf_tm_count;
16876 profile_start(&fp->uf_tm_start);
16877 profile_zero(&fp->uf_tm_children);
16878 }
16879 script_prof_save(&wait_start);
16880 }
16881#endif
16882
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016884 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 save_did_emsg = did_emsg;
16886 did_emsg = FALSE;
16887
16888 /* call do_cmdline() to execute the lines */
16889 do_cmdline(NULL, get_func_line, (void *)&fc,
16890 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16891
16892 --RedrawingDisabled;
16893
16894 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016895 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 clear_tv(rettv);
16898 rettv->v_type = VAR_NUMBER;
16899 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 }
16901
Bram Moolenaar05159a02005-02-26 23:04:13 +000016902#ifdef FEAT_PROFILE
16903 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16904 {
16905 profile_end(&fp->uf_tm_start);
16906 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16907 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16908 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16909 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16910 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16911 {
16912 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16913 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16914 }
16915 }
16916#endif
16917
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 /* when being verbose, mention the return value */
16919 if (p_verbose >= 12)
16920 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016921 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922
16923 ++no_wait_return;
16924 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16925
16926 /* Make sure the output fits in IObuff. */
16927 sn = sourcing_name;
16928 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16929 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16930
16931 if (aborting())
16932 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016935 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016938 char_u buf[MSG_BUF_LEN];
16939 char_u numbuf[NUMBUFLEN];
16940 char_u *tofree;
16941
16942 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16943 buf, MSG_BUF_LEN);
16944 smsg((char_u *)_("%s returning %s"), sn, buf);
16945 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 }
16947 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16948 cmdline_row = msg_row;
16949 --no_wait_return;
16950 }
16951
16952 vim_free(sourcing_name);
16953 sourcing_name = save_sourcing_name;
16954 sourcing_lnum = save_sourcing_lnum;
16955 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016956#ifdef FEAT_PROFILE
16957 if (do_profiling)
16958 script_prof_restore(&wait_start);
16959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960
16961 if (p_verbose >= 12 && sourcing_name != NULL)
16962 {
16963 ++no_wait_return;
16964 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16965 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16966 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16967 cmdline_row = msg_row;
16968 --no_wait_return;
16969 }
16970
16971 did_emsg |= save_did_emsg;
16972 current_funccal = save_fcp;
16973
Bram Moolenaar33570922005-01-25 22:26:29 +000016974 /* The a: variables typevals were not alloced, only free the allocated
16975 * variables. */
16976 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16977
16978 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 --depth;
16980}
16981
16982/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 * Add a number variable "name" to dict "dp" with value "nr".
16984 */
16985 static void
16986add_nr_var(dp, v, name, nr)
16987 dict_T *dp;
16988 dictitem_T *v;
16989 char *name;
16990 varnumber_T nr;
16991{
16992 STRCPY(v->di_key, name);
16993 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16994 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16995 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016996 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 v->di_tv.vval.v_number = nr;
16998}
16999
17000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 * ":return [expr]"
17002 */
17003 void
17004ex_return(eap)
17005 exarg_T *eap;
17006{
17007 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017008 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 int returning = FALSE;
17010
17011 if (current_funccal == NULL)
17012 {
17013 EMSG(_("E133: :return not inside a function"));
17014 return;
17015 }
17016
17017 if (eap->skip)
17018 ++emsg_skip;
17019
17020 eap->nextcmd = NULL;
17021 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 {
17024 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017027 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 }
17029 /* It's safer to return also on error. */
17030 else if (!eap->skip)
17031 {
17032 /*
17033 * Return unless the expression evaluation has been cancelled due to an
17034 * aborting error, an interrupt, or an exception.
17035 */
17036 if (!aborting())
17037 returning = do_return(eap, FALSE, TRUE, NULL);
17038 }
17039
17040 /* When skipping or the return gets pending, advance to the next command
17041 * in this line (!returning). Otherwise, ignore the rest of the line.
17042 * Following lines will be ignored by get_func_line(). */
17043 if (returning)
17044 eap->nextcmd = NULL;
17045 else if (eap->nextcmd == NULL) /* no argument */
17046 eap->nextcmd = check_nextcmd(arg);
17047
17048 if (eap->skip)
17049 --emsg_skip;
17050}
17051
17052/*
17053 * Return from a function. Possibly makes the return pending. Also called
17054 * for a pending return at the ":endtry" or after returning from an extra
17055 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017056 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 * FALSE when the return gets pending.
17059 */
17060 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 exarg_T *eap;
17063 int reanimate;
17064 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066{
17067 int idx;
17068 struct condstack *cstack = eap->cstack;
17069
17070 if (reanimate)
17071 /* Undo the return. */
17072 current_funccal->returned = FALSE;
17073
17074 /*
17075 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17076 * not in its finally clause (which then is to be executed next) is found.
17077 * In this case, make the ":return" pending for execution at the ":endtry".
17078 * Otherwise, return normally.
17079 */
17080 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17081 if (idx >= 0)
17082 {
17083 cstack->cs_pending[idx] = CSTP_RETURN;
17084
17085 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017086 /* A pending return again gets pending. "rettv" points to an
17087 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 else
17091 {
17092 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017093 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017095 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017097 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 {
17099 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017100 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 else
17103 EMSG(_(e_outofmem));
17104 }
17105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017106 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107
17108 if (reanimate)
17109 {
17110 /* The pending return value could be overwritten by a ":return"
17111 * without argument in a finally clause; reset the default
17112 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113 current_funccal->rettv->v_type = VAR_NUMBER;
17114 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 }
17116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 }
17119 else
17120 {
17121 current_funccal->returned = TRUE;
17122
17123 /* If the return is carried out now, store the return value. For
17124 * a return immediately after reanimation, the value is already
17125 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017126 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 }
17133 }
17134
17135 return idx < 0;
17136}
17137
17138/*
17139 * Free the variable with a pending return value.
17140 */
17141 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142discard_pending_return(rettv)
17143 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144{
Bram Moolenaar33570922005-01-25 22:26:29 +000017145 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146}
17147
17148/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017149 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 * is an allocated string. Used by report_pending() for verbose messages.
17151 */
17152 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017153get_return_cmd(rettv)
17154 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017156 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017157 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017158 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017160 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017161 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017162 if (s == NULL)
17163 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017164
17165 STRCPY(IObuff, ":return ");
17166 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17167 if (STRLEN(s) + 8 >= IOSIZE)
17168 STRCPY(IObuff + IOSIZE - 4, "...");
17169 vim_free(tofree);
17170 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171}
17172
17173/*
17174 * Get next function line.
17175 * Called by do_cmdline() to get the next line.
17176 * Returns allocated string, or NULL for end of function.
17177 */
17178/* ARGSUSED */
17179 char_u *
17180get_func_line(c, cookie, indent)
17181 int c; /* not used */
17182 void *cookie;
17183 int indent; /* not used */
17184{
Bram Moolenaar33570922005-01-25 22:26:29 +000017185 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017186 ufunc_T *fp = fcp->func;
17187 char_u *retval;
17188 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189
17190 /* If breakpoints have been added/deleted need to check for it. */
17191 if (fcp->dbg_tick != debug_tick)
17192 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017193 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 sourcing_lnum);
17195 fcp->dbg_tick = debug_tick;
17196 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017197#ifdef FEAT_PROFILE
17198 if (do_profiling)
17199 func_line_end(cookie);
17200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201
Bram Moolenaar05159a02005-02-26 23:04:13 +000017202 gap = &fp->uf_lines;
17203 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 retval = NULL;
17205 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17206 retval = NULL;
17207 else
17208 {
17209 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17210 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017211#ifdef FEAT_PROFILE
17212 if (do_profiling)
17213 func_line_start(cookie);
17214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 }
17216
17217 /* Did we encounter a breakpoint? */
17218 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17219 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017220 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017222 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 sourcing_lnum);
17224 fcp->dbg_tick = debug_tick;
17225 }
17226
17227 return retval;
17228}
17229
Bram Moolenaar05159a02005-02-26 23:04:13 +000017230#if defined(FEAT_PROFILE) || defined(PROTO)
17231/*
17232 * Called when starting to read a function line.
17233 * "sourcing_lnum" must be correct!
17234 * When skipping lines it may not actually be executed, but we won't find out
17235 * until later and we need to store the time now.
17236 */
17237 void
17238func_line_start(cookie)
17239 void *cookie;
17240{
17241 funccall_T *fcp = (funccall_T *)cookie;
17242 ufunc_T *fp = fcp->func;
17243
17244 if (fp->uf_profiling && sourcing_lnum >= 1
17245 && sourcing_lnum <= fp->uf_lines.ga_len)
17246 {
17247 fp->uf_tml_idx = sourcing_lnum - 1;
17248 fp->uf_tml_execed = FALSE;
17249 profile_start(&fp->uf_tml_start);
17250 profile_zero(&fp->uf_tml_children);
17251 profile_get_wait(&fp->uf_tml_wait);
17252 }
17253}
17254
17255/*
17256 * Called when actually executing a function line.
17257 */
17258 void
17259func_line_exec(cookie)
17260 void *cookie;
17261{
17262 funccall_T *fcp = (funccall_T *)cookie;
17263 ufunc_T *fp = fcp->func;
17264
17265 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17266 fp->uf_tml_execed = TRUE;
17267}
17268
17269/*
17270 * Called when done with a function line.
17271 */
17272 void
17273func_line_end(cookie)
17274 void *cookie;
17275{
17276 funccall_T *fcp = (funccall_T *)cookie;
17277 ufunc_T *fp = fcp->func;
17278
17279 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17280 {
17281 if (fp->uf_tml_execed)
17282 {
17283 ++fp->uf_tml_count[fp->uf_tml_idx];
17284 profile_end(&fp->uf_tml_start);
17285 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17286 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17287 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17288 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17289 }
17290 fp->uf_tml_idx = -1;
17291 }
17292}
17293#endif
17294
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295/*
17296 * Return TRUE if the currently active function should be ended, because a
17297 * return was encountered or an error occured. Used inside a ":while".
17298 */
17299 int
17300func_has_ended(cookie)
17301 void *cookie;
17302{
Bram Moolenaar33570922005-01-25 22:26:29 +000017303 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304
17305 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17306 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017307 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 || fcp->returned);
17309}
17310
17311/*
17312 * return TRUE if cookie indicates a function which "abort"s on errors.
17313 */
17314 int
17315func_has_abort(cookie)
17316 void *cookie;
17317{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017318 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319}
17320
17321#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17322typedef enum
17323{
17324 VAR_FLAVOUR_DEFAULT,
17325 VAR_FLAVOUR_SESSION,
17326 VAR_FLAVOUR_VIMINFO
17327} var_flavour_T;
17328
17329static var_flavour_T var_flavour __ARGS((char_u *varname));
17330
17331 static var_flavour_T
17332var_flavour(varname)
17333 char_u *varname;
17334{
17335 char_u *p = varname;
17336
17337 if (ASCII_ISUPPER(*p))
17338 {
17339 while (*(++p))
17340 if (ASCII_ISLOWER(*p))
17341 return VAR_FLAVOUR_SESSION;
17342 return VAR_FLAVOUR_VIMINFO;
17343 }
17344 else
17345 return VAR_FLAVOUR_DEFAULT;
17346}
17347#endif
17348
17349#if defined(FEAT_VIMINFO) || defined(PROTO)
17350/*
17351 * Restore global vars that start with a capital from the viminfo file
17352 */
17353 int
17354read_viminfo_varlist(virp, writing)
17355 vir_T *virp;
17356 int writing;
17357{
17358 char_u *tab;
17359 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361
17362 if (!writing && (find_viminfo_parameter('!') != NULL))
17363 {
17364 tab = vim_strchr(virp->vir_line + 1, '\t');
17365 if (tab != NULL)
17366 {
17367 *tab++ = '\0'; /* isolate the variable name */
17368 if (*tab == 'S') /* string var */
17369 is_string = TRUE;
17370
17371 tab = vim_strchr(tab, '\t');
17372 if (tab != NULL)
17373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 if (is_string)
17375 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017376 tv.v_type = VAR_STRING;
17377 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 }
17380 else
17381 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017382 tv.v_type = VAR_NUMBER;
17383 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017385 set_var(virp->vir_line + 1, &tv, FALSE);
17386 if (is_string)
17387 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 }
17389 }
17390 }
17391
17392 return viminfo_readline(virp);
17393}
17394
17395/*
17396 * Write global vars that start with a capital to the viminfo file
17397 */
17398 void
17399write_viminfo_varlist(fp)
17400 FILE *fp;
17401{
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 hashitem_T *hi;
17403 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017404 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017405 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017406 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017408 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409
17410 if (find_viminfo_parameter('!') == NULL)
17411 return;
17412
17413 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017414
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 todo = globvarht.ht_used;
17416 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017418 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017420 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017421 this_var = HI2DI(hi);
17422 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017424 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017425 {
17426 case VAR_STRING: s = "STR"; break;
17427 case VAR_NUMBER: s = "NUM"; break;
17428 default: continue;
17429 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017431 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17432 if (p != NULL)
17433 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017434 vim_free(tofree);
17435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 }
17437 }
17438}
17439#endif
17440
17441#if defined(FEAT_SESSION) || defined(PROTO)
17442 int
17443store_session_globals(fd)
17444 FILE *fd;
17445{
Bram Moolenaar33570922005-01-25 22:26:29 +000017446 hashitem_T *hi;
17447 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017448 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449 char_u *p, *t;
17450
Bram Moolenaar33570922005-01-25 22:26:29 +000017451 todo = globvarht.ht_used;
17452 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017454 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017456 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 this_var = HI2DI(hi);
17458 if ((this_var->di_tv.v_type == VAR_NUMBER
17459 || this_var->di_tv.v_type == VAR_STRING)
17460 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017461 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017462 /* Escape special characters with a backslash. Turn a LF and
17463 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017464 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017465 (char_u *)"\\\"\n\r");
17466 if (p == NULL) /* out of memory */
17467 break;
17468 for (t = p; *t != NUL; ++t)
17469 if (*t == '\n')
17470 *t = 'n';
17471 else if (*t == '\r')
17472 *t = 'r';
17473 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 this_var->di_key,
17475 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17476 : ' ',
17477 p,
17478 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17479 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017480 || put_eol(fd) == FAIL)
17481 {
17482 vim_free(p);
17483 return FAIL;
17484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 vim_free(p);
17486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 }
17488 }
17489 return OK;
17490}
17491#endif
17492
17493#endif /* FEAT_EVAL */
17494
17495#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17496
17497
17498#ifdef WIN3264
17499/*
17500 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17501 */
17502static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17503static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17504static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17505
17506/*
17507 * Get the short pathname of a file.
17508 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17509 */
17510 static int
17511get_short_pathname(fnamep, bufp, fnamelen)
17512 char_u **fnamep;
17513 char_u **bufp;
17514 int *fnamelen;
17515{
17516 int l,len;
17517 char_u *newbuf;
17518
17519 len = *fnamelen;
17520
17521 l = GetShortPathName(*fnamep, *fnamep, len);
17522 if (l > len - 1)
17523 {
17524 /* If that doesn't work (not enough space), then save the string
17525 * and try again with a new buffer big enough
17526 */
17527 newbuf = vim_strnsave(*fnamep, l);
17528 if (newbuf == NULL)
17529 return 0;
17530
17531 vim_free(*bufp);
17532 *fnamep = *bufp = newbuf;
17533
17534 l = GetShortPathName(*fnamep,*fnamep,l+1);
17535
17536 /* Really should always succeed, as the buffer is big enough */
17537 }
17538
17539 *fnamelen = l;
17540 return 1;
17541}
17542
17543/*
17544 * Create a short path name. Returns the length of the buffer it needs.
17545 * Doesn't copy over the end of the buffer passed in.
17546 */
17547 static int
17548shortpath_for_invalid_fname(fname, bufp, fnamelen)
17549 char_u **fname;
17550 char_u **bufp;
17551 int *fnamelen;
17552{
17553 char_u *s, *p, *pbuf2, *pbuf3;
17554 char_u ch;
17555 int l,len,len2,plen,slen;
17556
17557 /* Make a copy */
17558 len2 = *fnamelen;
17559 pbuf2 = vim_strnsave(*fname, len2);
17560 pbuf3 = NULL;
17561
17562 s = pbuf2 + len2 - 1; /* Find the end */
17563 slen = 1;
17564 plen = len2;
17565
17566 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017567 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 {
17569 --s;
17570 ++slen;
17571 --plen;
17572 }
17573
17574 do
17575 {
17576 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017577 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 {
17579 --s;
17580 ++slen;
17581 --plen;
17582 }
17583 if (s <= pbuf2)
17584 break;
17585
17586 /* Remeber the character that is about to be blatted */
17587 ch = *s;
17588 *s = 0; /* get_short_pathname requires a null-terminated string */
17589
17590 /* Try it in situ */
17591 p = pbuf2;
17592 if (!get_short_pathname(&p, &pbuf3, &plen))
17593 {
17594 vim_free(pbuf2);
17595 return -1;
17596 }
17597 *s = ch; /* Preserve the string */
17598 } while (plen == 0);
17599
17600 if (plen > 0)
17601 {
17602 /* Remeber the length of the new string. */
17603 *fnamelen = len = plen + slen;
17604 vim_free(*bufp);
17605 if (len > len2)
17606 {
17607 /* If there's not enough space in the currently allocated string,
17608 * then copy it to a buffer big enough.
17609 */
17610 *fname= *bufp = vim_strnsave(p, len);
17611 if (*fname == NULL)
17612 return -1;
17613 }
17614 else
17615 {
17616 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17617 *fname = *bufp = pbuf2;
17618 if (p != pbuf2)
17619 strncpy(*fname, p, plen);
17620 pbuf2 = NULL;
17621 }
17622 /* Concat the next bit */
17623 strncpy(*fname + plen, s, slen);
17624 (*fname)[len] = '\0';
17625 }
17626 vim_free(pbuf3);
17627 vim_free(pbuf2);
17628 return 0;
17629}
17630
17631/*
17632 * Get a pathname for a partial path.
17633 */
17634 static int
17635shortpath_for_partial(fnamep, bufp, fnamelen)
17636 char_u **fnamep;
17637 char_u **bufp;
17638 int *fnamelen;
17639{
17640 int sepcount, len, tflen;
17641 char_u *p;
17642 char_u *pbuf, *tfname;
17643 int hasTilde;
17644
17645 /* Count up the path seperators from the RHS.. so we know which part
17646 * of the path to return.
17647 */
17648 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017649 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 if (vim_ispathsep(*p))
17651 ++sepcount;
17652
17653 /* Need full path first (use expand_env() to remove a "~/") */
17654 hasTilde = (**fnamep == '~');
17655 if (hasTilde)
17656 pbuf = tfname = expand_env_save(*fnamep);
17657 else
17658 pbuf = tfname = FullName_save(*fnamep, FALSE);
17659
17660 len = tflen = STRLEN(tfname);
17661
17662 if (!get_short_pathname(&tfname, &pbuf, &len))
17663 return -1;
17664
17665 if (len == 0)
17666 {
17667 /* Don't have a valid filename, so shorten the rest of the
17668 * path if we can. This CAN give us invalid 8.3 filenames, but
17669 * there's not a lot of point in guessing what it might be.
17670 */
17671 len = tflen;
17672 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17673 return -1;
17674 }
17675
17676 /* Count the paths backward to find the beginning of the desired string. */
17677 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017678 {
17679#ifdef FEAT_MBYTE
17680 if (has_mbyte)
17681 p -= mb_head_off(tfname, p);
17682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 if (vim_ispathsep(*p))
17684 {
17685 if (sepcount == 0 || (hasTilde && sepcount == 1))
17686 break;
17687 else
17688 sepcount --;
17689 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 if (hasTilde)
17692 {
17693 --p;
17694 if (p >= tfname)
17695 *p = '~';
17696 else
17697 return -1;
17698 }
17699 else
17700 ++p;
17701
17702 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17703 vim_free(*bufp);
17704 *fnamelen = (int)STRLEN(p);
17705 *bufp = pbuf;
17706 *fnamep = p;
17707
17708 return 0;
17709}
17710#endif /* WIN3264 */
17711
17712/*
17713 * Adjust a filename, according to a string of modifiers.
17714 * *fnamep must be NUL terminated when called. When returning, the length is
17715 * determined by *fnamelen.
17716 * Returns valid flags.
17717 * When there is an error, *fnamep is set to NULL.
17718 */
17719 int
17720modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17721 char_u *src; /* string with modifiers */
17722 int *usedlen; /* characters after src that are used */
17723 char_u **fnamep; /* file name so far */
17724 char_u **bufp; /* buffer for allocated file name or NULL */
17725 int *fnamelen; /* length of fnamep */
17726{
17727 int valid = 0;
17728 char_u *tail;
17729 char_u *s, *p, *pbuf;
17730 char_u dirname[MAXPATHL];
17731 int c;
17732 int has_fullname = 0;
17733#ifdef WIN3264
17734 int has_shortname = 0;
17735#endif
17736
17737repeat:
17738 /* ":p" - full path/file_name */
17739 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17740 {
17741 has_fullname = 1;
17742
17743 valid |= VALID_PATH;
17744 *usedlen += 2;
17745
17746 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17747 if ((*fnamep)[0] == '~'
17748#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17749 && ((*fnamep)[1] == '/'
17750# ifdef BACKSLASH_IN_FILENAME
17751 || (*fnamep)[1] == '\\'
17752# endif
17753 || (*fnamep)[1] == NUL)
17754
17755#endif
17756 )
17757 {
17758 *fnamep = expand_env_save(*fnamep);
17759 vim_free(*bufp); /* free any allocated file name */
17760 *bufp = *fnamep;
17761 if (*fnamep == NULL)
17762 return -1;
17763 }
17764
17765 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017766 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 {
17768 if (vim_ispathsep(*p)
17769 && p[1] == '.'
17770 && (p[2] == NUL
17771 || vim_ispathsep(p[2])
17772 || (p[2] == '.'
17773 && (p[3] == NUL || vim_ispathsep(p[3])))))
17774 break;
17775 }
17776
17777 /* FullName_save() is slow, don't use it when not needed. */
17778 if (*p != NUL || !vim_isAbsName(*fnamep))
17779 {
17780 *fnamep = FullName_save(*fnamep, *p != NUL);
17781 vim_free(*bufp); /* free any allocated file name */
17782 *bufp = *fnamep;
17783 if (*fnamep == NULL)
17784 return -1;
17785 }
17786
17787 /* Append a path separator to a directory. */
17788 if (mch_isdir(*fnamep))
17789 {
17790 /* Make room for one or two extra characters. */
17791 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17792 vim_free(*bufp); /* free any allocated file name */
17793 *bufp = *fnamep;
17794 if (*fnamep == NULL)
17795 return -1;
17796 add_pathsep(*fnamep);
17797 }
17798 }
17799
17800 /* ":." - path relative to the current directory */
17801 /* ":~" - path relative to the home directory */
17802 /* ":8" - shortname path - postponed till after */
17803 while (src[*usedlen] == ':'
17804 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17805 {
17806 *usedlen += 2;
17807 if (c == '8')
17808 {
17809#ifdef WIN3264
17810 has_shortname = 1; /* Postpone this. */
17811#endif
17812 continue;
17813 }
17814 pbuf = NULL;
17815 /* Need full path first (use expand_env() to remove a "~/") */
17816 if (!has_fullname)
17817 {
17818 if (c == '.' && **fnamep == '~')
17819 p = pbuf = expand_env_save(*fnamep);
17820 else
17821 p = pbuf = FullName_save(*fnamep, FALSE);
17822 }
17823 else
17824 p = *fnamep;
17825
17826 has_fullname = 0;
17827
17828 if (p != NULL)
17829 {
17830 if (c == '.')
17831 {
17832 mch_dirname(dirname, MAXPATHL);
17833 s = shorten_fname(p, dirname);
17834 if (s != NULL)
17835 {
17836 *fnamep = s;
17837 if (pbuf != NULL)
17838 {
17839 vim_free(*bufp); /* free any allocated file name */
17840 *bufp = pbuf;
17841 pbuf = NULL;
17842 }
17843 }
17844 }
17845 else
17846 {
17847 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17848 /* Only replace it when it starts with '~' */
17849 if (*dirname == '~')
17850 {
17851 s = vim_strsave(dirname);
17852 if (s != NULL)
17853 {
17854 *fnamep = s;
17855 vim_free(*bufp);
17856 *bufp = s;
17857 }
17858 }
17859 }
17860 vim_free(pbuf);
17861 }
17862 }
17863
17864 tail = gettail(*fnamep);
17865 *fnamelen = (int)STRLEN(*fnamep);
17866
17867 /* ":h" - head, remove "/file_name", can be repeated */
17868 /* Don't remove the first "/" or "c:\" */
17869 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17870 {
17871 valid |= VALID_HEAD;
17872 *usedlen += 2;
17873 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017874 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 --tail;
17876 *fnamelen = (int)(tail - *fnamep);
17877#ifdef VMS
17878 if (*fnamelen > 0)
17879 *fnamelen += 1; /* the path separator is part of the path */
17880#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017881 while (tail > s && !after_pathsep(s, tail))
17882 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 }
17884
17885 /* ":8" - shortname */
17886 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17887 {
17888 *usedlen += 2;
17889#ifdef WIN3264
17890 has_shortname = 1;
17891#endif
17892 }
17893
17894#ifdef WIN3264
17895 /* Check shortname after we have done 'heads' and before we do 'tails'
17896 */
17897 if (has_shortname)
17898 {
17899 pbuf = NULL;
17900 /* Copy the string if it is shortened by :h */
17901 if (*fnamelen < (int)STRLEN(*fnamep))
17902 {
17903 p = vim_strnsave(*fnamep, *fnamelen);
17904 if (p == 0)
17905 return -1;
17906 vim_free(*bufp);
17907 *bufp = *fnamep = p;
17908 }
17909
17910 /* Split into two implementations - makes it easier. First is where
17911 * there isn't a full name already, second is where there is.
17912 */
17913 if (!has_fullname && !vim_isAbsName(*fnamep))
17914 {
17915 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17916 return -1;
17917 }
17918 else
17919 {
17920 int l;
17921
17922 /* Simple case, already have the full-name
17923 * Nearly always shorter, so try first time. */
17924 l = *fnamelen;
17925 if (!get_short_pathname(fnamep, bufp, &l))
17926 return -1;
17927
17928 if (l == 0)
17929 {
17930 /* Couldn't find the filename.. search the paths.
17931 */
17932 l = *fnamelen;
17933 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17934 return -1;
17935 }
17936 *fnamelen = l;
17937 }
17938 }
17939#endif /* WIN3264 */
17940
17941 /* ":t" - tail, just the basename */
17942 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17943 {
17944 *usedlen += 2;
17945 *fnamelen -= (int)(tail - *fnamep);
17946 *fnamep = tail;
17947 }
17948
17949 /* ":e" - extension, can be repeated */
17950 /* ":r" - root, without extension, can be repeated */
17951 while (src[*usedlen] == ':'
17952 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17953 {
17954 /* find a '.' in the tail:
17955 * - for second :e: before the current fname
17956 * - otherwise: The last '.'
17957 */
17958 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17959 s = *fnamep - 2;
17960 else
17961 s = *fnamep + *fnamelen - 1;
17962 for ( ; s > tail; --s)
17963 if (s[0] == '.')
17964 break;
17965 if (src[*usedlen + 1] == 'e') /* :e */
17966 {
17967 if (s > tail)
17968 {
17969 *fnamelen += (int)(*fnamep - (s + 1));
17970 *fnamep = s + 1;
17971#ifdef VMS
17972 /* cut version from the extension */
17973 s = *fnamep + *fnamelen - 1;
17974 for ( ; s > *fnamep; --s)
17975 if (s[0] == ';')
17976 break;
17977 if (s > *fnamep)
17978 *fnamelen = s - *fnamep;
17979#endif
17980 }
17981 else if (*fnamep <= tail)
17982 *fnamelen = 0;
17983 }
17984 else /* :r */
17985 {
17986 if (s > tail) /* remove one extension */
17987 *fnamelen = (int)(s - *fnamep);
17988 }
17989 *usedlen += 2;
17990 }
17991
17992 /* ":s?pat?foo?" - substitute */
17993 /* ":gs?pat?foo?" - global substitute */
17994 if (src[*usedlen] == ':'
17995 && (src[*usedlen + 1] == 's'
17996 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17997 {
17998 char_u *str;
17999 char_u *pat;
18000 char_u *sub;
18001 int sep;
18002 char_u *flags;
18003 int didit = FALSE;
18004
18005 flags = (char_u *)"";
18006 s = src + *usedlen + 2;
18007 if (src[*usedlen + 1] == 'g')
18008 {
18009 flags = (char_u *)"g";
18010 ++s;
18011 }
18012
18013 sep = *s++;
18014 if (sep)
18015 {
18016 /* find end of pattern */
18017 p = vim_strchr(s, sep);
18018 if (p != NULL)
18019 {
18020 pat = vim_strnsave(s, (int)(p - s));
18021 if (pat != NULL)
18022 {
18023 s = p + 1;
18024 /* find end of substitution */
18025 p = vim_strchr(s, sep);
18026 if (p != NULL)
18027 {
18028 sub = vim_strnsave(s, (int)(p - s));
18029 str = vim_strnsave(*fnamep, *fnamelen);
18030 if (sub != NULL && str != NULL)
18031 {
18032 *usedlen = (int)(p + 1 - src);
18033 s = do_string_sub(str, pat, sub, flags);
18034 if (s != NULL)
18035 {
18036 *fnamep = s;
18037 *fnamelen = (int)STRLEN(s);
18038 vim_free(*bufp);
18039 *bufp = s;
18040 didit = TRUE;
18041 }
18042 }
18043 vim_free(sub);
18044 vim_free(str);
18045 }
18046 vim_free(pat);
18047 }
18048 }
18049 /* after using ":s", repeat all the modifiers */
18050 if (didit)
18051 goto repeat;
18052 }
18053 }
18054
18055 return valid;
18056}
18057
18058/*
18059 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18060 * "flags" can be "g" to do a global substitute.
18061 * Returns an allocated string, NULL for error.
18062 */
18063 char_u *
18064do_string_sub(str, pat, sub, flags)
18065 char_u *str;
18066 char_u *pat;
18067 char_u *sub;
18068 char_u *flags;
18069{
18070 int sublen;
18071 regmatch_T regmatch;
18072 int i;
18073 int do_all;
18074 char_u *tail;
18075 garray_T ga;
18076 char_u *ret;
18077 char_u *save_cpo;
18078
18079 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18080 save_cpo = p_cpo;
18081 p_cpo = (char_u *)"";
18082
18083 ga_init2(&ga, 1, 200);
18084
18085 do_all = (flags[0] == 'g');
18086
18087 regmatch.rm_ic = p_ic;
18088 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18089 if (regmatch.regprog != NULL)
18090 {
18091 tail = str;
18092 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18093 {
18094 /*
18095 * Get some space for a temporary buffer to do the substitution
18096 * into. It will contain:
18097 * - The text up to where the match is.
18098 * - The substituted text.
18099 * - The text after the match.
18100 */
18101 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18102 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18103 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18104 {
18105 ga_clear(&ga);
18106 break;
18107 }
18108
18109 /* copy the text up to where the match is */
18110 i = (int)(regmatch.startp[0] - tail);
18111 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18112 /* add the substituted text */
18113 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18114 + ga.ga_len + i, TRUE, TRUE, FALSE);
18115 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 /* avoid getting stuck on a match with an empty string */
18117 if (tail == regmatch.endp[0])
18118 {
18119 if (*tail == NUL)
18120 break;
18121 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18122 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 }
18124 else
18125 {
18126 tail = regmatch.endp[0];
18127 if (*tail == NUL)
18128 break;
18129 }
18130 if (!do_all)
18131 break;
18132 }
18133
18134 if (ga.ga_data != NULL)
18135 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18136
18137 vim_free(regmatch.regprog);
18138 }
18139
18140 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18141 ga_clear(&ga);
18142 p_cpo = save_cpo;
18143
18144 return ret;
18145}
18146
18147#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */