blob: 5840cc5ad4dec4f2bf7ed29202637011bca66333 [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 Moolenaare9a41262005-01-15 22:18:47 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
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 Moolenaar33570922005-01-25 22:26:29 +0000319};
320
321/* shorthand */
322#define vv_type vv_di.di_tv.v_type
323#define vv_nr vv_di.di_tv.vval.v_number
324#define vv_str vv_di.di_tv.vval.v_string
325#define vv_tv vv_di.di_tv
326
327/*
328 * The v: variables are stored in dictionary "vimvardict".
329 * "vimvars_var" is the variable that is used for the "l:" scope.
330 */
331static dict_T vimvardict;
332static dictitem_T vimvars_var;
333#define vimvarht vimvardict.dv_hashtab
334
335static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
336static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
337static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
338static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
339static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
340static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
341static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
342static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000344static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static list_T *list_alloc __ARGS((void));
349static void list_unref __ARGS((list_T *l));
350static void list_free __ARGS((list_T *l));
351static listitem_T *listitem_alloc __ARGS((void));
352static void listitem_free __ARGS((listitem_T *item));
353static void listitem_remove __ARGS((list_T *l, listitem_T *item));
354static long list_len __ARGS((list_T *l));
355static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
356static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
357static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
358static int string_isa_number __ARGS((char_u *s));
359static listitem_T *list_find __ARGS((list_T *l, long n));
360static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000361static void list_append __ARGS((list_T *l, listitem_T *item));
362static int list_append_tv __ARGS((list_T *l, typval_T *tv));
363static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
364static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
365static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000366static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000367static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
368static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000369static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
Bram Moolenaar33570922005-01-25 22:26:29 +0000371static void dict_unref __ARGS((dict_T *d));
372static void dict_free __ARGS((dict_T *d));
373static dictitem_T *dictitem_alloc __ARGS((char_u *key));
374static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
375static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
376static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000377static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000378static int dict_add __ARGS((dict_T *d, dictitem_T *item));
379static long dict_len __ARGS((dict_T *d));
380static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
381static char_u *dict2string __ARGS((typval_T *tv));
382static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
383
384static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
385static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
386static char_u *string_quote __ARGS((char_u *str, int function));
387static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int find_internal_func __ARGS((char_u *name));
389static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
390static 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));
391static 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 +0000392static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000393
394static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
395static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
396static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
397static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
398static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
399static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
400static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
418static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000424static void f_errorlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000485static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000502static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000506#ifdef vim_mkdir
507static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000514static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
537#ifdef HAVE_STRFTIME
538static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
539#endif
540static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568
569static win_T *find_win_by_nr __ARGS((typval_T *vp));
570static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
571static int get_env_len __ARGS((char_u **arg));
572static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000573static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
575static int eval_isnamec __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
577static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static typval_T *alloc_tv __ARGS((void));
579static typval_T *alloc_string_tv __ARGS((char_u *string));
580static void free_tv __ARGS((typval_T *varp));
581static void clear_tv __ARGS((typval_T *varp));
582static void init_tv __ARGS((typval_T *varp));
583static long get_tv_number __ARGS((typval_T *varp));
584static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
585static char_u *get_tv_string __ARGS((typval_T *varp));
586static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
587static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000588static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
590static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
591static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
592static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
593static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
594static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
595static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000598static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
600static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
601static int eval_fname_script __ARGS((char_u *p));
602static int eval_fname_sid __ARGS((char_u *p));
603static void list_func_head __ARGS((ufunc_T *fp, int indent));
604static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
605static ufunc_T *find_func __ARGS((char_u *name));
606static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000607static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000608#ifdef FEAT_PROFILE
609static void func_do_profile __ARGS((ufunc_T *fp));
610#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000611static int script_autoload __ARGS((char_u *name));
612static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void func_free __ARGS((ufunc_T *fp));
614static void func_unref __ARGS((char_u *name));
615static void func_ref __ARGS((char_u *name));
616static 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));
617static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
618
619static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
620
621static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
622static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
623static char_u *skip_var_one __ARGS((char_u *arg));
624static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
625static void list_glob_vars __ARGS((void));
626static void list_buf_vars __ARGS((void));
627static void list_win_vars __ARGS((void));
628static void list_vim_vars __ARGS((void));
629static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
630static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
631static int check_changedtick __ARGS((char_u *arg));
632static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
633static void clear_lval __ARGS((lval_T *lp));
634static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
635static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
636static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
637static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
638static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000639static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000641static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
642static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000643static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644
645/*
646 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000647 */
648 void
649eval_init()
650{
Bram Moolenaar33570922005-01-25 22:26:29 +0000651 int i;
652 struct vimvar *p;
653
654 init_var_dict(&globvardict, &globvars_var);
655 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000656 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000657 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000658
659 for (i = 0; i < VV_LEN; ++i)
660 {
661 p = &vimvars[i];
662 STRCPY(p->vv_di.di_key, p->vv_name);
663 if (p->vv_flags & VV_RO)
664 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
665 else if (p->vv_flags & VV_RO_SBX)
666 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
667 else
668 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000669
670 /* add to v: scope dict, unless the value is not always available */
671 if (p->vv_type != VAR_UNKNOWN)
672 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000673 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000674 /* add to compat scope dict */
675 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000676 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000677}
678
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * Return the name of the executed function.
681 */
682 char_u *
683func_name(cookie)
684 void *cookie;
685{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687}
688
689/*
690 * Return the address holding the next breakpoint line for a funccall cookie.
691 */
692 linenr_T *
693func_breakpoint(cookie)
694 void *cookie;
695{
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
699/*
700 * Return the address holding the debug tick for a funccall cookie.
701 */
702 int *
703func_dbg_tick(cookie)
704 void *cookie;
705{
Bram Moolenaar33570922005-01-25 22:26:29 +0000706 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709/*
710 * Return the nesting level for a funccall cookie.
711 */
712 int
713func_level(cookie)
714 void *cookie;
715{
Bram Moolenaar33570922005-01-25 22:26:29 +0000716 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718
719/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000720funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
722/*
723 * Return TRUE when a function was ended by a ":return" command.
724 */
725 int
726current_func_returned()
727{
728 return current_funccal->returned;
729}
730
731
732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 * Set an internal variable to a string value. Creates the variable if it does
734 * not already exist.
735 */
736 void
737set_internal_string_var(name, value)
738 char_u *name;
739 char_u *value;
740{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000741 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 val = vim_strsave(value);
745 if (val != NULL)
746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000747 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000748 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000750 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000751 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753 }
754}
755
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000756static lval_T *redir_lval = NULL;
757static char_u *redir_endp = NULL;
758static char_u *redir_varname = NULL;
759
760/*
761 * Start recording command output to a variable
762 * Returns OK if successfully completed the setup. FAIL otherwise.
763 */
764 int
765var_redir_start(name, append)
766 char_u *name;
767 int append; /* append to an existing variable */
768{
769 int save_emsg;
770 int err;
771 typval_T tv;
772
773 /* Make sure a valid variable name is specified */
774 if (!eval_isnamec(*name) || VIM_ISDIGIT(*name))
775 {
776 EMSG(_(e_invarg));
777 return FAIL;
778 }
779
780 redir_varname = vim_strsave(name);
781 if (redir_varname == NULL)
782 return FAIL;
783
784 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
785 if (redir_lval == NULL)
786 {
787 var_redir_stop();
788 return FAIL;
789 }
790
791 /* Parse the variable name (can be a dict or list entry). */
792 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE);
793 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
794 {
795 if (redir_endp != NULL && *redir_endp != NUL)
796 /* Trailing characters are present after the variable name */
797 EMSG(_(e_trailing));
798 else
799 EMSG(_(e_invarg));
800 var_redir_stop();
801 return FAIL;
802 }
803
804 /* check if we can write to the variable: set it to or append an empty
805 * string */
806 save_emsg = did_emsg;
807 did_emsg = FALSE;
808 tv.v_type = VAR_STRING;
809 tv.vval.v_string = (char_u *)"";
810 if (append)
811 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
812 else
813 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
814 err = did_emsg;
815 did_emsg += save_emsg;
816 if (err)
817 {
818 var_redir_stop();
819 return FAIL;
820 }
821 if (redir_lval->ll_newkey != NULL)
822 {
823 /* Dictionary item was created, don't do it again. */
824 vim_free(redir_lval->ll_newkey);
825 redir_lval->ll_newkey = NULL;
826 }
827
828 return OK;
829}
830
831/*
832 * Append "value[len]" to the variable set by var_redir_start().
833 */
834 void
835var_redir_str(value, len)
836 char_u *value;
837 int len;
838{
839 char_u *val;
840 typval_T tv;
841 int save_emsg;
842 int err;
843
844 if (redir_lval == NULL)
845 return;
846
847 if (len == -1)
848 /* Append the entire string */
849 val = vim_strsave(value);
850 else
851 /* Append only the specified number of characters */
852 val = vim_strnsave(value, len);
853 if (val == NULL)
854 return;
855
856 tv.v_type = VAR_STRING;
857 tv.vval.v_string = val;
858
859 save_emsg = did_emsg;
860 did_emsg = FALSE;
861 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
862 err = did_emsg;
863 did_emsg += save_emsg;
864 if (err)
865 var_redir_stop();
866
867 vim_free(tv.vval.v_string);
868}
869
870/*
871 * Stop redirecting command output to a variable.
872 */
873 void
874var_redir_stop()
875{
876 if (redir_lval != NULL)
877 {
878 clear_lval(redir_lval);
879 vim_free(redir_lval);
880 redir_lval = NULL;
881 }
882 vim_free(redir_varname);
883 redir_varname = NULL;
884}
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886# if defined(FEAT_MBYTE) || defined(PROTO)
887 int
888eval_charconvert(enc_from, enc_to, fname_from, fname_to)
889 char_u *enc_from;
890 char_u *enc_to;
891 char_u *fname_from;
892 char_u *fname_to;
893{
894 int err = FALSE;
895
896 set_vim_var_string(VV_CC_FROM, enc_from, -1);
897 set_vim_var_string(VV_CC_TO, enc_to, -1);
898 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
899 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
900 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
901 err = TRUE;
902 set_vim_var_string(VV_CC_FROM, NULL, -1);
903 set_vim_var_string(VV_CC_TO, NULL, -1);
904 set_vim_var_string(VV_FNAME_IN, NULL, -1);
905 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
906
907 if (err)
908 return FAIL;
909 return OK;
910}
911# endif
912
913# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
914 int
915eval_printexpr(fname, args)
916 char_u *fname;
917 char_u *args;
918{
919 int err = FALSE;
920
921 set_vim_var_string(VV_FNAME_IN, fname, -1);
922 set_vim_var_string(VV_CMDARG, args, -1);
923 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
924 err = TRUE;
925 set_vim_var_string(VV_FNAME_IN, NULL, -1);
926 set_vim_var_string(VV_CMDARG, NULL, -1);
927
928 if (err)
929 {
930 mch_remove(fname);
931 return FAIL;
932 }
933 return OK;
934}
935# endif
936
937# if defined(FEAT_DIFF) || defined(PROTO)
938 void
939eval_diff(origfile, newfile, outfile)
940 char_u *origfile;
941 char_u *newfile;
942 char_u *outfile;
943{
944 int err = FALSE;
945
946 set_vim_var_string(VV_FNAME_IN, origfile, -1);
947 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
948 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
949 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
950 set_vim_var_string(VV_FNAME_IN, NULL, -1);
951 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
952 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
953}
954
955 void
956eval_patch(origfile, difffile, outfile)
957 char_u *origfile;
958 char_u *difffile;
959 char_u *outfile;
960{
961 int err;
962
963 set_vim_var_string(VV_FNAME_IN, origfile, -1);
964 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
965 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
966 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
967 set_vim_var_string(VV_FNAME_IN, NULL, -1);
968 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
969 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
970}
971# endif
972
973/*
974 * Top level evaluation function, returning a boolean.
975 * Sets "error" to TRUE if there was an error.
976 * Return TRUE or FALSE.
977 */
978 int
979eval_to_bool(arg, error, nextcmd, skip)
980 char_u *arg;
981 int *error;
982 char_u **nextcmd;
983 int skip; /* only parse, don't execute */
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 int retval = FALSE;
987
988 if (skip)
989 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000990 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 else
993 {
994 *error = FALSE;
995 if (!skip)
996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000997 retval = (get_tv_number(&tv) != 0);
998 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 }
1000 }
1001 if (skip)
1002 --emsg_skip;
1003
1004 return retval;
1005}
1006
1007/*
1008 * Top level evaluation function, returning a string. If "skip" is TRUE,
1009 * only parsing to "nextcmd" is done, without reporting errors. Return
1010 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1011 */
1012 char_u *
1013eval_to_string_skip(arg, nextcmd, skip)
1014 char_u *arg;
1015 char_u **nextcmd;
1016 int skip; /* only parse, don't execute */
1017{
Bram Moolenaar33570922005-01-25 22:26:29 +00001018 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 char_u *retval;
1020
1021 if (skip)
1022 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001023 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 retval = NULL;
1025 else
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 retval = vim_strsave(get_tv_string(&tv));
1028 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 if (skip)
1031 --emsg_skip;
1032
1033 return retval;
1034}
1035
1036/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001037 * Skip over an expression at "*pp".
1038 * Return FAIL for an error, OK otherwise.
1039 */
1040 int
1041skip_expr(pp)
1042 char_u **pp;
1043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001045
1046 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001048}
1049
1050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 * Top level evaluation function, returning a string.
1052 * Return pointer to allocated memory, or NULL for failure.
1053 */
1054 char_u *
1055eval_to_string(arg, nextcmd)
1056 char_u *arg;
1057 char_u **nextcmd;
1058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 char_u *retval;
1061
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 retval = NULL;
1064 else
1065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 retval = vim_strsave(get_tv_string(&tv));
1067 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069
1070 return retval;
1071}
1072
1073/*
1074 * Call eval_to_string() with "sandbox" set and not using local variables.
1075 */
1076 char_u *
1077eval_to_string_safe(arg, nextcmd)
1078 char_u *arg;
1079 char_u **nextcmd;
1080{
1081 char_u *retval;
1082 void *save_funccalp;
1083
1084 save_funccalp = save_funccal();
1085 ++sandbox;
1086 retval = eval_to_string(arg, nextcmd);
1087 --sandbox;
1088 restore_funccal(save_funccalp);
1089 return retval;
1090}
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092/*
1093 * Top level evaluation function, returning a number.
1094 * Evaluates "expr" silently.
1095 * Returns -1 for an error.
1096 */
1097 int
1098eval_to_number(expr)
1099 char_u *expr;
1100{
Bram Moolenaar33570922005-01-25 22:26:29 +00001101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001103 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
1105 ++emsg_off;
1106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001107 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 retval = -1;
1109 else
1110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001111 retval = get_tv_number(&rettv);
1112 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114 --emsg_off;
1115
1116 return retval;
1117}
1118
1119#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1120/*
1121 * Call some vimL function and return the result as a string
1122 * Uses argv[argc] for the function arguments.
1123 */
1124 char_u *
1125call_vim_function(func, argc, argv, safe)
1126 char_u *func;
1127 int argc;
1128 char_u **argv;
1129 int safe; /* use the sandbox */
1130{
1131 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001132 typval_T rettv;
1133 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 long n;
1135 int len;
1136 int i;
1137 int doesrange;
1138 void *save_funccalp = NULL;
1139
Bram Moolenaar33570922005-01-25 22:26:29 +00001140 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (argvars == NULL)
1142 return NULL;
1143
1144 for (i = 0; i < argc; i++)
1145 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001146 /* Pass a NULL or empty argument as an empty string */
1147 if (argv[i] == NULL || *argv[i] == NUL)
1148 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001149 argvars[i].v_type = VAR_STRING;
1150 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001151 continue;
1152 }
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 /* Recognize a number argument, the others must be strings. */
1155 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1156 if (len != 0 && len == (int)STRLEN(argv[i]))
1157 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001158 argvars[i].v_type = VAR_NUMBER;
1159 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 else
1162 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001163 argvars[i].v_type = VAR_STRING;
1164 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 }
1167
1168 if (safe)
1169 {
1170 save_funccalp = save_funccal();
1171 ++sandbox;
1172 }
1173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1175 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001177 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001180 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 vim_free(argvars);
1182
1183 if (safe)
1184 {
1185 --sandbox;
1186 restore_funccal(save_funccalp);
1187 }
1188 return retval;
1189}
1190#endif
1191
1192/*
1193 * Save the current function call pointer, and set it to NULL.
1194 * Used when executing autocommands and for ":source".
1195 */
1196 void *
1197save_funccal()
1198{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001199 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 current_funccal = NULL;
1202 return (void *)fc;
1203}
1204
1205 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001206restore_funccal(vfc)
1207 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001209 funccall_T *fc = (funccall_T *)vfc;
1210
1211 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212}
1213
Bram Moolenaar05159a02005-02-26 23:04:13 +00001214#if defined(FEAT_PROFILE) || defined(PROTO)
1215/*
1216 * Prepare profiling for entering a child or something else that is not
1217 * counted for the script/function itself.
1218 * Should always be called in pair with prof_child_exit().
1219 */
1220 void
1221prof_child_enter(tm)
1222 proftime_T *tm; /* place to store waittime */
1223{
1224 funccall_T *fc = current_funccal;
1225
1226 if (fc != NULL && fc->func->uf_profiling)
1227 profile_start(&fc->prof_child);
1228 script_prof_save(tm);
1229}
1230
1231/*
1232 * Take care of time spent in a child.
1233 * Should always be called after prof_child_enter().
1234 */
1235 void
1236prof_child_exit(tm)
1237 proftime_T *tm; /* where waittime was stored */
1238{
1239 funccall_T *fc = current_funccal;
1240
1241 if (fc != NULL && fc->func->uf_profiling)
1242 {
1243 profile_end(&fc->prof_child);
1244 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1245 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1246 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1247 }
1248 script_prof_restore(tm);
1249}
1250#endif
1251
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#ifdef FEAT_FOLDING
1254/*
1255 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1256 * it in "*cp". Doesn't give error messages.
1257 */
1258 int
1259eval_foldexpr(arg, cp)
1260 char_u *arg;
1261 int *cp;
1262{
Bram Moolenaar33570922005-01-25 22:26:29 +00001263 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 int retval;
1265 char_u *s;
1266
1267 ++emsg_off;
1268 ++sandbox;
1269 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 retval = 0;
1272 else
1273 {
1274 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001275 if (tv.v_type == VAR_NUMBER)
1276 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001277 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 retval = 0;
1279 else
1280 {
1281 /* If the result is a string, check if there is a non-digit before
1282 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (!VIM_ISDIGIT(*s) && *s != '-')
1285 *cp = *s++;
1286 retval = atol((char *)s);
1287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 --emsg_off;
1291 --sandbox;
1292
1293 return retval;
1294}
1295#endif
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001298 * ":let" list all variable values
1299 * ":let var1 var2" list variable values
1300 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001301 * ":let var += expr" assignment command.
1302 * ":let var -= expr" assignment command.
1303 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 */
1306 void
1307ex_let(eap)
1308 exarg_T *eap;
1309{
1310 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001312 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001314 int var_count = 0;
1315 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001318 expr = skip_var_list(arg, &var_count, &semicolon);
1319 if (expr == NULL)
1320 return;
1321 expr = vim_strchr(expr, '=');
1322 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001324 /*
1325 * ":let" without "=": list variables
1326 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001327 if (*arg == '[')
1328 EMSG(_(e_invarg));
1329 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 /* ":let var1 var2" */
1331 arg = list_arg_vars(eap, arg);
1332 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001335 list_glob_vars();
1336 list_buf_vars();
1337 list_win_vars();
1338 list_vim_vars();
1339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 eap->nextcmd = check_nextcmd(arg);
1341 }
1342 else
1343 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001344 op[0] = '=';
1345 op[1] = NUL;
1346 if (expr > arg)
1347 {
1348 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1349 op[0] = expr[-1]; /* +=, -= or .= */
1350 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001351 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (eap->skip)
1354 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001355 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (eap->skip)
1357 {
1358 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 --emsg_skip;
1361 }
1362 else if (i != FAIL)
1363 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 }
1369}
1370
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001371/*
1372 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1373 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001374 * When "nextchars" is not NULL it points to a string with characters that
1375 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1376 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001377 * Returns OK or FAIL;
1378 */
1379 static int
1380ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1381 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001382 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001383 int copy; /* copy values from "tv", don't move */
1384 int semicolon; /* from skip_var_list() */
1385 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001386 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001387{
1388 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001390 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 listitem_T *item;
1392 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393
1394 if (*arg != '[')
1395 {
1396 /*
1397 * ":let var = expr" or ":for var in list"
1398 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001400 return FAIL;
1401 return OK;
1402 }
1403
1404 /*
1405 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1406 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001407 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 {
1409 EMSG(_(e_listreq));
1410 return FAIL;
1411 }
1412
1413 i = list_len(l);
1414 if (semicolon == 0 && var_count < i)
1415 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001416 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417 return FAIL;
1418 }
1419 if (var_count - semicolon > i)
1420 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001421 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422 return FAIL;
1423 }
1424
1425 item = l->lv_first;
1426 while (*arg != ']')
1427 {
1428 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 item = item->li_next;
1431 if (arg == NULL)
1432 return FAIL;
1433
1434 arg = skipwhite(arg);
1435 if (*arg == ';')
1436 {
1437 /* Put the rest of the list (may be empty) in the var after ';'.
1438 * Create a new list for this. */
1439 l = list_alloc();
1440 if (l == NULL)
1441 return FAIL;
1442 while (item != NULL)
1443 {
1444 list_append_tv(l, &item->li_tv);
1445 item = item->li_next;
1446 }
1447
1448 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001449 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001450 ltv.vval.v_list = l;
1451 l->lv_refcount = 1;
1452
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1454 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001455 clear_tv(&ltv);
1456 if (arg == NULL)
1457 return FAIL;
1458 break;
1459 }
1460 else if (*arg != ',' && *arg != ']')
1461 {
1462 EMSG2(_(e_intern2), "ex_let_vars()");
1463 return FAIL;
1464 }
1465 }
1466
1467 return OK;
1468}
1469
1470/*
1471 * Skip over assignable variable "var" or list of variables "[var, var]".
1472 * Used for ":let varvar = expr" and ":for varvar in expr".
1473 * For "[var, var]" increment "*var_count" for each variable.
1474 * for "[var, var; var]" set "semicolon".
1475 * Return NULL for an error.
1476 */
1477 static char_u *
1478skip_var_list(arg, var_count, semicolon)
1479 char_u *arg;
1480 int *var_count;
1481 int *semicolon;
1482{
1483 char_u *p, *s;
1484
1485 if (*arg == '[')
1486 {
1487 /* "[var, var]": find the matching ']'. */
1488 p = arg;
1489 while (1)
1490 {
1491 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1492 s = skip_var_one(p);
1493 if (s == p)
1494 {
1495 EMSG2(_(e_invarg2), p);
1496 return NULL;
1497 }
1498 ++*var_count;
1499
1500 p = skipwhite(s);
1501 if (*p == ']')
1502 break;
1503 else if (*p == ';')
1504 {
1505 if (*semicolon == 1)
1506 {
1507 EMSG(_("Double ; in list of variables"));
1508 return NULL;
1509 }
1510 *semicolon = 1;
1511 }
1512 else if (*p != ',')
1513 {
1514 EMSG2(_(e_invarg2), p);
1515 return NULL;
1516 }
1517 }
1518 return p + 1;
1519 }
1520 else
1521 return skip_var_one(arg);
1522}
1523
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001524/*
1525 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1526 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 static char_u *
1528skip_var_one(arg)
1529 char_u *arg;
1530{
1531 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1532 ++arg;
1533 return find_name_end(arg, NULL, NULL, TRUE);
1534}
1535
Bram Moolenaara7043832005-01-21 11:56:39 +00001536/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001537 * List variables for hashtab "ht" with prefix "prefix".
1538 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001539 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001541list_hashtable_vars(ht, prefix, empty)
1542 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001543 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001544 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001545{
Bram Moolenaar33570922005-01-25 22:26:29 +00001546 hashitem_T *hi;
1547 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001548 int todo;
1549
1550 todo = ht->ht_used;
1551 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1552 {
1553 if (!HASHITEM_EMPTY(hi))
1554 {
1555 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001556 di = HI2DI(hi);
1557 if (empty || di->di_tv.v_type != VAR_STRING
1558 || di->di_tv.vval.v_string != NULL)
1559 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001560 }
1561 }
1562}
1563
1564/*
1565 * List global variables.
1566 */
1567 static void
1568list_glob_vars()
1569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001571}
1572
1573/*
1574 * List buffer variables.
1575 */
1576 static void
1577list_buf_vars()
1578{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 char_u numbuf[NUMBUFLEN];
1580
Bram Moolenaar33570922005-01-25 22:26:29 +00001581 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001582
1583 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1584 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001585}
1586
1587/*
1588 * List window variables.
1589 */
1590 static void
1591list_win_vars()
1592{
Bram Moolenaar33570922005-01-25 22:26:29 +00001593 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001594}
1595
1596/*
1597 * List Vim variables.
1598 */
1599 static void
1600list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603}
1604
1605/*
1606 * List variables in "arg".
1607 */
1608 static char_u *
1609list_arg_vars(eap, arg)
1610 exarg_T *eap;
1611 char_u *arg;
1612{
1613 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001614 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001616 char_u *name_start;
1617 char_u *arg_subsc;
1618 char_u *tofree;
1619 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620
1621 while (!ends_excmd(*arg) && !got_int)
1622 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001623 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001625 arg = find_name_end(arg, NULL, NULL, TRUE);
1626 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1627 {
1628 emsg_severe = TRUE;
1629 EMSG(_(e_trailing));
1630 break;
1631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001635 /* get_name_len() takes care of expanding curly braces */
1636 name_start = name = arg;
1637 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1638 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001640 /* This is mainly to keep test 49 working: when expanding
1641 * curly braces fails overrule the exception error message. */
1642 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001644 emsg_severe = TRUE;
1645 EMSG2(_(e_invarg2), arg);
1646 break;
1647 }
1648 error = TRUE;
1649 }
1650 else
1651 {
1652 if (tofree != NULL)
1653 name = tofree;
1654 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 else
1657 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001658 /* handle d.key, l[idx], f(expr) */
1659 arg_subsc = arg;
1660 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001661 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001663 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001666 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001667 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001668 case 'g': list_glob_vars(); break;
1669 case 'b': list_buf_vars(); break;
1670 case 'w': list_win_vars(); break;
1671 case 'v': list_vim_vars(); break;
1672 default:
1673 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001674 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001675 }
1676 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001677 {
1678 char_u numbuf[NUMBUFLEN];
1679 char_u *tf;
1680 int c;
1681 char_u *s;
1682
1683 s = echo_string(&tv, &tf, numbuf);
1684 c = *arg;
1685 *arg = NUL;
1686 list_one_var_a((char_u *)"",
1687 arg == arg_subsc ? name : name_start,
1688 tv.v_type, s == NULL ? (char_u *)"" : s);
1689 *arg = c;
1690 vim_free(tf);
1691 }
1692 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 }
1695 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001696
1697 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001699
1700 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701 }
1702
1703 return arg;
1704}
1705
1706/*
1707 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1708 * Returns a pointer to the char just after the var name.
1709 * Returns NULL if there is an error.
1710 */
1711 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001714 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001715 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718{
1719 int c1;
1720 char_u *name;
1721 char_u *p;
1722 char_u *arg_end = NULL;
1723 int len;
1724 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726
1727 /*
1728 * ":let $VAR = expr": Set environment variable.
1729 */
1730 if (*arg == '$')
1731 {
1732 /* Find the end of the name. */
1733 ++arg;
1734 name = arg;
1735 len = get_env_len(&arg);
1736 if (len == 0)
1737 EMSG2(_(e_invarg2), name - 1);
1738 else
1739 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 if (op != NULL && (*op == '+' || *op == '-'))
1741 EMSG2(_(e_letwrong), op);
1742 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 EMSG(_(e_letunexp));
1745 else
1746 {
1747 c1 = name[len];
1748 name[len] = NUL;
1749 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001750 if (op != NULL && *op == '.')
1751 {
1752 int mustfree = FALSE;
1753 char_u *s = vim_getenv(name, &mustfree);
1754
1755 if (s != NULL)
1756 {
1757 p = tofree = concat_str(s, p);
1758 if (mustfree)
1759 vim_free(s);
1760 }
1761 }
1762 if (p != NULL)
1763 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 if (STRICMP(name, "HOME") == 0)
1765 init_homedir();
1766 else if (didset_vim && STRICMP(name, "VIM") == 0)
1767 didset_vim = FALSE;
1768 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1769 didset_vimruntime = FALSE;
1770 name[len] = c1;
1771 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 }
1774 }
1775 }
1776
1777 /*
1778 * ":let &option = expr": Set option value.
1779 * ":let &l:option = expr": Set local option value.
1780 * ":let &g:option = expr": Set global option value.
1781 */
1782 else if (*arg == '&')
1783 {
1784 /* Find the end of the name. */
1785 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 if (p == NULL || (endchars != NULL
1787 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 EMSG(_(e_letunexp));
1789 else
1790 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 long n;
1792 int opt_type;
1793 long numval;
1794 char_u *stringval = NULL;
1795 char_u *s;
1796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 c1 = *p;
1798 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799
1800 n = get_tv_number(tv);
1801 s = get_tv_string(tv);
1802 if (op != NULL && *op != '=')
1803 {
1804 opt_type = get_option_value(arg, &numval,
1805 &stringval, opt_flags);
1806 if ((opt_type == 1 && *op == '.')
1807 || (opt_type == 0 && *op != '.'))
1808 EMSG2(_(e_letwrong), op);
1809 else
1810 {
1811 if (opt_type == 1) /* number */
1812 {
1813 if (*op == '+')
1814 n = numval + n;
1815 else
1816 n = numval - n;
1817 }
1818 else if (opt_type == 0 && stringval != NULL) /* string */
1819 {
1820 s = concat_str(stringval, s);
1821 vim_free(stringval);
1822 stringval = s;
1823 }
1824 }
1825 }
1826 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 *p = c1;
1828 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 }
1831 }
1832
1833 /*
1834 * ":let @r = expr": Set register contents.
1835 */
1836 else if (*arg == '@')
1837 {
1838 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 if (op != NULL && (*op == '+' || *op == '-'))
1840 EMSG2(_(e_letwrong), op);
1841 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 EMSG(_(e_letunexp));
1844 else
1845 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 char_u *tofree = NULL;
1847 char_u *s;
1848
1849 p = get_tv_string(tv);
1850 if (op != NULL && *op == '.')
1851 {
1852 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1853 if (s != NULL)
1854 {
1855 p = tofree = concat_str(s, p);
1856 vim_free(s);
1857 }
1858 }
1859 if (p != NULL)
1860 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 }
1864 }
1865
1866 /*
1867 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001870 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1875 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001877 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1878 EMSG(_(e_letunexp));
1879 else
1880 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001882 arg_end = p;
1883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001885 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 }
1887
1888 else
1889 EMSG2(_(e_invarg2), arg);
1890
1891 return arg_end;
1892}
1893
1894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001895 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1896 */
1897 static int
1898check_changedtick(arg)
1899 char_u *arg;
1900{
1901 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1902 {
1903 EMSG2(_(e_readonlyvar), arg);
1904 return TRUE;
1905 }
1906 return FALSE;
1907}
1908
1909/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001910 * Get an lval: variable, Dict item or List item that can be assigned a value
1911 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1912 * "name.key", "name.key[expr]" etc.
1913 * Indexing only works if "name" is an existing List or Dictionary.
1914 * "name" points to the start of the name.
1915 * If "rettv" is not NULL it points to the value to be assigned.
1916 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1917 * wrong; must end in space or cmd separator.
1918 *
1919 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001920 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 */
1923 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001924get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 typval_T *rettv;
1927 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001928 int unlet;
1929 int skip;
1930 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 char_u *expr_start, *expr_end;
1934 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 dictitem_T *v;
1936 typval_T var1;
1937 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001938 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001940 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001941 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946
1947 if (skip)
1948 {
1949 /* When skipping just find the end of the name. */
1950 lp->ll_name = name;
1951 return find_name_end(name, NULL, NULL, TRUE);
1952 }
1953
1954 /* Find the end of the name. */
1955 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1956 if (expr_start != NULL)
1957 {
1958 /* Don't expand the name when we already know there is an error. */
1959 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1960 && *p != '[' && *p != '.')
1961 {
1962 EMSG(_(e_trailing));
1963 return NULL;
1964 }
1965
1966 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1967 if (lp->ll_exp_name == NULL)
1968 {
1969 /* Report an invalid expression in braces, unless the
1970 * expression evaluation has been cancelled due to an
1971 * aborting error, an interrupt, or an exception. */
1972 if (!aborting() && !quiet)
1973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001974 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001975 EMSG2(_(e_invarg2), name);
1976 return NULL;
1977 }
1978 }
1979 lp->ll_name = lp->ll_exp_name;
1980 }
1981 else
1982 lp->ll_name = name;
1983
1984 /* Without [idx] or .key we are done. */
1985 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1986 return p;
1987
1988 cc = *p;
1989 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00001990 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 if (v == NULL && !quiet)
1992 EMSG2(_(e_undefvar), lp->ll_name);
1993 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 if (v == NULL)
1995 return NULL;
1996
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 /*
1998 * Loop until no more [idx] or .key is following.
1999 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002000 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002001 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2004 && !(lp->ll_tv->v_type == VAR_DICT
2005 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002007 if (!quiet)
2008 EMSG(_("E689: Can only index a List or Dictionary"));
2009 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002013 if (!quiet)
2014 EMSG(_("E708: [:] must come last"));
2015 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002017
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 len = -1;
2019 if (*p == '.')
2020 {
2021 key = p + 1;
2022 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2023 ;
2024 if (len == 0)
2025 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 if (!quiet)
2027 EMSG(_(e_emptykey));
2028 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002029 }
2030 p = key + len;
2031 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002032 else
2033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002034 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002035 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002036 if (*p == ':')
2037 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002038 else
2039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002040 empty1 = FALSE;
2041 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002043 }
2044
2045 /* Optionally get the second index [ :expr]. */
2046 if (*p == ':')
2047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002048 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002052 if (!empty1)
2053 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (rettv != NULL && (rettv->v_type != VAR_LIST
2057 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002058 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002059 if (!quiet)
2060 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002061 if (!empty1)
2062 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002063 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 }
2065 p = skipwhite(p + 1);
2066 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002067 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002068 else
2069 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002071 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 if (!empty1)
2074 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002076 }
2077 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002080 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002081 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002082
Bram Moolenaar8c711452005-01-14 21:53:12 +00002083 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 if (!quiet)
2086 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002087 if (!empty1)
2088 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002090 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002091 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002092 }
2093
2094 /* Skip to past ']'. */
2095 ++p;
2096 }
2097
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002099 {
2100 if (len == -1)
2101 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 key = get_tv_string(&var1);
2104 if (*key == NUL)
2105 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002106 if (!quiet)
2107 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002108 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 }
2111 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 lp->ll_list = NULL;
2113 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002114 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002117 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002121 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 if (len == -1)
2123 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002124 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002125 }
2126 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002130 if (len == -1)
2131 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 p = NULL;
2134 break;
2135 }
2136 if (len == -1)
2137 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002139 }
2140 else
2141 {
2142 /*
2143 * Get the number and item for the only or first index of the List.
2144 */
2145 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 else
2148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002150 clear_tv(&var1);
2151 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 lp->ll_list = lp->ll_tv->vval.v_list;
2154 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2155 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002156 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 if (!quiet)
2158 EMSGN(_(e_listidx), lp->ll_n1);
2159 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002160 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002162 }
2163
2164 /*
2165 * May need to find the item or absolute index for the second
2166 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 * When no index given: "lp->ll_empty2" is TRUE.
2168 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002169 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002170 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002172 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002173 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002177 if (ni == NULL)
2178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 if (!quiet)
2180 EMSGN(_(e_listidx), lp->ll_n2);
2181 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002182 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002184 }
2185
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2187 if (lp->ll_n1 < 0)
2188 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2189 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002190 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 if (!quiet)
2192 EMSGN(_(e_listidx), lp->ll_n2);
2193 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002194 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002195 }
2196
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002198 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
2200
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002201 return p;
2202}
2203
2204/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002205 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 */
2207 static void
2208clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002209 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210{
2211 vim_free(lp->ll_exp_name);
2212 vim_free(lp->ll_newkey);
2213}
2214
2215/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002216 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002218 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 */
2220 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002221set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002222 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002224 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002226 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227{
2228 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002229 listitem_T *ni;
2230 listitem_T *ri;
2231 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232
2233 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 cc = *endp;
2238 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239 if (op != NULL && *op != '=')
2240 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002241 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002243 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2245 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 {
2247 if (tv_op(&tv, rettv, op) == OK)
2248 set_var(lp->ll_name, &tv, FALSE);
2249 clear_tv(&tv);
2250 }
2251 }
2252 else
2253 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002257 else if (tv_check_lock(lp->ll_newkey == NULL
2258 ? lp->ll_tv->v_lock
2259 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2260 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 else if (lp->ll_range)
2262 {
2263 /*
2264 * Assign the List values to the list items.
2265 */
2266 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002267 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 if (op != NULL && *op != '=')
2269 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2270 else
2271 {
2272 clear_tv(&lp->ll_li->li_tv);
2273 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2274 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 ri = ri->li_next;
2276 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2277 break;
2278 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 /* Need to add an empty item. */
2281 ni = listitem_alloc();
2282 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002283 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 ri = NULL;
2285 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002286 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002288 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 ni->li_tv.vval.v_number = 0;
2290 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002291 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002292 lp->ll_li = lp->ll_li->li_next;
2293 ++lp->ll_n1;
2294 }
2295 if (ri != NULL)
2296 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297 else if (lp->ll_empty2
2298 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 : lp->ll_n1 != lp->ll_n2)
2300 EMSG(_("E711: List value has not enough items"));
2301 }
2302 else
2303 {
2304 /*
2305 * Assign to a List or Dictionary item.
2306 */
2307 if (lp->ll_newkey != NULL)
2308 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 if (op != NULL && *op != '=')
2310 {
2311 EMSG2(_(e_letwrong), op);
2312 return;
2313 }
2314
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002316 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 if (di == NULL)
2318 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002319 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2320 {
2321 vim_free(di);
2322 return;
2323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002325 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002326 else if (op != NULL && *op != '=')
2327 {
2328 tv_op(lp->ll_tv, rettv, op);
2329 return;
2330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002333
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 /*
2335 * Assign the value to the variable or list item.
2336 */
2337 if (copy)
2338 copy_tv(rettv, lp->ll_tv);
2339 else
2340 {
2341 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002342 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
2345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346}
2347
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002348/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2350 * Returns OK or FAIL.
2351 */
2352 static int
2353tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002354 typval_T *tv1;
2355 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 char_u *op;
2357{
2358 long n;
2359 char_u numbuf[NUMBUFLEN];
2360 char_u *s;
2361
2362 /* Can't do anything with a Funcref or a Dict on the right. */
2363 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2364 {
2365 switch (tv1->v_type)
2366 {
2367 case VAR_DICT:
2368 case VAR_FUNC:
2369 break;
2370
2371 case VAR_LIST:
2372 if (*op != '+' || tv2->v_type != VAR_LIST)
2373 break;
2374 /* List += List */
2375 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2376 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2377 return OK;
2378
2379 case VAR_NUMBER:
2380 case VAR_STRING:
2381 if (tv2->v_type == VAR_LIST)
2382 break;
2383 if (*op == '+' || *op == '-')
2384 {
2385 /* nr += nr or nr -= nr*/
2386 n = get_tv_number(tv1);
2387 if (*op == '+')
2388 n += get_tv_number(tv2);
2389 else
2390 n -= get_tv_number(tv2);
2391 clear_tv(tv1);
2392 tv1->v_type = VAR_NUMBER;
2393 tv1->vval.v_number = n;
2394 }
2395 else
2396 {
2397 /* str .= str */
2398 s = get_tv_string(tv1);
2399 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2400 clear_tv(tv1);
2401 tv1->v_type = VAR_STRING;
2402 tv1->vval.v_string = s;
2403 }
2404 return OK;
2405 }
2406 }
2407
2408 EMSG2(_(e_letwrong), op);
2409 return FAIL;
2410}
2411
2412/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002413 * Add a watcher to a list.
2414 */
2415 static void
2416list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002417 list_T *l;
2418 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419{
2420 lw->lw_next = l->lv_watch;
2421 l->lv_watch = lw;
2422}
2423
2424/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002425 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 * No warning when it isn't found...
2427 */
2428 static void
2429list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002430 list_T *l;
2431 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432{
Bram Moolenaar33570922005-01-25 22:26:29 +00002433 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002434
2435 lwp = &l->lv_watch;
2436 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2437 {
2438 if (lw == lwrem)
2439 {
2440 *lwp = lw->lw_next;
2441 break;
2442 }
2443 lwp = &lw->lw_next;
2444 }
2445}
2446
2447/*
2448 * Just before removing an item from a list: advance watchers to the next
2449 * item.
2450 */
2451 static void
2452list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002453 list_T *l;
2454 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455{
Bram Moolenaar33570922005-01-25 22:26:29 +00002456 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002457
2458 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2459 if (lw->lw_item == item)
2460 lw->lw_item = item->li_next;
2461}
2462
2463/*
2464 * Evaluate the expression used in a ":for var in expr" command.
2465 * "arg" points to "var".
2466 * Set "*errp" to TRUE for an error, FALSE otherwise;
2467 * Return a pointer that holds the info. Null when there is an error.
2468 */
2469 void *
2470eval_for_line(arg, errp, nextcmdp, skip)
2471 char_u *arg;
2472 int *errp;
2473 char_u **nextcmdp;
2474 int skip;
2475{
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002478 typval_T tv;
2479 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002480
2481 *errp = TRUE; /* default: there is an error */
2482
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002484 if (fi == NULL)
2485 return NULL;
2486
2487 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2488 if (expr == NULL)
2489 return fi;
2490
2491 expr = skipwhite(expr);
2492 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2493 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002494 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002495 return fi;
2496 }
2497
2498 if (skip)
2499 ++emsg_skip;
2500 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2501 {
2502 *errp = FALSE;
2503 if (!skip)
2504 {
2505 l = tv.vval.v_list;
2506 if (tv.v_type != VAR_LIST || l == NULL)
2507 EMSG(_(e_listreq));
2508 else
2509 {
2510 fi->fi_list = l;
2511 list_add_watch(l, &fi->fi_lw);
2512 fi->fi_lw.lw_item = l->lv_first;
2513 }
2514 }
2515 }
2516 if (skip)
2517 --emsg_skip;
2518
2519 return fi;
2520}
2521
2522/*
2523 * Use the first item in a ":for" list. Advance to the next.
2524 * Assign the values to the variable (list). "arg" points to the first one.
2525 * Return TRUE when a valid item was found, FALSE when at end of list or
2526 * something wrong.
2527 */
2528 int
2529next_for_item(fi_void, arg)
2530 void *fi_void;
2531 char_u *arg;
2532{
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002534 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002536
2537 item = fi->fi_lw.lw_item;
2538 if (item == NULL)
2539 result = FALSE;
2540 else
2541 {
2542 fi->fi_lw.lw_item = item->li_next;
2543 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2544 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2545 }
2546 return result;
2547}
2548
2549/*
2550 * Free the structure used to store info used by ":for".
2551 */
2552 void
2553free_for_info(fi_void)
2554 void *fi_void;
2555{
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002557
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002558 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002559 list_rem_watch(fi->fi_list, &fi->fi_lw);
2560 vim_free(fi);
2561}
2562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2564
2565 void
2566set_context_for_expression(xp, arg, cmdidx)
2567 expand_T *xp;
2568 char_u *arg;
2569 cmdidx_T cmdidx;
2570{
2571 int got_eq = FALSE;
2572 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002573 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002575 if (cmdidx == CMD_let)
2576 {
2577 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002578 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002579 {
2580 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002581 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002582 {
2583 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002585 if (vim_iswhite(*p))
2586 break;
2587 }
2588 return;
2589 }
2590 }
2591 else
2592 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2593 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 while ((xp->xp_pattern = vim_strpbrk(arg,
2595 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2596 {
2597 c = *xp->xp_pattern;
2598 if (c == '&')
2599 {
2600 c = xp->xp_pattern[1];
2601 if (c == '&')
2602 {
2603 ++xp->xp_pattern;
2604 xp->xp_context = cmdidx != CMD_let || got_eq
2605 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2606 }
2607 else if (c != ' ')
2608 xp->xp_context = EXPAND_SETTINGS;
2609 }
2610 else if (c == '$')
2611 {
2612 /* environment variable */
2613 xp->xp_context = EXPAND_ENV_VARS;
2614 }
2615 else if (c == '=')
2616 {
2617 got_eq = TRUE;
2618 xp->xp_context = EXPAND_EXPRESSION;
2619 }
2620 else if (c == '<'
2621 && xp->xp_context == EXPAND_FUNCTIONS
2622 && vim_strchr(xp->xp_pattern, '(') == NULL)
2623 {
2624 /* Function name can start with "<SNR>" */
2625 break;
2626 }
2627 else if (cmdidx != CMD_let || got_eq)
2628 {
2629 if (c == '"') /* string */
2630 {
2631 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2632 if (c == '\\' && xp->xp_pattern[1] != NUL)
2633 ++xp->xp_pattern;
2634 xp->xp_context = EXPAND_NOTHING;
2635 }
2636 else if (c == '\'') /* literal string */
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2640 /* skip */ ;
2641 xp->xp_context = EXPAND_NOTHING;
2642 }
2643 else if (c == '|')
2644 {
2645 if (xp->xp_pattern[1] == '|')
2646 {
2647 ++xp->xp_pattern;
2648 xp->xp_context = EXPAND_EXPRESSION;
2649 }
2650 else
2651 xp->xp_context = EXPAND_COMMANDS;
2652 }
2653 else
2654 xp->xp_context = EXPAND_EXPRESSION;
2655 }
2656 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002657 /* Doesn't look like something valid, expand as an expression
2658 * anyway. */
2659 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 arg = xp->xp_pattern;
2661 if (*arg != NUL)
2662 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2663 /* skip */ ;
2664 }
2665 xp->xp_pattern = arg;
2666}
2667
2668#endif /* FEAT_CMDL_COMPL */
2669
2670/*
2671 * ":1,25call func(arg1, arg2)" function call.
2672 */
2673 void
2674ex_call(eap)
2675 exarg_T *eap;
2676{
2677 char_u *arg = eap->arg;
2678 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002682 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 linenr_T lnum;
2684 int doesrange;
2685 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002686 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002688 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2689 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 if (tofree == NULL)
2691 return;
2692
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002693 /* Increase refcount on dictionary, it could get deleted when evaluating
2694 * the arguments. */
2695 if (fudi.fd_dict != NULL)
2696 ++fudi.fd_dict->dv_refcount;
2697
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2699 len = STRLEN(tofree);
2700 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaar532c7802005-01-27 14:44:31 +00002702 /* Skip white space to allow ":call func ()". Not good, but required for
2703 * backward compatibility. */
2704 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
2707 if (*startarg != '(')
2708 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002709 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 goto end;
2711 }
2712
2713 /*
2714 * When skipping, evaluate the function once, to find the end of the
2715 * arguments.
2716 * When the function takes a range, this is discovered after the first
2717 * call, and the loop is broken.
2718 */
2719 if (eap->skip)
2720 {
2721 ++emsg_skip;
2722 lnum = eap->line2; /* do it once, also with an invalid range */
2723 }
2724 else
2725 lnum = eap->line1;
2726 for ( ; lnum <= eap->line2; ++lnum)
2727 {
2728 if (!eap->skip && eap->addr_count > 0)
2729 {
2730 curwin->w_cursor.lnum = lnum;
2731 curwin->w_cursor.col = 0;
2732 }
2733 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002734 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002735 eap->line1, eap->line2, &doesrange,
2736 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 failed = TRUE;
2739 break;
2740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (doesrange || eap->skip)
2743 break;
2744 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002745 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002746 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002747 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (aborting())
2749 break;
2750 }
2751 if (eap->skip)
2752 --emsg_skip;
2753
2754 if (!failed)
2755 {
2756 /* Check for trailing illegal characters and a following command. */
2757 if (!ends_excmd(*arg))
2758 {
2759 emsg_severe = TRUE;
2760 EMSG(_(e_trailing));
2761 }
2762 else
2763 eap->nextcmd = check_nextcmd(arg);
2764 }
2765
2766end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002767 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769}
2770
2771/*
2772 * ":unlet[!] var1 ... " command.
2773 */
2774 void
2775ex_unlet(eap)
2776 exarg_T *eap;
2777{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002778 ex_unletlock(eap, eap->arg, 0);
2779}
2780
2781/*
2782 * ":lockvar" and ":unlockvar" commands
2783 */
2784 void
2785ex_lockvar(eap)
2786 exarg_T *eap;
2787{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002789 int deep = 2;
2790
2791 if (eap->forceit)
2792 deep = -1;
2793 else if (vim_isdigit(*arg))
2794 {
2795 deep = getdigits(&arg);
2796 arg = skipwhite(arg);
2797 }
2798
2799 ex_unletlock(eap, arg, deep);
2800}
2801
2802/*
2803 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2804 */
2805 static void
2806ex_unletlock(eap, argstart, deep)
2807 exarg_T *eap;
2808 char_u *argstart;
2809 int deep;
2810{
2811 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002814 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816 do
2817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 /* Parse the name and find the end. */
2819 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2820 if (lv.ll_name == NULL)
2821 error = TRUE; /* error but continue parsing */
2822 if (name_end == NULL || (!vim_iswhite(*name_end)
2823 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (name_end != NULL)
2826 {
2827 emsg_severe = TRUE;
2828 EMSG(_(e_trailing));
2829 }
2830 if (!(eap->skip || error))
2831 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 break;
2833 }
2834
2835 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002836 {
2837 if (eap->cmdidx == CMD_unlet)
2838 {
2839 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2840 error = TRUE;
2841 }
2842 else
2843 {
2844 if (do_lock_var(&lv, name_end, deep,
2845 eap->cmdidx == CMD_lockvar) == FAIL)
2846 error = TRUE;
2847 }
2848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (!eap->skip)
2851 clear_lval(&lv);
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 arg = skipwhite(name_end);
2854 } while (!ends_excmd(*arg));
2855
2856 eap->nextcmd = check_nextcmd(arg);
2857}
2858
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 int forceit;
2864{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 int ret = OK;
2866 int cc;
2867
2868 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 cc = *name_end;
2871 *name_end = NUL;
2872
2873 /* Normal name or expanded name. */
2874 if (check_changedtick(lp->ll_name))
2875 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002876 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002880 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2881 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 else if (lp->ll_range)
2883 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885
2886 /* Delete a range of List items. */
2887 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2888 {
2889 li = lp->ll_li->li_next;
2890 listitem_remove(lp->ll_list, lp->ll_li);
2891 lp->ll_li = li;
2892 ++lp->ll_n1;
2893 }
2894 }
2895 else
2896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 /* unlet a List item. */
2899 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002902 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 }
2904
2905 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002906}
2907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908/*
2909 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 */
2912 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 hashtab_T *ht;
2918 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002919 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 ht = find_var_ht(name, &varname);
2922 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 hi = hash_find(ht, varname);
2925 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002926 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 if (var_check_ro(HI2DI(hi)->di_flags, name))
2928 return FAIL;
2929 delete_var(ht, hi);
2930 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 if (forceit)
2934 return OK;
2935 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 return FAIL;
2937}
2938
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002939/*
2940 * Lock or unlock variable indicated by "lp".
2941 * "deep" is the levels to go (-1 for unlimited);
2942 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2943 */
2944 static int
2945do_lock_var(lp, name_end, deep, lock)
2946 lval_T *lp;
2947 char_u *name_end;
2948 int deep;
2949 int lock;
2950{
2951 int ret = OK;
2952 int cc;
2953 dictitem_T *di;
2954
2955 if (deep == 0) /* nothing to do */
2956 return OK;
2957
2958 if (lp->ll_tv == NULL)
2959 {
2960 cc = *name_end;
2961 *name_end = NUL;
2962
2963 /* Normal name or expanded name. */
2964 if (check_changedtick(lp->ll_name))
2965 ret = FAIL;
2966 else
2967 {
2968 di = find_var(lp->ll_name, NULL);
2969 if (di == NULL)
2970 ret = FAIL;
2971 else
2972 {
2973 if (lock)
2974 di->di_flags |= DI_FLAGS_LOCK;
2975 else
2976 di->di_flags &= ~DI_FLAGS_LOCK;
2977 item_lock(&di->di_tv, deep, lock);
2978 }
2979 }
2980 *name_end = cc;
2981 }
2982 else if (lp->ll_range)
2983 {
2984 listitem_T *li = lp->ll_li;
2985
2986 /* (un)lock a range of List items. */
2987 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2988 {
2989 item_lock(&li->li_tv, deep, lock);
2990 li = li->li_next;
2991 ++lp->ll_n1;
2992 }
2993 }
2994 else if (lp->ll_list != NULL)
2995 /* (un)lock a List item. */
2996 item_lock(&lp->ll_li->li_tv, deep, lock);
2997 else
2998 /* un(lock) a Dictionary item. */
2999 item_lock(&lp->ll_di->di_tv, deep, lock);
3000
3001 return ret;
3002}
3003
3004/*
3005 * Lock or unlock an item. "deep" is nr of levels to go.
3006 */
3007 static void
3008item_lock(tv, deep, lock)
3009 typval_T *tv;
3010 int deep;
3011 int lock;
3012{
3013 static int recurse = 0;
3014 list_T *l;
3015 listitem_T *li;
3016 dict_T *d;
3017 hashitem_T *hi;
3018 int todo;
3019
3020 if (recurse >= DICT_MAXNEST)
3021 {
3022 EMSG(_("E743: variable nested too deep for (un)lock"));
3023 return;
3024 }
3025 if (deep == 0)
3026 return;
3027 ++recurse;
3028
3029 /* lock/unlock the item itself */
3030 if (lock)
3031 tv->v_lock |= VAR_LOCKED;
3032 else
3033 tv->v_lock &= ~VAR_LOCKED;
3034
3035 switch (tv->v_type)
3036 {
3037 case VAR_LIST:
3038 if ((l = tv->vval.v_list) != NULL)
3039 {
3040 if (lock)
3041 l->lv_lock |= VAR_LOCKED;
3042 else
3043 l->lv_lock &= ~VAR_LOCKED;
3044 if (deep < 0 || deep > 1)
3045 /* recursive: lock/unlock the items the List contains */
3046 for (li = l->lv_first; li != NULL; li = li->li_next)
3047 item_lock(&li->li_tv, deep - 1, lock);
3048 }
3049 break;
3050 case VAR_DICT:
3051 if ((d = tv->vval.v_dict) != NULL)
3052 {
3053 if (lock)
3054 d->dv_lock |= VAR_LOCKED;
3055 else
3056 d->dv_lock &= ~VAR_LOCKED;
3057 if (deep < 0 || deep > 1)
3058 {
3059 /* recursive: lock/unlock the items the List contains */
3060 todo = d->dv_hashtab.ht_used;
3061 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3062 {
3063 if (!HASHITEM_EMPTY(hi))
3064 {
3065 --todo;
3066 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3067 }
3068 }
3069 }
3070 }
3071 }
3072 --recurse;
3073}
3074
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3076/*
3077 * Delete all "menutrans_" variables.
3078 */
3079 void
3080del_menutrans_vars()
3081{
Bram Moolenaar33570922005-01-25 22:26:29 +00003082 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003083 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
Bram Moolenaar33570922005-01-25 22:26:29 +00003085 hash_lock(&globvarht);
3086 todo = globvarht.ht_used;
3087 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003088 {
3089 if (!HASHITEM_EMPTY(hi))
3090 {
3091 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003092 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3093 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003094 }
3095 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098#endif
3099
3100#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3101
3102/*
3103 * Local string buffer for the next two functions to store a variable name
3104 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3105 * get_user_var_name().
3106 */
3107
3108static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3109
3110static char_u *varnamebuf = NULL;
3111static int varnamebuflen = 0;
3112
3113/*
3114 * Function to concatenate a prefix and a variable name.
3115 */
3116 static char_u *
3117cat_prefix_varname(prefix, name)
3118 int prefix;
3119 char_u *name;
3120{
3121 int len;
3122
3123 len = (int)STRLEN(name) + 3;
3124 if (len > varnamebuflen)
3125 {
3126 vim_free(varnamebuf);
3127 len += 10; /* some additional space */
3128 varnamebuf = alloc(len);
3129 if (varnamebuf == NULL)
3130 {
3131 varnamebuflen = 0;
3132 return NULL;
3133 }
3134 varnamebuflen = len;
3135 }
3136 *varnamebuf = prefix;
3137 varnamebuf[1] = ':';
3138 STRCPY(varnamebuf + 2, name);
3139 return varnamebuf;
3140}
3141
3142/*
3143 * Function given to ExpandGeneric() to obtain the list of user defined
3144 * (global/buffer/window/built-in) variable names.
3145 */
3146/*ARGSUSED*/
3147 char_u *
3148get_user_var_name(xp, idx)
3149 expand_T *xp;
3150 int idx;
3151{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003152 static long_u gdone;
3153 static long_u bdone;
3154 static long_u wdone;
3155 static int vidx;
3156 static hashitem_T *hi;
3157 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003160 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003161
3162 /* Global variables */
3163 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003165 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003167 else
3168 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003169 while (HASHITEM_EMPTY(hi))
3170 ++hi;
3171 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3172 return cat_prefix_varname('g', hi->hi_key);
3173 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003175
3176 /* b: variables */
3177 ht = &curbuf->b_vars.dv_hashtab;
3178 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003180 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003182 else
3183 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003184 while (HASHITEM_EMPTY(hi))
3185 ++hi;
3186 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003190 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 return (char_u *)"b:changedtick";
3192 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003193
3194 /* w: variables */
3195 ht = &curwin->w_vars.dv_hashtab;
3196 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003198 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003200 else
3201 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003202 while (HASHITEM_EMPTY(hi))
3203 ++hi;
3204 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003206
3207 /* v: variables */
3208 if (vidx < VV_LEN)
3209 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 vim_free(varnamebuf);
3212 varnamebuf = NULL;
3213 varnamebuflen = 0;
3214 return NULL;
3215}
3216
3217#endif /* FEAT_CMDL_COMPL */
3218
3219/*
3220 * types for expressions.
3221 */
3222typedef enum
3223{
3224 TYPE_UNKNOWN = 0
3225 , TYPE_EQUAL /* == */
3226 , TYPE_NEQUAL /* != */
3227 , TYPE_GREATER /* > */
3228 , TYPE_GEQUAL /* >= */
3229 , TYPE_SMALLER /* < */
3230 , TYPE_SEQUAL /* <= */
3231 , TYPE_MATCH /* =~ */
3232 , TYPE_NOMATCH /* !~ */
3233} exptype_T;
3234
3235/*
3236 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3239 */
3240
3241/*
3242 * Handle zero level expression.
3243 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003244 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * Return OK or FAIL.
3246 */
3247 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003248eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u **nextcmd;
3252 int evaluate;
3253{
3254 int ret;
3255 char_u *p;
3256
3257 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003258 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (ret == FAIL || !ends_excmd(*p))
3260 {
3261 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003262 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 /*
3264 * Report the invalid expression unless the expression evaluation has
3265 * been cancelled due to an aborting error, an interrupt, or an
3266 * exception.
3267 */
3268 if (!aborting())
3269 EMSG2(_(e_invexpr2), arg);
3270 ret = FAIL;
3271 }
3272 if (nextcmd != NULL)
3273 *nextcmd = check_nextcmd(p);
3274
3275 return ret;
3276}
3277
3278/*
3279 * Handle top level expression:
3280 * expr1 ? expr0 : expr0
3281 *
3282 * "arg" must point to the first non-white of the expression.
3283 * "arg" is advanced to the next non-white after the recognized expression.
3284 *
3285 * Return OK or FAIL.
3286 */
3287 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003288eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 int evaluate;
3292{
3293 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003294 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 /*
3297 * Get the first variable.
3298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003299 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 return FAIL;
3301
3302 if ((*arg)[0] == '?')
3303 {
3304 result = FALSE;
3305 if (evaluate)
3306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003307 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003309 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311
3312 /*
3313 * Get the second variable.
3314 */
3315 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003316 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return FAIL;
3318
3319 /*
3320 * Check for the ":".
3321 */
3322 if ((*arg)[0] != ':')
3323 {
3324 EMSG(_("E109: Missing ':' after '?'"));
3325 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003326 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return FAIL;
3328 }
3329
3330 /*
3331 * Get the third variable.
3332 */
3333 *arg = skipwhite(*arg + 1);
3334 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3335 {
3336 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003337 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 return FAIL;
3339 }
3340 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343
3344 return OK;
3345}
3346
3347/*
3348 * Handle first level expression:
3349 * expr2 || expr2 || expr2 logical OR
3350 *
3351 * "arg" must point to the first non-white of the expression.
3352 * "arg" is advanced to the next non-white after the recognized expression.
3353 *
3354 * Return OK or FAIL.
3355 */
3356 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 int evaluate;
3361{
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 long result;
3364 int first;
3365
3366 /*
3367 * Get the first variable.
3368 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003369 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return FAIL;
3371
3372 /*
3373 * Repeat until there is no following "||".
3374 */
3375 first = TRUE;
3376 result = FALSE;
3377 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3378 {
3379 if (evaluate && first)
3380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003383 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 first = FALSE;
3385 }
3386
3387 /*
3388 * Get the second variable.
3389 */
3390 *arg = skipwhite(*arg + 2);
3391 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3392 return FAIL;
3393
3394 /*
3395 * Compute the result.
3396 */
3397 if (evaluate && !result)
3398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 if (evaluate)
3404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 rettv->v_type = VAR_NUMBER;
3406 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408 }
3409
3410 return OK;
3411}
3412
3413/*
3414 * Handle second level expression:
3415 * expr3 && expr3 && expr3 logical AND
3416 *
3417 * "arg" must point to the first non-white of the expression.
3418 * "arg" is advanced to the next non-white after the recognized expression.
3419 *
3420 * Return OK or FAIL.
3421 */
3422 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003423eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 int evaluate;
3427{
Bram Moolenaar33570922005-01-25 22:26:29 +00003428 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 long result;
3430 int first;
3431
3432 /*
3433 * Get the first variable.
3434 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return FAIL;
3437
3438 /*
3439 * Repeat until there is no following "&&".
3440 */
3441 first = TRUE;
3442 result = TRUE;
3443 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3444 {
3445 if (evaluate && first)
3446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003447 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 first = FALSE;
3451 }
3452
3453 /*
3454 * Get the second variable.
3455 */
3456 *arg = skipwhite(*arg + 2);
3457 if (eval4(arg, &var2, evaluate && result) == FAIL)
3458 return FAIL;
3459
3460 /*
3461 * Compute the result.
3462 */
3463 if (evaluate && result)
3464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 if (evaluate)
3470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 rettv->v_type = VAR_NUMBER;
3472 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474 }
3475
3476 return OK;
3477}
3478
3479/*
3480 * Handle third level expression:
3481 * var1 == var2
3482 * var1 =~ var2
3483 * var1 != var2
3484 * var1 !~ var2
3485 * var1 > var2
3486 * var1 >= var2
3487 * var1 < var2
3488 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003489 * var1 is var2
3490 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 *
3492 * "arg" must point to the first non-white of the expression.
3493 * "arg" is advanced to the next non-white after the recognized expression.
3494 *
3495 * Return OK or FAIL.
3496 */
3497 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003498eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 int evaluate;
3502{
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 char_u *p;
3505 int i;
3506 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003507 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 int len = 2;
3509 long n1, n2;
3510 char_u *s1, *s2;
3511 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3512 regmatch_T regmatch;
3513 int ic;
3514 char_u *save_cpo;
3515
3516 /*
3517 * Get the first variable.
3518 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return FAIL;
3521
3522 p = *arg;
3523 switch (p[0])
3524 {
3525 case '=': if (p[1] == '=')
3526 type = TYPE_EQUAL;
3527 else if (p[1] == '~')
3528 type = TYPE_MATCH;
3529 break;
3530 case '!': if (p[1] == '=')
3531 type = TYPE_NEQUAL;
3532 else if (p[1] == '~')
3533 type = TYPE_NOMATCH;
3534 break;
3535 case '>': if (p[1] != '=')
3536 {
3537 type = TYPE_GREATER;
3538 len = 1;
3539 }
3540 else
3541 type = TYPE_GEQUAL;
3542 break;
3543 case '<': if (p[1] != '=')
3544 {
3545 type = TYPE_SMALLER;
3546 len = 1;
3547 }
3548 else
3549 type = TYPE_SEQUAL;
3550 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003551 case 'i': if (p[1] == 's')
3552 {
3553 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3554 len = 5;
3555 if (!vim_isIDc(p[len]))
3556 {
3557 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3558 type_is = TRUE;
3559 }
3560 }
3561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563
3564 /*
3565 * If there is a comparitive operator, use it.
3566 */
3567 if (type != TYPE_UNKNOWN)
3568 {
3569 /* extra question mark appended: ignore case */
3570 if (p[len] == '?')
3571 {
3572 ic = TRUE;
3573 ++len;
3574 }
3575 /* extra '#' appended: match case */
3576 else if (p[len] == '#')
3577 {
3578 ic = FALSE;
3579 ++len;
3580 }
3581 /* nothing appened: use 'ignorecase' */
3582 else
3583 ic = p_ic;
3584
3585 /*
3586 * Get the second variable.
3587 */
3588 *arg = skipwhite(p + len);
3589 if (eval5(arg, &var2, evaluate) == FAIL)
3590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 return FAIL;
3593 }
3594
3595 if (evaluate)
3596 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003597 if (type_is && rettv->v_type != var2.v_type)
3598 {
3599 /* For "is" a different type always means FALSE, for "notis"
3600 * it means TRUE. */
3601 n1 = (type == TYPE_NEQUAL);
3602 }
3603 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3604 {
3605 if (type_is)
3606 {
3607 n1 = (rettv->v_type == var2.v_type
3608 && rettv->vval.v_list == var2.vval.v_list);
3609 if (type == TYPE_NEQUAL)
3610 n1 = !n1;
3611 }
3612 else if (rettv->v_type != var2.v_type
3613 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3614 {
3615 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003616 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003617 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003618 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003619 clear_tv(rettv);
3620 clear_tv(&var2);
3621 return FAIL;
3622 }
3623 else
3624 {
3625 /* Compare two Lists for being equal or unequal. */
3626 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3627 if (type == TYPE_NEQUAL)
3628 n1 = !n1;
3629 }
3630 }
3631
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003632 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3633 {
3634 if (type_is)
3635 {
3636 n1 = (rettv->v_type == var2.v_type
3637 && rettv->vval.v_dict == var2.vval.v_dict);
3638 if (type == TYPE_NEQUAL)
3639 n1 = !n1;
3640 }
3641 else if (rettv->v_type != var2.v_type
3642 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3643 {
3644 if (rettv->v_type != var2.v_type)
3645 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3646 else
3647 EMSG(_("E736: Invalid operation for Dictionary"));
3648 clear_tv(rettv);
3649 clear_tv(&var2);
3650 return FAIL;
3651 }
3652 else
3653 {
3654 /* Compare two Dictionaries for being equal or unequal. */
3655 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3656 if (type == TYPE_NEQUAL)
3657 n1 = !n1;
3658 }
3659 }
3660
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003661 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3662 {
3663 if (rettv->v_type != var2.v_type
3664 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3665 {
3666 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003667 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003668 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003669 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003670 clear_tv(rettv);
3671 clear_tv(&var2);
3672 return FAIL;
3673 }
3674 else
3675 {
3676 /* Compare two Funcrefs for being equal or unequal. */
3677 if (rettv->vval.v_string == NULL
3678 || var2.vval.v_string == NULL)
3679 n1 = FALSE;
3680 else
3681 n1 = STRCMP(rettv->vval.v_string,
3682 var2.vval.v_string) == 0;
3683 if (type == TYPE_NEQUAL)
3684 n1 = !n1;
3685 }
3686 }
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 /*
3689 * If one of the two variables is a number, compare as a number.
3690 * When using "=~" or "!~", always compare as string.
3691 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003692 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 n1 = get_tv_number(rettv);
3696 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 switch (type)
3698 {
3699 case TYPE_EQUAL: n1 = (n1 == n2); break;
3700 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3701 case TYPE_GREATER: n1 = (n1 > n2); break;
3702 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3703 case TYPE_SMALLER: n1 = (n1 < n2); break;
3704 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3705 case TYPE_UNKNOWN:
3706 case TYPE_MATCH:
3707 case TYPE_NOMATCH: break; /* avoid gcc warning */
3708 }
3709 }
3710 else
3711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003712 s1 = get_tv_string_buf(rettv, buf1);
3713 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3715 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3716 else
3717 i = 0;
3718 n1 = FALSE;
3719 switch (type)
3720 {
3721 case TYPE_EQUAL: n1 = (i == 0); break;
3722 case TYPE_NEQUAL: n1 = (i != 0); break;
3723 case TYPE_GREATER: n1 = (i > 0); break;
3724 case TYPE_GEQUAL: n1 = (i >= 0); break;
3725 case TYPE_SMALLER: n1 = (i < 0); break;
3726 case TYPE_SEQUAL: n1 = (i <= 0); break;
3727
3728 case TYPE_MATCH:
3729 case TYPE_NOMATCH:
3730 /* avoid 'l' flag in 'cpoptions' */
3731 save_cpo = p_cpo;
3732 p_cpo = (char_u *)"";
3733 regmatch.regprog = vim_regcomp(s2,
3734 RE_MAGIC + RE_STRING);
3735 regmatch.rm_ic = ic;
3736 if (regmatch.regprog != NULL)
3737 {
3738 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3739 vim_free(regmatch.regprog);
3740 if (type == TYPE_NOMATCH)
3741 n1 = !n1;
3742 }
3743 p_cpo = save_cpo;
3744 break;
3745
3746 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3747 }
3748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003749 clear_tv(rettv);
3750 clear_tv(&var2);
3751 rettv->v_type = VAR_NUMBER;
3752 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 }
3755
3756 return OK;
3757}
3758
3759/*
3760 * Handle fourth level expression:
3761 * + number addition
3762 * - number subtraction
3763 * . string concatenation
3764 *
3765 * "arg" must point to the first non-white of the expression.
3766 * "arg" is advanced to the next non-white after the recognized expression.
3767 *
3768 * Return OK or FAIL.
3769 */
3770 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int evaluate;
3775{
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 typval_T var2;
3777 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 int op;
3779 long n1, n2;
3780 char_u *s1, *s2;
3781 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3782 char_u *p;
3783
3784 /*
3785 * Get the first variable.
3786 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 return FAIL;
3789
3790 /*
3791 * Repeat computing, until no '+', '-' or '.' is following.
3792 */
3793 for (;;)
3794 {
3795 op = **arg;
3796 if (op != '+' && op != '-' && op != '.')
3797 break;
3798
3799 /*
3800 * Get the second variable.
3801 */
3802 *arg = skipwhite(*arg + 1);
3803 if (eval6(arg, &var2, evaluate) == FAIL)
3804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 return FAIL;
3807 }
3808
3809 if (evaluate)
3810 {
3811 /*
3812 * Compute the result.
3813 */
3814 if (op == '.')
3815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 s1 = get_tv_string_buf(rettv, buf1);
3817 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003818 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003819 clear_tv(rettv);
3820 rettv->v_type = VAR_STRING;
3821 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003823 else if (op == '+' && rettv->v_type == VAR_LIST
3824 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003825 {
3826 /* concatenate Lists */
3827 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3828 &var3) == FAIL)
3829 {
3830 clear_tv(rettv);
3831 clear_tv(&var2);
3832 return FAIL;
3833 }
3834 clear_tv(rettv);
3835 *rettv = var3;
3836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 else
3838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 n1 = get_tv_number(rettv);
3840 n2 = get_tv_number(&var2);
3841 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (op == '+')
3843 n1 = n1 + n2;
3844 else
3845 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 rettv->v_type = VAR_NUMBER;
3847 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003849 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 }
3852 return OK;
3853}
3854
3855/*
3856 * Handle fifth level expression:
3857 * * number multiplication
3858 * / number division
3859 * % number modulo
3860 *
3861 * "arg" must point to the first non-white of the expression.
3862 * "arg" is advanced to the next non-white after the recognized expression.
3863 *
3864 * Return OK or FAIL.
3865 */
3866 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int evaluate;
3871{
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 int op;
3874 long n1, n2;
3875
3876 /*
3877 * Get the first variable.
3878 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return FAIL;
3881
3882 /*
3883 * Repeat computing, until no '*', '/' or '%' is following.
3884 */
3885 for (;;)
3886 {
3887 op = **arg;
3888 if (op != '*' && op != '/' && op != '%')
3889 break;
3890
3891 if (evaluate)
3892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003893 n1 = get_tv_number(rettv);
3894 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 else
3897 n1 = 0;
3898
3899 /*
3900 * Get the second variable.
3901 */
3902 *arg = skipwhite(*arg + 1);
3903 if (eval7(arg, &var2, evaluate) == FAIL)
3904 return FAIL;
3905
3906 if (evaluate)
3907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 n2 = get_tv_number(&var2);
3909 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 /*
3912 * Compute the result.
3913 */
3914 if (op == '*')
3915 n1 = n1 * n2;
3916 else if (op == '/')
3917 {
3918 if (n2 == 0) /* give an error message? */
3919 n1 = 0x7fffffffL;
3920 else
3921 n1 = n1 / n2;
3922 }
3923 else
3924 {
3925 if (n2 == 0) /* give an error message? */
3926 n1 = 0;
3927 else
3928 n1 = n1 % n2;
3929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003930 rettv->v_type = VAR_NUMBER;
3931 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 }
3934
3935 return OK;
3936}
3937
3938/*
3939 * Handle sixth level expression:
3940 * number number constant
3941 * "string" string contstant
3942 * 'string' literal string contstant
3943 * &option-name option value
3944 * @r register contents
3945 * identifier variable value
3946 * function() function call
3947 * $VAR environment variable
3948 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003949 * [expr, expr] List
3950 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 *
3952 * Also handle:
3953 * ! in front logical NOT
3954 * - in front unary minus
3955 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003956 * trailing [] subscript in String or List
3957 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 *
3959 * "arg" must point to the first non-white of the expression.
3960 * "arg" is advanced to the next non-white after the recognized expression.
3961 *
3962 * Return OK or FAIL.
3963 */
3964 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 int evaluate;
3969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 long n;
3971 int len;
3972 char_u *s;
3973 int val;
3974 char_u *start_leader, *end_leader;
3975 int ret = OK;
3976 char_u *alias;
3977
3978 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003979 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003980 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003982 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 /*
3985 * Skip '!' and '-' characters. They are handled later.
3986 */
3987 start_leader = *arg;
3988 while (**arg == '!' || **arg == '-' || **arg == '+')
3989 *arg = skipwhite(*arg + 1);
3990 end_leader = *arg;
3991
3992 switch (**arg)
3993 {
3994 /*
3995 * Number constant.
3996 */
3997 case '0':
3998 case '1':
3999 case '2':
4000 case '3':
4001 case '4':
4002 case '5':
4003 case '6':
4004 case '7':
4005 case '8':
4006 case '9':
4007 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4008 *arg += len;
4009 if (evaluate)
4010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 rettv->v_type = VAR_NUMBER;
4012 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014 break;
4015
4016 /*
4017 * String constant: "string".
4018 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 break;
4021
4022 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004026 break;
4027
4028 /*
4029 * List: [expr, expr]
4030 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 break;
4033
4034 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004035 * Dictionary: {key: val, key: val}
4036 */
4037 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4038 break;
4039
4040 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004041 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004043 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 break;
4045
4046 /*
4047 * Environment variable: $VAR.
4048 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 break;
4051
4052 /*
4053 * Register contents: @r.
4054 */
4055 case '@': ++*arg;
4056 if (evaluate)
4057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 rettv->v_type = VAR_STRING;
4059 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 if (**arg != NUL)
4062 ++*arg;
4063 break;
4064
4065 /*
4066 * nested expression: (expression).
4067 */
4068 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (**arg == ')')
4071 ++*arg;
4072 else if (ret == OK)
4073 {
4074 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 ret = FAIL;
4077 }
4078 break;
4079
Bram Moolenaar8c711452005-01-14 21:53:12 +00004080 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 break;
4082 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004083
4084 if (ret == NOTDONE)
4085 {
4086 /*
4087 * Must be a variable or function name.
4088 * Can also be a curly-braces kind of name: {expr}.
4089 */
4090 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004091 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004092 if (alias != NULL)
4093 s = alias;
4094
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004095 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004096 ret = FAIL;
4097 else
4098 {
4099 if (**arg == '(') /* recursive! */
4100 {
4101 /* If "s" is the name of a variable of type VAR_FUNC
4102 * use its contents. */
4103 s = deref_func_name(s, &len);
4104
4105 /* Invoke the function. */
4106 ret = get_func_tv(s, len, rettv, arg,
4107 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004108 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004109 /* Stop the expression evaluation when immediately
4110 * aborting on error, or when an interrupt occurred or
4111 * an exception was thrown but not caught. */
4112 if (aborting())
4113 {
4114 if (ret == OK)
4115 clear_tv(rettv);
4116 ret = FAIL;
4117 }
4118 }
4119 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004120 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004121 else
4122 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004123 }
4124
4125 if (alias != NULL)
4126 vim_free(alias);
4127 }
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 *arg = skipwhite(*arg);
4130
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004131 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4132 * expr(expr). */
4133 if (ret == OK)
4134 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 /*
4137 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4138 */
4139 if (ret == OK && evaluate && end_leader > start_leader)
4140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 while (end_leader > start_leader)
4143 {
4144 --end_leader;
4145 if (*end_leader == '!')
4146 val = !val;
4147 else if (*end_leader == '-')
4148 val = -val;
4149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 clear_tv(rettv);
4151 rettv->v_type = VAR_NUMBER;
4152 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154
4155 return ret;
4156}
4157
4158/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004159 * Evaluate an "[expr]" or "[expr:expr]" index.
4160 * "*arg" points to the '['.
4161 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4162 */
4163 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004164eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004168 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004169{
4170 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004173 long len = -1;
4174 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004175 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004176 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004177
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004179 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004180 if (verbose)
4181 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004182 return FAIL;
4183 }
4184
Bram Moolenaar8c711452005-01-14 21:53:12 +00004185 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004186 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004187 /*
4188 * dict.name
4189 */
4190 key = *arg + 1;
4191 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4192 ;
4193 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004194 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004195 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004196 }
4197 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004198 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004199 /*
4200 * something[idx]
4201 *
4202 * Get the (first) variable from inside the [].
4203 */
4204 *arg = skipwhite(*arg + 1);
4205 if (**arg == ':')
4206 empty1 = TRUE;
4207 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4208 return FAIL;
4209
4210 /*
4211 * Get the second variable from inside the [:].
4212 */
4213 if (**arg == ':')
4214 {
4215 range = TRUE;
4216 *arg = skipwhite(*arg + 1);
4217 if (**arg == ']')
4218 empty2 = TRUE;
4219 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4220 {
4221 clear_tv(&var1);
4222 return FAIL;
4223 }
4224 }
4225
4226 /* Check for the ']'. */
4227 if (**arg != ']')
4228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004229 if (verbose)
4230 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 clear_tv(&var1);
4232 if (range)
4233 clear_tv(&var2);
4234 return FAIL;
4235 }
4236 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004237 }
4238
4239 if (evaluate)
4240 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004241 n1 = 0;
4242 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 n1 = get_tv_number(&var1);
4245 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004246 }
4247 if (range)
4248 {
4249 if (empty2)
4250 n2 = -1;
4251 else
4252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 n2 = get_tv_number(&var2);
4254 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255 }
4256 }
4257
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004259 {
4260 case VAR_NUMBER:
4261 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263 len = (long)STRLEN(s);
4264 if (range)
4265 {
4266 /* The resulting variable is a substring. If the indexes
4267 * are out of range the result is empty. */
4268 if (n1 < 0)
4269 {
4270 n1 = len + n1;
4271 if (n1 < 0)
4272 n1 = 0;
4273 }
4274 if (n2 < 0)
4275 n2 = len + n2;
4276 else if (n2 >= len)
4277 n2 = len;
4278 if (n1 >= len || n2 < 0 || n1 > n2)
4279 s = NULL;
4280 else
4281 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4282 }
4283 else
4284 {
4285 /* The resulting variable is a string of a single
4286 * character. If the index is too big or negative the
4287 * result is empty. */
4288 if (n1 >= len || n1 < 0)
4289 s = NULL;
4290 else
4291 s = vim_strnsave(s + n1, 1);
4292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
4294 rettv->v_type = VAR_STRING;
4295 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004296 break;
4297
4298 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004300 if (n1 < 0)
4301 n1 = len + n1;
4302 if (!empty1 && (n1 < 0 || n1 >= len))
4303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004304 if (verbose)
4305 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004306 return FAIL;
4307 }
4308 if (range)
4309 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 list_T *l;
4311 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004312
4313 if (n2 < 0)
4314 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004317 if (verbose)
4318 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004319 return FAIL;
4320 }
4321 l = list_alloc();
4322 if (l == NULL)
4323 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004325 n1 <= n2; ++n1)
4326 {
4327 if (list_append_tv(l, &item->li_tv) == FAIL)
4328 {
4329 list_free(l);
4330 return FAIL;
4331 }
4332 item = item->li_next;
4333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 clear_tv(rettv);
4335 rettv->v_type = VAR_LIST;
4336 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004337 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004338 }
4339 else
4340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004342 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(rettv);
4344 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004345 }
4346 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004347
4348 case VAR_DICT:
4349 if (range)
4350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004351 if (verbose)
4352 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004353 if (len == -1)
4354 clear_tv(&var1);
4355 return FAIL;
4356 }
4357 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004358 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359
4360 if (len == -1)
4361 {
4362 key = get_tv_string(&var1);
4363 if (*key == NUL)
4364 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004365 if (verbose)
4366 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 clear_tv(&var1);
4368 return FAIL;
4369 }
4370 }
4371
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004372 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004374 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004375 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376 if (len == -1)
4377 clear_tv(&var1);
4378 if (item == NULL)
4379 return FAIL;
4380
4381 copy_tv(&item->di_tv, &var1);
4382 clear_tv(rettv);
4383 *rettv = var1;
4384 }
4385 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 }
4387 }
4388
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 return OK;
4390}
4391
4392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 * Get an option value.
4394 * "arg" points to the '&' or '+' before the option name.
4395 * "arg" is advanced to character after the option name.
4396 * Return OK or FAIL.
4397 */
4398 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int evaluate;
4403{
4404 char_u *option_end;
4405 long numval;
4406 char_u *stringval;
4407 int opt_type;
4408 int c;
4409 int working = (**arg == '+'); /* has("+option") */
4410 int ret = OK;
4411 int opt_flags;
4412
4413 /*
4414 * Isolate the option name and find its value.
4415 */
4416 option_end = find_option_end(arg, &opt_flags);
4417 if (option_end == NULL)
4418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004419 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 EMSG2(_("E112: Option name missing: %s"), *arg);
4421 return FAIL;
4422 }
4423
4424 if (!evaluate)
4425 {
4426 *arg = option_end;
4427 return OK;
4428 }
4429
4430 c = *option_end;
4431 *option_end = NUL;
4432 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 if (opt_type == -3) /* invalid name */
4436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 EMSG2(_("E113: Unknown option: %s"), *arg);
4439 ret = FAIL;
4440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 if (opt_type == -2) /* hidden string option */
4444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445 rettv->v_type = VAR_STRING;
4446 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else if (opt_type == -1) /* hidden number option */
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 rettv->v_type = VAR_NUMBER;
4451 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 else if (opt_type == 1) /* number option */
4454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 rettv->v_type = VAR_NUMBER;
4456 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 else /* string option */
4459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 rettv->v_type = VAR_STRING;
4461 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 }
4464 else if (working && (opt_type == -2 || opt_type == -1))
4465 ret = FAIL;
4466
4467 *option_end = c; /* put back for error messages */
4468 *arg = option_end;
4469
4470 return ret;
4471}
4472
4473/*
4474 * Allocate a variable for a string constant.
4475 * Return OK or FAIL.
4476 */
4477 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 int evaluate;
4482{
4483 char_u *p;
4484 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 int extra = 0;
4486
4487 /*
4488 * Find the end of the string, skipping backslashed characters.
4489 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004490 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 if (*p == '\\' && p[1] != NUL)
4493 {
4494 ++p;
4495 /* A "\<x>" form occupies at least 4 characters, and produces up
4496 * to 6 characters: reserve space for 2 extra */
4497 if (*p == '<')
4498 extra += 2;
4499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501
4502 if (*p != '"')
4503 {
4504 EMSG2(_("E114: Missing quote: %s"), *arg);
4505 return FAIL;
4506 }
4507
4508 /* If only parsing, set *arg and return here */
4509 if (!evaluate)
4510 {
4511 *arg = p + 1;
4512 return OK;
4513 }
4514
4515 /*
4516 * Copy the string into allocated memory, handling backslashed
4517 * characters.
4518 */
4519 name = alloc((unsigned)(p - *arg + extra));
4520 if (name == NULL)
4521 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 rettv->v_type = VAR_STRING;
4523 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 if (*p == '\\')
4528 {
4529 switch (*++p)
4530 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531 case 'b': *name++ = BS; ++p; break;
4532 case 'e': *name++ = ESC; ++p; break;
4533 case 'f': *name++ = FF; ++p; break;
4534 case 'n': *name++ = NL; ++p; break;
4535 case 'r': *name++ = CAR; ++p; break;
4536 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 case 'X': /* hex: "\x1", "\x12" */
4539 case 'x':
4540 case 'u': /* Unicode: "\u0023" */
4541 case 'U':
4542 if (vim_isxdigit(p[1]))
4543 {
4544 int n, nr;
4545 int c = toupper(*p);
4546
4547 if (c == 'X')
4548 n = 2;
4549 else
4550 n = 4;
4551 nr = 0;
4552 while (--n >= 0 && vim_isxdigit(p[1]))
4553 {
4554 ++p;
4555 nr = (nr << 4) + hex2nr(*p);
4556 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#ifdef FEAT_MBYTE
4559 /* For "\u" store the number according to
4560 * 'encoding'. */
4561 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else
4564#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 break;
4568
4569 /* octal: "\1", "\12", "\123" */
4570 case '0':
4571 case '1':
4572 case '2':
4573 case '3':
4574 case '4':
4575 case '5':
4576 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 case '7': *name = *p++ - '0';
4578 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 *name = (*name << 3) + *p++ - '0';
4581 if (*p >= '0' && *p <= '7')
4582 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 break;
4586
4587 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (extra != 0)
4590 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593 }
4594 /* FALLTHROUGH */
4595
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 break;
4598 }
4599 }
4600 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004604 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 *arg = p + 1;
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 return OK;
4608}
4609
4610/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004611 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 * Return OK or FAIL.
4613 */
4614 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 int evaluate;
4619{
4620 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004621 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004622 int reduce = 0;
4623
4624 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004626 */
4627 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004631 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004632 break;
4633 ++reduce;
4634 ++p;
4635 }
4636 }
4637
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004641 return FAIL;
4642 }
4643
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004645 if (!evaluate)
4646 {
4647 *arg = p + 1;
4648 return OK;
4649 }
4650
4651 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004653 */
4654 str = alloc((unsigned)((p - *arg) - reduce));
4655 if (str == NULL)
4656 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 rettv->v_type = VAR_STRING;
4658 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004659
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665 break;
4666 ++p;
4667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004671 *arg = p + 1;
4672
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004673 return OK;
4674}
4675
4676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 * Allocate a variable for a List and fill it from "*arg".
4678 * Return OK or FAIL.
4679 */
4680 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004683 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 int evaluate;
4685{
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 list_T *l = NULL;
4687 typval_T tv;
4688 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689
4690 if (evaluate)
4691 {
4692 l = list_alloc();
4693 if (l == NULL)
4694 return FAIL;
4695 }
4696
4697 *arg = skipwhite(*arg + 1);
4698 while (**arg != ']' && **arg != NUL)
4699 {
4700 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4701 goto failret;
4702 if (evaluate)
4703 {
4704 item = listitem_alloc();
4705 if (item != NULL)
4706 {
4707 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004708 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 list_append(l, item);
4710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004711 else
4712 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 }
4714
4715 if (**arg == ']')
4716 break;
4717 if (**arg != ',')
4718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004719 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 goto failret;
4721 }
4722 *arg = skipwhite(*arg + 1);
4723 }
4724
4725 if (**arg != ']')
4726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728failret:
4729 if (evaluate)
4730 list_free(l);
4731 return FAIL;
4732 }
4733
4734 *arg = skipwhite(*arg + 1);
4735 if (evaluate)
4736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 rettv->v_type = VAR_LIST;
4738 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 ++l->lv_refcount;
4740 }
4741
4742 return OK;
4743}
4744
4745/*
4746 * Allocate an empty header for a list.
4747 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004748 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749list_alloc()
4750{
Bram Moolenaar33570922005-01-25 22:26:29 +00004751 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752}
4753
4754/*
4755 * Unreference a list: decrement the reference count and free it when it
4756 * becomes zero.
4757 */
4758 static void
4759list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761{
4762 if (l != NULL && --l->lv_refcount <= 0)
4763 list_free(l);
4764}
4765
4766/*
4767 * Free a list, including all items it points to.
4768 * Ignores the reference count.
4769 */
4770 static void
4771list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004772 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773{
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 listitem_T *item;
4775 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776
4777 for (item = l->lv_first; item != NULL; item = next)
4778 {
4779 next = item->li_next;
4780 listitem_free(item);
4781 }
4782 vim_free(l);
4783}
4784
4785/*
4786 * Allocate a list item.
4787 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789listitem_alloc()
4790{
Bram Moolenaar33570922005-01-25 22:26:29 +00004791 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792}
4793
4794/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004795 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 */
4797 static void
4798listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004799 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 vim_free(item);
4803}
4804
4805/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004806 * Remove a list item from a List and free it. Also clears the value.
4807 */
4808 static void
4809listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 list_T *l;
4811 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004812{
4813 list_remove(l, item, item);
4814 listitem_free(item);
4815}
4816
4817/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 * Get the number of items in a list.
4819 */
4820 static long
4821list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004822 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 if (l == NULL)
4825 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004826 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827}
4828
4829/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004830 * Return TRUE when two lists have exactly the same values.
4831 */
4832 static int
4833list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004834 list_T *l1;
4835 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004836 int ic; /* ignore case for strings */
4837{
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004839
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004840 if (list_len(l1) != list_len(l2))
4841 return FALSE;
4842
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004843 for (item1 = l1->lv_first, item2 = l2->lv_first;
4844 item1 != NULL && item2 != NULL;
4845 item1 = item1->li_next, item2 = item2->li_next)
4846 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4847 return FALSE;
4848 return item1 == NULL && item2 == NULL;
4849}
4850
4851/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004852 * Return TRUE when two dictionaries have exactly the same key/values.
4853 */
4854 static int
4855dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004856 dict_T *d1;
4857 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004858 int ic; /* ignore case for strings */
4859{
Bram Moolenaar33570922005-01-25 22:26:29 +00004860 hashitem_T *hi;
4861 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004862 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004863
4864 if (dict_len(d1) != dict_len(d2))
4865 return FALSE;
4866
Bram Moolenaar33570922005-01-25 22:26:29 +00004867 todo = d1->dv_hashtab.ht_used;
4868 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004870 if (!HASHITEM_EMPTY(hi))
4871 {
4872 item2 = dict_find(d2, hi->hi_key, -1);
4873 if (item2 == NULL)
4874 return FALSE;
4875 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4876 return FALSE;
4877 --todo;
4878 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004879 }
4880 return TRUE;
4881}
4882
4883/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004884 * Return TRUE if "tv1" and "tv2" have the same value.
4885 * Compares the items just like "==" would compare them.
4886 */
4887 static int
4888tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004889 typval_T *tv1;
4890 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004891 int ic; /* ignore case */
4892{
4893 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4894
4895 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4896 {
4897 /* recursive! */
4898 if (tv1->v_type != tv2->v_type
4899 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4900 return FALSE;
4901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004902 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4903 {
4904 /* recursive! */
4905 if (tv1->v_type != tv2->v_type
4906 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4907 return FALSE;
4908 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004909 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4910 {
4911 if (tv1->v_type != tv2->v_type
4912 || tv1->vval.v_string == NULL
4913 || tv2->vval.v_string == NULL
4914 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4915 return FALSE;
4916 }
4917 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4918 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004919 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4920 * Don't consider "4x" to be equal to 4. */
4921 if ((tv1->v_type == VAR_STRING
4922 && !string_isa_number(tv1->vval.v_string))
4923 || (tv2->v_type == VAR_STRING
4924 && !string_isa_number(tv2->vval.v_string)))
4925 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004926 if (get_tv_number(tv1) != get_tv_number(tv2))
4927 return FALSE;
4928 }
4929 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4930 get_tv_string_buf(tv2, buf2)) != 0)
4931 return FALSE;
4932 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4933 get_tv_string_buf(tv2, buf2)) != 0)
4934 return FALSE;
4935 return TRUE;
4936}
4937
4938/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004939 * Return TRUE if "tv" is a number without other non-white characters.
4940 */
4941 static int
4942string_isa_number(s)
4943 char_u *s;
4944{
4945 int len;
4946
4947 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4948 return len > 0 && *skipwhite(s + len) == NUL;
4949}
4950
4951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 * Locate item with index "n" in list "l" and return it.
4953 * A negative index is counted from the end; -1 is the last item.
4954 * Returns NULL when "n" is out of range.
4955 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004958 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 long n;
4960{
Bram Moolenaar33570922005-01-25 22:26:29 +00004961 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004962 long idx;
4963
4964 if (l == NULL)
4965 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004966
4967 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004968 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004969 n = l->lv_len + n;
4970
4971 /* Check for index out of range. */
4972 if (n < 0 || n >= l->lv_len)
4973 return NULL;
4974
4975 /* When there is a cached index may start search from there. */
4976 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004978 if (n < l->lv_idx / 2)
4979 {
4980 /* closest to the start of the list */
4981 item = l->lv_first;
4982 idx = 0;
4983 }
4984 else if (n > (l->lv_idx + l->lv_len) / 2)
4985 {
4986 /* closest to the end of the list */
4987 item = l->lv_last;
4988 idx = l->lv_len - 1;
4989 }
4990 else
4991 {
4992 /* closest to the cached index */
4993 item = l->lv_idx_item;
4994 idx = l->lv_idx;
4995 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 }
4997 else
4998 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004999 if (n < l->lv_len / 2)
5000 {
5001 /* closest to the start of the list */
5002 item = l->lv_first;
5003 idx = 0;
5004 }
5005 else
5006 {
5007 /* closest to the end of the list */
5008 item = l->lv_last;
5009 idx = l->lv_len - 1;
5010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005012
5013 while (n > idx)
5014 {
5015 /* search forward */
5016 item = item->li_next;
5017 ++idx;
5018 }
5019 while (n < idx)
5020 {
5021 /* search backward */
5022 item = item->li_prev;
5023 --idx;
5024 }
5025
5026 /* cache the used index */
5027 l->lv_idx = idx;
5028 l->lv_idx_item = item;
5029
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005030 return item;
5031}
5032
5033/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005034 * Locate "item" list "l" and return its index.
5035 * Returns -1 when "item" is not in the list.
5036 */
5037 static long
5038list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005039 list_T *l;
5040 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005041{
5042 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005043 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005044
5045 if (l == NULL)
5046 return -1;
5047 idx = 0;
5048 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5049 ++idx;
5050 if (li == NULL)
5051 return -1;
5052 return idx;;
5053}
5054
5055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005056 * Append item "item" to the end of list "l".
5057 */
5058 static void
5059list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005060 list_T *l;
5061 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062{
5063 if (l->lv_last == NULL)
5064 {
5065 /* empty list */
5066 l->lv_first = item;
5067 l->lv_last = item;
5068 item->li_prev = NULL;
5069 }
5070 else
5071 {
5072 l->lv_last->li_next = item;
5073 item->li_prev = l->lv_last;
5074 l->lv_last = item;
5075 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005076 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005077 item->li_next = NULL;
5078}
5079
5080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005081 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005082 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005083 */
5084 static int
5085list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005086 list_T *l;
5087 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005089 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005090
Bram Moolenaar05159a02005-02-26 23:04:13 +00005091 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005093 copy_tv(tv, &li->li_tv);
5094 list_append(l, li);
5095 return OK;
5096}
5097
5098/*
5099 * Add a dictionary to a list. Used by errorlist().
5100 * Return FAIL when out of memory.
5101 */
5102 int
5103list_append_dict(list, dict)
5104 list_T *list;
5105 dict_T *dict;
5106{
5107 listitem_T *li = listitem_alloc();
5108
5109 if (li == NULL)
5110 return FAIL;
5111 li->li_tv.v_type = VAR_DICT;
5112 li->li_tv.v_lock = 0;
5113 li->li_tv.vval.v_dict = dict;
5114 list_append(list, li);
5115 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 return OK;
5117}
5118
5119/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005120 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005121 * If "item" is NULL append at the end.
5122 * Return FAIL when out of memory.
5123 */
5124 static int
5125list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005126 list_T *l;
5127 typval_T *tv;
5128 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005129{
Bram Moolenaar33570922005-01-25 22:26:29 +00005130 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005131
5132 if (ni == NULL)
5133 return FAIL;
5134 copy_tv(tv, &ni->li_tv);
5135 if (item == NULL)
5136 /* Append new item at end of list. */
5137 list_append(l, ni);
5138 else
5139 {
5140 /* Insert new item before existing item. */
5141 ni->li_prev = item->li_prev;
5142 ni->li_next = item;
5143 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005144 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005145 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005146 ++l->lv_idx;
5147 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005148 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005149 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005150 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005151 l->lv_idx_item = NULL;
5152 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005153 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005154 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005155 }
5156 return OK;
5157}
5158
5159/*
5160 * Extend "l1" with "l2".
5161 * If "bef" is NULL append at the end, otherwise insert before this item.
5162 * Returns FAIL when out of memory.
5163 */
5164 static int
5165list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005166 list_T *l1;
5167 list_T *l2;
5168 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005169{
Bram Moolenaar33570922005-01-25 22:26:29 +00005170 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005171
5172 for (item = l2->lv_first; item != NULL; item = item->li_next)
5173 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5174 return FAIL;
5175 return OK;
5176}
5177
5178/*
5179 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5180 * Return FAIL when out of memory.
5181 */
5182 static int
5183list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005184 list_T *l1;
5185 list_T *l2;
5186 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005187{
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189
5190 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005191 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192 if (l == NULL)
5193 return FAIL;
5194 tv->v_type = VAR_LIST;
5195 tv->vval.v_list = l;
5196
5197 /* append all items from the second list */
5198 return list_extend(l, l2, NULL);
5199}
5200
5201/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005202 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005204 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205 * Returns NULL when out of memory.
5206 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005207 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005208list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005209 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005211 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212{
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 list_T *copy;
5214 listitem_T *item;
5215 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216
5217 if (orig == NULL)
5218 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219
5220 copy = list_alloc();
5221 if (copy != NULL)
5222 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005223 if (copyID != 0)
5224 {
5225 /* Do this before adding the items, because one of the items may
5226 * refer back to this list. */
5227 orig->lv_copyID = copyID;
5228 orig->lv_copylist = copy;
5229 }
5230 for (item = orig->lv_first; item != NULL && !got_int;
5231 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 {
5233 ni = listitem_alloc();
5234 if (ni == NULL)
5235 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005236 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005237 {
5238 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5239 {
5240 vim_free(ni);
5241 break;
5242 }
5243 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005245 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 list_append(copy, ni);
5247 }
5248 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005249 if (item != NULL)
5250 {
5251 list_unref(copy);
5252 copy = NULL;
5253 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 }
5255
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 return copy;
5257}
5258
5259/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005260 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005261 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005263 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005264list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 list_T *l;
5266 listitem_T *item;
5267 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005268{
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005271 /* notify watchers */
5272 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005274 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005275 list_fix_watch(l, ip);
5276 if (ip == item2)
5277 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005279
5280 if (item2->li_next == NULL)
5281 l->lv_last = item->li_prev;
5282 else
5283 item2->li_next->li_prev = item->li_prev;
5284 if (item->li_prev == NULL)
5285 l->lv_first = item2->li_next;
5286 else
5287 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005288 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289}
5290
5291/*
5292 * Return an allocated string with the string representation of a list.
5293 * May return NULL.
5294 */
5295 static char_u *
5296list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298{
5299 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
5301 if (tv->vval.v_list == NULL)
5302 return NULL;
5303 ga_init2(&ga, (int)sizeof(char), 80);
5304 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005305 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5306 {
5307 vim_free(ga.ga_data);
5308 return NULL;
5309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 ga_append(&ga, ']');
5311 ga_append(&ga, NUL);
5312 return (char_u *)ga.ga_data;
5313}
5314
5315/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005316 * Join list "l" into a string in "*gap", using separator "sep".
5317 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005318 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005319 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005320 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005321list_join(gap, l, sep, echo)
5322 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005323 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005324 char_u *sep;
5325 int echo;
5326{
5327 int first = TRUE;
5328 char_u *tofree;
5329 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005330 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005331 char_u *s;
5332
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005333 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005334 {
5335 if (first)
5336 first = FALSE;
5337 else
5338 ga_concat(gap, sep);
5339
5340 if (echo)
5341 s = echo_string(&item->li_tv, &tofree, numbuf);
5342 else
5343 s = tv2string(&item->li_tv, &tofree, numbuf);
5344 if (s != NULL)
5345 ga_concat(gap, s);
5346 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005347 if (s == NULL)
5348 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005349 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005350 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005351}
5352
5353/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 * Allocate an empty header for a dictionary.
5355 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005356 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357dict_alloc()
5358{
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005360
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005362 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005363 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005365 d->dv_lock = 0;
5366 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005367 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005369 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370}
5371
5372/*
5373 * Unreference a Dictionary: decrement the reference count and free it when it
5374 * becomes zero.
5375 */
5376 static void
5377dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379{
5380 if (d != NULL && --d->dv_refcount <= 0)
5381 dict_free(d);
5382}
5383
5384/*
5385 * Free a Dictionary, including all items it contains.
5386 * Ignores the reference count.
5387 */
5388 static void
5389dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005390 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005392 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005395 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 * hashtab. Must not try to resize the hashtab! */
5397 todo = d->dv_hashtab.ht_used;
5398 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005400 if (!HASHITEM_EMPTY(hi))
5401 {
5402 dictitem_free(HI2DI(hi));
5403 --todo;
5404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 vim_free(d);
5408}
5409
5410/*
5411 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005412 * The "key" is copied to the new item.
5413 * Note that the value of the item "di_tv" still needs to be initialized!
5414 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005417dictitem_alloc(key)
5418 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419{
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005421
Bram Moolenaar33570922005-01-25 22:26:29 +00005422 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005423 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005425 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005426 di->di_flags = 0;
5427 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005428 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429}
5430
5431/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005432 * Make a copy of a Dictionary item.
5433 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005434 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005435dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005437{
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005439
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005441 if (di != NULL)
5442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005443 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005444 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005445 copy_tv(&org->di_tv, &di->di_tv);
5446 }
5447 return di;
5448}
5449
5450/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005451 * Remove item "item" from Dictionary "dict" and free it.
5452 */
5453 static void
5454dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 dict_T *dict;
5456 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005457{
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005459
Bram Moolenaar33570922005-01-25 22:26:29 +00005460 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005461 if (HASHITEM_EMPTY(hi))
5462 EMSG2(_(e_intern2), "dictitem_remove()");
5463 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005465 dictitem_free(item);
5466}
5467
5468/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 * Free a dict item. Also clears the value.
5470 */
5471 static void
5472dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 clear_tv(&item->di_tv);
5476 vim_free(item);
5477}
5478
5479/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005480 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5481 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005482 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005483 * Returns NULL when out of memory.
5484 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005486dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005488 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005489 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005490{
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 dict_T *copy;
5492 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005493 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005495
5496 if (orig == NULL)
5497 return NULL;
5498
5499 copy = dict_alloc();
5500 if (copy != NULL)
5501 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005502 if (copyID != 0)
5503 {
5504 orig->dv_copyID = copyID;
5505 orig->dv_copydict = copy;
5506 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005508 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005509 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005510 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005512 --todo;
5513
5514 di = dictitem_alloc(hi->hi_key);
5515 if (di == NULL)
5516 break;
5517 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005518 {
5519 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5520 copyID) == FAIL)
5521 {
5522 vim_free(di);
5523 break;
5524 }
5525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005526 else
5527 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5528 if (dict_add(copy, di) == FAIL)
5529 {
5530 dictitem_free(di);
5531 break;
5532 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005533 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005534 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005535
Bram Moolenaare9a41262005-01-15 22:18:47 +00005536 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005537 if (todo > 0)
5538 {
5539 dict_unref(copy);
5540 copy = NULL;
5541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005542 }
5543
5544 return copy;
5545}
5546
5547/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005548 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005553 dict_T *d;
5554 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555{
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005557}
5558
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005560 * Add a number or string entry to dictionary "d".
5561 * When "str" is NULL use number "nr", otherwise use "str".
5562 * Returns FAIL when out of memory and when key already exists.
5563 */
5564 int
5565dict_add_nr_str(d, key, nr, str)
5566 dict_T *d;
5567 char *key;
5568 long nr;
5569 char_u *str;
5570{
5571 dictitem_T *item;
5572
5573 item = dictitem_alloc((char_u *)key);
5574 if (item == NULL)
5575 return FAIL;
5576 item->di_tv.v_lock = 0;
5577 if (str == NULL)
5578 {
5579 item->di_tv.v_type = VAR_NUMBER;
5580 item->di_tv.vval.v_number = nr;
5581 }
5582 else
5583 {
5584 item->di_tv.v_type = VAR_STRING;
5585 item->di_tv.vval.v_string = vim_strsave(str);
5586 }
5587 if (dict_add(d, item) == FAIL)
5588 {
5589 dictitem_free(item);
5590 return FAIL;
5591 }
5592 return OK;
5593}
5594
5595/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005596 * Get the number of items in a Dictionary.
5597 */
5598 static long
5599dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005601{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005602 if (d == NULL)
5603 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005605}
5606
5607/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 * Find item "key[len]" in Dictionary "d".
5609 * If "len" is negative use strlen(key).
5610 * Returns NULL when not found.
5611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005613dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 char_u *key;
5616 int len;
5617{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005618#define AKEYLEN 200
5619 char_u buf[AKEYLEN];
5620 char_u *akey;
5621 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005624 if (len < 0)
5625 akey = key;
5626 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005628 tofree = akey = vim_strnsave(key, len);
5629 if (akey == NULL)
5630 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005631 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005632 else
5633 {
5634 /* Avoid a malloc/free by using buf[]. */
5635 STRNCPY(buf, key, len);
5636 buf[len] = NUL;
5637 akey = buf;
5638 }
5639
Bram Moolenaar33570922005-01-25 22:26:29 +00005640 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005641 vim_free(tofree);
5642 if (HASHITEM_EMPTY(hi))
5643 return NULL;
5644 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645}
5646
5647/*
5648 * Return an allocated string with the string representation of a Dictionary.
5649 * May return NULL.
5650 */
5651 static char_u *
5652dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654{
5655 garray_T ga;
5656 int first = TRUE;
5657 char_u *tofree;
5658 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005661 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005662 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005664 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 return NULL;
5666 ga_init2(&ga, (int)sizeof(char), 80);
5667 ga_append(&ga, '{');
5668
Bram Moolenaar33570922005-01-25 22:26:29 +00005669 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005670 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005672 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005674 --todo;
5675
5676 if (first)
5677 first = FALSE;
5678 else
5679 ga_concat(&ga, (char_u *)", ");
5680
5681 tofree = string_quote(hi->hi_key, FALSE);
5682 if (tofree != NULL)
5683 {
5684 ga_concat(&ga, tofree);
5685 vim_free(tofree);
5686 }
5687 ga_concat(&ga, (char_u *)": ");
5688 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5689 if (s != NULL)
5690 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005692 if (s == NULL)
5693 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005696 if (todo > 0)
5697 {
5698 vim_free(ga.ga_data);
5699 return NULL;
5700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701
5702 ga_append(&ga, '}');
5703 ga_append(&ga, NUL);
5704 return (char_u *)ga.ga_data;
5705}
5706
5707/*
5708 * Allocate a variable for a Dictionary and fill it from "*arg".
5709 * Return OK or FAIL. Returns NOTDONE for {expr}.
5710 */
5711 static int
5712get_dict_tv(arg, rettv, evaluate)
5713 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005714 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 int evaluate;
5716{
Bram Moolenaar33570922005-01-25 22:26:29 +00005717 dict_T *d = NULL;
5718 typval_T tvkey;
5719 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005721 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005723 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724
5725 /*
5726 * First check if it's not a curly-braces thing: {expr}.
5727 * Must do this without evaluating, otherwise a function may be called
5728 * twice. Unfortunately this means we need to call eval1() twice for the
5729 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005730 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005732 if (*start != '}')
5733 {
5734 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5735 return FAIL;
5736 if (*start == '}')
5737 return NOTDONE;
5738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739
5740 if (evaluate)
5741 {
5742 d = dict_alloc();
5743 if (d == NULL)
5744 return FAIL;
5745 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005746 tvkey.v_type = VAR_UNKNOWN;
5747 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748
5749 *arg = skipwhite(*arg + 1);
5750 while (**arg != '}' && **arg != NUL)
5751 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005752 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 goto failret;
5754 if (**arg != ':')
5755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005756 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005757 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 goto failret;
5759 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005760 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 if (*key == NUL)
5762 {
5763 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005764 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 goto failret;
5766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767
5768 *arg = skipwhite(*arg + 1);
5769 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005771 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 goto failret;
5773 }
5774 if (evaluate)
5775 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005776 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (item != NULL)
5778 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005779 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005780 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 clear_tv(&tv);
5782 goto failret;
5783 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005784 item = dictitem_alloc(key);
5785 clear_tv(&tvkey);
5786 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005789 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005790 if (dict_add(d, item) == FAIL)
5791 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 }
5793 }
5794
5795 if (**arg == '}')
5796 break;
5797 if (**arg != ',')
5798 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005799 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 goto failret;
5801 }
5802 *arg = skipwhite(*arg + 1);
5803 }
5804
5805 if (**arg != '}')
5806 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005807 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808failret:
5809 if (evaluate)
5810 dict_free(d);
5811 return FAIL;
5812 }
5813
5814 *arg = skipwhite(*arg + 1);
5815 if (evaluate)
5816 {
5817 rettv->v_type = VAR_DICT;
5818 rettv->vval.v_dict = d;
5819 ++d->dv_refcount;
5820 }
5821
5822 return OK;
5823}
5824
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 * Return a string with the string representation of a variable.
5827 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005828 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 * May return NULL;
5831 */
5832 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005836 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005838 static int recurse = 0;
5839 char_u *r = NULL;
5840
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005843 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005844 *tofree = NULL;
5845 return NULL;
5846 }
5847 ++recurse;
5848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 switch (tv->v_type)
5850 {
5851 case VAR_FUNC:
5852 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005853 r = tv->vval.v_string;
5854 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 case VAR_LIST:
5856 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 r = *tofree;
5858 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 case VAR_DICT:
5860 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005861 r = *tofree;
5862 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005863 case VAR_STRING:
5864 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 *tofree = NULL;
5866 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005868 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005870 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872
5873 --recurse;
5874 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875}
5876
5877/*
5878 * Return a string with the string representation of a variable.
5879 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5880 * "numbuf" is used for a number.
5881 * Puts quotes around strings, so that they can be parsed back by eval().
5882 * May return NULL;
5883 */
5884 static char_u *
5885tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 char_u **tofree;
5888 char_u *numbuf;
5889{
5890 switch (tv->v_type)
5891 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 case VAR_FUNC:
5893 *tofree = string_quote(tv->vval.v_string, TRUE);
5894 return *tofree;
5895 case VAR_STRING:
5896 *tofree = string_quote(tv->vval.v_string, FALSE);
5897 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005901 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005903 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906}
5907
5908/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 * Return string "str" in ' quotes, doubling ' characters.
5910 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 */
5913 static char_u *
5914string_quote(str, function)
5915 char_u *str;
5916 int function;
5917{
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 char_u *p, *r, *s;
5920
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 len = (function ? 13 : 3);
5922 if (str != NULL)
5923 {
5924 len += STRLEN(str);
5925 for (p = str; *p != NUL; mb_ptr_adv(p))
5926 if (*p == '\'')
5927 ++len;
5928 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 s = r = alloc(len);
5930 if (r != NULL)
5931 {
5932 if (function)
5933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 r += 10;
5936 }
5937 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 if (str != NULL)
5940 for (p = str; *p != NUL; )
5941 {
5942 if (*p == '\'')
5943 *r++ = '\'';
5944 MB_COPY_CHAR(p, r);
5945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 if (function)
5948 *r++ = ')';
5949 *r++ = NUL;
5950 }
5951 return s;
5952}
5953
5954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 * Get the value of an environment variable.
5956 * "arg" is pointing to the '$'. It is advanced to after the name.
5957 * If the environment variable was not set, silently assume it is empty.
5958 * Always return OK.
5959 */
5960 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 int evaluate;
5965{
5966 char_u *string = NULL;
5967 int len;
5968 int cc;
5969 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005970 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
5972 ++*arg;
5973 name = *arg;
5974 len = get_env_len(arg);
5975 if (evaluate)
5976 {
5977 if (len != 0)
5978 {
5979 cc = name[len];
5980 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005981 /* first try vim_getenv(), fast for normal environment vars */
5982 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005984 {
5985 if (!mustfree)
5986 string = vim_strsave(string);
5987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 else
5989 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00005990 if (mustfree)
5991 vim_free(string);
5992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 /* next try expanding things like $VIM and ${HOME} */
5994 string = expand_env_save(name - 1);
5995 if (string != NULL && *string == '$')
5996 {
5997 vim_free(string);
5998 string = NULL;
5999 }
6000 }
6001 name[len] = cc;
6002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006003 rettv->v_type = VAR_STRING;
6004 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
6006
6007 return OK;
6008}
6009
6010/*
6011 * Array with names and number of arguments of all internal functions
6012 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6013 */
6014static struct fst
6015{
6016 char *f_name; /* function name */
6017 char f_min_argc; /* minimal number of arguments */
6018 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021} functions[] =
6022{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006023 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {"append", 2, 2, f_append},
6025 {"argc", 0, 0, f_argc},
6026 {"argidx", 0, 0, f_argidx},
6027 {"argv", 1, 1, f_argv},
6028 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006029 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {"bufexists", 1, 1, f_bufexists},
6031 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6032 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6033 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6034 {"buflisted", 1, 1, f_buflisted},
6035 {"bufloaded", 1, 1, f_bufloaded},
6036 {"bufname", 1, 1, f_bufname},
6037 {"bufnr", 1, 1, f_bufnr},
6038 {"bufwinnr", 1, 1, f_bufwinnr},
6039 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006040 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {"char2nr", 1, 1, f_char2nr},
6043 {"cindent", 1, 1, f_cindent},
6044 {"col", 1, 1, f_col},
6045 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {"cscope_connection",0,3, f_cscope_connection},
6049 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006050 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 {"delete", 1, 1, f_delete},
6052 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006053 {"diff_filler", 1, 1, f_diff_filler},
6054 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006055 {"empty", 1, 1, f_empty},
Bram Moolenaar05159a02005-02-26 23:04:13 +00006056 {"errorlist", 0, 0, f_errorlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006058 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {"eventhandler", 0, 0, f_eventhandler},
6060 {"executable", 1, 1, f_executable},
6061 {"exists", 1, 1, f_exists},
6062 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006063 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6065 {"filereadable", 1, 1, f_filereadable},
6066 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006067 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006068 {"finddir", 1, 3, f_finddir},
6069 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {"fnamemodify", 2, 2, f_fnamemodify},
6071 {"foldclosed", 1, 1, f_foldclosed},
6072 {"foldclosedend", 1, 1, f_foldclosedend},
6073 {"foldlevel", 1, 1, f_foldlevel},
6074 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006075 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006078 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {"getbufvar", 2, 2, f_getbufvar},
6080 {"getchar", 0, 1, f_getchar},
6081 {"getcharmod", 0, 0, f_getcharmod},
6082 {"getcmdline", 0, 0, f_getcmdline},
6083 {"getcmdpos", 0, 0, f_getcmdpos},
6084 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006085 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006086 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 {"getfsize", 1, 1, f_getfsize},
6088 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006089 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006090 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 {"getreg", 0, 1, f_getreg},
6092 {"getregtype", 0, 1, f_getregtype},
6093 {"getwinposx", 0, 0, f_getwinposx},
6094 {"getwinposy", 0, 0, f_getwinposy},
6095 {"getwinvar", 2, 2, f_getwinvar},
6096 {"glob", 1, 1, f_glob},
6097 {"globpath", 2, 2, f_globpath},
6098 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {"hasmapto", 1, 2, f_hasmapto},
6101 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6102 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6103 {"histadd", 2, 2, f_histadd},
6104 {"histdel", 1, 2, f_histdel},
6105 {"histget", 1, 2, f_histget},
6106 {"histnr", 1, 1, f_histnr},
6107 {"hlID", 1, 1, f_hlID},
6108 {"hlexists", 1, 1, f_hlexists},
6109 {"hostname", 0, 0, f_hostname},
6110 {"iconv", 3, 3, f_iconv},
6111 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006112 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {"input", 1, 2, f_input},
6114 {"inputdialog", 1, 3, f_inputdialog},
6115 {"inputrestore", 0, 0, f_inputrestore},
6116 {"inputsave", 0, 0, f_inputsave},
6117 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006120 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006121 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006122 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006123 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {"libcall", 3, 3, f_libcall},
6127 {"libcallnr", 3, 3, f_libcallnr},
6128 {"line", 1, 1, f_line},
6129 {"line2byte", 1, 1, f_line2byte},
6130 {"lispindent", 1, 1, f_lispindent},
6131 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006132 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {"maparg", 1, 2, f_maparg},
6134 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006135 {"match", 2, 4, f_match},
6136 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006137 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006138 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006139 {"max", 1, 1, f_max},
6140 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006141#ifdef vim_mkdir
6142 {"mkdir", 1, 3, f_mkdir},
6143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {"mode", 0, 0, f_mode},
6145 {"nextnonblank", 1, 1, f_nextnonblank},
6146 {"nr2char", 1, 1, f_nr2char},
6147 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006149 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 {"remote_expr", 2, 3, f_remote_expr},
6151 {"remote_foreground", 1, 1, f_remote_foreground},
6152 {"remote_peek", 1, 2, f_remote_peek},
6153 {"remote_read", 1, 1, f_remote_read},
6154 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006155 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006157 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006159 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {"search", 1, 2, f_search},
6161 {"searchpair", 3, 5, f_searchpair},
6162 {"server2client", 2, 2, f_server2client},
6163 {"serverlist", 0, 0, f_serverlist},
6164 {"setbufvar", 3, 3, f_setbufvar},
6165 {"setcmdpos", 1, 1, f_setcmdpos},
6166 {"setline", 2, 2, f_setline},
6167 {"setreg", 2, 3, f_setreg},
6168 {"setwinvar", 3, 3, f_setwinvar},
6169 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006170 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006171 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#ifdef HAVE_STRFTIME
6173 {"strftime", 1, 2, f_strftime},
6174#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 {"strlen", 1, 1, f_strlen},
6178 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006179 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {"strtrans", 1, 1, f_strtrans},
6181 {"submatch", 1, 1, f_submatch},
6182 {"substitute", 4, 4, f_substitute},
6183 {"synID", 3, 3, f_synID},
6184 {"synIDattr", 2, 3, f_synIDattr},
6185 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006186 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {"tempname", 0, 0, f_tempname},
6188 {"tolower", 1, 1, f_tolower},
6189 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006190 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006192 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {"virtcol", 1, 1, f_virtcol},
6194 {"visualmode", 0, 1, f_visualmode},
6195 {"winbufnr", 1, 1, f_winbufnr},
6196 {"wincol", 0, 0, f_wincol},
6197 {"winheight", 1, 1, f_winheight},
6198 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006199 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {"winrestcmd", 0, 0, f_winrestcmd},
6201 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006202 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203};
6204
6205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6206
6207/*
6208 * Function given to ExpandGeneric() to obtain the list of internal
6209 * or user defined function names.
6210 */
6211 char_u *
6212get_function_name(xp, idx)
6213 expand_T *xp;
6214 int idx;
6215{
6216 static int intidx = -1;
6217 char_u *name;
6218
6219 if (idx == 0)
6220 intidx = -1;
6221 if (intidx < 0)
6222 {
6223 name = get_user_func_name(xp, idx);
6224 if (name != NULL)
6225 return name;
6226 }
6227 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6228 {
6229 STRCPY(IObuff, functions[intidx].f_name);
6230 STRCAT(IObuff, "(");
6231 if (functions[intidx].f_max_argc == 0)
6232 STRCAT(IObuff, ")");
6233 return IObuff;
6234 }
6235
6236 return NULL;
6237}
6238
6239/*
6240 * Function given to ExpandGeneric() to obtain the list of internal or
6241 * user defined variable or function names.
6242 */
6243/*ARGSUSED*/
6244 char_u *
6245get_expr_name(xp, idx)
6246 expand_T *xp;
6247 int idx;
6248{
6249 static int intidx = -1;
6250 char_u *name;
6251
6252 if (idx == 0)
6253 intidx = -1;
6254 if (intidx < 0)
6255 {
6256 name = get_function_name(xp, idx);
6257 if (name != NULL)
6258 return name;
6259 }
6260 return get_user_var_name(xp, ++intidx);
6261}
6262
6263#endif /* FEAT_CMDL_COMPL */
6264
6265/*
6266 * Find internal function in table above.
6267 * Return index, or -1 if not found
6268 */
6269 static int
6270find_internal_func(name)
6271 char_u *name; /* name of the function */
6272{
6273 int first = 0;
6274 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6275 int cmp;
6276 int x;
6277
6278 /*
6279 * Find the function name in the table. Binary search.
6280 */
6281 while (first <= last)
6282 {
6283 x = first + ((unsigned)(last - first) >> 1);
6284 cmp = STRCMP(name, functions[x].f_name);
6285 if (cmp < 0)
6286 last = x - 1;
6287 else if (cmp > 0)
6288 first = x + 1;
6289 else
6290 return x;
6291 }
6292 return -1;
6293}
6294
6295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6297 * name it contains, otherwise return "name".
6298 */
6299 static char_u *
6300deref_func_name(name, lenp)
6301 char_u *name;
6302 int *lenp;
6303{
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 int cc;
6306
6307 cc = name[*lenp];
6308 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006309 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 {
6315 *lenp = 0;
6316 return (char_u *)""; /* just in case */
6317 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 *lenp = STRLEN(v->di_tv.vval.v_string);
6319 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 }
6321
6322 return name;
6323}
6324
6325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 * Allocate a variable for the result of a function.
6327 * Return OK or FAIL.
6328 */
6329 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6331 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 char_u *name; /* name of the function */
6333 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 char_u **arg; /* argument, pointing to the '(' */
6336 linenr_T firstline; /* first line of range */
6337 linenr_T lastline; /* last line of range */
6338 int *doesrange; /* return: function handled range */
6339 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 char_u *argp;
6343 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 int argcount = 0; /* number of arguments found */
6346
6347 /*
6348 * Get the arguments.
6349 */
6350 argp = *arg;
6351 while (argcount < MAX_FUNC_ARGS)
6352 {
6353 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6354 if (*argp == ')' || *argp == ',' || *argp == NUL)
6355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6357 {
6358 ret = FAIL;
6359 break;
6360 }
6361 ++argcount;
6362 if (*argp != ',')
6363 break;
6364 }
6365 if (*argp == ')')
6366 ++argp;
6367 else
6368 ret = FAIL;
6369
6370 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006371 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 {
6375 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006382 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384 *arg = skipwhite(argp);
6385 return ret;
6386}
6387
6388
6389/*
6390 * Call a function with its resolved parameters
6391 * Return OK or FAIL.
6392 */
6393 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006394call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 char_u *name; /* name of the function */
6397 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 linenr_T firstline; /* first line of range */
6402 linenr_T lastline; /* last line of range */
6403 int *doesrange; /* return: function handled range */
6404 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408#define ERROR_UNKNOWN 0
6409#define ERROR_TOOMANY 1
6410#define ERROR_TOOFEW 2
6411#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412#define ERROR_DICT 4
6413#define ERROR_NONE 5
6414#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 int error = ERROR_NONE;
6416 int i;
6417 int llen;
6418 ufunc_T *fp;
6419 int cc;
6420#define FLEN_FIXED 40
6421 char_u fname_buf[FLEN_FIXED + 1];
6422 char_u *fname;
6423
6424 /*
6425 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6426 * Change <SNR>123_name() to K_SNR 123_name().
6427 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6428 */
6429 cc = name[len];
6430 name[len] = NUL;
6431 llen = eval_fname_script(name);
6432 if (llen > 0)
6433 {
6434 fname_buf[0] = K_SPECIAL;
6435 fname_buf[1] = KS_EXTRA;
6436 fname_buf[2] = (int)KE_SNR;
6437 i = 3;
6438 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6439 {
6440 if (current_SID <= 0)
6441 error = ERROR_SCRIPT;
6442 else
6443 {
6444 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6445 i = (int)STRLEN(fname_buf);
6446 }
6447 }
6448 if (i + STRLEN(name + llen) < FLEN_FIXED)
6449 {
6450 STRCPY(fname_buf + i, name + llen);
6451 fname = fname_buf;
6452 }
6453 else
6454 {
6455 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6456 if (fname == NULL)
6457 error = ERROR_OTHER;
6458 else
6459 {
6460 mch_memmove(fname, fname_buf, (size_t)i);
6461 STRCPY(fname + i, name + llen);
6462 }
6463 }
6464 }
6465 else
6466 fname = name;
6467
6468 *doesrange = FALSE;
6469
6470
6471 /* execute the function if no errors detected and executing */
6472 if (evaluate && error == ERROR_NONE)
6473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 error = ERROR_UNKNOWN;
6476
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006477 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
6479 /*
6480 * User defined function.
6481 */
6482 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006485 /* Trigger FuncUndefined event, may load the function. */
6486 if (fp == NULL
6487 && apply_autocmds(EVENT_FUNCUNDEFINED,
6488 fname, fname, TRUE, NULL)
6489 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006491 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 fp = find_func(fname);
6493 }
6494#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006495 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006496 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006497 {
6498 /* loaded a package, search for the function again */
6499 fp = find_func(fname);
6500 }
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (fp != NULL)
6503 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006504 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006506 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006508 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006510 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 else
6513 {
6514 /*
6515 * Call the user function.
6516 * Save and restore search patterns, script variables and
6517 * redo buffer.
6518 */
6519 save_search_patterns();
6520 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006521 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006522 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006524 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6525 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6526 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006527 /* Function was unreferenced while being used, free it
6528 * now. */
6529 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 restoreRedobuff();
6531 restore_search_patterns();
6532 error = ERROR_NONE;
6533 }
6534 }
6535 }
6536 else
6537 {
6538 /*
6539 * Find the function name in the table, call its implementation.
6540 */
6541 i = find_internal_func(fname);
6542 if (i >= 0)
6543 {
6544 if (argcount < functions[i].f_min_argc)
6545 error = ERROR_TOOFEW;
6546 else if (argcount > functions[i].f_max_argc)
6547 error = ERROR_TOOMANY;
6548 else
6549 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006551 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 error = ERROR_NONE;
6553 }
6554 }
6555 }
6556 /*
6557 * The function call (or "FuncUndefined" autocommand sequence) might
6558 * have been aborted by an error, an interrupt, or an explicitly thrown
6559 * exception that has not been caught so far. This situation can be
6560 * tested for by calling aborting(). For an error in an internal
6561 * function or for the "E132" error in call_user_func(), however, the
6562 * throw point at which the "force_abort" flag (temporarily reset by
6563 * emsg()) is normally updated has not been reached yet. We need to
6564 * update that flag first to make aborting() reliable.
6565 */
6566 update_force_abort();
6567 }
6568 if (error == ERROR_NONE)
6569 ret = OK;
6570
6571 /*
6572 * Report an error unless the argument evaluation or function call has been
6573 * cancelled due to an aborting error, an interrupt, or an exception.
6574 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006575 if (!aborting())
6576 {
6577 switch (error)
6578 {
6579 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006581 break;
6582 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006584 break;
6585 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 name);
6588 break;
6589 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591 name);
6592 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 name);
6596 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 }
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600 name[len] = cc;
6601 if (fname != name && fname != fname_buf)
6602 vim_free(fname);
6603
6604 return ret;
6605}
6606
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607/*
6608 * Give an error message with a function name. Handle <SNR> things.
6609 */
6610 static void
6611emsg_funcname(msg, name)
6612 char *msg;
6613 char_u *name;
6614{
6615 char_u *p;
6616
6617 if (*name == K_SPECIAL)
6618 p = concat_str((char_u *)"<SNR>", name + 3);
6619 else
6620 p = name;
6621 EMSG2(_(msg), p);
6622 if (p != name)
6623 vim_free(p);
6624}
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626/*********************************************
6627 * Implementation of the built-in functions
6628 */
6629
6630/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006631 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 */
6633 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006634f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006635 typval_T *argvars;
6636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
Bram Moolenaar33570922005-01-25 22:26:29 +00006638 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006640 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006643 if ((l = argvars[0].vval.v_list) != NULL
6644 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6645 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006646 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 }
6648 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006649 EMSG(_(e_listreq));
6650}
6651
6652/*
6653 * "append(lnum, string/list)" function
6654 */
6655 static void
6656f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006657 typval_T *argvars;
6658 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006659{
6660 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006661 list_T *l = NULL;
6662 listitem_T *li = NULL;
6663 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006664 long added = 0;
6665
6666 rettv->vval.v_number = 1; /* Default: Failed */
6667 lnum = get_tv_lnum(argvars);
6668 if (lnum >= 0
6669 && lnum <= curbuf->b_ml.ml_line_count
6670 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006672 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006674 l = argvars[1].vval.v_list;
6675 if (l == NULL)
6676 return;
6677 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006679 for (;;)
6680 {
6681 if (l == NULL)
6682 tv = &argvars[1]; /* append a string */
6683 else if (li == NULL)
6684 break; /* end of list */
6685 else
6686 tv = &li->li_tv; /* append item from list */
6687 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6688 ++added;
6689 if (l == NULL)
6690 break;
6691 li = li->li_next;
6692 }
6693
6694 appended_lines_mark(lnum, added);
6695 if (curwin->w_cursor.lnum > lnum)
6696 curwin->w_cursor.lnum += added;
6697 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699}
6700
6701/*
6702 * "argc()" function
6703 */
6704/* ARGSUSED */
6705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006706f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006707 typval_T *argvars;
6708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006710 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711}
6712
6713/*
6714 * "argidx()" function
6715 */
6716/* ARGSUSED */
6717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006718f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006719 typval_T *argvars;
6720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006722 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
6725/*
6726 * "argv(nr)" function
6727 */
6728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006729f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006730 typval_T *argvars;
6731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 int idx;
6734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006735 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006739 rettv->vval.v_string = NULL;
6740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741}
6742
6743/*
6744 * "browse(save, title, initdir, default)" function
6745 */
6746/* ARGSUSED */
6747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006748f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006749 typval_T *argvars;
6750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752#ifdef FEAT_BROWSE
6753 int save;
6754 char_u *title;
6755 char_u *initdir;
6756 char_u *defname;
6757 char_u buf[NUMBUFLEN];
6758 char_u buf2[NUMBUFLEN];
6759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006760 save = get_tv_number(&argvars[0]);
6761 title = get_tv_string(&argvars[1]);
6762 initdir = get_tv_string_buf(&argvars[2], buf);
6763 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006765 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006766 do_browse(save ? BROWSE_SAVE : 0,
6767 title, defname, NULL, initdir, NULL, curbuf);
6768#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006769 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006770#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006771 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006772}
6773
6774/*
6775 * "browsedir(title, initdir)" function
6776 */
6777/* ARGSUSED */
6778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006779f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 typval_T *argvars;
6781 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006782{
6783#ifdef FEAT_BROWSE
6784 char_u *title;
6785 char_u *initdir;
6786 char_u buf[NUMBUFLEN];
6787
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006788 title = get_tv_string(&argvars[0]);
6789 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006792 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006796 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797}
6798
Bram Moolenaar33570922005-01-25 22:26:29 +00006799static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801/*
6802 * Find a buffer by number or exact name.
6803 */
6804 static buf_T *
6805find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006810 if (avar->v_type == VAR_NUMBER)
6811 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006812 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006815 if (buf == NULL)
6816 {
6817 /* No full path name match, try a match with a URL or a "nofile"
6818 * buffer, these don't use the full path. */
6819 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6820 if (buf->b_fname != NULL
6821 && (path_with_url(buf->b_fname)
6822#ifdef FEAT_QUICKFIX
6823 || bt_nofile(buf)
6824#endif
6825 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006826 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006827 break;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830 return buf;
6831}
6832
6833/*
6834 * "bufexists(expr)" function
6835 */
6836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 typval_T *argvars;
6839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842}
6843
6844/*
6845 * "buflisted(expr)" function
6846 */
6847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006848f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006849 typval_T *argvars;
6850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 buf_T *buf;
6853
6854 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006855 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
6858/*
6859 * "bufloaded(expr)" function
6860 */
6861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 typval_T *argvars;
6864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 buf_T *buf;
6867
6868 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870}
6871
Bram Moolenaar33570922005-01-25 22:26:29 +00006872static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874/*
6875 * Get buffer by number or pattern.
6876 */
6877 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 int save_magic;
6883 char_u *save_cpo;
6884 buf_T *buf;
6885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 if (tv->v_type == VAR_NUMBER)
6887 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006888 if (tv->v_type != VAR_STRING)
6889 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (name == NULL || *name == NUL)
6891 return curbuf;
6892 if (name[0] == '$' && name[1] == NUL)
6893 return lastbuf;
6894
6895 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6896 save_magic = p_magic;
6897 p_magic = TRUE;
6898 save_cpo = p_cpo;
6899 p_cpo = (char_u *)"";
6900
6901 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6902 TRUE, FALSE));
6903
6904 p_magic = save_magic;
6905 p_cpo = save_cpo;
6906
6907 /* If not found, try expanding the name, like done for bufexists(). */
6908 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
6911 return buf;
6912}
6913
6914/*
6915 * "bufname(expr)" function
6916 */
6917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006918f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 typval_T *argvars;
6920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 buf_T *buf;
6923
6924 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006925 buf = get_buf_tv(&argvars[0]);
6926 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006928 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 --emsg_off;
6932}
6933
6934/*
6935 * "bufnr(expr)" function
6936 */
6937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006938f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 typval_T *argvars;
6940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
6942 buf_T *buf;
6943
6944 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006945 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006947 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 --emsg_off;
6951}
6952
6953/*
6954 * "bufwinnr(nr)" function
6955 */
6956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006957f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 typval_T *argvars;
6959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961#ifdef FEAT_WINDOWS
6962 win_T *wp;
6963 int winnr = 0;
6964#endif
6965 buf_T *buf;
6966
6967 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006968 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_WINDOWS
6970 for (wp = firstwin; wp; wp = wp->w_next)
6971 {
6972 ++winnr;
6973 if (wp->w_buffer == buf)
6974 break;
6975 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006976 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006978 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979#endif
6980 --emsg_off;
6981}
6982
6983/*
6984 * "byte2line(byte)" function
6985 */
6986/*ARGSUSED*/
6987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006988f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 typval_T *argvars;
6990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#else
6995 long boff = 0;
6996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007001 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 (linenr_T)0, &boff);
7003#endif
7004}
7005
7006/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007007 * "byteidx()" function
7008 */
7009/*ARGSUSED*/
7010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007011f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 typval_T *argvars;
7013 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007014{
7015#ifdef FEAT_MBYTE
7016 char_u *t;
7017#endif
7018 char_u *str;
7019 long idx;
7020
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007021 str = get_tv_string(&argvars[0]);
7022 idx = get_tv_number(&argvars[1]);
7023 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007024 if (idx < 0)
7025 return;
7026
7027#ifdef FEAT_MBYTE
7028 t = str;
7029 for ( ; idx > 0; idx--)
7030 {
7031 if (*t == NUL) /* EOL reached */
7032 return;
7033 t += mb_ptr2len_check(t);
7034 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007035 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007036#else
7037 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007038 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007039#endif
7040}
7041
7042/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007043 * "call(func, arglist)" function
7044 */
7045 static void
7046f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007047 typval_T *argvars;
7048 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007049{
7050 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007052 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007053 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007054 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007055 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007056
7057 rettv->vval.v_number = 0;
7058 if (argvars[1].v_type != VAR_LIST)
7059 {
7060 EMSG(_(e_listreq));
7061 return;
7062 }
7063 if (argvars[1].vval.v_list == NULL)
7064 return;
7065
7066 if (argvars[0].v_type == VAR_FUNC)
7067 func = argvars[0].vval.v_string;
7068 else
7069 func = get_tv_string(&argvars[0]);
7070
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071 if (argvars[2].v_type != VAR_UNKNOWN)
7072 {
7073 if (argvars[2].v_type != VAR_DICT)
7074 {
7075 EMSG(_(e_dictreq));
7076 return;
7077 }
7078 selfdict = argvars[2].vval.v_dict;
7079 }
7080
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007081 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7082 item = item->li_next)
7083 {
7084 if (argc == MAX_FUNC_ARGS)
7085 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007086 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007087 break;
7088 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007089 /* Make a copy of each argument. This is needed to be able to set
7090 * v_lock to VAR_FIXED in the copy without changing the original list.
7091 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007092 copy_tv(&item->li_tv, &argv[argc++]);
7093 }
7094
7095 if (item == NULL)
7096 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7098 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007099
7100 /* Free the arguments. */
7101 while (argc > 0)
7102 clear_tv(&argv[--argc]);
7103}
7104
7105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 * "char2nr(string)" function
7107 */
7108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007109f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 typval_T *argvars;
7111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113#ifdef FEAT_MBYTE
7114 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007115 rettv->vval.v_number =
7116 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 else
7118#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
7122/*
7123 * "cindent(lnum)" function
7124 */
7125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007126f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 typval_T *argvars;
7128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130#ifdef FEAT_CINDENT
7131 pos_T pos;
7132 linenr_T lnum;
7133
7134 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007135 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7137 {
7138 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007139 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 curwin->w_cursor = pos;
7141 }
7142 else
7143#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145}
7146
7147/*
7148 * "col(string)" function
7149 */
7150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007151f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 typval_T *argvars;
7153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154{
7155 colnr_T col = 0;
7156 pos_T *fp;
7157
7158 fp = var2fpos(&argvars[0], FALSE);
7159 if (fp != NULL)
7160 {
7161 if (fp->col == MAXCOL)
7162 {
7163 /* '> can be MAXCOL, get the length of the line then */
7164 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7165 col = STRLEN(ml_get(fp->lnum)) + 1;
7166 else
7167 col = MAXCOL;
7168 }
7169 else
7170 {
7171 col = fp->col + 1;
7172#ifdef FEAT_VIRTUALEDIT
7173 /* col(".") when the cursor is on the NUL at the end of the line
7174 * because of "coladd" can be seen as an extra column. */
7175 if (virtual_active() && fp == &curwin->w_cursor)
7176 {
7177 char_u *p = ml_get_cursor();
7178
7179 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7180 curwin->w_virtcol - curwin->w_cursor.coladd))
7181 {
7182# ifdef FEAT_MBYTE
7183 int l;
7184
7185 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7186 col += l;
7187# else
7188 if (*p != NUL && p[1] == NUL)
7189 ++col;
7190# endif
7191 }
7192 }
7193#endif
7194 }
7195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007196 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197}
7198
7199/*
7200 * "confirm(message, buttons[, default [, type]])" function
7201 */
7202/*ARGSUSED*/
7203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007204f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 typval_T *argvars;
7206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7209 char_u *message;
7210 char_u *buttons = NULL;
7211 char_u buf[NUMBUFLEN];
7212 char_u buf2[NUMBUFLEN];
7213 int def = 1;
7214 int type = VIM_GENERIC;
7215 int c;
7216
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007220 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007221 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007226 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007227 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 switch (TOUPPER_ASC(c))
7229 {
7230 case 'E': type = VIM_ERROR; break;
7231 case 'Q': type = VIM_QUESTION; break;
7232 case 'I': type = VIM_INFO; break;
7233 case 'W': type = VIM_WARNING; break;
7234 case 'G': type = VIM_GENERIC; break;
7235 }
7236 }
7237 }
7238 }
7239
7240 if (buttons == NULL || *buttons == NUL)
7241 buttons = (char_u *)_("&Ok");
7242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 def, NULL);
7245#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247#endif
7248}
7249
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007250/*
7251 * "copy()" function
7252 */
7253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007254f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 typval_T *argvars;
7256 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007257{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007258 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007259}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007262 * "count()" function
7263 */
7264 static void
7265f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 typval_T *argvars;
7267 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007268{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007269 long n = 0;
7270 int ic = FALSE;
7271
Bram Moolenaare9a41262005-01-15 22:18:47 +00007272 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007273 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 listitem_T *li;
7275 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007277
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 if ((l = argvars[0].vval.v_list) != NULL)
7279 {
7280 li = l->lv_first;
7281 if (argvars[2].v_type != VAR_UNKNOWN)
7282 {
7283 ic = get_tv_number(&argvars[2]);
7284 if (argvars[3].v_type != VAR_UNKNOWN)
7285 {
7286 idx = get_tv_number(&argvars[3]);
7287 li = list_find(l, idx);
7288 if (li == NULL)
7289 EMSGN(_(e_listidx), idx);
7290 }
7291 }
7292
7293 for ( ; li != NULL; li = li->li_next)
7294 if (tv_equal(&li->li_tv, &argvars[1], ic))
7295 ++n;
7296 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 else if (argvars[0].v_type == VAR_DICT)
7299 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 int todo;
7301 dict_T *d;
7302 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 if ((d = argvars[0].vval.v_dict) != NULL)
7305 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 if (argvars[2].v_type != VAR_UNKNOWN)
7307 {
7308 ic = get_tv_number(&argvars[2]);
7309 if (argvars[3].v_type != VAR_UNKNOWN)
7310 EMSG(_(e_invarg));
7311 }
7312
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 todo = d->dv_hashtab.ht_used;
7314 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 {
7316 if (!HASHITEM_EMPTY(hi))
7317 {
7318 --todo;
7319 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7320 ++n;
7321 }
7322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323 }
7324 }
7325 else
7326 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007327 rettv->vval.v_number = n;
7328}
7329
7330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7332 *
7333 * Checks the existence of a cscope connection.
7334 */
7335/*ARGSUSED*/
7336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007337f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 typval_T *argvars;
7339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341#ifdef FEAT_CSCOPE
7342 int num = 0;
7343 char_u *dbpath = NULL;
7344 char_u *prepend = NULL;
7345 char_u buf[NUMBUFLEN];
7346
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007347 if (argvars[0].v_type != VAR_UNKNOWN
7348 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007350 num = (int)get_tv_number(&argvars[0]);
7351 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007352 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007353 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
7355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007356 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#endif
7360}
7361
7362/*
7363 * "cursor(lnum, col)" function
7364 *
7365 * Moves the cursor to the specified line and column
7366 */
7367/*ARGSUSED*/
7368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007369f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 typval_T *argvars;
7371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 long line, col;
7374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007375 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (line > 0)
7377 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (col > 0)
7380 curwin->w_cursor.col = col - 1;
7381#ifdef FEAT_VIRTUALEDIT
7382 curwin->w_cursor.coladd = 0;
7383#endif
7384
7385 /* Make sure the cursor is in a valid position. */
7386 check_cursor();
7387#ifdef FEAT_MBYTE
7388 /* Correct cursor for multi-byte character. */
7389 if (has_mbyte)
7390 mb_adjust_cursor();
7391#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007392
7393 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394}
7395
7396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 */
7399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007400f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 typval_T *argvars;
7402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007404 static int copyID = 0;
7405 int noref = 0;
7406
7407 if (argvars[1].v_type != VAR_UNKNOWN)
7408 noref = get_tv_number(&argvars[1]);
7409 if (noref < 0 || noref > 1)
7410 EMSG(_(e_invarg));
7411 else
7412 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
7415/*
7416 * "delete()" function
7417 */
7418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007419f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 typval_T *argvars;
7421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427}
7428
7429/*
7430 * "did_filetype()" function
7431 */
7432/*ARGSUSED*/
7433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007434f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 typval_T *argvars;
7436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437{
7438#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007439 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#endif
7443}
7444
7445/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007446 * "diff_filler()" function
7447 */
7448/*ARGSUSED*/
7449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 typval_T *argvars;
7452 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007453{
7454#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007456#endif
7457}
7458
7459/*
7460 * "diff_hlID()" function
7461 */
7462/*ARGSUSED*/
7463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 typval_T *argvars;
7466 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007467{
7468#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007470 static linenr_T prev_lnum = 0;
7471 static int changedtick = 0;
7472 static int fnum = 0;
7473 static int change_start = 0;
7474 static int change_end = 0;
7475 static enum hlf_value hlID = 0;
7476 int filler_lines;
7477 int col;
7478
7479 if (lnum != prev_lnum
7480 || changedtick != curbuf->b_changedtick
7481 || fnum != curbuf->b_fnum)
7482 {
7483 /* New line, buffer, change: need to get the values. */
7484 filler_lines = diff_check(curwin, lnum);
7485 if (filler_lines < 0)
7486 {
7487 if (filler_lines == -1)
7488 {
7489 change_start = MAXCOL;
7490 change_end = -1;
7491 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7492 hlID = HLF_ADD; /* added line */
7493 else
7494 hlID = HLF_CHD; /* changed line */
7495 }
7496 else
7497 hlID = HLF_ADD; /* added line */
7498 }
7499 else
7500 hlID = (enum hlf_value)0;
7501 prev_lnum = lnum;
7502 changedtick = curbuf->b_changedtick;
7503 fnum = curbuf->b_fnum;
7504 }
7505
7506 if (hlID == HLF_CHD || hlID == HLF_TXD)
7507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007509 if (col >= change_start && col <= change_end)
7510 hlID = HLF_TXD; /* changed text */
7511 else
7512 hlID = HLF_CHD; /* changed line */
7513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007514 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007515#endif
7516}
7517
7518/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007519 * "empty({expr})" function
7520 */
7521 static void
7522f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 typval_T *argvars;
7524 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007525{
7526 int n;
7527
7528 switch (argvars[0].v_type)
7529 {
7530 case VAR_STRING:
7531 case VAR_FUNC:
7532 n = argvars[0].vval.v_string == NULL
7533 || *argvars[0].vval.v_string == NUL;
7534 break;
7535 case VAR_NUMBER:
7536 n = argvars[0].vval.v_number == 0;
7537 break;
7538 case VAR_LIST:
7539 n = argvars[0].vval.v_list == NULL
7540 || argvars[0].vval.v_list->lv_first == NULL;
7541 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007542 case VAR_DICT:
7543 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007545 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007546 default:
7547 EMSG2(_(e_intern2), "f_empty()");
7548 n = 0;
7549 }
7550
7551 rettv->vval.v_number = n;
7552}
7553
7554/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007555 * "errorlist()" function
7556 */
7557/*ARGSUSED*/
7558 static void
7559f_errorlist(argvars, rettv)
7560 typval_T *argvars;
7561 typval_T *rettv;
7562{
7563#ifdef FEAT_QUICKFIX
7564 list_T *l;
7565#endif
7566
7567 rettv->vval.v_number = FALSE;
7568#ifdef FEAT_QUICKFIX
7569 l = list_alloc();
7570 if (l != NULL)
7571 {
7572 if (get_errorlist(l) != FAIL)
7573 {
7574 rettv->vval.v_list = l;
7575 rettv->v_type = VAR_LIST;
7576 ++l->lv_refcount;
7577 }
7578 else
7579 list_free(l);
7580 }
7581#endif
7582}
7583
7584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 * "escape({string}, {chars})" function
7586 */
7587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007588f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 typval_T *argvars;
7590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 char_u buf[NUMBUFLEN];
7593
Bram Moolenaar758711c2005-02-02 23:11:38 +00007594 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7595 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597}
7598
7599/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007600 * "eval()" function
7601 */
7602/*ARGSUSED*/
7603 static void
7604f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 typval_T *argvars;
7606 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007607{
7608 char_u *s;
7609
7610 s = get_tv_string(&argvars[0]);
7611 s = skipwhite(s);
7612
7613 if (eval1(&s, rettv, TRUE) == FAIL)
7614 rettv->vval.v_number = 0;
7615 else if (*s != NUL)
7616 EMSG(_(e_trailing));
7617}
7618
7619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 * "eventhandler()" function
7621 */
7622/*ARGSUSED*/
7623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007624f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 typval_T *argvars;
7626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629}
7630
7631/*
7632 * "executable()" function
7633 */
7634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *argvars;
7637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642/*
7643 * "exists()" function
7644 */
7645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007646f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 typval_T *argvars;
7648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 char_u *p;
7651 char_u *name;
7652 int n = FALSE;
7653 int len = 0;
7654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 if (*p == '$') /* environment variable */
7657 {
7658 /* first try "normal" environment variables (fast) */
7659 if (mch_getenv(p + 1) != NULL)
7660 n = TRUE;
7661 else
7662 {
7663 /* try expanding things like $VIM and ${HOME} */
7664 p = expand_env_save(p);
7665 if (p != NULL && *p != '$')
7666 n = TRUE;
7667 vim_free(p);
7668 }
7669 }
7670 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 else if (*p == '*') /* internal or user defined function */
7673 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
7676 else if (*p == ':')
7677 {
7678 n = cmd_exists(p + 1);
7679 }
7680 else if (*p == '#')
7681 {
7682#ifdef FEAT_AUTOCMD
7683 name = p + 1;
7684 p = vim_strchr(name, '#');
7685 if (p != NULL)
7686 n = au_exists(name, p, p + 1);
7687 else
7688 n = au_exists(name, name + STRLEN(name), NULL);
7689#endif
7690 }
7691 else /* internal variable */
7692 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007693 char_u *tofree;
7694 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007696 /* get_name_len() takes care of expanding curly braces */
7697 name = p;
7698 len = get_name_len(&p, &tofree, TRUE, FALSE);
7699 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007701 if (tofree != NULL)
7702 name = tofree;
7703 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7704 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007706 /* handle d.key, l[idx], f(expr) */
7707 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7708 if (n)
7709 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007713 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717}
7718
7719/*
7720 * "expand()" function
7721 */
7722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 typval_T *argvars;
7725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 char_u *s;
7728 int len;
7729 char_u *errormsg;
7730 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7731 expand_T xpc;
7732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733 rettv->v_type = VAR_STRING;
7734 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (*s == '%' || *s == '#' || *s == '<')
7736 {
7737 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 --emsg_off;
7740 }
7741 else
7742 {
7743 /* When the optional second argument is non-zero, don't remove matches
7744 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007745 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 flags |= WILD_KEEP_ALL;
7747 ExpandInit(&xpc);
7748 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007749 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 ExpandCleanup(&xpc);
7751 }
7752}
7753
7754/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007755 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007757 */
7758 static void
7759f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 typval_T *argvars;
7761 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007762{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007763 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007765 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 list_T *l1, *l2;
7767 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007768 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007769
Bram Moolenaare9a41262005-01-15 22:18:47 +00007770 l1 = argvars[0].vval.v_list;
7771 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007772 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7773 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007774 {
7775 if (argvars[2].v_type != VAR_UNKNOWN)
7776 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007777 before = get_tv_number(&argvars[2]);
7778 if (before == l1->lv_len)
7779 item = NULL;
7780 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007781 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007782 item = list_find(l1, before);
7783 if (item == NULL)
7784 {
7785 EMSGN(_(e_listidx), before);
7786 return;
7787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788 }
7789 }
7790 else
7791 item = NULL;
7792 list_extend(l1, l2, item);
7793
7794 ++l1->lv_refcount;
7795 copy_tv(&argvars[0], rettv);
7796 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7799 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 dict_T *d1, *d2;
7801 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 char_u *action;
7803 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007804 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007805 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806
7807 d1 = argvars[0].vval.v_dict;
7808 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007809 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7810 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 {
7812 /* Check the third argument. */
7813 if (argvars[2].v_type != VAR_UNKNOWN)
7814 {
7815 static char *(av[]) = {"keep", "force", "error"};
7816
7817 action = get_tv_string(&argvars[2]);
7818 for (i = 0; i < 3; ++i)
7819 if (STRCMP(action, av[i]) == 0)
7820 break;
7821 if (i == 3)
7822 {
7823 EMSGN(_(e_invarg2), action);
7824 return;
7825 }
7826 }
7827 else
7828 action = (char_u *)"force";
7829
7830 /* Go over all entries in the second dict and add them to the
7831 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 todo = d2->dv_hashtab.ht_used;
7833 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007834 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007835 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007837 --todo;
7838 di1 = dict_find(d1, hi2->hi_key, -1);
7839 if (di1 == NULL)
7840 {
7841 di1 = dictitem_copy(HI2DI(hi2));
7842 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7843 dictitem_free(di1);
7844 }
7845 else if (*action == 'e')
7846 {
7847 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7848 break;
7849 }
7850 else if (*action == 'f')
7851 {
7852 clear_tv(&di1->di_tv);
7853 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 }
7856 }
7857
7858 ++d1->dv_refcount;
7859 copy_tv(&argvars[0], rettv);
7860 }
7861 }
7862 else
7863 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007864}
7865
7866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 * "filereadable()" function
7868 */
7869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 typval_T *argvars;
7872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 FILE *fd;
7875 char_u *p;
7876 int n;
7877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7880 {
7881 n = TRUE;
7882 fclose(fd);
7883 }
7884 else
7885 n = FALSE;
7886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
7891 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7892 * rights to write into.
7893 */
7894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 typval_T *argvars;
7897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 char_u *p;
7900 int retval = 0;
7901#if defined(UNIX) || defined(VMS)
7902 int perm = 0;
7903#endif
7904
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007905 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906#if defined(UNIX) || defined(VMS)
7907 perm = mch_getperm(p);
7908#endif
7909#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7910 if (
7911# ifdef WIN3264
7912 mch_writable(p) &&
7913# else
7914# if defined(UNIX) || defined(VMS)
7915 (perm & 0222) &&
7916# endif
7917# endif
7918 mch_access((char *)p, W_OK) == 0
7919 )
7920#endif
7921 {
7922 ++retval;
7923 if (mch_isdir(p))
7924 ++retval;
7925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927}
7928
Bram Moolenaar33570922005-01-25 22:26:29 +00007929static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007930
7931 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007932findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 typval_T *argvars;
7934 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007935 int dir;
7936{
7937#ifdef FEAT_SEARCHPATH
7938 char_u *fname;
7939 char_u *fresult = NULL;
7940 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7941 char_u *p;
7942 char_u pathbuf[NUMBUFLEN];
7943 int count = 1;
7944 int first = TRUE;
7945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007950 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007951 if (*p != NUL)
7952 path = p;
7953
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007956 }
7957
7958 do
7959 {
7960 vim_free(fresult);
7961 fresult = find_file_in_path_option(first ? fname : NULL,
7962 first ? (int)STRLEN(fname) : 0,
7963 0, first, path, dir, NULL);
7964 first = FALSE;
7965 } while (--count > 0 && fresult != NULL);
7966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007969 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007972}
7973
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007974static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7975static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007976static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7977static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978
7979/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007980 * Prepare v: variable "idx" to be used.
7981 * Save the current typeval in "save_tv".
7982 * When not used yet add the variable to the v: hashtable.
7983 */
7984 static void
7985prepare_vimvar(idx, save_tv)
7986 int idx;
7987 typval_T *save_tv;
7988{
7989 *save_tv = vimvars[idx].vv_tv;
7990 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7991 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
7992}
7993
7994/*
7995 * Restore v: variable "idx" to typeval "save_tv".
7996 * When no longer defined, remove the variable from the v: hashtable.
7997 */
7998 static void
7999restore_vimvar(idx, save_tv)
8000 int idx;
8001 typval_T *save_tv;
8002{
8003 hashitem_T *hi;
8004
8005 clear_tv(&vimvars[idx].vv_tv);
8006 vimvars[idx].vv_tv = *save_tv;
8007 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8008 {
8009 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8010 if (HASHITEM_EMPTY(hi))
8011 EMSG2(_(e_intern2), "restore_vimvar()");
8012 else
8013 hash_remove(&vimvarht, hi);
8014 }
8015}
8016
8017/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018 * Implementation of map() and filter().
8019 */
8020 static void
8021filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 typval_T *argvars;
8023 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 int map;
8025{
8026 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 listitem_T *li, *nli;
8029 list_T *l = NULL;
8030 dictitem_T *di;
8031 hashtab_T *ht;
8032 hashitem_T *hi;
8033 dict_T *d = NULL;
8034 typval_T save_val;
8035 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008037 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008038 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8039
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040
8041 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008042 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008043 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008044 if ((l = argvars[0].vval.v_list) == NULL
8045 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008046 return;
8047 }
8048 else if (argvars[0].v_type == VAR_DICT)
8049 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008050 if ((d = argvars[0].vval.v_dict) == NULL
8051 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008052 return;
8053 }
8054 else
8055 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008056 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008057 return;
8058 }
8059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008060 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008061 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062
8063 if (argvars[0].v_type == VAR_DICT)
8064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008065 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008067
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008069 hash_lock(ht);
8070 todo = ht->ht_used;
8071 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008072 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008073 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008075 --todo;
8076 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008077 if (tv_check_lock(di->di_tv.v_lock, msg))
8078 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008079 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008080 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8081 break;
8082 if (!map && rem)
8083 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008085 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008086 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008087 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008088
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008089 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008091 else
8092 {
8093 for (li = l->lv_first; li != NULL; li = nli)
8094 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008095 if (tv_check_lock(li->li_tv.v_lock, msg))
8096 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008097 nli = li->li_next;
8098 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8099 break;
8100 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008101 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008102 }
8103 }
8104
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008105 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106
8107 copy_tv(&argvars[0], rettv);
8108}
8109
8110 static int
8111filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 char_u *expr;
8114 int map;
8115 int *remp;
8116{
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 char_u *s;
8119
Bram Moolenaar33570922005-01-25 22:26:29 +00008120 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 s = expr;
8122 if (eval1(&s, &rettv, TRUE) == FAIL)
8123 return FAIL;
8124 if (*s != NUL) /* check for trailing chars after expr */
8125 {
8126 EMSG2(_(e_invexpr2), s);
8127 return FAIL;
8128 }
8129 if (map)
8130 {
8131 /* map(): replace the list item value */
8132 clear_tv(tv);
8133 *tv = rettv;
8134 }
8135 else
8136 {
8137 /* filter(): when expr is zero remove the item */
8138 *remp = (get_tv_number(&rettv) == 0);
8139 clear_tv(&rettv);
8140 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008142 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008143}
8144
8145/*
8146 * "filter()" function
8147 */
8148 static void
8149f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 typval_T *argvars;
8151 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008152{
8153 filter_map(argvars, rettv, FALSE);
8154}
8155
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008157 * "finddir({fname}[, {path}[, {count}]])" function
8158 */
8159 static void
8160f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 typval_T *argvars;
8162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163{
8164 findfilendir(argvars, rettv, TRUE);
8165}
8166
8167/*
8168 * "findfile({fname}[, {path}[, {count}]])" function
8169 */
8170 static void
8171f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 typval_T *argvars;
8173 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008174{
8175 findfilendir(argvars, rettv, FALSE);
8176}
8177
8178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 * "fnamemodify({fname}, {mods})" function
8180 */
8181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008182f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 typval_T *argvars;
8184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 char_u *fname;
8187 char_u *mods;
8188 int usedlen = 0;
8189 int len;
8190 char_u *fbuf = NULL;
8191 char_u buf[NUMBUFLEN];
8192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008193 fname = get_tv_string(&argvars[0]);
8194 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 len = (int)STRLEN(fname);
8196
8197 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 vim_free(fbuf);
8205}
8206
Bram Moolenaar33570922005-01-25 22:26:29 +00008207static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
8209/*
8210 * "foldclosed()" function
8211 */
8212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008213foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008214 typval_T *argvars;
8215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 int end;
8217{
8218#ifdef FEAT_FOLDING
8219 linenr_T lnum;
8220 linenr_T first, last;
8221
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8224 {
8225 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8226 {
8227 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008228 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 return;
8232 }
8233 }
8234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
8238/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008239 * "foldclosed()" function
8240 */
8241 static void
8242f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 typval_T *argvars;
8244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008245{
8246 foldclosed_both(argvars, rettv, FALSE);
8247}
8248
8249/*
8250 * "foldclosedend()" function
8251 */
8252 static void
8253f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008256{
8257 foldclosed_both(argvars, rettv, TRUE);
8258}
8259
8260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 * "foldlevel()" function
8262 */
8263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 typval_T *argvars;
8266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267{
8268#ifdef FEAT_FOLDING
8269 linenr_T lnum;
8270
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 else
8275#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008276 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
8279/*
8280 * "foldtext()" function
8281 */
8282/*ARGSUSED*/
8283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 typval_T *argvars;
8286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
8288#ifdef FEAT_FOLDING
8289 linenr_T lnum;
8290 char_u *s;
8291 char_u *r;
8292 int len;
8293 char *txt;
8294#endif
8295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008296 rettv->v_type = VAR_STRING;
8297 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008299 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8300 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8301 <= curbuf->b_ml.ml_line_count
8302 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
8304 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8306 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 if (!linewhite(lnum))
8309 break;
8310 ++lnum;
8311 }
8312
8313 /* Find interesting text in this line. */
8314 s = skipwhite(ml_get(lnum));
8315 /* skip C comment-start */
8316 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008319 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008320 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008321 {
8322 s = skipwhite(ml_get(lnum + 1));
8323 if (*s == '*')
8324 s = skipwhite(s + 1);
8325 }
8326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 txt = _("+-%s%3ld lines: ");
8328 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008329 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 + 20 /* for %3ld */
8331 + STRLEN(s))); /* concatenated */
8332 if (r != NULL)
8333 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8335 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8336 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 len = (int)STRLEN(r);
8338 STRCAT(r, s);
8339 /* remove 'foldmarker' and 'commentstring' */
8340 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343 }
8344#endif
8345}
8346
8347/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008348 * "foldtextresult(lnum)" function
8349 */
8350/*ARGSUSED*/
8351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008352f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 typval_T *argvars;
8354 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008355{
8356#ifdef FEAT_FOLDING
8357 linenr_T lnum;
8358 char_u *text;
8359 char_u buf[51];
8360 foldinfo_T foldinfo;
8361 int fold_count;
8362#endif
8363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 rettv->v_type = VAR_STRING;
8365 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008366#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008368 fold_count = foldedCount(curwin, lnum, &foldinfo);
8369 if (fold_count > 0)
8370 {
8371 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8372 &foldinfo, buf);
8373 if (text == buf)
8374 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008375 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008376 }
8377#endif
8378}
8379
8380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 * "foreground()" function
8382 */
8383/*ARGSUSED*/
8384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008386 typval_T *argvars;
8387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390#ifdef FEAT_GUI
8391 if (gui.in_use)
8392 gui_mch_set_foreground();
8393#else
8394# ifdef WIN32
8395 win32_set_foreground();
8396# endif
8397#endif
8398}
8399
8400/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008401 * "function()" function
8402 */
8403/*ARGSUSED*/
8404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008406 typval_T *argvars;
8407 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008408{
8409 char_u *s;
8410
Bram Moolenaara7043832005-01-21 11:56:39 +00008411 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008412 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008413 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008414 EMSG2(_(e_invarg2), s);
8415 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008416 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008417 else
8418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008419 rettv->vval.v_string = vim_strsave(s);
8420 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008421 }
8422}
8423
8424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008425 * "get()" function
8426 */
8427 static void
8428f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 typval_T *argvars;
8430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008431{
Bram Moolenaar33570922005-01-25 22:26:29 +00008432 listitem_T *li;
8433 list_T *l;
8434 dictitem_T *di;
8435 dict_T *d;
8436 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008437
Bram Moolenaare9a41262005-01-15 22:18:47 +00008438 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008439 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008440 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008441 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008442 li = list_find(l, get_tv_number(&argvars[1]));
8443 if (li != NULL)
8444 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008445 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008447 else if (argvars[0].v_type == VAR_DICT)
8448 {
8449 if ((d = argvars[0].vval.v_dict) != NULL)
8450 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008451 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 if (di != NULL)
8453 tv = &di->di_tv;
8454 }
8455 }
8456 else
8457 EMSG2(_(e_listdictarg), "get()");
8458
8459 if (tv == NULL)
8460 {
8461 if (argvars[2].v_type == VAR_UNKNOWN)
8462 rettv->vval.v_number = 0;
8463 else
8464 copy_tv(&argvars[2], rettv);
8465 }
8466 else
8467 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008468}
8469
8470/*
8471 * "getbufvar()" function
8472 */
8473 static void
8474f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 typval_T *argvars;
8476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008477{
8478 buf_T *buf;
8479 buf_T *save_curbuf;
8480 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482
8483 ++emsg_off;
8484 buf = get_buf_tv(&argvars[0]);
8485 varname = get_tv_string(&argvars[1]);
8486
8487 rettv->v_type = VAR_STRING;
8488 rettv->vval.v_string = NULL;
8489
8490 if (buf != NULL && varname != NULL)
8491 {
8492 if (*varname == '&') /* buffer-local-option */
8493 {
8494 /* set curbuf to be our buf, temporarily */
8495 save_curbuf = curbuf;
8496 curbuf = buf;
8497
8498 get_option_tv(&varname, rettv, TRUE);
8499
8500 /* restore previous notion of curbuf */
8501 curbuf = save_curbuf;
8502 }
8503 else
8504 {
8505 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008506 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008507 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008508 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008509 }
8510 }
8511
8512 --emsg_off;
8513}
8514
8515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 * "getchar()" function
8517 */
8518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 typval_T *argvars;
8521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 varnumber_T n;
8524
8525 ++no_mapping;
8526 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 /* getchar(): blocking wait. */
8529 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 /* getchar(1): only check if char avail */
8532 n = vpeekc();
8533 else if (vpeekc() == NUL)
8534 /* getchar(0) and no char avail: return zero */
8535 n = 0;
8536 else
8537 /* getchar(0) and char avail: return char */
8538 n = safe_vgetc();
8539 --no_mapping;
8540 --allow_keys;
8541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008542 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (IS_SPECIAL(n) || mod_mask != 0)
8544 {
8545 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8546 int i = 0;
8547
8548 /* Turn a special key into three bytes, plus modifier. */
8549 if (mod_mask != 0)
8550 {
8551 temp[i++] = K_SPECIAL;
8552 temp[i++] = KS_MODIFIER;
8553 temp[i++] = mod_mask;
8554 }
8555 if (IS_SPECIAL(n))
8556 {
8557 temp[i++] = K_SPECIAL;
8558 temp[i++] = K_SECOND(n);
8559 temp[i++] = K_THIRD(n);
8560 }
8561#ifdef FEAT_MBYTE
8562 else if (has_mbyte)
8563 i += (*mb_char2bytes)(n, temp + i);
8564#endif
8565 else
8566 temp[i++] = n;
8567 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 rettv->v_type = VAR_STRING;
8569 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 }
8571}
8572
8573/*
8574 * "getcharmod()" function
8575 */
8576/*ARGSUSED*/
8577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 typval_T *argvars;
8580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584
8585/*
8586 * "getcmdline()" function
8587 */
8588/*ARGSUSED*/
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->v_type = VAR_STRING;
8595 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597
8598/*
8599 * "getcmdpos()" function
8600 */
8601/*ARGSUSED*/
8602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *argvars;
8605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608}
8609
8610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 * "getcwd()" function
8612 */
8613/*ARGSUSED*/
8614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008616 typval_T *argvars;
8617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 char_u cwd[MAXPATHL];
8620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 else
8625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#endif
8630 }
8631}
8632
8633/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008634 * "getfontname()" function
8635 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008636/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008641{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->v_type = VAR_STRING;
8643 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008644#ifdef FEAT_GUI
8645 if (gui.in_use)
8646 {
8647 GuiFont font;
8648 char_u *name = NULL;
8649
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008651 {
8652 /* Get the "Normal" font. Either the name saved by
8653 * hl_set_font_name() or from the font ID. */
8654 font = gui.norm_font;
8655 name = hl_get_font_name();
8656 }
8657 else
8658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008660 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8661 return;
8662 font = gui_mch_get_font(name, FALSE);
8663 if (font == NOFONT)
8664 return; /* Invalid font name, return empty string. */
8665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008668 gui_mch_free_font(font);
8669 }
8670#endif
8671}
8672
8673/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008674 * "getfperm({fname})" function
8675 */
8676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008680{
8681 char_u *fname;
8682 struct stat st;
8683 char_u *perm = NULL;
8684 char_u flags[] = "rwx";
8685 int i;
8686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008690 if (mch_stat((char *)fname, &st) >= 0)
8691 {
8692 perm = vim_strsave((char_u *)"---------");
8693 if (perm != NULL)
8694 {
8695 for (i = 0; i < 9; i++)
8696 {
8697 if (st.st_mode & (1 << (8 - i)))
8698 perm[i] = flags[i % 3];
8699 }
8700 }
8701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008703}
8704
8705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 * "getfsize({fname})" function
8707 */
8708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 typval_T *argvars;
8711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 char_u *fname;
8714 struct stat st;
8715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 if (mch_stat((char *)fname, &st) >= 0)
8721 {
8722 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 }
8727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729}
8730
8731/*
8732 * "getftime({fname})" function
8733 */
8734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 char_u *fname;
8740 struct stat st;
8741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
8744 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748}
8749
8750/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008751 * "getftype({fname})" function
8752 */
8753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 typval_T *argvars;
8756 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008757{
8758 char_u *fname;
8759 struct stat st;
8760 char_u *type = NULL;
8761 char *t;
8762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008766 if (mch_lstat((char *)fname, &st) >= 0)
8767 {
8768#ifdef S_ISREG
8769 if (S_ISREG(st.st_mode))
8770 t = "file";
8771 else if (S_ISDIR(st.st_mode))
8772 t = "dir";
8773# ifdef S_ISLNK
8774 else if (S_ISLNK(st.st_mode))
8775 t = "link";
8776# endif
8777# ifdef S_ISBLK
8778 else if (S_ISBLK(st.st_mode))
8779 t = "bdev";
8780# endif
8781# ifdef S_ISCHR
8782 else if (S_ISCHR(st.st_mode))
8783 t = "cdev";
8784# endif
8785# ifdef S_ISFIFO
8786 else if (S_ISFIFO(st.st_mode))
8787 t = "fifo";
8788# endif
8789# ifdef S_ISSOCK
8790 else if (S_ISSOCK(st.st_mode))
8791 t = "fifo";
8792# endif
8793 else
8794 t = "other";
8795#else
8796# ifdef S_IFMT
8797 switch (st.st_mode & S_IFMT)
8798 {
8799 case S_IFREG: t = "file"; break;
8800 case S_IFDIR: t = "dir"; break;
8801# ifdef S_IFLNK
8802 case S_IFLNK: t = "link"; break;
8803# endif
8804# ifdef S_IFBLK
8805 case S_IFBLK: t = "bdev"; break;
8806# endif
8807# ifdef S_IFCHR
8808 case S_IFCHR: t = "cdev"; break;
8809# endif
8810# ifdef S_IFIFO
8811 case S_IFIFO: t = "fifo"; break;
8812# endif
8813# ifdef S_IFSOCK
8814 case S_IFSOCK: t = "socket"; break;
8815# endif
8816 default: t = "other";
8817 }
8818# else
8819 if (mch_isdir(fname))
8820 t = "dir";
8821 else
8822 t = "file";
8823# endif
8824#endif
8825 type = vim_strsave((char_u *)t);
8826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008828}
8829
8830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008831 * "getline(lnum)" function
8832 */
8833 static void
8834f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 typval_T *argvars;
8836 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008837{
8838 linenr_T lnum;
8839 linenr_T end;
8840 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008841 list_T *l;
8842 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008843
8844 lnum = get_tv_lnum(argvars);
8845
8846 if (argvars[1].v_type == VAR_UNKNOWN)
8847 {
8848 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8849 p = ml_get(lnum);
8850 else
8851 p = (char_u *)"";
8852
8853 rettv->v_type = VAR_STRING;
8854 rettv->vval.v_string = vim_strsave(p);
8855 }
8856 else
8857 {
8858 end = get_tv_lnum(&argvars[1]);
8859 if (end < lnum)
8860 {
8861 EMSG(_(e_invrange));
8862 rettv->vval.v_number = 0;
8863 }
8864 else
8865 {
8866 l = list_alloc();
8867 if (l != NULL)
8868 {
8869 if (lnum < 1)
8870 lnum = 1;
8871 if (end > curbuf->b_ml.ml_line_count)
8872 end = curbuf->b_ml.ml_line_count;
8873 while (lnum <= end)
8874 {
8875 li = listitem_alloc();
8876 if (li == NULL)
8877 break;
8878 list_append(l, li);
8879 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008880 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008881 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8882 }
8883 rettv->vval.v_list = l;
8884 rettv->v_type = VAR_LIST;
8885 ++l->lv_refcount;
8886 }
8887 }
8888 }
8889}
8890
8891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 * "getreg()" function
8893 */
8894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 typval_T *argvars;
8897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899 char_u *strregname;
8900 int regname;
8901
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008902 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008905 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 regname = (strregname == NULL ? '"' : *strregname);
8907 if (regname == 0)
8908 regname = '"';
8909
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910 rettv->v_type = VAR_STRING;
8911 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912}
8913
8914/*
8915 * "getregtype()" function
8916 */
8917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008919 typval_T *argvars;
8920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 char_u *strregname;
8923 int regname;
8924 char_u buf[NUMBUFLEN + 2];
8925 long reglen = 0;
8926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008927 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 else
8930 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
8933 regname = (strregname == NULL ? '"' : *strregname);
8934 if (regname == 0)
8935 regname = '"';
8936
8937 buf[0] = NUL;
8938 buf[1] = NUL;
8939 switch (get_reg_type(regname, &reglen))
8940 {
8941 case MLINE: buf[0] = 'V'; break;
8942 case MCHAR: buf[0] = 'v'; break;
8943#ifdef FEAT_VISUAL
8944 case MBLOCK:
8945 buf[0] = Ctrl_V;
8946 sprintf((char *)buf + 1, "%ld", reglen + 1);
8947 break;
8948#endif
8949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->v_type = VAR_STRING;
8951 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
8954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 * "getwinposx()" function
8956 */
8957/*ARGSUSED*/
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964#ifdef FEAT_GUI
8965 if (gui.in_use)
8966 {
8967 int x, y;
8968
8969 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
8972#endif
8973}
8974
8975/*
8976 * "getwinposy()" function
8977 */
8978/*ARGSUSED*/
8979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980f_getwinposy(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#ifdef FEAT_GUI
8986 if (gui.in_use)
8987 {
8988 int x, y;
8989
8990 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993#endif
8994}
8995
8996/*
8997 * "getwinvar()" function
8998 */
8999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 win_T *win, *oldcurwin;
9005 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 ++emsg_off;
9009 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->v_type = VAR_STRING;
9013 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
9015 if (win != NULL && varname != NULL)
9016 {
9017 if (*varname == '&') /* window-local-option */
9018 {
9019 /* set curwin to be our win, temporarily */
9020 oldcurwin = curwin;
9021 curwin = win;
9022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
9025 /* restore previous notion of curwin */
9026 curwin = oldcurwin;
9027 }
9028 else
9029 {
9030 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009031 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 }
9035 }
9036
9037 --emsg_off;
9038}
9039
9040/*
9041 * "glob()" function
9042 */
9043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 typval_T *argvars;
9046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 expand_T xpc;
9049
9050 ExpandInit(&xpc);
9051 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 rettv->v_type = VAR_STRING;
9053 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9055 ExpandCleanup(&xpc);
9056}
9057
9058/*
9059 * "globpath()" function
9060 */
9061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062f_globpath(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 char_u buf1[NUMBUFLEN];
9067
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->v_type = VAR_STRING;
9069 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9070 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071}
9072
9073/*
9074 * "has()" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 int i;
9082 char_u *name;
9083 int n = FALSE;
9084 static char *(has_list[]) =
9085 {
9086#ifdef AMIGA
9087 "amiga",
9088# ifdef FEAT_ARP
9089 "arp",
9090# endif
9091#endif
9092#ifdef __BEOS__
9093 "beos",
9094#endif
9095#ifdef MSDOS
9096# ifdef DJGPP
9097 "dos32",
9098# else
9099 "dos16",
9100# endif
9101#endif
9102#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9103 "mac",
9104#endif
9105#if defined(MACOS_X_UNIX)
9106 "macunix",
9107#endif
9108#ifdef OS2
9109 "os2",
9110#endif
9111#ifdef __QNX__
9112 "qnx",
9113#endif
9114#ifdef RISCOS
9115 "riscos",
9116#endif
9117#ifdef UNIX
9118 "unix",
9119#endif
9120#ifdef VMS
9121 "vms",
9122#endif
9123#ifdef WIN16
9124 "win16",
9125#endif
9126#ifdef WIN32
9127 "win32",
9128#endif
9129#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9130 "win32unix",
9131#endif
9132#ifdef WIN64
9133 "win64",
9134#endif
9135#ifdef EBCDIC
9136 "ebcdic",
9137#endif
9138#ifndef CASE_INSENSITIVE_FILENAME
9139 "fname_case",
9140#endif
9141#ifdef FEAT_ARABIC
9142 "arabic",
9143#endif
9144#ifdef FEAT_AUTOCMD
9145 "autocmd",
9146#endif
9147#ifdef FEAT_BEVAL
9148 "balloon_eval",
9149#endif
9150#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9151 "builtin_terms",
9152# ifdef ALL_BUILTIN_TCAPS
9153 "all_builtin_terms",
9154# endif
9155#endif
9156#ifdef FEAT_BYTEOFF
9157 "byte_offset",
9158#endif
9159#ifdef FEAT_CINDENT
9160 "cindent",
9161#endif
9162#ifdef FEAT_CLIENTSERVER
9163 "clientserver",
9164#endif
9165#ifdef FEAT_CLIPBOARD
9166 "clipboard",
9167#endif
9168#ifdef FEAT_CMDL_COMPL
9169 "cmdline_compl",
9170#endif
9171#ifdef FEAT_CMDHIST
9172 "cmdline_hist",
9173#endif
9174#ifdef FEAT_COMMENTS
9175 "comments",
9176#endif
9177#ifdef FEAT_CRYPT
9178 "cryptv",
9179#endif
9180#ifdef FEAT_CSCOPE
9181 "cscope",
9182#endif
9183#ifdef DEBUG
9184 "debug",
9185#endif
9186#ifdef FEAT_CON_DIALOG
9187 "dialog_con",
9188#endif
9189#ifdef FEAT_GUI_DIALOG
9190 "dialog_gui",
9191#endif
9192#ifdef FEAT_DIFF
9193 "diff",
9194#endif
9195#ifdef FEAT_DIGRAPHS
9196 "digraphs",
9197#endif
9198#ifdef FEAT_DND
9199 "dnd",
9200#endif
9201#ifdef FEAT_EMACS_TAGS
9202 "emacs_tags",
9203#endif
9204 "eval", /* always present, of course! */
9205#ifdef FEAT_EX_EXTRA
9206 "ex_extra",
9207#endif
9208#ifdef FEAT_SEARCH_EXTRA
9209 "extra_search",
9210#endif
9211#ifdef FEAT_FKMAP
9212 "farsi",
9213#endif
9214#ifdef FEAT_SEARCHPATH
9215 "file_in_path",
9216#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009217#if defined(UNIX) && !defined(USE_SYSTEM)
9218 "filterpipe",
9219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#ifdef FEAT_FIND_ID
9221 "find_in_path",
9222#endif
9223#ifdef FEAT_FOLDING
9224 "folding",
9225#endif
9226#ifdef FEAT_FOOTER
9227 "footer",
9228#endif
9229#if !defined(USE_SYSTEM) && defined(UNIX)
9230 "fork",
9231#endif
9232#ifdef FEAT_GETTEXT
9233 "gettext",
9234#endif
9235#ifdef FEAT_GUI
9236 "gui",
9237#endif
9238#ifdef FEAT_GUI_ATHENA
9239# ifdef FEAT_GUI_NEXTAW
9240 "gui_neXtaw",
9241# else
9242 "gui_athena",
9243# endif
9244#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009245#ifdef FEAT_GUI_KDE
9246 "gui_kde",
9247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248#ifdef FEAT_GUI_GTK
9249 "gui_gtk",
9250# ifdef HAVE_GTK2
9251 "gui_gtk2",
9252# endif
9253#endif
9254#ifdef FEAT_GUI_MAC
9255 "gui_mac",
9256#endif
9257#ifdef FEAT_GUI_MOTIF
9258 "gui_motif",
9259#endif
9260#ifdef FEAT_GUI_PHOTON
9261 "gui_photon",
9262#endif
9263#ifdef FEAT_GUI_W16
9264 "gui_win16",
9265#endif
9266#ifdef FEAT_GUI_W32
9267 "gui_win32",
9268#endif
9269#ifdef FEAT_HANGULIN
9270 "hangul_input",
9271#endif
9272#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9273 "iconv",
9274#endif
9275#ifdef FEAT_INS_EXPAND
9276 "insert_expand",
9277#endif
9278#ifdef FEAT_JUMPLIST
9279 "jumplist",
9280#endif
9281#ifdef FEAT_KEYMAP
9282 "keymap",
9283#endif
9284#ifdef FEAT_LANGMAP
9285 "langmap",
9286#endif
9287#ifdef FEAT_LIBCALL
9288 "libcall",
9289#endif
9290#ifdef FEAT_LINEBREAK
9291 "linebreak",
9292#endif
9293#ifdef FEAT_LISP
9294 "lispindent",
9295#endif
9296#ifdef FEAT_LISTCMDS
9297 "listcmds",
9298#endif
9299#ifdef FEAT_LOCALMAP
9300 "localmap",
9301#endif
9302#ifdef FEAT_MENU
9303 "menu",
9304#endif
9305#ifdef FEAT_SESSION
9306 "mksession",
9307#endif
9308#ifdef FEAT_MODIFY_FNAME
9309 "modify_fname",
9310#endif
9311#ifdef FEAT_MOUSE
9312 "mouse",
9313#endif
9314#ifdef FEAT_MOUSESHAPE
9315 "mouseshape",
9316#endif
9317#if defined(UNIX) || defined(VMS)
9318# ifdef FEAT_MOUSE_DEC
9319 "mouse_dec",
9320# endif
9321# ifdef FEAT_MOUSE_GPM
9322 "mouse_gpm",
9323# endif
9324# ifdef FEAT_MOUSE_JSB
9325 "mouse_jsbterm",
9326# endif
9327# ifdef FEAT_MOUSE_NET
9328 "mouse_netterm",
9329# endif
9330# ifdef FEAT_MOUSE_PTERM
9331 "mouse_pterm",
9332# endif
9333# ifdef FEAT_MOUSE_XTERM
9334 "mouse_xterm",
9335# endif
9336#endif
9337#ifdef FEAT_MBYTE
9338 "multi_byte",
9339#endif
9340#ifdef FEAT_MBYTE_IME
9341 "multi_byte_ime",
9342#endif
9343#ifdef FEAT_MULTI_LANG
9344 "multi_lang",
9345#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009346#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009347#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009348 "mzscheme",
9349#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351#ifdef FEAT_OLE
9352 "ole",
9353#endif
9354#ifdef FEAT_OSFILETYPE
9355 "osfiletype",
9356#endif
9357#ifdef FEAT_PATH_EXTRA
9358 "path_extra",
9359#endif
9360#ifdef FEAT_PERL
9361#ifndef DYNAMIC_PERL
9362 "perl",
9363#endif
9364#endif
9365#ifdef FEAT_PYTHON
9366#ifndef DYNAMIC_PYTHON
9367 "python",
9368#endif
9369#endif
9370#ifdef FEAT_POSTSCRIPT
9371 "postscript",
9372#endif
9373#ifdef FEAT_PRINTER
9374 "printer",
9375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009376#ifdef FEAT_PROFILE
9377 "profile",
9378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#ifdef FEAT_QUICKFIX
9380 "quickfix",
9381#endif
9382#ifdef FEAT_RIGHTLEFT
9383 "rightleft",
9384#endif
9385#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9386 "ruby",
9387#endif
9388#ifdef FEAT_SCROLLBIND
9389 "scrollbind",
9390#endif
9391#ifdef FEAT_CMDL_INFO
9392 "showcmd",
9393 "cmdline_info",
9394#endif
9395#ifdef FEAT_SIGNS
9396 "signs",
9397#endif
9398#ifdef FEAT_SMARTINDENT
9399 "smartindent",
9400#endif
9401#ifdef FEAT_SNIFF
9402 "sniff",
9403#endif
9404#ifdef FEAT_STL_OPT
9405 "statusline",
9406#endif
9407#ifdef FEAT_SUN_WORKSHOP
9408 "sun_workshop",
9409#endif
9410#ifdef FEAT_NETBEANS_INTG
9411 "netbeans_intg",
9412#endif
9413#ifdef FEAT_SYN_HL
9414 "syntax",
9415#endif
9416#if defined(USE_SYSTEM) || !defined(UNIX)
9417 "system",
9418#endif
9419#ifdef FEAT_TAG_BINS
9420 "tag_binary",
9421#endif
9422#ifdef FEAT_TAG_OLDSTATIC
9423 "tag_old_static",
9424#endif
9425#ifdef FEAT_TAG_ANYWHITE
9426 "tag_any_white",
9427#endif
9428#ifdef FEAT_TCL
9429# ifndef DYNAMIC_TCL
9430 "tcl",
9431# endif
9432#endif
9433#ifdef TERMINFO
9434 "terminfo",
9435#endif
9436#ifdef FEAT_TERMRESPONSE
9437 "termresponse",
9438#endif
9439#ifdef FEAT_TEXTOBJ
9440 "textobjects",
9441#endif
9442#ifdef HAVE_TGETENT
9443 "tgetent",
9444#endif
9445#ifdef FEAT_TITLE
9446 "title",
9447#endif
9448#ifdef FEAT_TOOLBAR
9449 "toolbar",
9450#endif
9451#ifdef FEAT_USR_CMDS
9452 "user-commands", /* was accidentally included in 5.4 */
9453 "user_commands",
9454#endif
9455#ifdef FEAT_VIMINFO
9456 "viminfo",
9457#endif
9458#ifdef FEAT_VERTSPLIT
9459 "vertsplit",
9460#endif
9461#ifdef FEAT_VIRTUALEDIT
9462 "virtualedit",
9463#endif
9464#ifdef FEAT_VISUAL
9465 "visual",
9466#endif
9467#ifdef FEAT_VISUALEXTRA
9468 "visualextra",
9469#endif
9470#ifdef FEAT_VREPLACE
9471 "vreplace",
9472#endif
9473#ifdef FEAT_WILDIGN
9474 "wildignore",
9475#endif
9476#ifdef FEAT_WILDMENU
9477 "wildmenu",
9478#endif
9479#ifdef FEAT_WINDOWS
9480 "windows",
9481#endif
9482#ifdef FEAT_WAK
9483 "winaltkeys",
9484#endif
9485#ifdef FEAT_WRITEBACKUP
9486 "writebackup",
9487#endif
9488#ifdef FEAT_XIM
9489 "xim",
9490#endif
9491#ifdef FEAT_XFONTSET
9492 "xfontset",
9493#endif
9494#ifdef USE_XSMP
9495 "xsmp",
9496#endif
9497#ifdef USE_XSMP_INTERACT
9498 "xsmp_interact",
9499#endif
9500#ifdef FEAT_XCLIPBOARD
9501 "xterm_clipboard",
9502#endif
9503#ifdef FEAT_XTERM_SAVE
9504 "xterm_save",
9505#endif
9506#if defined(UNIX) && defined(FEAT_X11)
9507 "X11",
9508#endif
9509 NULL
9510 };
9511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 for (i = 0; has_list[i] != NULL; ++i)
9514 if (STRICMP(name, has_list[i]) == 0)
9515 {
9516 n = TRUE;
9517 break;
9518 }
9519
9520 if (n == FALSE)
9521 {
9522 if (STRNICMP(name, "patch", 5) == 0)
9523 n = has_patch(atoi((char *)name + 5));
9524 else if (STRICMP(name, "vim_starting") == 0)
9525 n = (starting != 0);
9526#ifdef DYNAMIC_TCL
9527 else if (STRICMP(name, "tcl") == 0)
9528 n = tcl_enabled(FALSE);
9529#endif
9530#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9531 else if (STRICMP(name, "iconv") == 0)
9532 n = iconv_enabled(FALSE);
9533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009534#ifdef DYNAMIC_MZSCHEME
9535 else if (STRICMP(name, "mzscheme") == 0)
9536 n = mzscheme_enabled(FALSE);
9537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538#ifdef DYNAMIC_RUBY
9539 else if (STRICMP(name, "ruby") == 0)
9540 n = ruby_enabled(FALSE);
9541#endif
9542#ifdef DYNAMIC_PYTHON
9543 else if (STRICMP(name, "python") == 0)
9544 n = python_enabled(FALSE);
9545#endif
9546#ifdef DYNAMIC_PERL
9547 else if (STRICMP(name, "perl") == 0)
9548 n = perl_enabled(FALSE);
9549#endif
9550#ifdef FEAT_GUI
9551 else if (STRICMP(name, "gui_running") == 0)
9552 n = (gui.in_use || gui.starting);
9553# ifdef FEAT_GUI_W32
9554 else if (STRICMP(name, "gui_win32s") == 0)
9555 n = gui_is_win32s();
9556# endif
9557# ifdef FEAT_BROWSE
9558 else if (STRICMP(name, "browse") == 0)
9559 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9560# endif
9561#endif
9562#ifdef FEAT_SYN_HL
9563 else if (STRICMP(name, "syntax_items") == 0)
9564 n = syntax_present(curbuf);
9565#endif
9566#if defined(WIN3264)
9567 else if (STRICMP(name, "win95") == 0)
9568 n = mch_windows95();
9569#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009570#ifdef FEAT_NETBEANS_INTG
9571 else if (STRICMP(name, "netbeans_enabled") == 0)
9572 n = usingNetbeans;
9573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 }
9575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577}
9578
9579/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009580 * "has_key()" function
9581 */
9582 static void
9583f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009584 typval_T *argvars;
9585 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009586{
9587 rettv->vval.v_number = 0;
9588 if (argvars[0].v_type != VAR_DICT)
9589 {
9590 EMSG(_(e_dictreq));
9591 return;
9592 }
9593 if (argvars[0].vval.v_dict == NULL)
9594 return;
9595
9596 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009597 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * "hasmapto()" function
9602 */
9603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 typval_T *argvars;
9606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
9608 char_u *name;
9609 char_u *mode;
9610 char_u buf[NUMBUFLEN];
9611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 mode = (char_u *)"nvo";
9615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617
9618 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622}
9623
9624/*
9625 * "histadd()" function
9626 */
9627/*ARGSUSED*/
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633#ifdef FEAT_CMDHIST
9634 int histype;
9635 char_u *str;
9636 char_u buf[NUMBUFLEN];
9637#endif
9638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 if (check_restricted() || check_secure())
9641 return;
9642#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 if (histype >= 0)
9645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 if (*str != NUL)
9648 {
9649 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 return;
9652 }
9653 }
9654#endif
9655}
9656
9657/*
9658 * "histdel()" function
9659 */
9660/*ARGSUSED*/
9661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009663 typval_T *argvars;
9664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666#ifdef FEAT_CMDHIST
9667 int n;
9668 char_u buf[NUMBUFLEN];
9669
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009670 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009673 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9676 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 else
9678 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9680 get_tv_string_buf(&argvars[1], buf));
9681 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684#endif
9685}
9686
9687/*
9688 * "histget()" function
9689 */
9690/*ARGSUSED*/
9691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009693 typval_T *argvars;
9694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696#ifdef FEAT_CMDHIST
9697 int type;
9698 int idx;
9699
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009701 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 idx = get_history_idx(type);
9703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 idx = (int)get_tv_number(&argvars[1]);
9705 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710}
9711
9712/*
9713 * "histnr()" function
9714 */
9715/*ARGSUSED*/
9716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009718 typval_T *argvars;
9719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 int i;
9722
9723#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 if (i >= HIST_CMD && i < HIST_COUNT)
9726 i = get_history_idx(i);
9727 else
9728#endif
9729 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 * "highlightID(name)" function
9735 */
9736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009745 * "highlight_exists()" function
9746 */
9747 static void
9748f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009751{
9752 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9753}
9754
9755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 * "hostname()" function
9757 */
9758/*ARGSUSED*/
9759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009761 typval_T *argvars;
9762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
9764 char_u hostname[256];
9765
9766 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->v_type = VAR_STRING;
9768 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771/*
9772 * iconv() function
9773 */
9774/*ARGSUSED*/
9775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *argvars;
9778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780#ifdef FEAT_MBYTE
9781 char_u buf1[NUMBUFLEN];
9782 char_u buf2[NUMBUFLEN];
9783 char_u *from, *to, *str;
9784 vimconv_T vimconv;
9785#endif
9786
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->v_type = VAR_STRING;
9788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789
9790#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 str = get_tv_string(&argvars[0]);
9792 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9793 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 vimconv.vc_type = CONV_NONE;
9795 convert_setup(&vimconv, from, to);
9796
9797 /* If the encodings are equal, no conversion needed. */
9798 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802
9803 convert_setup(&vimconv, NULL, NULL);
9804 vim_free(from);
9805 vim_free(to);
9806#endif
9807}
9808
9809/*
9810 * "indent()" function
9811 */
9812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009814 typval_T *argvars;
9815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 linenr_T lnum;
9818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824}
9825
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009826/*
9827 * "index()" function
9828 */
9829 static void
9830f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009833{
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 list_T *l;
9835 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009836 long idx = 0;
9837 int ic = FALSE;
9838
9839 rettv->vval.v_number = -1;
9840 if (argvars[0].v_type != VAR_LIST)
9841 {
9842 EMSG(_(e_listreq));
9843 return;
9844 }
9845 l = argvars[0].vval.v_list;
9846 if (l != NULL)
9847 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009848 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009849 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009850 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009851 /* Start at specified item. Use the cached index that list_find()
9852 * sets, so that a negative number also works. */
9853 item = list_find(l, get_tv_number(&argvars[2]));
9854 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009855 if (argvars[3].v_type != VAR_UNKNOWN)
9856 ic = get_tv_number(&argvars[3]);
9857 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009858
Bram Moolenaar758711c2005-02-02 23:11:38 +00009859 for ( ; item != NULL; item = item->li_next, ++idx)
9860 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009861 {
9862 rettv->vval.v_number = idx;
9863 break;
9864 }
9865 }
9866}
9867
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868static int inputsecret_flag = 0;
9869
9870/*
9871 * "input()" function
9872 * Also handles inputsecret() when inputsecret is set.
9873 */
9874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 typval_T *argvars;
9877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 char_u *p = NULL;
9881 int c;
9882 char_u buf[NUMBUFLEN];
9883 int cmd_silent_save = cmd_silent;
9884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
9887#ifdef NO_CONSOLE_INPUT
9888 /* While starting up, there is no place to enter text. */
9889 if (no_console_input())
9890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 return;
9893 }
9894#endif
9895
9896 cmd_silent = FALSE; /* Want to see the prompt. */
9897 if (prompt != NULL)
9898 {
9899 /* Only the part of the message after the last NL is considered as
9900 * prompt for the command line */
9901 p = vim_strrchr(prompt, '\n');
9902 if (p == NULL)
9903 p = prompt;
9904 else
9905 {
9906 ++p;
9907 c = *p;
9908 *p = NUL;
9909 msg_start();
9910 msg_clr_eos();
9911 msg_puts_attr(prompt, echo_attr);
9912 msg_didout = FALSE;
9913 msg_starthere();
9914 *p = c;
9915 }
9916 cmdline_row = msg_row;
9917 }
9918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009919 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009922 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9924
9925 /* since the user typed this, no need to wait for return */
9926 need_wait_return = FALSE;
9927 msg_didout = FALSE;
9928 cmd_silent = cmd_silent_save;
9929}
9930
9931/*
9932 * "inputdialog()" function
9933 */
9934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009936 typval_T *argvars;
9937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939#if defined(FEAT_GUI_TEXTDIALOG)
9940 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9941 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9942 {
9943 char_u *message;
9944 char_u buf[NUMBUFLEN];
9945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009947 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 IObuff[IOSIZE - 1] = NUL;
9951 }
9952 else
9953 IObuff[0] = NUL;
9954 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9955 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 else
9958 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009959 if (argvars[1].v_type != VAR_UNKNOWN
9960 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_string = vim_strsave(
9962 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 }
9968 else
9969#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971}
9972
9973static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9974
9975/*
9976 * "inputrestore()" function
9977 */
9978/*ARGSUSED*/
9979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009981 typval_T *argvars;
9982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
9984 if (ga_userinput.ga_len > 0)
9985 {
9986 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9988 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 }
9991 else if (p_verbose > 1)
9992 {
9993 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 }
9996}
9997
9998/*
9999 * "inputsave()" function
10000 */
10001/*ARGSUSED*/
10002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010004 typval_T *argvars;
10005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 /* Add an entry to the stack of typehead storage. */
10008 if (ga_grow(&ga_userinput, 1) == OK)
10009 {
10010 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10011 + ga_userinput.ga_len);
10012 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 }
10015 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017}
10018
10019/*
10020 * "inputsecret()" function
10021 */
10022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 ++cmdline_star;
10028 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 --cmdline_star;
10031 --inputsecret_flag;
10032}
10033
10034/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010035 * "insert()" function
10036 */
10037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *argvars;
10040 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010041{
10042 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010043 listitem_T *item;
10044 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010045
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010046 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010047 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010048 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010049 else if ((l = argvars[0].vval.v_list) != NULL
10050 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010051 {
10052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010054
Bram Moolenaar758711c2005-02-02 23:11:38 +000010055 if (before == l->lv_len)
10056 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010057 else
10058 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010059 item = list_find(l, before);
10060 if (item == NULL)
10061 {
10062 EMSGN(_(e_listidx), before);
10063 l = NULL;
10064 }
10065 }
10066 if (l != NULL)
10067 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010068 list_insert_tv(l, &argvars[1], item);
10069 ++l->lv_refcount;
10070 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010071 }
10072 }
10073}
10074
10075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 * "isdirectory()" function
10077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010086/*
10087 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10088 * it refers to a List or Dictionary that is locked.
10089 */
10090 static int
10091tv_islocked(tv)
10092 typval_T *tv;
10093{
10094 return (tv->v_lock & VAR_LOCKED)
10095 || (tv->v_type == VAR_LIST
10096 && tv->vval.v_list != NULL
10097 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10098 || (tv->v_type == VAR_DICT
10099 && tv->vval.v_dict != NULL
10100 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10101}
10102
10103/*
10104 * "islocked()" function
10105 */
10106 static void
10107f_islocked(argvars, rettv)
10108 typval_T *argvars;
10109 typval_T *rettv;
10110{
10111 lval_T lv;
10112 char_u *end;
10113 dictitem_T *di;
10114
10115 rettv->vval.v_number = -1;
10116 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
10117 if (end != NULL && lv.ll_name != NULL)
10118 {
10119 if (*end != NUL)
10120 EMSG(_(e_trailing));
10121 else
10122 {
10123 if (lv.ll_tv == NULL)
10124 {
10125 if (check_changedtick(lv.ll_name))
10126 rettv->vval.v_number = 1; /* always locked */
10127 else
10128 {
10129 di = find_var(lv.ll_name, NULL);
10130 if (di != NULL)
10131 {
10132 /* Consider a variable locked when:
10133 * 1. the variable itself is locked
10134 * 2. the value of the variable is locked.
10135 * 3. the List or Dict value is locked.
10136 */
10137 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10138 || tv_islocked(&di->di_tv));
10139 }
10140 }
10141 }
10142 else if (lv.ll_range)
10143 EMSG(_("E745: Range not allowed"));
10144 else if (lv.ll_newkey != NULL)
10145 EMSG2(_(e_dictkey), lv.ll_newkey);
10146 else if (lv.ll_list != NULL)
10147 /* List item. */
10148 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10149 else
10150 /* Dictionary item. */
10151 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10152 }
10153 }
10154
10155 clear_lval(&lv);
10156}
10157
Bram Moolenaar33570922005-01-25 22:26:29 +000010158static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010159
10160/*
10161 * Turn a dict into a list:
10162 * "what" == 0: list of keys
10163 * "what" == 1: list of values
10164 * "what" == 2: list of items
10165 */
10166 static void
10167dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010168 typval_T *argvars;
10169 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010170 int what;
10171{
Bram Moolenaar33570922005-01-25 22:26:29 +000010172 list_T *l;
10173 list_T *l2;
10174 dictitem_T *di;
10175 hashitem_T *hi;
10176 listitem_T *li;
10177 listitem_T *li2;
10178 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010179 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010180
10181 rettv->vval.v_number = 0;
10182 if (argvars[0].v_type != VAR_DICT)
10183 {
10184 EMSG(_(e_dictreq));
10185 return;
10186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010187 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010188 return;
10189
10190 l = list_alloc();
10191 if (l == NULL)
10192 return;
10193 rettv->v_type = VAR_LIST;
10194 rettv->vval.v_list = l;
10195 ++l->lv_refcount;
10196
Bram Moolenaar33570922005-01-25 22:26:29 +000010197 todo = d->dv_hashtab.ht_used;
10198 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010202 --todo;
10203 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010204
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010205 li = listitem_alloc();
10206 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010207 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010208 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010209
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010210 if (what == 0)
10211 {
10212 /* keys() */
10213 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010214 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010215 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10216 }
10217 else if (what == 1)
10218 {
10219 /* values() */
10220 copy_tv(&di->di_tv, &li->li_tv);
10221 }
10222 else
10223 {
10224 /* items() */
10225 l2 = list_alloc();
10226 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010227 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010228 li->li_tv.vval.v_list = l2;
10229 if (l2 == NULL)
10230 break;
10231 ++l2->lv_refcount;
10232
10233 li2 = listitem_alloc();
10234 if (li2 == NULL)
10235 break;
10236 list_append(l2, li2);
10237 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010238 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010239 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10240
10241 li2 = listitem_alloc();
10242 if (li2 == NULL)
10243 break;
10244 list_append(l2, li2);
10245 copy_tv(&di->di_tv, &li2->li_tv);
10246 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010247 }
10248 }
10249}
10250
10251/*
10252 * "items(dict)" function
10253 */
10254 static void
10255f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 typval_T *argvars;
10257 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010258{
10259 dict_list(argvars, rettv, 2);
10260}
10261
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010263 * "join()" function
10264 */
10265 static void
10266f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 typval_T *argvars;
10268 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269{
10270 garray_T ga;
10271 char_u *sep;
10272
10273 rettv->vval.v_number = 0;
10274 if (argvars[0].v_type != VAR_LIST)
10275 {
10276 EMSG(_(e_listreq));
10277 return;
10278 }
10279 if (argvars[0].vval.v_list == NULL)
10280 return;
10281 if (argvars[1].v_type == VAR_UNKNOWN)
10282 sep = (char_u *)" ";
10283 else
10284 sep = get_tv_string(&argvars[1]);
10285
10286 ga_init2(&ga, (int)sizeof(char), 80);
10287 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10288 ga_append(&ga, NUL);
10289
10290 rettv->v_type = VAR_STRING;
10291 rettv->vval.v_string = (char_u *)ga.ga_data;
10292}
10293
10294/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010295 * "keys()" function
10296 */
10297 static void
10298f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 typval_T *argvars;
10300 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010301{
10302 dict_list(argvars, rettv, 0);
10303}
10304
10305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 * "last_buffer_nr()" function.
10307 */
10308/*ARGSUSED*/
10309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010311 typval_T *argvars;
10312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313{
10314 int n = 0;
10315 buf_T *buf;
10316
10317 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10318 if (n < buf->b_fnum)
10319 n = buf->b_fnum;
10320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010322}
10323
10324/*
10325 * "len()" function
10326 */
10327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010329 typval_T *argvars;
10330 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010331{
10332 switch (argvars[0].v_type)
10333 {
10334 case VAR_STRING:
10335 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336 rettv->vval.v_number = (varnumber_T)STRLEN(
10337 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010338 break;
10339 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010340 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010341 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 case VAR_DICT:
10343 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10344 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010345 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010346 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010347 break;
10348 }
10349}
10350
Bram Moolenaar33570922005-01-25 22:26:29 +000010351static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010352
10353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010354libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010355 typval_T *argvars;
10356 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010357 int type;
10358{
10359#ifdef FEAT_LIBCALL
10360 char_u *string_in;
10361 char_u **string_result;
10362 int nr_result;
10363#endif
10364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010366 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010367 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010368 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010369 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010370
10371 if (check_restricted() || check_secure())
10372 return;
10373
10374#ifdef FEAT_LIBCALL
10375 /* The first two args must be strings, otherwise its meaningless */
10376 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010378 string_in = NULL;
10379 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010380 string_in = argvars[2].vval.v_string;
10381 if (type == VAR_NUMBER)
10382 string_result = NULL;
10383 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010384 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010385 if (mch_libcall(argvars[0].vval.v_string,
10386 argvars[1].vval.v_string,
10387 string_in,
10388 argvars[2].vval.v_number,
10389 string_result,
10390 &nr_result) == OK
10391 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010393 }
10394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395}
10396
10397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010398 * "libcall()" function
10399 */
10400 static void
10401f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010402 typval_T *argvars;
10403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010404{
10405 libcall_common(argvars, rettv, VAR_STRING);
10406}
10407
10408/*
10409 * "libcallnr()" function
10410 */
10411 static void
10412f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010413 typval_T *argvars;
10414 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010415{
10416 libcall_common(argvars, rettv, VAR_NUMBER);
10417}
10418
10419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 * "line(string)" function
10421 */
10422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010424 typval_T *argvars;
10425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426{
10427 linenr_T lnum = 0;
10428 pos_T *fp;
10429
10430 fp = var2fpos(&argvars[0], TRUE);
10431 if (fp != NULL)
10432 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434}
10435
10436/*
10437 * "line2byte(lnum)" function
10438 */
10439/*ARGSUSED*/
10440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 typval_T *argvars;
10443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
10445#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447#else
10448 linenr_T lnum;
10449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10455 if (rettv->vval.v_number >= 0)
10456 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457#endif
10458}
10459
10460/*
10461 * "lispindent(lnum)" function
10462 */
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *argvars;
10466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468#ifdef FEAT_LISP
10469 pos_T pos;
10470 linenr_T lnum;
10471
10472 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10475 {
10476 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 curwin->w_cursor = pos;
10479 }
10480 else
10481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483}
10484
10485/*
10486 * "localtime()" function
10487 */
10488/*ARGSUSED*/
10489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 typval_T *argvars;
10492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495}
10496
Bram Moolenaar33570922005-01-25 22:26:29 +000010497static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498
10499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 int exact;
10504{
10505 char_u *keys;
10506 char_u *which;
10507 char_u buf[NUMBUFLEN];
10508 char_u *keys_buf = NULL;
10509 char_u *rhs;
10510 int mode;
10511 garray_T ga;
10512
10513 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 rettv->v_type = VAR_STRING;
10515 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 if (*keys == NUL)
10519 return;
10520
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010521 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
10524 which = (char_u *)"";
10525 mode = get_map_mode(&which, 0);
10526
10527 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10528 rhs = check_map(keys, mode, exact);
10529 vim_free(keys_buf);
10530 if (rhs != NULL)
10531 {
10532 ga_init(&ga);
10533 ga.ga_itemsize = 1;
10534 ga.ga_growsize = 40;
10535
10536 while (*rhs != NUL)
10537 ga_concat(&ga, str2special(&rhs, FALSE));
10538
10539 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 }
10542}
10543
10544/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010545 * "map()" function
10546 */
10547 static void
10548f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 typval_T *argvars;
10550 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010551{
10552 filter_map(argvars, rettv, TRUE);
10553}
10554
10555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010556 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 */
10558 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010559f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 typval_T *argvars;
10561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010563 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564}
10565
10566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 */
10569 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010570f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 typval_T *argvars;
10572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010574 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575}
10576
Bram Moolenaar33570922005-01-25 22:26:29 +000010577static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
10579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010581 typval_T *argvars;
10582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 int type;
10584{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010585 char_u *str = NULL;
10586 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 char_u *pat;
10588 regmatch_T regmatch;
10589 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010590 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 char_u *save_cpo;
10592 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010593 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010594 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010595 list_T *l = NULL;
10596 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010597 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010598 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599
10600 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10601 save_cpo = p_cpo;
10602 p_cpo = (char_u *)"";
10603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010604 rettv->vval.v_number = -1;
10605 if (type == 3)
10606 {
10607 /* return empty list when there are no matches */
10608 if ((rettv->vval.v_list = list_alloc()) == NULL)
10609 goto theend;
10610 rettv->v_type = VAR_LIST;
10611 ++rettv->vval.v_list->lv_refcount;
10612 }
10613 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->v_type = VAR_STRING;
10616 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619 if (argvars[0].v_type == VAR_LIST)
10620 {
10621 if ((l = argvars[0].vval.v_list) == NULL)
10622 goto theend;
10623 li = l->lv_first;
10624 }
10625 else
10626 expr = str = get_tv_string(&argvars[0]);
10627
10628 pat = get_tv_string_buf(&argvars[1], patbuf);
10629
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010630 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010633 if (l != NULL)
10634 {
10635 li = list_find(l, start);
10636 if (li == NULL)
10637 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010638 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010639 }
10640 else
10641 {
10642 if (start < 0)
10643 start = 0;
10644 if (start > (long)STRLEN(str))
10645 goto theend;
10646 str += start;
10647 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010648
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010649 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 }
10652
10653 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10654 if (regmatch.regprog != NULL)
10655 {
10656 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010657
10658 while (1)
10659 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010660 if (l != NULL)
10661 {
10662 if (li == NULL)
10663 {
10664 match = FALSE;
10665 break;
10666 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010667 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010668 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010669 if (str == NULL)
10670 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010671 }
10672
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010673 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010674
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010675 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010676 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010677 if (l == NULL && !match)
10678 break;
10679
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010680 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010681 if (l != NULL)
10682 {
10683 li = li->li_next;
10684 ++idx;
10685 }
10686 else
10687 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010688#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010689 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010690#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010691 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010692#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010694 }
10695
10696 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010698 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010699 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010700 int i;
10701
10702 /* return list with matched string and submatches */
10703 for (i = 0; i < NSUBEXP; ++i)
10704 {
10705 if (regmatch.endp[i] == NULL)
10706 break;
10707 li = listitem_alloc();
10708 if (li == NULL)
10709 break;
10710 li->li_tv.v_type = VAR_STRING;
10711 li->li_tv.v_lock = 0;
10712 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10713 (int)(regmatch.endp[i] - regmatch.startp[i]));
10714 list_append(rettv->vval.v_list, li);
10715 }
10716 }
10717 else if (type == 2)
10718 {
10719 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010720 if (l != NULL)
10721 copy_tv(&li->li_tv, rettv);
10722 else
10723 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010725 }
10726 else if (l != NULL)
10727 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 else
10729 {
10730 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 (varnumber_T)(regmatch.startp[0] - str);
10733 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 }
10738 }
10739 vim_free(regmatch.regprog);
10740 }
10741
10742theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010743 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 p_cpo = save_cpo;
10745}
10746
10747/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010748 * "match()" function
10749 */
10750 static void
10751f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010752 typval_T *argvars;
10753 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754{
10755 find_some_match(argvars, rettv, 1);
10756}
10757
10758/*
10759 * "matchend()" function
10760 */
10761 static void
10762f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010763 typval_T *argvars;
10764 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010765{
10766 find_some_match(argvars, rettv, 0);
10767}
10768
10769/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010770 * "matchlist()" function
10771 */
10772 static void
10773f_matchlist(argvars, rettv)
10774 typval_T *argvars;
10775 typval_T *rettv;
10776{
10777 find_some_match(argvars, rettv, 3);
10778}
10779
10780/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781 * "matchstr()" function
10782 */
10783 static void
10784f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 typval_T *argvars;
10786 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010787{
10788 find_some_match(argvars, rettv, 2);
10789}
10790
Bram Moolenaar33570922005-01-25 22:26:29 +000010791static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010792
10793 static void
10794max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010795 typval_T *argvars;
10796 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010797 int domax;
10798{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010799 long n = 0;
10800 long i;
10801
10802 if (argvars[0].v_type == VAR_LIST)
10803 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010804 list_T *l;
10805 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010806
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010807 l = argvars[0].vval.v_list;
10808 if (l != NULL)
10809 {
10810 li = l->lv_first;
10811 if (li != NULL)
10812 {
10813 n = get_tv_number(&li->li_tv);
10814 while (1)
10815 {
10816 li = li->li_next;
10817 if (li == NULL)
10818 break;
10819 i = get_tv_number(&li->li_tv);
10820 if (domax ? i > n : i < n)
10821 n = i;
10822 }
10823 }
10824 }
10825 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 else if (argvars[0].v_type == VAR_DICT)
10827 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010829 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010831 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832
10833 d = argvars[0].vval.v_dict;
10834 if (d != NULL)
10835 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 todo = d->dv_hashtab.ht_used;
10837 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010839 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010841 --todo;
10842 i = get_tv_number(&HI2DI(hi)->di_tv);
10843 if (first)
10844 {
10845 n = i;
10846 first = FALSE;
10847 }
10848 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 n = i;
10850 }
10851 }
10852 }
10853 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010854 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010855 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010856 rettv->vval.v_number = n;
10857}
10858
10859/*
10860 * "max()" function
10861 */
10862 static void
10863f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010864 typval_T *argvars;
10865 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010866{
10867 max_min(argvars, rettv, TRUE);
10868}
10869
10870/*
10871 * "min()" function
10872 */
10873 static void
10874f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010875 typval_T *argvars;
10876 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010877{
10878 max_min(argvars, rettv, FALSE);
10879}
10880
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010881static int mkdir_recurse __ARGS((char_u *dir, int prot));
10882
10883/*
10884 * Create the directory in which "dir" is located, and higher levels when
10885 * needed.
10886 */
10887 static int
10888mkdir_recurse(dir, prot)
10889 char_u *dir;
10890 int prot;
10891{
10892 char_u *p;
10893 char_u *updir;
10894 int r = FAIL;
10895
10896 /* Get end of directory name in "dir".
10897 * We're done when it's "/" or "c:/". */
10898 p = gettail_sep(dir);
10899 if (p <= get_past_head(dir))
10900 return OK;
10901
10902 /* If the directory exists we're done. Otherwise: create it.*/
10903 updir = vim_strnsave(dir, (int)(p - dir));
10904 if (updir == NULL)
10905 return FAIL;
10906 if (mch_isdir(updir))
10907 r = OK;
10908 else if (mkdir_recurse(updir, prot) == OK)
10909 r = vim_mkdir_emsg(updir, prot);
10910 vim_free(updir);
10911 return r;
10912}
10913
10914#ifdef vim_mkdir
10915/*
10916 * "mkdir()" function
10917 */
10918 static void
10919f_mkdir(argvars, rettv)
10920 typval_T *argvars;
10921 typval_T *rettv;
10922{
10923 char_u *dir;
10924 char_u buf[NUMBUFLEN];
10925 int prot = 0755;
10926
10927 rettv->vval.v_number = FAIL;
10928 if (check_restricted() || check_secure())
10929 return;
10930
10931 dir = get_tv_string_buf(&argvars[0], buf);
10932 if (argvars[1].v_type != VAR_UNKNOWN)
10933 {
10934 if (argvars[2].v_type != VAR_UNKNOWN)
10935 prot = get_tv_number(&argvars[2]);
10936 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
10937 mkdir_recurse(dir, prot);
10938 }
10939 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
10940}
10941#endif
10942
Bram Moolenaar0d660222005-01-07 21:51:51 +000010943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 * "mode()" function
10945 */
10946/*ARGSUSED*/
10947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010949 typval_T *argvars;
10950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951{
10952 char_u buf[2];
10953
10954#ifdef FEAT_VISUAL
10955 if (VIsual_active)
10956 {
10957 if (VIsual_select)
10958 buf[0] = VIsual_mode + 's' - 'v';
10959 else
10960 buf[0] = VIsual_mode;
10961 }
10962 else
10963#endif
10964 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10965 buf[0] = 'r';
10966 else if (State & INSERT)
10967 {
10968 if (State & REPLACE_FLAG)
10969 buf[0] = 'R';
10970 else
10971 buf[0] = 'i';
10972 }
10973 else if (State & CMDLINE)
10974 buf[0] = 'c';
10975 else
10976 buf[0] = 'n';
10977
10978 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_string = vim_strsave(buf);
10980 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981}
10982
10983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010984 * "nextnonblank()" function
10985 */
10986 static void
10987f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990{
10991 linenr_T lnum;
10992
10993 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10994 {
10995 if (lnum > curbuf->b_ml.ml_line_count)
10996 {
10997 lnum = 0;
10998 break;
10999 }
11000 if (*skipwhite(ml_get(lnum)) != NUL)
11001 break;
11002 }
11003 rettv->vval.v_number = lnum;
11004}
11005
11006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 * "nr2char()" function
11008 */
11009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011011 typval_T *argvars;
11012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014 char_u buf[NUMBUFLEN];
11015
11016#ifdef FEAT_MBYTE
11017 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 else
11020#endif
11021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 buf[1] = NUL;
11024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->v_type = VAR_STRING;
11026 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011027}
11028
11029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011030 * "prevnonblank()" function
11031 */
11032 static void
11033f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036{
11037 linenr_T lnum;
11038
11039 lnum = get_tv_lnum(argvars);
11040 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11041 lnum = 0;
11042 else
11043 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11044 --lnum;
11045 rettv->vval.v_number = lnum;
11046}
11047
Bram Moolenaar8c711452005-01-14 21:53:12 +000011048/*
11049 * "range()" function
11050 */
11051 static void
11052f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011055{
11056 long start;
11057 long end;
11058 long stride = 1;
11059 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011060 list_T *l;
11061 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011062
11063 start = get_tv_number(&argvars[0]);
11064 if (argvars[1].v_type == VAR_UNKNOWN)
11065 {
11066 end = start - 1;
11067 start = 0;
11068 }
11069 else
11070 {
11071 end = get_tv_number(&argvars[1]);
11072 if (argvars[2].v_type != VAR_UNKNOWN)
11073 stride = get_tv_number(&argvars[2]);
11074 }
11075
11076 rettv->vval.v_number = 0;
11077 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011078 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011079 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011080 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011081 else
11082 {
11083 l = list_alloc();
11084 if (l != NULL)
11085 {
11086 rettv->v_type = VAR_LIST;
11087 rettv->vval.v_list = l;
11088 ++l->lv_refcount;
11089
11090 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11091 {
11092 li = listitem_alloc();
11093 if (li == NULL)
11094 break;
11095 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011096 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011097 li->li_tv.vval.v_number = i;
11098 list_append(l, li);
11099 }
11100 }
11101 }
11102}
11103
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011104/*
11105 * "readfile()" function
11106 */
11107 static void
11108f_readfile(argvars, rettv)
11109 typval_T *argvars;
11110 typval_T *rettv;
11111{
11112 int binary = FALSE;
11113 char_u *fname;
11114 FILE *fd;
11115 list_T *l;
11116 listitem_T *li;
11117#define FREAD_SIZE 200 /* optimized for text lines */
11118 char_u buf[FREAD_SIZE];
11119 int readlen; /* size of last fread() */
11120 int buflen; /* nr of valid chars in buf[] */
11121 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11122 int tolist; /* first byte in buf[] still to be put in list */
11123 int chop; /* how many CR to chop off */
11124 char_u *prev = NULL; /* previously read bytes, if any */
11125 int prevlen = 0; /* length of "prev" if not NULL */
11126 char_u *s;
11127 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011128 long maxline = MAXLNUM;
11129 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011131 if (argvars[1].v_type != VAR_UNKNOWN)
11132 {
11133 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11134 binary = TRUE;
11135 if (argvars[2].v_type != VAR_UNKNOWN)
11136 maxline = get_tv_number(&argvars[2]);
11137 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011138
11139 l = list_alloc();
11140 if (l == NULL)
11141 return;
11142 rettv->v_type = VAR_LIST;
11143 rettv->vval.v_list = l;
11144 l->lv_refcount = 1;
11145
11146 /* Always open the file in binary mode, library functions have a mind of
11147 * their own about CR-LF conversion. */
11148 fname = get_tv_string(&argvars[0]);
11149 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11150 {
11151 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11152 return;
11153 }
11154
11155 filtd = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011156 while (cnt < maxline)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011157 {
11158 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11159 buflen = filtd + readlen;
11160 tolist = 0;
11161 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11162 {
11163 if (buf[filtd] == '\n' || readlen <= 0)
11164 {
11165 /* Only when in binary mode add an empty list item when the
11166 * last line ends in a '\n'. */
11167 if (!binary && readlen == 0 && filtd == 0)
11168 break;
11169
11170 /* Found end-of-line or end-of-file: add a text line to the
11171 * list. */
11172 chop = 0;
11173 if (!binary)
11174 while (filtd - chop - 1 >= tolist
11175 && buf[filtd - chop - 1] == '\r')
11176 ++chop;
11177 len = filtd - tolist - chop;
11178 if (prev == NULL)
11179 s = vim_strnsave(buf + tolist, len);
11180 else
11181 {
11182 s = alloc((unsigned)(prevlen + len + 1));
11183 if (s != NULL)
11184 {
11185 mch_memmove(s, prev, prevlen);
11186 vim_free(prev);
11187 prev = NULL;
11188 mch_memmove(s + prevlen, buf + tolist, len);
11189 s[prevlen + len] = NUL;
11190 }
11191 }
11192 tolist = filtd + 1;
11193
11194 li = listitem_alloc();
11195 if (li == NULL)
11196 {
11197 vim_free(s);
11198 break;
11199 }
11200 li->li_tv.v_type = VAR_STRING;
11201 li->li_tv.v_lock = 0;
11202 li->li_tv.vval.v_string = s;
11203 list_append(l, li);
11204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011205 if (++cnt >= maxline)
11206 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011207 if (readlen <= 0)
11208 break;
11209 }
11210 else if (buf[filtd] == NUL)
11211 buf[filtd] = '\n';
11212 }
11213 if (readlen <= 0)
11214 break;
11215
11216 if (tolist == 0)
11217 {
11218 /* "buf" is full, need to move text to an allocated buffer */
11219 if (prev == NULL)
11220 {
11221 prev = vim_strnsave(buf, buflen);
11222 prevlen = buflen;
11223 }
11224 else
11225 {
11226 s = alloc((unsigned)(prevlen + buflen));
11227 if (s != NULL)
11228 {
11229 mch_memmove(s, prev, prevlen);
11230 mch_memmove(s + prevlen, buf, buflen);
11231 vim_free(prev);
11232 prev = s;
11233 prevlen += buflen;
11234 }
11235 }
11236 filtd = 0;
11237 }
11238 else
11239 {
11240 mch_memmove(buf, buf + tolist, buflen - tolist);
11241 filtd -= tolist;
11242 }
11243 }
11244
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011245 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011246 fclose(fd);
11247}
11248
11249
Bram Moolenaar0d660222005-01-07 21:51:51 +000011250#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11251static void make_connection __ARGS((void));
11252static int check_connection __ARGS((void));
11253
11254 static void
11255make_connection()
11256{
11257 if (X_DISPLAY == NULL
11258# ifdef FEAT_GUI
11259 && !gui.in_use
11260# endif
11261 )
11262 {
11263 x_force_connect = TRUE;
11264 setup_term_clip();
11265 x_force_connect = FALSE;
11266 }
11267}
11268
11269 static int
11270check_connection()
11271{
11272 make_connection();
11273 if (X_DISPLAY == NULL)
11274 {
11275 EMSG(_("E240: No connection to Vim server"));
11276 return FAIL;
11277 }
11278 return OK;
11279}
11280#endif
11281
11282#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011283static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284
11285 static void
11286remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *argvars;
11288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011289 int expr;
11290{
11291 char_u *server_name;
11292 char_u *keys;
11293 char_u *r = NULL;
11294 char_u buf[NUMBUFLEN];
11295# ifdef WIN32
11296 HWND w;
11297# else
11298 Window w;
11299# endif
11300
11301 if (check_restricted() || check_secure())
11302 return;
11303
11304# ifdef FEAT_X11
11305 if (check_connection() == FAIL)
11306 return;
11307# endif
11308
11309 server_name = get_tv_string(&argvars[0]);
11310 keys = get_tv_string_buf(&argvars[1], buf);
11311# ifdef WIN32
11312 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11313# else
11314 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11315 < 0)
11316# endif
11317 {
11318 if (r != NULL)
11319 EMSG(r); /* sending worked but evaluation failed */
11320 else
11321 EMSG2(_("E241: Unable to send to %s"), server_name);
11322 return;
11323 }
11324
11325 rettv->vval.v_string = r;
11326
11327 if (argvars[2].v_type != VAR_UNKNOWN)
11328 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011330 char_u str[30];
11331
11332 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 v.di_tv.v_type = VAR_STRING;
11334 v.di_tv.vval.v_string = vim_strsave(str);
11335 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11336 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337 }
11338}
11339#endif
11340
11341/*
11342 * "remote_expr()" function
11343 */
11344/*ARGSUSED*/
11345 static void
11346f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349{
11350 rettv->v_type = VAR_STRING;
11351 rettv->vval.v_string = NULL;
11352#ifdef FEAT_CLIENTSERVER
11353 remote_common(argvars, rettv, TRUE);
11354#endif
11355}
11356
11357/*
11358 * "remote_foreground()" function
11359 */
11360/*ARGSUSED*/
11361 static void
11362f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011365{
11366 rettv->vval.v_number = 0;
11367#ifdef FEAT_CLIENTSERVER
11368# ifdef WIN32
11369 /* On Win32 it's done in this application. */
11370 serverForeground(get_tv_string(&argvars[0]));
11371# else
11372 /* Send a foreground() expression to the server. */
11373 argvars[1].v_type = VAR_STRING;
11374 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11375 argvars[2].v_type = VAR_UNKNOWN;
11376 remote_common(argvars, rettv, TRUE);
11377 vim_free(argvars[1].vval.v_string);
11378# endif
11379#endif
11380}
11381
11382/*ARGSUSED*/
11383 static void
11384f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011385 typval_T *argvars;
11386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011387{
11388#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011390 char_u *s = NULL;
11391# ifdef WIN32
11392 int n = 0;
11393# endif
11394
11395 if (check_restricted() || check_secure())
11396 {
11397 rettv->vval.v_number = -1;
11398 return;
11399 }
11400# ifdef WIN32
11401 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11402 if (n == 0)
11403 rettv->vval.v_number = -1;
11404 else
11405 {
11406 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11407 rettv->vval.v_number = (s != NULL);
11408 }
11409# else
11410 rettv->vval.v_number = 0;
11411 if (check_connection() == FAIL)
11412 return;
11413
11414 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11415 serverStrToWin(get_tv_string(&argvars[0])), &s);
11416# endif
11417
11418 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 v.di_tv.v_type = VAR_STRING;
11421 v.di_tv.vval.v_string = vim_strsave(s);
11422 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11423 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424 }
11425#else
11426 rettv->vval.v_number = -1;
11427#endif
11428}
11429
11430/*ARGSUSED*/
11431 static void
11432f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 typval_T *argvars;
11434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011435{
11436 char_u *r = NULL;
11437
11438#ifdef FEAT_CLIENTSERVER
11439 if (!check_restricted() && !check_secure())
11440 {
11441# ifdef WIN32
11442 /* The server's HWND is encoded in the 'id' parameter */
11443 int n = 0;
11444
11445 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11446 if (n != 0)
11447 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11448 if (r == NULL)
11449# else
11450 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11451 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11452# endif
11453 EMSG(_("E277: Unable to read a server reply"));
11454 }
11455#endif
11456 rettv->v_type = VAR_STRING;
11457 rettv->vval.v_string = r;
11458}
11459
11460/*
11461 * "remote_send()" function
11462 */
11463/*ARGSUSED*/
11464 static void
11465f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 typval_T *argvars;
11467 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011468{
11469 rettv->v_type = VAR_STRING;
11470 rettv->vval.v_string = NULL;
11471#ifdef FEAT_CLIENTSERVER
11472 remote_common(argvars, rettv, FALSE);
11473#endif
11474}
11475
11476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011477 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 */
11479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 typval_T *argvars;
11482 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011483{
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 list_T *l;
11485 listitem_T *item, *item2;
11486 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011487 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011488 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011489 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011490 dict_T *d;
11491 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011492
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011493 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011494 if (argvars[0].v_type == VAR_DICT)
11495 {
11496 if (argvars[2].v_type != VAR_UNKNOWN)
11497 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011498 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011499 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011500 {
11501 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011502 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011503 if (di == NULL)
11504 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011505 else
11506 {
11507 *rettv = di->di_tv;
11508 init_tv(&di->di_tv);
11509 dictitem_remove(d, di);
11510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011511 }
11512 }
11513 else if (argvars[0].v_type != VAR_LIST)
11514 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011515 else if ((l = argvars[0].vval.v_list) != NULL
11516 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011519 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011520 if (item == NULL)
11521 EMSGN(_(e_listidx), idx);
11522 else
11523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011524 if (argvars[2].v_type == VAR_UNKNOWN)
11525 {
11526 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011527 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011528 *rettv = item->li_tv;
11529 vim_free(item);
11530 }
11531 else
11532 {
11533 /* Remove range of items, return list with values. */
11534 end = get_tv_number(&argvars[2]);
11535 item2 = list_find(l, end);
11536 if (item2 == NULL)
11537 EMSGN(_(e_listidx), end);
11538 else
11539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011540 int cnt = 0;
11541
11542 for (li = item; li != NULL; li = li->li_next)
11543 {
11544 ++cnt;
11545 if (li == item2)
11546 break;
11547 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011548 if (li == NULL) /* didn't find "item2" after "item" */
11549 EMSG(_(e_invrange));
11550 else
11551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011552 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011553 l = list_alloc();
11554 if (l != NULL)
11555 {
11556 rettv->v_type = VAR_LIST;
11557 rettv->vval.v_list = l;
11558 l->lv_first = item;
11559 l->lv_last = item2;
11560 l->lv_refcount = 1;
11561 item->li_prev = NULL;
11562 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011563 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 }
11565 }
11566 }
11567 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011568 }
11569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570}
11571
11572/*
11573 * "rename({from}, {to})" function
11574 */
11575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 typval_T *argvars;
11578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579{
11580 char_u buf[NUMBUFLEN];
11581
11582 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11586 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587}
11588
11589/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011590 * "repeat()" function
11591 */
11592/*ARGSUSED*/
11593 static void
11594f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 typval_T *argvars;
11596 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011597{
11598 char_u *p;
11599 int n;
11600 int slen;
11601 int len;
11602 char_u *r;
11603 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011604 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011605
11606 n = get_tv_number(&argvars[1]);
11607 if (argvars[0].v_type == VAR_LIST)
11608 {
11609 l = list_alloc();
11610 if (l != NULL && argvars[0].vval.v_list != NULL)
11611 {
11612 l->lv_refcount = 1;
11613 while (n-- > 0)
11614 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11615 break;
11616 }
11617 rettv->v_type = VAR_LIST;
11618 rettv->vval.v_list = l;
11619 }
11620 else
11621 {
11622 p = get_tv_string(&argvars[0]);
11623 rettv->v_type = VAR_STRING;
11624 rettv->vval.v_string = NULL;
11625
11626 slen = (int)STRLEN(p);
11627 len = slen * n;
11628 if (len <= 0)
11629 return;
11630
11631 r = alloc(len + 1);
11632 if (r != NULL)
11633 {
11634 for (i = 0; i < n; i++)
11635 mch_memmove(r + i * slen, p, (size_t)slen);
11636 r[len] = NUL;
11637 }
11638
11639 rettv->vval.v_string = r;
11640 }
11641}
11642
11643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 * "resolve()" function
11645 */
11646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650{
11651 char_u *p;
11652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654#ifdef FEAT_SHORTCUT
11655 {
11656 char_u *v = NULL;
11657
11658 v = mch_resolve_shortcut(p);
11659 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 }
11664#else
11665# ifdef HAVE_READLINK
11666 {
11667 char_u buf[MAXPATHL + 1];
11668 char_u *cpy;
11669 int len;
11670 char_u *remain = NULL;
11671 char_u *q;
11672 int is_relative_to_current = FALSE;
11673 int has_trailing_pathsep = FALSE;
11674 int limit = 100;
11675
11676 p = vim_strsave(p);
11677
11678 if (p[0] == '.' && (vim_ispathsep(p[1])
11679 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11680 is_relative_to_current = TRUE;
11681
11682 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011683 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 has_trailing_pathsep = TRUE;
11685
11686 q = getnextcomp(p);
11687 if (*q != NUL)
11688 {
11689 /* Separate the first path component in "p", and keep the
11690 * remainder (beginning with the path separator). */
11691 remain = vim_strsave(q - 1);
11692 q[-1] = NUL;
11693 }
11694
11695 for (;;)
11696 {
11697 for (;;)
11698 {
11699 len = readlink((char *)p, (char *)buf, MAXPATHL);
11700 if (len <= 0)
11701 break;
11702 buf[len] = NUL;
11703
11704 if (limit-- == 0)
11705 {
11706 vim_free(p);
11707 vim_free(remain);
11708 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 goto fail;
11711 }
11712
11713 /* Ensure that the result will have a trailing path separator
11714 * if the argument has one. */
11715 if (remain == NULL && has_trailing_pathsep)
11716 add_pathsep(buf);
11717
11718 /* Separate the first path component in the link value and
11719 * concatenate the remainders. */
11720 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11721 if (*q != NUL)
11722 {
11723 if (remain == NULL)
11724 remain = vim_strsave(q - 1);
11725 else
11726 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011727 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 if (cpy != NULL)
11729 {
11730 STRCAT(cpy, remain);
11731 vim_free(remain);
11732 remain = cpy;
11733 }
11734 }
11735 q[-1] = NUL;
11736 }
11737
11738 q = gettail(p);
11739 if (q > p && *q == NUL)
11740 {
11741 /* Ignore trailing path separator. */
11742 q[-1] = NUL;
11743 q = gettail(p);
11744 }
11745 if (q > p && !mch_isFullName(buf))
11746 {
11747 /* symlink is relative to directory of argument */
11748 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11749 if (cpy != NULL)
11750 {
11751 STRCPY(cpy, p);
11752 STRCPY(gettail(cpy), buf);
11753 vim_free(p);
11754 p = cpy;
11755 }
11756 }
11757 else
11758 {
11759 vim_free(p);
11760 p = vim_strsave(buf);
11761 }
11762 }
11763
11764 if (remain == NULL)
11765 break;
11766
11767 /* Append the first path component of "remain" to "p". */
11768 q = getnextcomp(remain + 1);
11769 len = q - remain - (*q != NUL);
11770 cpy = vim_strnsave(p, STRLEN(p) + len);
11771 if (cpy != NULL)
11772 {
11773 STRNCAT(cpy, remain, len);
11774 vim_free(p);
11775 p = cpy;
11776 }
11777 /* Shorten "remain". */
11778 if (*q != NUL)
11779 STRCPY(remain, q - 1);
11780 else
11781 {
11782 vim_free(remain);
11783 remain = NULL;
11784 }
11785 }
11786
11787 /* If the result is a relative path name, make it explicitly relative to
11788 * the current directory if and only if the argument had this form. */
11789 if (!vim_ispathsep(*p))
11790 {
11791 if (is_relative_to_current
11792 && *p != NUL
11793 && !(p[0] == '.'
11794 && (p[1] == NUL
11795 || vim_ispathsep(p[1])
11796 || (p[1] == '.'
11797 && (p[2] == NUL
11798 || vim_ispathsep(p[2]))))))
11799 {
11800 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011801 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 if (cpy != NULL)
11803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 vim_free(p);
11805 p = cpy;
11806 }
11807 }
11808 else if (!is_relative_to_current)
11809 {
11810 /* Strip leading "./". */
11811 q = p;
11812 while (q[0] == '.' && vim_ispathsep(q[1]))
11813 q += 2;
11814 if (q > p)
11815 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11816 }
11817 }
11818
11819 /* Ensure that the result will have no trailing path separator
11820 * if the argument had none. But keep "/" or "//". */
11821 if (!has_trailing_pathsep)
11822 {
11823 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011824 if (after_pathsep(p, q))
11825 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 }
11827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 }
11830# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832# endif
11833#endif
11834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836
11837#ifdef HAVE_READLINK
11838fail:
11839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841}
11842
11843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 */
11846 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011847f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 list_T *l;
11852 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853
Bram Moolenaar0d660222005-01-07 21:51:51 +000011854 rettv->vval.v_number = 0;
11855 if (argvars[0].v_type != VAR_LIST)
11856 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011857 else if ((l = argvars[0].vval.v_list) != NULL
11858 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859 {
11860 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011861 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011862 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863 while (li != NULL)
11864 {
11865 ni = li->li_prev;
11866 list_append(l, li);
11867 li = ni;
11868 }
11869 rettv->vval.v_list = l;
11870 rettv->v_type = VAR_LIST;
11871 ++l->lv_refcount;
11872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873}
11874
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011875#define SP_NOMOVE 1 /* don't move cursor */
11876#define SP_REPEAT 2 /* repeat to find outer pair */
11877#define SP_RETCOUNT 4 /* return matchcount */
11878
Bram Moolenaar33570922005-01-25 22:26:29 +000011879static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011880
11881/*
11882 * Get flags for a search function.
11883 * Possibly sets "p_ws".
11884 * Returns BACKWARD, FORWARD or zero (for an error).
11885 */
11886 static int
11887get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011888 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011889 int *flagsp;
11890{
11891 int dir = FORWARD;
11892 char_u *flags;
11893 char_u nbuf[NUMBUFLEN];
11894 int mask;
11895
11896 if (varp->v_type != VAR_UNKNOWN)
11897 {
11898 flags = get_tv_string_buf(varp, nbuf);
11899 while (*flags != NUL)
11900 {
11901 switch (*flags)
11902 {
11903 case 'b': dir = BACKWARD; break;
11904 case 'w': p_ws = TRUE; break;
11905 case 'W': p_ws = FALSE; break;
11906 default: mask = 0;
11907 if (flagsp != NULL)
11908 switch (*flags)
11909 {
11910 case 'n': mask = SP_NOMOVE; break;
11911 case 'r': mask = SP_REPEAT; break;
11912 case 'm': mask = SP_RETCOUNT; break;
11913 }
11914 if (mask == 0)
11915 {
11916 EMSG2(_(e_invarg2), flags);
11917 dir = 0;
11918 }
11919 else
11920 *flagsp |= mask;
11921 }
11922 if (dir == 0)
11923 break;
11924 ++flags;
11925 }
11926 }
11927 return dir;
11928}
11929
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930/*
11931 * "search()" function
11932 */
11933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *argvars;
11936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937{
11938 char_u *pat;
11939 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011940 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 int save_p_ws = p_ws;
11942 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011943 int flags = 0;
11944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011948 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11949 if (dir == 0)
11950 goto theend;
11951 if ((flags & ~SP_NOMOVE) != 0)
11952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011954 goto theend;
11955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011957 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11959 SEARCH_KEEP, RE_SEARCH) != FAIL)
11960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 curwin->w_cursor = pos;
11963 /* "/$" will put the cursor after the end of the line, may need to
11964 * correct that here */
11965 check_cursor();
11966 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011967
11968 /* If 'n' flag is used: restore cursor position. */
11969 if (flags & SP_NOMOVE)
11970 curwin->w_cursor = save_cursor;
11971theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 p_ws = save_p_ws;
11973}
11974
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975/*
11976 * "searchpair()" function
11977 */
11978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 typval_T *argvars;
11981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
11983 char_u *spat, *mpat, *epat;
11984 char_u *skip;
11985 char_u *pat, *pat2, *pat3;
11986 pos_T pos;
11987 pos_T firstpos;
11988 pos_T save_cursor;
11989 pos_T save_pos;
11990 int save_p_ws = p_ws;
11991 char_u *save_cpo;
11992 int dir;
11993 int flags = 0;
11994 char_u nbuf1[NUMBUFLEN];
11995 char_u nbuf2[NUMBUFLEN];
11996 char_u nbuf3[NUMBUFLEN];
11997 int n;
11998 int r;
11999 int nest = 1;
12000 int err;
12001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003
12004 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12005 save_cpo = p_cpo;
12006 p_cpo = (char_u *)"";
12007
12008 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 spat = get_tv_string(&argvars[0]);
12010 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12011 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012
12013 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12014 * start/middle/end (pat3, for the top pair). */
12015 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12016 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12017 if (pat2 == NULL || pat3 == NULL)
12018 goto theend;
12019 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12020 if (*mpat == NUL)
12021 STRCPY(pat3, pat2);
12022 else
12023 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12024 spat, epat, mpat);
12025
12026 /* Handle the optional fourth argument: flags */
12027 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012028 if (dir == 0)
12029 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030
12031 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012032 if (argvars[3].v_type == VAR_UNKNOWN
12033 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 skip = (char_u *)"";
12035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037
12038 save_cursor = curwin->w_cursor;
12039 pos = curwin->w_cursor;
12040 firstpos.lnum = 0;
12041 pat = pat3;
12042 for (;;)
12043 {
12044 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12045 SEARCH_KEEP, RE_SEARCH);
12046 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12047 /* didn't find it or found the first match again: FAIL */
12048 break;
12049
12050 if (firstpos.lnum == 0)
12051 firstpos = pos;
12052
12053 /* If the skip pattern matches, ignore this match. */
12054 if (*skip != NUL)
12055 {
12056 save_pos = curwin->w_cursor;
12057 curwin->w_cursor = pos;
12058 r = eval_to_bool(skip, &err, NULL, FALSE);
12059 curwin->w_cursor = save_pos;
12060 if (err)
12061 {
12062 /* Evaluating {skip} caused an error, break here. */
12063 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 break;
12066 }
12067 if (r)
12068 continue;
12069 }
12070
12071 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12072 {
12073 /* Found end when searching backwards or start when searching
12074 * forward: nested pair. */
12075 ++nest;
12076 pat = pat2; /* nested, don't search for middle */
12077 }
12078 else
12079 {
12080 /* Found end when searching forward or start when searching
12081 * backward: end of (nested) pair; or found middle in outer pair. */
12082 if (--nest == 1)
12083 pat = pat3; /* outer level, search for middle */
12084 }
12085
12086 if (nest == 0)
12087 {
12088 /* Found the match: return matchcount or line number. */
12089 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 curwin->w_cursor = pos;
12094 if (!(flags & SP_REPEAT))
12095 break;
12096 nest = 1; /* search for next unmatched */
12097 }
12098 }
12099
12100 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 curwin->w_cursor = save_cursor;
12103
12104theend:
12105 vim_free(pat2);
12106 vim_free(pat3);
12107 p_ws = save_p_ws;
12108 p_cpo = save_cpo;
12109}
12110
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111/*ARGSUSED*/
12112 static void
12113f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012114 typval_T *argvars;
12115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012117#ifdef FEAT_CLIENTSERVER
12118 char_u buf[NUMBUFLEN];
12119 char_u *server = get_tv_string(&argvars[0]);
12120 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122 rettv->vval.v_number = -1;
12123 if (check_restricted() || check_secure())
12124 return;
12125# ifdef FEAT_X11
12126 if (check_connection() == FAIL)
12127 return;
12128# endif
12129
12130 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132 EMSG(_("E258: Unable to send to client"));
12133 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135 rettv->vval.v_number = 0;
12136#else
12137 rettv->vval.v_number = -1;
12138#endif
12139}
12140
12141/*ARGSUSED*/
12142 static void
12143f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012144 typval_T *argvars;
12145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146{
12147 char_u *r = NULL;
12148
12149#ifdef FEAT_CLIENTSERVER
12150# ifdef WIN32
12151 r = serverGetVimNames();
12152# else
12153 make_connection();
12154 if (X_DISPLAY != NULL)
12155 r = serverGetVimNames(X_DISPLAY);
12156# endif
12157#endif
12158 rettv->v_type = VAR_STRING;
12159 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160}
12161
12162/*
12163 * "setbufvar()" function
12164 */
12165/*ARGSUSED*/
12166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012168 typval_T *argvars;
12169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170{
12171 buf_T *buf;
12172#ifdef FEAT_AUTOCMD
12173 aco_save_T aco;
12174#else
12175 buf_T *save_curbuf;
12176#endif
12177 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 char_u nbuf[NUMBUFLEN];
12180
12181 if (check_restricted() || check_secure())
12182 return;
12183 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 buf = get_buf_tv(&argvars[0]);
12185 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 varp = &argvars[2];
12187
12188 if (buf != NULL && varname != NULL && varp != NULL)
12189 {
12190 /* set curbuf to be our buf, temporarily */
12191#ifdef FEAT_AUTOCMD
12192 aucmd_prepbuf(&aco, buf);
12193#else
12194 save_curbuf = curbuf;
12195 curbuf = buf;
12196#endif
12197
12198 if (*varname == '&')
12199 {
12200 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 set_option_value(varname, get_tv_number(varp),
12202 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 }
12204 else
12205 {
12206 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12207 if (bufvarname != NULL)
12208 {
12209 STRCPY(bufvarname, "b:");
12210 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012211 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 vim_free(bufvarname);
12213 }
12214 }
12215
12216 /* reset notion of buffer */
12217#ifdef FEAT_AUTOCMD
12218 aucmd_restbuf(&aco);
12219#else
12220 curbuf = save_curbuf;
12221#endif
12222 }
12223 --emsg_off;
12224}
12225
12226/*
12227 * "setcmdpos()" function
12228 */
12229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012231 typval_T *argvars;
12232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234 rettv->vval.v_number = set_cmdline_pos(
12235 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236}
12237
12238/*
12239 * "setline()" function
12240 */
12241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 typval_T *argvars;
12244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
12246 linenr_T lnum;
12247 char_u *line;
12248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 lnum = get_tv_lnum(argvars);
12250 line = get_tv_string(&argvars[1]);
12251 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252
12253 if (lnum >= 1
12254 && lnum <= curbuf->b_ml.ml_line_count
12255 && u_savesub(lnum) == OK
12256 && ml_replace(lnum, line, TRUE) == OK)
12257 {
12258 changed_bytes(lnum, 0);
12259 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 }
12262}
12263
12264/*
12265 * "setreg()" function
12266 */
12267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012269 typval_T *argvars;
12270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271{
12272 int regname;
12273 char_u *strregname;
12274 char_u *stropt;
12275 int append;
12276 char_u yank_type;
12277 long block_len;
12278
12279 block_len = -1;
12280 yank_type = MAUTO;
12281 append = FALSE;
12282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 strregname = get_tv_string(argvars);
12284 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285
12286 regname = (strregname == NULL ? '"' : *strregname);
12287 if (regname == 0 || regname == '@')
12288 regname = '"';
12289 else if (regname == '=')
12290 return;
12291
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012292 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 switch (*stropt)
12296 {
12297 case 'a': case 'A': /* append */
12298 append = TRUE;
12299 break;
12300 case 'v': case 'c': /* character-wise selection */
12301 yank_type = MCHAR;
12302 break;
12303 case 'V': case 'l': /* line-wise selection */
12304 yank_type = MLINE;
12305 break;
12306#ifdef FEAT_VISUAL
12307 case 'b': case Ctrl_V: /* block-wise selection */
12308 yank_type = MBLOCK;
12309 if (VIM_ISDIGIT(stropt[1]))
12310 {
12311 ++stropt;
12312 block_len = getdigits(&stropt) - 1;
12313 --stropt;
12314 }
12315 break;
12316#endif
12317 }
12318 }
12319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323}
12324
12325
12326/*
12327 * "setwinvar(expr)" function
12328 */
12329/*ARGSUSED*/
12330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331f_setwinvar(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 win_T *win;
12336#ifdef FEAT_WINDOWS
12337 win_T *save_curwin;
12338#endif
12339 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012340 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 char_u nbuf[NUMBUFLEN];
12342
12343 if (check_restricted() || check_secure())
12344 return;
12345 ++emsg_off;
12346 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 varp = &argvars[2];
12349
12350 if (win != NULL && varname != NULL && varp != NULL)
12351 {
12352#ifdef FEAT_WINDOWS
12353 /* set curwin to be our win, temporarily */
12354 save_curwin = curwin;
12355 curwin = win;
12356 curbuf = curwin->w_buffer;
12357#endif
12358
12359 if (*varname == '&')
12360 {
12361 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 set_option_value(varname, get_tv_number(varp),
12363 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 }
12365 else
12366 {
12367 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12368 if (winvarname != NULL)
12369 {
12370 STRCPY(winvarname, "w:");
12371 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012372 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 vim_free(winvarname);
12374 }
12375 }
12376
12377#ifdef FEAT_WINDOWS
12378 /* Restore current window, if it's still valid (autocomands can make
12379 * it invalid). */
12380 if (win_valid(save_curwin))
12381 {
12382 curwin = save_curwin;
12383 curbuf = curwin->w_buffer;
12384 }
12385#endif
12386 }
12387 --emsg_off;
12388}
12389
12390/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012391 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 */
12393 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012395 typval_T *argvars;
12396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012398 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399
Bram Moolenaar0d660222005-01-07 21:51:51 +000012400 p = get_tv_string(&argvars[0]);
12401 rettv->vval.v_string = vim_strsave(p);
12402 simplify_filename(rettv->vval.v_string); /* simplify in place */
12403 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404}
12405
Bram Moolenaar0d660222005-01-07 21:51:51 +000012406static int
12407#ifdef __BORLANDC__
12408 _RTLENTRYF
12409#endif
12410 item_compare __ARGS((const void *s1, const void *s2));
12411static int
12412#ifdef __BORLANDC__
12413 _RTLENTRYF
12414#endif
12415 item_compare2 __ARGS((const void *s1, const void *s2));
12416
12417static int item_compare_ic;
12418static char_u *item_compare_func;
12419#define ITEM_COMPARE_FAIL 999
12420
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012424 static int
12425#ifdef __BORLANDC__
12426_RTLENTRYF
12427#endif
12428item_compare(s1, s2)
12429 const void *s1;
12430 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012432 char_u *p1, *p2;
12433 char_u *tofree1, *tofree2;
12434 int res;
12435 char_u numbuf1[NUMBUFLEN];
12436 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437
Bram Moolenaar33570922005-01-25 22:26:29 +000012438 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12439 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012440 if (item_compare_ic)
12441 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443 res = STRCMP(p1, p2);
12444 vim_free(tofree1);
12445 vim_free(tofree2);
12446 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447}
12448
12449 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450#ifdef __BORLANDC__
12451_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012453item_compare2(s1, s2)
12454 const void *s1;
12455 const void *s2;
12456{
12457 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012458 typval_T rettv;
12459 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012460 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012462 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12463 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12465 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012466
12467 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12468 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012469 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012470 clear_tv(&argv[0]);
12471 clear_tv(&argv[1]);
12472
12473 if (res == FAIL)
12474 res = ITEM_COMPARE_FAIL;
12475 else
12476 res = get_tv_number(&rettv);
12477 clear_tv(&rettv);
12478 return res;
12479}
12480
12481/*
12482 * "sort({list})" function
12483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012486 typval_T *argvars;
12487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488{
Bram Moolenaar33570922005-01-25 22:26:29 +000012489 list_T *l;
12490 listitem_T *li;
12491 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492 long len;
12493 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495 rettv->vval.v_number = 0;
12496 if (argvars[0].v_type != VAR_LIST)
12497 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 else
12499 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012500 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012501 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012502 return;
12503 rettv->vval.v_list = l;
12504 rettv->v_type = VAR_LIST;
12505 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506
Bram Moolenaar0d660222005-01-07 21:51:51 +000012507 len = list_len(l);
12508 if (len <= 1)
12509 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 item_compare_ic = FALSE;
12512 item_compare_func = NULL;
12513 if (argvars[1].v_type != VAR_UNKNOWN)
12514 {
12515 if (argvars[1].v_type == VAR_FUNC)
12516 item_compare_func = argvars[0].vval.v_string;
12517 else
12518 {
12519 i = get_tv_number(&argvars[1]);
12520 if (i == 1)
12521 item_compare_ic = TRUE;
12522 else
12523 item_compare_func = get_tv_string(&argvars[1]);
12524 }
12525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012528 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529 if (ptrs == NULL)
12530 return;
12531 i = 0;
12532 for (li = l->lv_first; li != NULL; li = li->li_next)
12533 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534
Bram Moolenaar0d660222005-01-07 21:51:51 +000012535 /* test the compare function */
12536 if (item_compare_func != NULL
12537 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12538 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012539 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012541 {
12542 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544 item_compare_func == NULL ? item_compare : item_compare2);
12545
12546 /* Clear the List and append the items in the sorted order. */
12547 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012548 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549 for (i = 0; i < len; ++i)
12550 list_append(l, ptrs[i]);
12551 }
12552
12553 vim_free(ptrs);
12554 }
12555}
12556
12557 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012558f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012559 typval_T *argvars;
12560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561{
12562 char_u *str;
12563 char_u *end;
12564 char_u *pat;
12565 regmatch_T regmatch;
12566 char_u patbuf[NUMBUFLEN];
12567 char_u *save_cpo;
12568 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 listitem_T *ni;
12570 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571 colnr_T col = 0;
12572
12573 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12574 save_cpo = p_cpo;
12575 p_cpo = (char_u *)"";
12576
12577 str = get_tv_string(&argvars[0]);
12578 if (argvars[1].v_type == VAR_UNKNOWN)
12579 pat = (char_u *)"[\\x01- ]\\+";
12580 else
12581 pat = get_tv_string_buf(&argvars[1], patbuf);
12582
12583 l = list_alloc();
12584 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012586 rettv->v_type = VAR_LIST;
12587 rettv->vval.v_list = l;
12588 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12591 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012593 regmatch.rm_ic = FALSE;
12594 while (*str != NUL)
12595 {
12596 match = vim_regexec_nl(&regmatch, str, col);
12597 if (match)
12598 end = regmatch.startp[0];
12599 else
12600 end = str + STRLEN(str);
12601 if (end > str)
12602 {
12603 ni = listitem_alloc();
12604 if (ni == NULL)
12605 break;
12606 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012607 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12609 list_append(l, ni);
12610 }
12611 if (!match)
12612 break;
12613 /* Advance to just after the match. */
12614 if (regmatch.endp[0] > str)
12615 col = 0;
12616 else
12617 {
12618 /* Don't get stuck at the same match. */
12619#ifdef FEAT_MBYTE
12620 col = mb_ptr2len_check(regmatch.endp[0]);
12621#else
12622 col = 1;
12623#endif
12624 }
12625 str = regmatch.endp[0];
12626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627
Bram Moolenaar0d660222005-01-07 21:51:51 +000012628 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632}
12633
12634#ifdef HAVE_STRFTIME
12635/*
12636 * "strftime({format}[, {time}])" function
12637 */
12638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012640 typval_T *argvars;
12641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642{
12643 char_u result_buf[256];
12644 struct tm *curtime;
12645 time_t seconds;
12646 char_u *p;
12647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012651 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 seconds = time(NULL);
12653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 curtime = localtime(&seconds);
12656 /* MSVC returns NULL for an invalid value of seconds. */
12657 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659 else
12660 {
12661# ifdef FEAT_MBYTE
12662 vimconv_T conv;
12663 char_u *enc;
12664
12665 conv.vc_type = CONV_NONE;
12666 enc = enc_locale();
12667 convert_setup(&conv, p_enc, enc);
12668 if (conv.vc_type != CONV_NONE)
12669 p = string_convert(&conv, p, NULL);
12670# endif
12671 if (p != NULL)
12672 (void)strftime((char *)result_buf, sizeof(result_buf),
12673 (char *)p, curtime);
12674 else
12675 result_buf[0] = NUL;
12676
12677# ifdef FEAT_MBYTE
12678 if (conv.vc_type != CONV_NONE)
12679 vim_free(p);
12680 convert_setup(&conv, enc, p_enc);
12681 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 else
12684# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012685 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686
12687# ifdef FEAT_MBYTE
12688 /* Release conversion descriptors */
12689 convert_setup(&conv, NULL, NULL);
12690 vim_free(enc);
12691# endif
12692 }
12693}
12694#endif
12695
12696/*
12697 * "stridx()" function
12698 */
12699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012701 typval_T *argvars;
12702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703{
12704 char_u buf[NUMBUFLEN];
12705 char_u *needle;
12706 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012709 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012712 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12713 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 if (argvars[2].v_type != VAR_UNKNOWN)
12716 {
12717 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012718 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012720 if (start_idx >= 0)
12721 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 }
12723
12724 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12725 if (pos != NULL)
12726 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727}
12728
12729/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012730 * "string()" function
12731 */
12732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 typval_T *argvars;
12735 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012736{
12737 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012738 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012741 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012742 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744}
12745
12746/*
12747 * "strlen()" function
12748 */
12749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 typval_T *argvars;
12752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_number = (varnumber_T)(STRLEN(
12755 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756}
12757
12758/*
12759 * "strpart()" function
12760 */
12761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766 char_u *p;
12767 int n;
12768 int len;
12769 int slen;
12770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 slen = (int)STRLEN(p);
12773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 else
12778 len = slen - n; /* default len: all bytes that are available. */
12779
12780 /*
12781 * Only return the overlap between the specified part and the actual
12782 * string.
12783 */
12784 if (n < 0)
12785 {
12786 len += n;
12787 n = 0;
12788 }
12789 else if (n > slen)
12790 n = slen;
12791 if (len < 0)
12792 len = 0;
12793 else if (n + len > slen)
12794 len = slen - n;
12795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv->v_type = VAR_STRING;
12797 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
12800/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801 * "strridx()" function
12802 */
12803 static void
12804f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 typval_T *argvars;
12806 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012807{
12808 char_u buf[NUMBUFLEN];
12809 char_u *needle;
12810 char_u *haystack;
12811 char_u *rest;
12812 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012813 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012814
12815 needle = get_tv_string(&argvars[1]);
12816 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012817 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012818 if (argvars[2].v_type != VAR_UNKNOWN)
12819 {
12820 /* Third argument: upper limit for index */
12821 end_idx = get_tv_number(&argvars[2]);
12822 if (end_idx < 0)
12823 {
12824 /* can never find a match */
12825 rettv->vval.v_number = -1;
12826 return;
12827 }
12828 }
12829 else
12830 end_idx = haystack_len;
12831
Bram Moolenaar0d660222005-01-07 21:51:51 +000012832 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012833 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012834 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012835 lastmatch = haystack + end_idx;
12836 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012837 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012838 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012839 for (rest = haystack; *rest != '\0'; ++rest)
12840 {
12841 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012842 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012843 break;
12844 lastmatch = rest;
12845 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012846 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847
12848 if (lastmatch == NULL)
12849 rettv->vval.v_number = -1;
12850 else
12851 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12852}
12853
12854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 * "strtrans()" function
12856 */
12857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012859 typval_T *argvars;
12860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->v_type = VAR_STRING;
12863 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864}
12865
12866/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867 * "submatch()" function
12868 */
12869 static void
12870f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012871 typval_T *argvars;
12872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873{
12874 rettv->v_type = VAR_STRING;
12875 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12876}
12877
12878/*
12879 * "substitute()" function
12880 */
12881 static void
12882f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012883 typval_T *argvars;
12884 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012885{
12886 char_u patbuf[NUMBUFLEN];
12887 char_u subbuf[NUMBUFLEN];
12888 char_u flagsbuf[NUMBUFLEN];
12889
12890 rettv->v_type = VAR_STRING;
12891 rettv->vval.v_string = do_string_sub(
12892 get_tv_string(&argvars[0]),
12893 get_tv_string_buf(&argvars[1], patbuf),
12894 get_tv_string_buf(&argvars[2], subbuf),
12895 get_tv_string_buf(&argvars[3], flagsbuf));
12896}
12897
12898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 * "synID(line, col, trans)" function
12900 */
12901/*ARGSUSED*/
12902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 typval_T *argvars;
12905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906{
12907 int id = 0;
12908#ifdef FEAT_SYN_HL
12909 long line;
12910 long col;
12911 int trans;
12912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 line = get_tv_lnum(argvars);
12914 col = get_tv_number(&argvars[1]) - 1;
12915 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916
12917 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12918 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12919 id = syn_get_id(line, col, trans);
12920#endif
12921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923}
12924
12925/*
12926 * "synIDattr(id, what [, mode])" function
12927 */
12928/*ARGSUSED*/
12929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012931 typval_T *argvars;
12932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933{
12934 char_u *p = NULL;
12935#ifdef FEAT_SYN_HL
12936 int id;
12937 char_u *what;
12938 char_u *mode;
12939 char_u modebuf[NUMBUFLEN];
12940 int modec;
12941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 id = get_tv_number(&argvars[0]);
12943 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 modec = TOLOWER_ASC(mode[0]);
12948 if (modec != 't' && modec != 'c'
12949#ifdef FEAT_GUI
12950 && modec != 'g'
12951#endif
12952 )
12953 modec = 0; /* replace invalid with current */
12954 }
12955 else
12956 {
12957#ifdef FEAT_GUI
12958 if (gui.in_use)
12959 modec = 'g';
12960 else
12961#endif
12962 if (t_colors > 1)
12963 modec = 'c';
12964 else
12965 modec = 't';
12966 }
12967
12968
12969 switch (TOLOWER_ASC(what[0]))
12970 {
12971 case 'b':
12972 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12973 p = highlight_color(id, what, modec);
12974 else /* bold */
12975 p = highlight_has_attr(id, HL_BOLD, modec);
12976 break;
12977
12978 case 'f': /* fg[#] */
12979 p = highlight_color(id, what, modec);
12980 break;
12981
12982 case 'i':
12983 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12984 p = highlight_has_attr(id, HL_INVERSE, modec);
12985 else /* italic */
12986 p = highlight_has_attr(id, HL_ITALIC, modec);
12987 break;
12988
12989 case 'n': /* name */
12990 p = get_highlight_name(NULL, id - 1);
12991 break;
12992
12993 case 'r': /* reverse */
12994 p = highlight_has_attr(id, HL_INVERSE, modec);
12995 break;
12996
12997 case 's': /* standout */
12998 p = highlight_has_attr(id, HL_STANDOUT, modec);
12999 break;
13000
13001 case 'u': /* underline */
13002 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13003 break;
13004 }
13005
13006 if (p != NULL)
13007 p = vim_strsave(p);
13008#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013009 rettv->v_type = VAR_STRING;
13010 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011}
13012
13013/*
13014 * "synIDtrans(id)" function
13015 */
13016/*ARGSUSED*/
13017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021{
13022 int id;
13023
13024#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
13027 if (id > 0)
13028 id = syn_get_final_id(id);
13029 else
13030#endif
13031 id = 0;
13032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034}
13035
13036/*
13037 * "system()" function
13038 */
13039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 typval_T *argvars;
13042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013044 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013046 char_u *infile = NULL;
13047 char_u buf[NUMBUFLEN];
13048 int err = FALSE;
13049 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013051 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013052 {
13053 /*
13054 * Write the string to a temp file, to be used for input of the shell
13055 * command.
13056 */
13057 if ((infile = vim_tempname('i')) == NULL)
13058 {
13059 EMSG(_(e_notmp));
13060 return;
13061 }
13062
13063 fd = mch_fopen((char *)infile, WRITEBIN);
13064 if (fd == NULL)
13065 {
13066 EMSG2(_(e_notopen), infile);
13067 goto done;
13068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013070 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13071 err = TRUE;
13072 if (fclose(fd) != 0)
13073 err = TRUE;
13074 if (err)
13075 {
13076 EMSG(_("E677: Error writing temp file"));
13077 goto done;
13078 }
13079 }
13080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013081 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013082
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083#ifdef USE_CR
13084 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013085 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 {
13087 char_u *s;
13088
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013089 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 {
13091 if (*s == CAR)
13092 *s = NL;
13093 }
13094 }
13095#else
13096# ifdef USE_CRNL
13097 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013098 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 {
13100 char_u *s, *d;
13101
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013102 d = res;
13103 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 {
13105 if (s[0] == CAR && s[1] == NL)
13106 ++s;
13107 *d++ = *s;
13108 }
13109 *d = NUL;
13110 }
13111# endif
13112#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013113
13114done:
13115 if (infile != NULL)
13116 {
13117 mch_remove(infile);
13118 vim_free(infile);
13119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013120 rettv->v_type = VAR_STRING;
13121 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122}
13123
13124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 * "tempname()" function
13126 */
13127/*ARGSUSED*/
13128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013129f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 typval_T *argvars;
13131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132{
13133 static int x = 'A';
13134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135 rettv->v_type = VAR_STRING;
13136 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137
13138 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13139 * names. Skip 'I' and 'O', they are used for shell redirection. */
13140 do
13141 {
13142 if (x == 'Z')
13143 x = '0';
13144 else if (x == '9')
13145 x = 'A';
13146 else
13147 {
13148#ifdef EBCDIC
13149 if (x == 'I')
13150 x = 'J';
13151 else if (x == 'R')
13152 x = 'S';
13153 else
13154#endif
13155 ++x;
13156 }
13157 } while (x == 'I' || x == 'O');
13158}
13159
13160/*
13161 * "tolower(string)" function
13162 */
13163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *argvars;
13166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167{
13168 char_u *p;
13169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 p = vim_strsave(get_tv_string(&argvars[0]));
13171 rettv->v_type = VAR_STRING;
13172 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173
13174 if (p != NULL)
13175 while (*p != NUL)
13176 {
13177#ifdef FEAT_MBYTE
13178 int l;
13179
13180 if (enc_utf8)
13181 {
13182 int c, lc;
13183
13184 c = utf_ptr2char(p);
13185 lc = utf_tolower(c);
13186 l = utf_ptr2len_check(p);
13187 /* TODO: reallocate string when byte count changes. */
13188 if (utf_char2len(lc) == l)
13189 utf_char2bytes(lc, p);
13190 p += l;
13191 }
13192 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13193 p += l; /* skip multi-byte character */
13194 else
13195#endif
13196 {
13197 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13198 ++p;
13199 }
13200 }
13201}
13202
13203/*
13204 * "toupper(string)" function
13205 */
13206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013208 typval_T *argvars;
13209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210{
13211 char_u *p;
13212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 p = vim_strsave(get_tv_string(&argvars[0]));
13214 rettv->v_type = VAR_STRING;
13215 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216
13217 if (p != NULL)
13218 while (*p != NUL)
13219 {
13220#ifdef FEAT_MBYTE
13221 int l;
13222
13223 if (enc_utf8)
13224 {
13225 int c, uc;
13226
13227 c = utf_ptr2char(p);
13228 uc = utf_toupper(c);
13229 l = utf_ptr2len_check(p);
13230 /* TODO: reallocate string when byte count changes. */
13231 if (utf_char2len(uc) == l)
13232 utf_char2bytes(uc, p);
13233 p += l;
13234 }
13235 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13236 p += l; /* skip multi-byte character */
13237 else
13238#endif
13239 {
13240 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13241 p++;
13242 }
13243 }
13244}
13245
13246/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013247 * "tr(string, fromstr, tostr)" function
13248 */
13249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 typval_T *argvars;
13252 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013253{
13254 char_u *instr;
13255 char_u *fromstr;
13256 char_u *tostr;
13257 char_u *p;
13258#ifdef FEAT_MBYTE
13259 int inlen;
13260 int fromlen;
13261 int tolen;
13262 int idx;
13263 char_u *cpstr;
13264 int cplen;
13265 int first = TRUE;
13266#endif
13267 char_u buf[NUMBUFLEN];
13268 char_u buf2[NUMBUFLEN];
13269 garray_T ga;
13270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271 instr = get_tv_string(&argvars[0]);
13272 fromstr = get_tv_string_buf(&argvars[1], buf);
13273 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013274
13275 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->v_type = VAR_STRING;
13277 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013278 ga_init2(&ga, (int)sizeof(char), 80);
13279
13280#ifdef FEAT_MBYTE
13281 if (!has_mbyte)
13282#endif
13283 /* not multi-byte: fromstr and tostr must be the same length */
13284 if (STRLEN(fromstr) != STRLEN(tostr))
13285 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013286#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013287error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013288#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013289 EMSG2(_(e_invarg2), fromstr);
13290 ga_clear(&ga);
13291 return;
13292 }
13293
13294 /* fromstr and tostr have to contain the same number of chars */
13295 while (*instr != NUL)
13296 {
13297#ifdef FEAT_MBYTE
13298 if (has_mbyte)
13299 {
13300 inlen = mb_ptr2len_check(instr);
13301 cpstr = instr;
13302 cplen = inlen;
13303 idx = 0;
13304 for (p = fromstr; *p != NUL; p += fromlen)
13305 {
13306 fromlen = mb_ptr2len_check(p);
13307 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13308 {
13309 for (p = tostr; *p != NUL; p += tolen)
13310 {
13311 tolen = mb_ptr2len_check(p);
13312 if (idx-- == 0)
13313 {
13314 cplen = tolen;
13315 cpstr = p;
13316 break;
13317 }
13318 }
13319 if (*p == NUL) /* tostr is shorter than fromstr */
13320 goto error;
13321 break;
13322 }
13323 ++idx;
13324 }
13325
13326 if (first && cpstr == instr)
13327 {
13328 /* Check that fromstr and tostr have the same number of
13329 * (multi-byte) characters. Done only once when a character
13330 * of instr doesn't appear in fromstr. */
13331 first = FALSE;
13332 for (p = tostr; *p != NUL; p += tolen)
13333 {
13334 tolen = mb_ptr2len_check(p);
13335 --idx;
13336 }
13337 if (idx != 0)
13338 goto error;
13339 }
13340
13341 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013342 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013343 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013344
13345 instr += inlen;
13346 }
13347 else
13348#endif
13349 {
13350 /* When not using multi-byte chars we can do it faster. */
13351 p = vim_strchr(fromstr, *instr);
13352 if (p != NULL)
13353 ga_append(&ga, tostr[p - fromstr]);
13354 else
13355 ga_append(&ga, *instr);
13356 ++instr;
13357 }
13358 }
13359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013361}
13362
13363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 * "type(expr)" function
13365 */
13366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 typval_T *argvars;
13369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013371 int n;
13372
13373 switch (argvars[0].v_type)
13374 {
13375 case VAR_NUMBER: n = 0; break;
13376 case VAR_STRING: n = 1; break;
13377 case VAR_FUNC: n = 2; break;
13378 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013379 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013380 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13381 }
13382 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383}
13384
13385/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013386 * "values(dict)" function
13387 */
13388 static void
13389f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013390 typval_T *argvars;
13391 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013392{
13393 dict_list(argvars, rettv, 1);
13394}
13395
13396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 * "virtcol(string)" function
13398 */
13399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013400f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013401 typval_T *argvars;
13402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403{
13404 colnr_T vcol = 0;
13405 pos_T *fp;
13406
13407 fp = var2fpos(&argvars[0], FALSE);
13408 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13409 {
13410 getvvcol(curwin, fp, NULL, NULL, &vcol);
13411 ++vcol;
13412 }
13413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415}
13416
13417/*
13418 * "visualmode()" function
13419 */
13420/*ARGSUSED*/
13421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425{
13426#ifdef FEAT_VISUAL
13427 char_u str[2];
13428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 str[0] = curbuf->b_visual_mode_eval;
13431 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433
13434 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013435 if ((argvars[0].v_type == VAR_NUMBER
13436 && argvars[0].vval.v_number != 0)
13437 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 curbuf->b_visual_mode_eval = NUL;
13440#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#endif
13443}
13444
13445/*
13446 * "winbufnr(nr)" function
13447 */
13448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
13453 win_T *wp;
13454
13455 wp = find_win_by_nr(&argvars[0]);
13456 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460}
13461
13462/*
13463 * "wincol()" function
13464 */
13465/*ARGSUSED*/
13466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
13471 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473}
13474
13475/*
13476 * "winheight(nr)" function
13477 */
13478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 typval_T *argvars;
13481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
13483 win_T *wp;
13484
13485 wp = find_win_by_nr(&argvars[0]);
13486 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490}
13491
13492/*
13493 * "winline()" function
13494 */
13495/*ARGSUSED*/
13496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 typval_T *argvars;
13499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500{
13501 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503}
13504
13505/*
13506 * "winnr()" function
13507 */
13508/* ARGSUSED */
13509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013511 typval_T *argvars;
13512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513{
13514 int nr = 1;
13515#ifdef FEAT_WINDOWS
13516 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013517 win_T *twin = curwin;
13518 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013523 if (STRCMP(arg, "$") == 0)
13524 twin = lastwin;
13525 else if (STRCMP(arg, "#") == 0)
13526 {
13527 twin = prevwin;
13528 if (prevwin == NULL)
13529 nr = 0;
13530 }
13531 else
13532 {
13533 EMSG2(_(e_invexpr2), arg);
13534 nr = 0;
13535 }
13536 }
13537
13538 if (nr > 0)
13539 for (wp = firstwin; wp != twin; wp = wp->w_next)
13540 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543}
13544
13545/*
13546 * "winrestcmd()" function
13547 */
13548/* ARGSUSED */
13549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013551 typval_T *argvars;
13552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553{
13554#ifdef FEAT_WINDOWS
13555 win_T *wp;
13556 int winnr = 1;
13557 garray_T ga;
13558 char_u buf[50];
13559
13560 ga_init2(&ga, (int)sizeof(char), 70);
13561 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13562 {
13563 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13564 ga_concat(&ga, buf);
13565# ifdef FEAT_VERTSPLIT
13566 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13567 ga_concat(&ga, buf);
13568# endif
13569 ++winnr;
13570 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013571 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578}
13579
13580/*
13581 * "winwidth(nr)" function
13582 */
13583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *argvars;
13586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587{
13588 win_T *wp;
13589
13590 wp = find_win_by_nr(&argvars[0]);
13591 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 else
13594#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598#endif
13599}
13600
13601 static win_T *
13602find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604{
13605#ifdef FEAT_WINDOWS
13606 win_T *wp;
13607#endif
13608 int nr;
13609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611
13612#ifdef FEAT_WINDOWS
13613 if (nr == 0)
13614 return curwin;
13615
13616 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13617 if (--nr <= 0)
13618 break;
13619 return wp;
13620#else
13621 if (nr == 0 || nr == 1)
13622 return curwin;
13623 return NULL;
13624#endif
13625}
13626
13627/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013628 * "writefile()" function
13629 */
13630 static void
13631f_writefile(argvars, rettv)
13632 typval_T *argvars;
13633 typval_T *rettv;
13634{
13635 int binary = FALSE;
13636 char_u *fname;
13637 FILE *fd;
13638 listitem_T *li;
13639 char_u *s;
13640 int ret = 0;
13641 int c;
13642
13643 if (argvars[0].v_type != VAR_LIST)
13644 {
13645 EMSG2(_(e_listarg), "writefile()");
13646 return;
13647 }
13648 if (argvars[0].vval.v_list == NULL)
13649 return;
13650
13651 if (argvars[2].v_type != VAR_UNKNOWN
13652 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13653 binary = TRUE;
13654
13655 /* Always open the file in binary mode, library functions have a mind of
13656 * their own about CR-LF conversion. */
13657 fname = get_tv_string(&argvars[1]);
13658 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13659 {
13660 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13661 ret = -1;
13662 }
13663 else
13664 {
13665 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13666 li = li->li_next)
13667 {
13668 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13669 {
13670 if (*s == '\n')
13671 c = putc(NUL, fd);
13672 else
13673 c = putc(*s, fd);
13674 if (c == EOF)
13675 {
13676 ret = -1;
13677 break;
13678 }
13679 }
13680 if (!binary || li->li_next != NULL)
13681 if (putc('\n', fd) == EOF)
13682 {
13683 ret = -1;
13684 break;
13685 }
13686 if (ret < 0)
13687 {
13688 EMSG(_(e_write));
13689 break;
13690 }
13691 }
13692 fclose(fd);
13693 }
13694
13695 rettv->vval.v_number = ret;
13696}
13697
13698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 * Translate a String variable into a position.
13700 */
13701 static pos_T *
13702var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 int lnum; /* TRUE when $ is last line */
13705{
13706 char_u *name;
13707 static pos_T pos;
13708 pos_T *pp;
13709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 if (name[0] == '.') /* cursor */
13712 return &curwin->w_cursor;
13713 if (name[0] == '\'') /* mark */
13714 {
13715 pp = getmark(name[1], FALSE);
13716 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13717 return NULL;
13718 return pp;
13719 }
13720 if (name[0] == '$') /* last column or line */
13721 {
13722 if (lnum)
13723 {
13724 pos.lnum = curbuf->b_ml.ml_line_count;
13725 pos.col = 0;
13726 }
13727 else
13728 {
13729 pos.lnum = curwin->w_cursor.lnum;
13730 pos.col = (colnr_T)STRLEN(ml_get_curline());
13731 }
13732 return &pos;
13733 }
13734 return NULL;
13735}
13736
13737/*
13738 * Get the length of an environment variable name.
13739 * Advance "arg" to the first character after the name.
13740 * Return 0 for error.
13741 */
13742 static int
13743get_env_len(arg)
13744 char_u **arg;
13745{
13746 char_u *p;
13747 int len;
13748
13749 for (p = *arg; vim_isIDc(*p); ++p)
13750 ;
13751 if (p == *arg) /* no name found */
13752 return 0;
13753
13754 len = (int)(p - *arg);
13755 *arg = p;
13756 return len;
13757}
13758
13759/*
13760 * Get the length of the name of a function or internal variable.
13761 * "arg" is advanced to the first non-white character after the name.
13762 * Return 0 if something is wrong.
13763 */
13764 static int
13765get_id_len(arg)
13766 char_u **arg;
13767{
13768 char_u *p;
13769 int len;
13770
13771 /* Find the end of the name. */
13772 for (p = *arg; eval_isnamec(*p); ++p)
13773 ;
13774 if (p == *arg) /* no name found */
13775 return 0;
13776
13777 len = (int)(p - *arg);
13778 *arg = skipwhite(p);
13779
13780 return len;
13781}
13782
13783/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013784 * Get the length of the name of a variable or function.
13785 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013787 * Return -1 if curly braces expansion failed.
13788 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 * If the name contains 'magic' {}'s, expand them and return the
13790 * expanded name in an allocated string via 'alias' - caller must free.
13791 */
13792 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013793get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 char_u **arg;
13795 char_u **alias;
13796 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013797 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798{
13799 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 char_u *p;
13801 char_u *expr_start;
13802 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803
13804 *alias = NULL; /* default to no alias */
13805
13806 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13807 && (*arg)[2] == (int)KE_SNR)
13808 {
13809 /* hard coded <SNR>, already translated */
13810 *arg += 3;
13811 return get_id_len(arg) + 3;
13812 }
13813 len = eval_fname_script(*arg);
13814 if (len > 0)
13815 {
13816 /* literal "<SID>", "s:" or "<SNR>" */
13817 *arg += len;
13818 }
13819
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013821 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013823 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 if (expr_start != NULL)
13825 {
13826 char_u *temp_string;
13827
13828 if (!evaluate)
13829 {
13830 len += (int)(p - *arg);
13831 *arg = skipwhite(p);
13832 return len;
13833 }
13834
13835 /*
13836 * Include any <SID> etc in the expanded string:
13837 * Thus the -len here.
13838 */
13839 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13840 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013841 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 *alias = temp_string;
13843 *arg = skipwhite(p);
13844 return (int)STRLEN(temp_string);
13845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846
13847 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013848 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 EMSG2(_(e_invexpr2), *arg);
13850
13851 return len;
13852}
13853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854/*
13855 * Find the end of a variable or function name, taking care of magic braces.
13856 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13857 * start and end of the first magic braces item.
13858 * Return a pointer to just after the name. Equal to "arg" if there is no
13859 * valid name.
13860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 char_u *arg;
13864 char_u **expr_start;
13865 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013866 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 int mb_nest = 0;
13869 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 char_u *p;
13871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013872 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874 *expr_start = NULL;
13875 *expr_end = NULL;
13876 }
13877
13878 for (p = arg; *p != NUL
13879 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013880 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013881 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 || mb_nest != 0
13883 || br_nest != 0); ++p)
13884 {
13885 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 if (*p == '[')
13888 ++br_nest;
13889 else if (*p == ']')
13890 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 if (*p == '{')
13895 {
13896 mb_nest++;
13897 if (expr_start != NULL && *expr_start == NULL)
13898 *expr_start = p;
13899 }
13900 else if (*p == '}')
13901 {
13902 mb_nest--;
13903 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13904 *expr_end = p;
13905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 }
13908
13909 return p;
13910}
13911
13912/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013913 * Expands out the 'magic' {}'s in a variable/function name.
13914 * Note that this can call itself recursively, to deal with
13915 * constructs like foo{bar}{baz}{bam}
13916 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13917 * "in_start" ^
13918 * "expr_start" ^
13919 * "expr_end" ^
13920 * "in_end" ^
13921 *
13922 * Returns a new allocated string, which the caller must free.
13923 * Returns NULL for failure.
13924 */
13925 static char_u *
13926make_expanded_name(in_start, expr_start, expr_end, in_end)
13927 char_u *in_start;
13928 char_u *expr_start;
13929 char_u *expr_end;
13930 char_u *in_end;
13931{
13932 char_u c1;
13933 char_u *retval = NULL;
13934 char_u *temp_result;
13935 char_u *nextcmd = NULL;
13936
13937 if (expr_end == NULL || in_end == NULL)
13938 return NULL;
13939 *expr_start = NUL;
13940 *expr_end = NUL;
13941 c1 = *in_end;
13942 *in_end = NUL;
13943
13944 temp_result = eval_to_string(expr_start + 1, &nextcmd);
13945 if (temp_result != NULL && nextcmd == NULL)
13946 {
13947 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
13948 + (in_end - expr_end) + 1));
13949 if (retval != NULL)
13950 {
13951 STRCPY(retval, in_start);
13952 STRCAT(retval, temp_result);
13953 STRCAT(retval, expr_end + 1);
13954 }
13955 }
13956 vim_free(temp_result);
13957
13958 *in_end = c1; /* put char back for error messages */
13959 *expr_start = '{';
13960 *expr_end = '}';
13961
13962 if (retval != NULL)
13963 {
13964 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
13965 if (expr_start != NULL)
13966 {
13967 /* Further expansion! */
13968 temp_result = make_expanded_name(retval, expr_start,
13969 expr_end, temp_result);
13970 vim_free(retval);
13971 retval = temp_result;
13972 }
13973 }
13974
13975 return retval;
13976}
13977
13978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013980 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 */
13982 static int
13983eval_isnamec(c)
13984 int c;
13985{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013986 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987}
13988
13989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 * Set number v: variable to "val".
13991 */
13992 void
13993set_vim_var_nr(idx, val)
13994 int idx;
13995 long val;
13996{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013997 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998}
13999
14000/*
14001 * Get number v: variable value;
14002 */
14003 long
14004get_vim_var_nr(idx)
14005 int idx;
14006{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014007 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008}
14009
14010/*
14011 * Set v:count, v:count1 and v:prevcount.
14012 */
14013 void
14014set_vcount(count, count1)
14015 long count;
14016 long count1;
14017{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014018 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14019 vimvars[VV_COUNT].vv_nr = count;
14020 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021}
14022
14023/*
14024 * Set string v: variable to a copy of "val".
14025 */
14026 void
14027set_vim_var_string(idx, val, len)
14028 int idx;
14029 char_u *val;
14030 int len; /* length of "val" to use or -1 (whole string) */
14031{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014032 /* Need to do this (at least) once, since we can't initialize a union.
14033 * Will always be invoked when "v:progname" is set. */
14034 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14035
Bram Moolenaare9a41262005-01-15 22:18:47 +000014036 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014038 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014040 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014042 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043}
14044
14045/*
14046 * Set v:register if needed.
14047 */
14048 void
14049set_reg_var(c)
14050 int c;
14051{
14052 char_u regname;
14053
14054 if (c == 0 || c == ' ')
14055 regname = '"';
14056 else
14057 regname = c;
14058 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014059 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 set_vim_var_string(VV_REG, &regname, 1);
14061}
14062
14063/*
14064 * Get or set v:exception. If "oldval" == NULL, return the current value.
14065 * Otherwise, restore the value to "oldval" and return NULL.
14066 * Must always be called in pairs to save and restore v:exception! Does not
14067 * take care of memory allocations.
14068 */
14069 char_u *
14070v_exception(oldval)
14071 char_u *oldval;
14072{
14073 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014074 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075
Bram Moolenaare9a41262005-01-15 22:18:47 +000014076 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 return NULL;
14078}
14079
14080/*
14081 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14082 * Otherwise, restore the value to "oldval" and return NULL.
14083 * Must always be called in pairs to save and restore v:throwpoint! Does not
14084 * take care of memory allocations.
14085 */
14086 char_u *
14087v_throwpoint(oldval)
14088 char_u *oldval;
14089{
14090 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014091 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092
Bram Moolenaare9a41262005-01-15 22:18:47 +000014093 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 return NULL;
14095}
14096
14097#if defined(FEAT_AUTOCMD) || defined(PROTO)
14098/*
14099 * Set v:cmdarg.
14100 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14101 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14102 * Must always be called in pairs!
14103 */
14104 char_u *
14105set_cmdarg(eap, oldarg)
14106 exarg_T *eap;
14107 char_u *oldarg;
14108{
14109 char_u *oldval;
14110 char_u *newval;
14111 unsigned len;
14112
Bram Moolenaare9a41262005-01-15 22:18:47 +000014113 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014114 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014116 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014117 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014118 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 }
14120
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014121 if (eap->force_bin == FORCE_BIN)
14122 len = 6;
14123 else if (eap->force_bin == FORCE_NOBIN)
14124 len = 8;
14125 else
14126 len = 0;
14127 if (eap->force_ff != 0)
14128 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14129# ifdef FEAT_MBYTE
14130 if (eap->force_enc != 0)
14131 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14132# endif
14133
14134 newval = alloc(len + 1);
14135 if (newval == NULL)
14136 return NULL;
14137
14138 if (eap->force_bin == FORCE_BIN)
14139 sprintf((char *)newval, " ++bin");
14140 else if (eap->force_bin == FORCE_NOBIN)
14141 sprintf((char *)newval, " ++nobin");
14142 else
14143 *newval = NUL;
14144 if (eap->force_ff != 0)
14145 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14146 eap->cmd + eap->force_ff);
14147# ifdef FEAT_MBYTE
14148 if (eap->force_enc != 0)
14149 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14150 eap->cmd + eap->force_enc);
14151# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014152 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014153 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154}
14155#endif
14156
14157/*
14158 * Get the value of internal variable "name".
14159 * Return OK or FAIL.
14160 */
14161 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014162get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 char_u *name;
14164 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014166 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167{
14168 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *tv = NULL;
14170 typval_T atv;
14171 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173
14174 /* truncate the name, so that we can use strcmp() */
14175 cc = name[len];
14176 name[len] = NUL;
14177
14178 /*
14179 * Check for "b:changedtick".
14180 */
14181 if (STRCMP(name, "b:changedtick") == 0)
14182 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014183 atv.v_type = VAR_NUMBER;
14184 atv.vval.v_number = curbuf->b_changedtick;
14185 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 }
14187
14188 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 * Check for user-defined variables.
14190 */
14191 else
14192 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014193 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014195 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 }
14197
Bram Moolenaare9a41262005-01-15 22:18:47 +000014198 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014200 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014201 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 ret = FAIL;
14203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014205 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206
14207 name[len] = cc;
14208
14209 return ret;
14210}
14211
14212/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014213 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14214 * Also handle function call with Funcref variable: func(expr)
14215 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14216 */
14217 static int
14218handle_subscript(arg, rettv, evaluate, verbose)
14219 char_u **arg;
14220 typval_T *rettv;
14221 int evaluate; /* do more than finding the end */
14222 int verbose; /* give error messages */
14223{
14224 int ret = OK;
14225 dict_T *selfdict = NULL;
14226 char_u *s;
14227 int len;
14228
14229 while (ret == OK
14230 && (**arg == '['
14231 || (**arg == '.' && rettv->v_type == VAR_DICT)
14232 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14233 && !vim_iswhite(*(*arg - 1)))
14234 {
14235 if (**arg == '(')
14236 {
14237 s = rettv->vval.v_string;
14238
14239 /* Invoke the function. Recursive! */
14240 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14242 &len, evaluate, selfdict);
14243
14244 /* Stop the expression evaluation when immediately aborting on
14245 * error, or when an interrupt occurred or an exception was thrown
14246 * but not caught. */
14247 if (aborting())
14248 {
14249 if (ret == OK)
14250 clear_tv(rettv);
14251 ret = FAIL;
14252 }
14253 dict_unref(selfdict);
14254 selfdict = NULL;
14255 }
14256 else /* **arg == '[' || **arg == '.' */
14257 {
14258 dict_unref(selfdict);
14259 if (rettv->v_type == VAR_DICT)
14260 {
14261 selfdict = rettv->vval.v_dict;
14262 if (selfdict != NULL)
14263 ++selfdict->dv_refcount;
14264 }
14265 else
14266 selfdict = NULL;
14267 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14268 {
14269 clear_tv(rettv);
14270 ret = FAIL;
14271 }
14272 }
14273 }
14274 dict_unref(selfdict);
14275 return ret;
14276}
14277
14278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14280 * value).
14281 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014282 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014284{
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014286}
14287
14288/*
14289 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 * The string "s" must have been allocated, it is consumed.
14291 * Return NULL for out of memory, the variable otherwise.
14292 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 char_u *s;
14296{
Bram Moolenaar33570922005-01-25 22:26:29 +000014297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv = alloc_tv();
14300 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->v_type = VAR_STRING;
14303 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 }
14305 else
14306 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308}
14309
14310/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014311 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 */
14313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014315 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316{
14317 if (varp != NULL)
14318 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014319 switch (varp->v_type)
14320 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014321 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014322 func_unref(varp->vval.v_string);
14323 /*FALLTHROUGH*/
14324 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014325 vim_free(varp->vval.v_string);
14326 break;
14327 case VAR_LIST:
14328 list_unref(varp->vval.v_list);
14329 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014330 case VAR_DICT:
14331 dict_unref(varp->vval.v_dict);
14332 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014333 case VAR_NUMBER:
14334 case VAR_UNKNOWN:
14335 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014336 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014337 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014338 break;
14339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 vim_free(varp);
14341 }
14342}
14343
14344/*
14345 * Free the memory for a variable value and set the value to NULL or 0.
14346 */
14347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350{
14351 if (varp != NULL)
14352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014353 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014355 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014356 func_unref(varp->vval.v_string);
14357 /*FALLTHROUGH*/
14358 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014359 vim_free(varp->vval.v_string);
14360 varp->vval.v_string = NULL;
14361 break;
14362 case VAR_LIST:
14363 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014364 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014366 case VAR_DICT:
14367 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014368 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014369 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014371 varp->vval.v_number = 0;
14372 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 case VAR_UNKNOWN:
14374 break;
14375 default:
14376 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014378 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 }
14380}
14381
14382/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383 * Set the value of a variable to NULL without freeing items.
14384 */
14385 static void
14386init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014387 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388{
14389 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014390 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391}
14392
14393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 * Get the number value of a variable.
14395 * If it is a String variable, uses vim_str2nr().
14396 */
14397 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014399 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014401 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014403 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014405 case VAR_NUMBER:
14406 n = (long)(varp->vval.v_number);
14407 break;
14408 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014409 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410 break;
14411 case VAR_STRING:
14412 if (varp->vval.v_string != NULL)
14413 vim_str2nr(varp->vval.v_string, NULL, NULL,
14414 TRUE, TRUE, &n, NULL);
14415 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014416 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014417 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014418 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014419 case VAR_DICT:
14420 EMSG(_("E728: Using a Dictionary as a number"));
14421 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014422 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014423 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427}
14428
14429/*
14430 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14431 */
14432 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014434 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
Bram Moolenaar33570922005-01-25 22:26:29 +000014436 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 linenr_T lnum;
14438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 if (lnum == 0) /* no valid number, try using line() */
14441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442 rettv.v_type = VAR_NUMBER;
14443 f_line(argvars, &rettv);
14444 lnum = rettv.vval.v_number;
14445 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 }
14447 return lnum;
14448}
14449
14450/*
14451 * Get the string value of a variable.
14452 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014453 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14454 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 * If the String variable has never been set, return an empty string.
14456 * Never returns NULL;
14457 */
14458 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014460 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461{
14462 static char_u mybuf[NUMBUFLEN];
14463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
14467 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014469 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014470 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014474 case VAR_NUMBER:
14475 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14476 return buf;
14477 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014478 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014479 break;
14480 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014481 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014482 break;
14483 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014484 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014485 break;
14486 case VAR_STRING:
14487 if (varp->vval.v_string != NULL)
14488 return varp->vval.v_string;
14489 break;
14490 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014494 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495}
14496
14497/*
14498 * Find variable "name" in the list of variables.
14499 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014500 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014501 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014502 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014504 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014505find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014507 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
Bram Moolenaara7043832005-01-21 11:56:39 +000014512 ht = find_var_ht(name, &varname);
14513 if (htp != NULL)
14514 *htp = ht;
14515 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014517 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518}
14519
14520/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014522 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014525find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014526 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014527 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014528 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014529{
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 hashitem_T *hi;
14531
14532 if (*varname == NUL)
14533 {
14534 /* Must be something like "s:", otherwise "ht" would be NULL. */
14535 switch (varname[-2])
14536 {
14537 case 's': return &SCRIPT_SV(current_SID).sv_var;
14538 case 'g': return &globvars_var;
14539 case 'v': return &vimvars_var;
14540 case 'b': return &curbuf->b_bufvar;
14541 case 'w': return &curwin->w_winvar;
14542 case 'l': return &current_funccal->l_vars_var;
14543 case 'a': return &current_funccal->l_avars_var;
14544 }
14545 return NULL;
14546 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014547
14548 hi = hash_find(ht, varname);
14549 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014550 {
14551 /* For global variables we may try auto-loading the script. If it
14552 * worked find the variable again. */
14553 if (ht == &globvarht && !writing
14554 && script_autoload(varname) && !aborting())
14555 hi = hash_find(ht, varname);
14556 if (HASHITEM_EMPTY(hi))
14557 return NULL;
14558 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014560}
14561
14562/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014563 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014564 * Set "varname" to the start of name without ':'.
14565 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014566 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014567find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 char_u *name;
14569 char_u **varname;
14570{
14571 if (name[1] != ':')
14572 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014573 /* The name must not start with a colon. */
14574 if (name[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 return NULL;
14576 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014577
14578 /* "version" is "v:version" in all scopes */
14579 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14580 return &compat_hashtab;
14581
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 return &globvarht; /* global variable */
14584 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 }
14586 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014587 if (*name == 'g') /* global variable */
14588 return &globvarht;
14589 /* There must be no ':' in the rest of the name, unless g: is used */
14590 if (vim_strchr(name + 2, ':') != NULL)
14591 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 if (*name == 'v') /* v: variable */
14597 return &vimvarht;
14598 if (*name == 'a' && current_funccal != NULL) /* function argument */
14599 return &current_funccal->l_avars.dv_hashtab;
14600 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14601 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 if (*name == 's' /* script variable */
14603 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14604 return &SCRIPT_VARS(current_SID);
14605 return NULL;
14606}
14607
14608/*
14609 * Get the string value of a (global/local) variable.
14610 * Returns NULL when it doesn't exist.
14611 */
14612 char_u *
14613get_var_value(name)
14614 char_u *name;
14615{
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617
Bram Moolenaara7043832005-01-21 11:56:39 +000014618 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 if (v == NULL)
14620 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622}
14623
14624/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 * sourcing this script and when executing functions defined in the script.
14627 */
14628 void
14629new_script_vars(id)
14630 scid_T id;
14631{
Bram Moolenaara7043832005-01-21 11:56:39 +000014632 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 hashtab_T *ht;
14634 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014635
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14637 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014638 /* Re-allocating ga_data means that an ht_array pointing to
14639 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014640 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014641 for (i = 1; i <= ga_scripts.ga_len; ++i)
14642 {
14643 ht = &SCRIPT_VARS(i);
14644 if (ht->ht_mask == HT_INIT_SIZE - 1)
14645 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 sv = &SCRIPT_SV(i);
14647 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014648 }
14649
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 while (ga_scripts.ga_len < id)
14651 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014652 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14653 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 }
14656 }
14657}
14658
14659/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14661 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 */
14663 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014664init_var_dict(dict, dict_var)
14665 dict_T *dict;
14666 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667{
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 hash_init(&dict->dv_hashtab);
14669 dict->dv_refcount = 99999;
14670 dict_var->di_tv.vval.v_dict = dict;
14671 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014672 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014673 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14674 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675}
14676
14677/*
14678 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014679 * Frees all allocated variables and the value they contain.
14680 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 */
14682 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014683vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014684 hashtab_T *ht;
14685{
14686 vars_clear_ext(ht, TRUE);
14687}
14688
14689/*
14690 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14691 */
14692 static void
14693vars_clear_ext(ht, free_val)
14694 hashtab_T *ht;
14695 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696{
Bram Moolenaara7043832005-01-21 11:56:39 +000014697 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014698 hashitem_T *hi;
14699 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700
Bram Moolenaar33570922005-01-25 22:26:29 +000014701 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014702 todo = ht->ht_used;
14703 for (hi = ht->ht_array; todo > 0; ++hi)
14704 {
14705 if (!HASHITEM_EMPTY(hi))
14706 {
14707 --todo;
14708
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014710 * ht_array might change then. hash_clear() takes care of it
14711 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014712 v = HI2DI(hi);
14713 if (free_val)
14714 clear_tv(&v->di_tv);
14715 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14716 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014717 }
14718 }
14719 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720}
14721
Bram Moolenaara7043832005-01-21 11:56:39 +000014722/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014723 * Delete a variable from hashtab "ht" at item "hi".
14724 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014727delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014728 hashtab_T *ht;
14729 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730{
Bram Moolenaar33570922005-01-25 22:26:29 +000014731 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014732
14733 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014734 clear_tv(&di->di_tv);
14735 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736}
14737
14738/*
14739 * List the value of one internal variable.
14740 */
14741 static void
14742list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 char_u *prefix;
14745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 char_u *tofree;
14747 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014748 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014749
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 s = echo_string(&v->di_tv, &tofree, numbuf);
14751 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014752 s == NULL ? (char_u *)"" : s);
14753 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754}
14755
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 static void
14757list_one_var_a(prefix, name, type, string)
14758 char_u *prefix;
14759 char_u *name;
14760 int type;
14761 char_u *string;
14762{
14763 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14764 if (name != NULL) /* "a:" vars don't have a name stored */
14765 msg_puts(name);
14766 msg_putchar(' ');
14767 msg_advance(22);
14768 if (type == VAR_NUMBER)
14769 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014770 else if (type == VAR_FUNC)
14771 msg_putchar('*');
14772 else if (type == VAR_LIST)
14773 {
14774 msg_putchar('[');
14775 if (*string == '[')
14776 ++string;
14777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014778 else if (type == VAR_DICT)
14779 {
14780 msg_putchar('{');
14781 if (*string == '{')
14782 ++string;
14783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 else
14785 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014786
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014788
14789 if (type == VAR_FUNC)
14790 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791}
14792
14793/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 * If the variable already exists, the value is updated.
14796 * Otherwise the variable is created.
14797 */
14798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014799set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803{
Bram Moolenaar33570922005-01-25 22:26:29 +000014804 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014808 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014809 {
14810 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14811 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14812 ? name[2] : name[0]))
14813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014814 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014815 return;
14816 }
14817 if (function_exists(name))
14818 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014819 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14820 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014821 return;
14822 }
14823 }
14824
Bram Moolenaara7043832005-01-21 11:56:39 +000014825 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014826 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014827 {
14828 EMSG2(_("E461: Illegal variable name: %s"), name);
14829 return;
14830 }
14831
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014832 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014836 if (var_check_ro(v->di_flags, name)
14837 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014838 return;
14839 if (v->di_tv.v_type != tv->v_type
14840 && !((v->di_tv.v_type == VAR_STRING
14841 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014842 && (tv->v_type == VAR_STRING
14843 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014844 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014845 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846 return;
14847 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014848
14849 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014850 * Handle setting internal v: variables separately: we don't change
14851 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014852 */
14853 if (ht == &vimvarht)
14854 {
14855 if (v->di_tv.v_type == VAR_STRING)
14856 {
14857 vim_free(v->di_tv.vval.v_string);
14858 if (copy || tv->v_type != VAR_STRING)
14859 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14860 else
14861 {
14862 /* Take over the string to avoid an extra alloc/free. */
14863 v->di_tv.vval.v_string = tv->vval.v_string;
14864 tv->vval.v_string = NULL;
14865 }
14866 }
14867 else if (v->di_tv.v_type != VAR_NUMBER)
14868 EMSG2(_(e_intern2), "set_var()");
14869 else
14870 v->di_tv.vval.v_number = get_tv_number(tv);
14871 return;
14872 }
14873
14874 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 }
14876 else /* add a new variable */
14877 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014878 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14879 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014880 if (v == NULL)
14881 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014882 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014885 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 return;
14887 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014888 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014892 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014893 else
14894 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014895 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014896 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899}
14900
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014901/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014902 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14903 * Also give an error message.
14904 */
14905 static int
14906var_check_ro(flags, name)
14907 int flags;
14908 char_u *name;
14909{
14910 if (flags & DI_FLAGS_RO)
14911 {
14912 EMSG2(_(e_readonlyvar), name);
14913 return TRUE;
14914 }
14915 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14916 {
14917 EMSG2(_(e_readonlysbx), name);
14918 return TRUE;
14919 }
14920 return FALSE;
14921}
14922
14923/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014924 * Return TRUE if typeval "tv" is set to be locked (immutable).
14925 * Also give an error message, using "name".
14926 */
14927 static int
14928tv_check_lock(lock, name)
14929 int lock;
14930 char_u *name;
14931{
14932 if (lock & VAR_LOCKED)
14933 {
14934 EMSG2(_("E741: Value is locked: %s"),
14935 name == NULL ? (char_u *)_("Unknown") : name);
14936 return TRUE;
14937 }
14938 if (lock & VAR_FIXED)
14939 {
14940 EMSG2(_("E742: Cannot change value of %s"),
14941 name == NULL ? (char_u *)_("Unknown") : name);
14942 return TRUE;
14943 }
14944 return FALSE;
14945}
14946
14947/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014948 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014949 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014950 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *from;
14955 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014957 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014958 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014959 switch (from->v_type)
14960 {
14961 case VAR_NUMBER:
14962 to->vval.v_number = from->vval.v_number;
14963 break;
14964 case VAR_STRING:
14965 case VAR_FUNC:
14966 if (from->vval.v_string == NULL)
14967 to->vval.v_string = NULL;
14968 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014969 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014970 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014971 if (from->v_type == VAR_FUNC)
14972 func_ref(to->vval.v_string);
14973 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014974 break;
14975 case VAR_LIST:
14976 if (from->vval.v_list == NULL)
14977 to->vval.v_list = NULL;
14978 else
14979 {
14980 to->vval.v_list = from->vval.v_list;
14981 ++to->vval.v_list->lv_refcount;
14982 }
14983 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014984 case VAR_DICT:
14985 if (from->vval.v_dict == NULL)
14986 to->vval.v_dict = NULL;
14987 else
14988 {
14989 to->vval.v_dict = from->vval.v_dict;
14990 ++to->vval.v_dict->dv_refcount;
14991 }
14992 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014993 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014995 break;
14996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997}
14998
14999/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015000 * Make a copy of an item.
15001 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015002 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15003 * reference to an already copied list/dict can be used.
15004 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015005 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015006 static int
15007item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015008 typval_T *from;
15009 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015010 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015011 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015012{
15013 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015014 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015015
Bram Moolenaar33570922005-01-25 22:26:29 +000015016 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015017 {
15018 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015019 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015020 }
15021 ++recurse;
15022
15023 switch (from->v_type)
15024 {
15025 case VAR_NUMBER:
15026 case VAR_STRING:
15027 case VAR_FUNC:
15028 copy_tv(from, to);
15029 break;
15030 case VAR_LIST:
15031 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015032 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015033 if (from->vval.v_list == NULL)
15034 to->vval.v_list = NULL;
15035 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15036 {
15037 /* use the copy made earlier */
15038 to->vval.v_list = from->vval.v_list->lv_copylist;
15039 ++to->vval.v_list->lv_refcount;
15040 }
15041 else
15042 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15043 if (to->vval.v_list == NULL)
15044 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015045 break;
15046 case VAR_DICT:
15047 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015048 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015049 if (from->vval.v_dict == NULL)
15050 to->vval.v_dict = NULL;
15051 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15052 {
15053 /* use the copy made earlier */
15054 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15055 ++to->vval.v_dict->dv_refcount;
15056 }
15057 else
15058 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15059 if (to->vval.v_dict == NULL)
15060 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015061 break;
15062 default:
15063 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015064 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015065 }
15066 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015067 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015068}
15069
15070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 * ":echo expr1 ..." print each argument separated with a space, add a
15072 * newline at the end.
15073 * ":echon expr1 ..." print each argument plain.
15074 */
15075 void
15076ex_echo(eap)
15077 exarg_T *eap;
15078{
15079 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015080 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015081 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 char_u *p;
15083 int needclr = TRUE;
15084 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015085 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
15087 if (eap->skip)
15088 ++emsg_skip;
15089 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15090 {
15091 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 {
15094 /*
15095 * Report the invalid expression unless the expression evaluation
15096 * has been cancelled due to an aborting error, an interrupt, or an
15097 * exception.
15098 */
15099 if (!aborting())
15100 EMSG2(_(e_invexpr2), p);
15101 break;
15102 }
15103 if (!eap->skip)
15104 {
15105 if (atstart)
15106 {
15107 atstart = FALSE;
15108 /* Call msg_start() after eval1(), evaluating the expression
15109 * may cause a message to appear. */
15110 if (eap->cmdidx == CMD_echo)
15111 msg_start();
15112 }
15113 else if (eap->cmdidx == CMD_echo)
15114 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015115 p = echo_string(&rettv, &tofree, numbuf);
15116 if (p != NULL)
15117 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015119 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015121 if (*p != TAB && needclr)
15122 {
15123 /* remove any text still there from the command */
15124 msg_clr_eos();
15125 needclr = FALSE;
15126 }
15127 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 }
15129 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015130 {
15131#ifdef FEAT_MBYTE
15132 if (has_mbyte)
15133 {
15134 int i = (*mb_ptr2len_check)(p);
15135
15136 (void)msg_outtrans_len_attr(p, i, echo_attr);
15137 p += i - 1;
15138 }
15139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015141 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015144 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 arg = skipwhite(arg);
15148 }
15149 eap->nextcmd = check_nextcmd(arg);
15150
15151 if (eap->skip)
15152 --emsg_skip;
15153 else
15154 {
15155 /* remove text that may still be there from the command */
15156 if (needclr)
15157 msg_clr_eos();
15158 if (eap->cmdidx == CMD_echo)
15159 msg_end();
15160 }
15161}
15162
15163/*
15164 * ":echohl {name}".
15165 */
15166 void
15167ex_echohl(eap)
15168 exarg_T *eap;
15169{
15170 int id;
15171
15172 id = syn_name2id(eap->arg);
15173 if (id == 0)
15174 echo_attr = 0;
15175 else
15176 echo_attr = syn_id2attr(id);
15177}
15178
15179/*
15180 * ":execute expr1 ..." execute the result of an expression.
15181 * ":echomsg expr1 ..." Print a message
15182 * ":echoerr expr1 ..." Print an error
15183 * Each gets spaces around each argument and a newline at the end for
15184 * echo commands
15185 */
15186 void
15187ex_execute(eap)
15188 exarg_T *eap;
15189{
15190 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 int ret = OK;
15193 char_u *p;
15194 garray_T ga;
15195 int len;
15196 int save_did_emsg;
15197
15198 ga_init2(&ga, 1, 80);
15199
15200 if (eap->skip)
15201 ++emsg_skip;
15202 while (*arg != NUL && *arg != '|' && *arg != '\n')
15203 {
15204 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 {
15207 /*
15208 * Report the invalid expression unless the expression evaluation
15209 * has been cancelled due to an aborting error, an interrupt, or an
15210 * exception.
15211 */
15212 if (!aborting())
15213 EMSG2(_(e_invexpr2), p);
15214 ret = FAIL;
15215 break;
15216 }
15217
15218 if (!eap->skip)
15219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015220 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 len = (int)STRLEN(p);
15222 if (ga_grow(&ga, len + 2) == FAIL)
15223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015224 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 ret = FAIL;
15226 break;
15227 }
15228 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 ga.ga_len += len;
15232 }
15233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015234 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 arg = skipwhite(arg);
15236 }
15237
15238 if (ret != FAIL && ga.ga_data != NULL)
15239 {
15240 if (eap->cmdidx == CMD_echomsg)
15241 MSG_ATTR(ga.ga_data, echo_attr);
15242 else if (eap->cmdidx == CMD_echoerr)
15243 {
15244 /* We don't want to abort following commands, restore did_emsg. */
15245 save_did_emsg = did_emsg;
15246 EMSG((char_u *)ga.ga_data);
15247 if (!force_abort)
15248 did_emsg = save_did_emsg;
15249 }
15250 else if (eap->cmdidx == CMD_execute)
15251 do_cmdline((char_u *)ga.ga_data,
15252 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15253 }
15254
15255 ga_clear(&ga);
15256
15257 if (eap->skip)
15258 --emsg_skip;
15259
15260 eap->nextcmd = check_nextcmd(arg);
15261}
15262
15263/*
15264 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15265 * "arg" points to the "&" or '+' when called, to "option" when returning.
15266 * Returns NULL when no option name found. Otherwise pointer to the char
15267 * after the option name.
15268 */
15269 static char_u *
15270find_option_end(arg, opt_flags)
15271 char_u **arg;
15272 int *opt_flags;
15273{
15274 char_u *p = *arg;
15275
15276 ++p;
15277 if (*p == 'g' && p[1] == ':')
15278 {
15279 *opt_flags = OPT_GLOBAL;
15280 p += 2;
15281 }
15282 else if (*p == 'l' && p[1] == ':')
15283 {
15284 *opt_flags = OPT_LOCAL;
15285 p += 2;
15286 }
15287 else
15288 *opt_flags = 0;
15289
15290 if (!ASCII_ISALPHA(*p))
15291 return NULL;
15292 *arg = p;
15293
15294 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15295 p += 4; /* termcap option */
15296 else
15297 while (ASCII_ISALPHA(*p))
15298 ++p;
15299 return p;
15300}
15301
15302/*
15303 * ":function"
15304 */
15305 void
15306ex_function(eap)
15307 exarg_T *eap;
15308{
15309 char_u *theline;
15310 int j;
15311 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 char_u *name = NULL;
15314 char_u *p;
15315 char_u *arg;
15316 garray_T newargs;
15317 garray_T newlines;
15318 int varargs = FALSE;
15319 int mustend = FALSE;
15320 int flags = 0;
15321 ufunc_T *fp;
15322 int indent;
15323 int nesting;
15324 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015325 dictitem_T *v;
15326 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015327 static int func_nr = 0; /* number for nameless function */
15328 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015329 hashtab_T *ht;
15330 int todo;
15331 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332
15333 /*
15334 * ":function" without argument: list functions.
15335 */
15336 if (ends_excmd(*eap->arg))
15337 {
15338 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015339 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015340 todo = func_hashtab.ht_used;
15341 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015342 {
15343 if (!HASHITEM_EMPTY(hi))
15344 {
15345 --todo;
15346 fp = HI2UF(hi);
15347 if (!isdigit(*fp->uf_name))
15348 list_func_head(fp, FALSE);
15349 }
15350 }
15351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 eap->nextcmd = check_nextcmd(eap->arg);
15353 return;
15354 }
15355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015356 /*
15357 * Get the function name. There are these situations:
15358 * func normal function name
15359 * "name" == func, "fudi.fd_dict" == NULL
15360 * dict.func new dictionary entry
15361 * "name" == NULL, "fudi.fd_dict" set,
15362 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15363 * dict.func existing dict entry with a Funcref
15364 * "name" == fname, "fudi.fd_dict" set,
15365 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15366 * dict.func existing dict entry that's not a Funcref
15367 * "name" == NULL, "fudi.fd_dict" set,
15368 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015371 name = trans_function_name(&p, eap->skip, 0, &fudi);
15372 paren = (vim_strchr(p, '(') != NULL);
15373 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 {
15375 /*
15376 * Return on an invalid expression in braces, unless the expression
15377 * evaluation has been cancelled due to an aborting error, an
15378 * interrupt, or an exception.
15379 */
15380 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015382 if (!eap->skip && fudi.fd_newkey != NULL)
15383 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015384 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 else
15388 eap->skip = TRUE;
15389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 /* An error in a function call during evaluation of an expression in magic
15391 * braces should not cause the function not to be defined. */
15392 saved_did_emsg = did_emsg;
15393 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394
15395 /*
15396 * ":function func" with only function name: list function.
15397 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015398 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 {
15400 if (!ends_excmd(*skipwhite(p)))
15401 {
15402 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015403 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 }
15405 eap->nextcmd = check_nextcmd(p);
15406 if (eap->nextcmd != NULL)
15407 *p = NUL;
15408 if (!eap->skip && !got_int)
15409 {
15410 fp = find_func(name);
15411 if (fp != NULL)
15412 {
15413 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015414 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 {
15416 msg_putchar('\n');
15417 msg_outnum((long)(j + 1));
15418 if (j < 9)
15419 msg_putchar(' ');
15420 if (j < 99)
15421 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015422 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 out_flush(); /* show a line at a time */
15424 ui_breakcheck();
15425 }
15426 if (!got_int)
15427 {
15428 msg_putchar('\n');
15429 msg_puts((char_u *)" endfunction");
15430 }
15431 }
15432 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015433 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015435 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 }
15437
15438 /*
15439 * ":function name(arg1, arg2)" Define function.
15440 */
15441 p = skipwhite(p);
15442 if (*p != '(')
15443 {
15444 if (!eap->skip)
15445 {
15446 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015447 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 }
15449 /* attempt to continue by skipping some text */
15450 if (vim_strchr(p, '(') != NULL)
15451 p = vim_strchr(p, '(');
15452 }
15453 p = skipwhite(p + 1);
15454
15455 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15456 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15457
15458 /*
15459 * Isolate the arguments: "arg1, arg2, ...)"
15460 */
15461 while (*p != ')')
15462 {
15463 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15464 {
15465 varargs = TRUE;
15466 p += 3;
15467 mustend = TRUE;
15468 }
15469 else
15470 {
15471 arg = p;
15472 while (ASCII_ISALNUM(*p) || *p == '_')
15473 ++p;
15474 if (arg == p || isdigit(*arg)
15475 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15476 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15477 {
15478 if (!eap->skip)
15479 EMSG2(_("E125: Illegal argument: %s"), arg);
15480 break;
15481 }
15482 if (ga_grow(&newargs, 1) == FAIL)
15483 goto erret;
15484 c = *p;
15485 *p = NUL;
15486 arg = vim_strsave(arg);
15487 if (arg == NULL)
15488 goto erret;
15489 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15490 *p = c;
15491 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 if (*p == ',')
15493 ++p;
15494 else
15495 mustend = TRUE;
15496 }
15497 p = skipwhite(p);
15498 if (mustend && *p != ')')
15499 {
15500 if (!eap->skip)
15501 EMSG2(_(e_invarg2), eap->arg);
15502 break;
15503 }
15504 }
15505 ++p; /* skip the ')' */
15506
Bram Moolenaare9a41262005-01-15 22:18:47 +000015507 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 for (;;)
15509 {
15510 p = skipwhite(p);
15511 if (STRNCMP(p, "range", 5) == 0)
15512 {
15513 flags |= FC_RANGE;
15514 p += 5;
15515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015516 else if (STRNCMP(p, "dict", 4) == 0)
15517 {
15518 flags |= FC_DICT;
15519 p += 4;
15520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 else if (STRNCMP(p, "abort", 5) == 0)
15522 {
15523 flags |= FC_ABORT;
15524 p += 5;
15525 }
15526 else
15527 break;
15528 }
15529
15530 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15531 EMSG(_(e_trailing));
15532
15533 /*
15534 * Read the body of the function, until ":endfunction" is found.
15535 */
15536 if (KeyTyped)
15537 {
15538 /* Check if the function already exists, don't let the user type the
15539 * whole function before telling him it doesn't work! For a script we
15540 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015541 if (!eap->skip && !eap->forceit)
15542 {
15543 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15544 EMSG(_(e_funcdict));
15545 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015546 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548
15549 msg_putchar('\n'); /* don't overwrite the function name */
15550 cmdline_row = msg_row;
15551 }
15552
15553 indent = 2;
15554 nesting = 0;
15555 for (;;)
15556 {
15557 msg_scroll = TRUE;
15558 need_wait_return = FALSE;
15559 if (eap->getline == NULL)
15560 theline = getcmdline(':', 0L, indent);
15561 else
15562 theline = eap->getline(':', eap->cookie, indent);
15563 if (KeyTyped)
15564 lines_left = Rows - 1;
15565 if (theline == NULL)
15566 {
15567 EMSG(_("E126: Missing :endfunction"));
15568 goto erret;
15569 }
15570
15571 if (skip_until != NULL)
15572 {
15573 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15574 * don't check for ":endfunc". */
15575 if (STRCMP(theline, skip_until) == 0)
15576 {
15577 vim_free(skip_until);
15578 skip_until = NULL;
15579 }
15580 }
15581 else
15582 {
15583 /* skip ':' and blanks*/
15584 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15585 ;
15586
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015587 /* Check for "endfunction". */
15588 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 {
15590 vim_free(theline);
15591 break;
15592 }
15593
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015594 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 * at "end". */
15596 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15597 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015598 else if (STRNCMP(p, "if", 2) == 0
15599 || STRNCMP(p, "wh", 2) == 0
15600 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 || STRNCMP(p, "try", 3) == 0)
15602 indent += 2;
15603
15604 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015605 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015607 if (*p == '!')
15608 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 p += eval_fname_script(p);
15610 if (ASCII_ISALPHA(*p))
15611 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015612 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 if (*skipwhite(p) == '(')
15614 {
15615 ++nesting;
15616 indent += 2;
15617 }
15618 }
15619 }
15620
15621 /* Check for ":append" or ":insert". */
15622 p = skip_range(p, NULL);
15623 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15624 || (p[0] == 'i'
15625 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15626 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15627 skip_until = vim_strsave((char_u *)".");
15628
15629 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15630 arg = skipwhite(skiptowhite(p));
15631 if (arg[0] == '<' && arg[1] =='<'
15632 && ((p[0] == 'p' && p[1] == 'y'
15633 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15634 || (p[0] == 'p' && p[1] == 'e'
15635 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15636 || (p[0] == 't' && p[1] == 'c'
15637 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15638 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15639 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015640 || (p[0] == 'm' && p[1] == 'z'
15641 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 ))
15643 {
15644 /* ":python <<" continues until a dot, like ":append" */
15645 p = skipwhite(arg + 2);
15646 if (*p == NUL)
15647 skip_until = vim_strsave((char_u *)".");
15648 else
15649 skip_until = vim_strsave(p);
15650 }
15651 }
15652
15653 /* Add the line to the function. */
15654 if (ga_grow(&newlines, 1) == FAIL)
15655 goto erret;
15656 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15657 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 }
15659
15660 /* Don't define the function when skipping commands or when an error was
15661 * detected. */
15662 if (eap->skip || did_emsg)
15663 goto erret;
15664
15665 /*
15666 * If there are no errors, add the function
15667 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015668 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015669 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015670 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015672 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015673 emsg_funcname("E707: Function name conflicts with variable: %s",
15674 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015675 goto erret;
15676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015678 fp = find_func(name);
15679 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015681 if (!eap->forceit)
15682 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015683 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015684 goto erret;
15685 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015686 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015687 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015688 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015689 name);
15690 goto erret;
15691 }
15692 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015693 ga_clear_strings(&(fp->uf_args));
15694 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015695 vim_free(name);
15696 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 }
15699 else
15700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015701 char numbuf[20];
15702
15703 fp = NULL;
15704 if (fudi.fd_newkey == NULL && !eap->forceit)
15705 {
15706 EMSG(_(e_funcdict));
15707 goto erret;
15708 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015709 if (fudi.fd_di == NULL)
15710 {
15711 /* Can't add a function to a locked dictionary */
15712 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15713 goto erret;
15714 }
15715 /* Can't change an existing function if it is locked */
15716 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15717 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015718
15719 /* Give the function a sequential number. Can only be used with a
15720 * Funcref! */
15721 vim_free(name);
15722 sprintf(numbuf, "%d", ++func_nr);
15723 name = vim_strsave((char_u *)numbuf);
15724 if (name == NULL)
15725 goto erret;
15726 }
15727
15728 if (fp == NULL)
15729 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015730 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL)
15731 {
15732 int slen, plen;
15733 char_u *scriptname;
15734
15735 /* Check that the autoload name matches the script name. */
15736 j = FAIL;
15737 if (sourcing_name != NULL)
15738 {
15739 scriptname = autoload_name(name);
15740 if (scriptname != NULL)
15741 {
15742 p = vim_strchr(scriptname, '/');
15743 plen = STRLEN(p);
15744 slen = STRLEN(sourcing_name);
15745 if (slen > plen && fnamecmp(p,
15746 sourcing_name + slen - plen) == 0)
15747 j = OK;
15748 vim_free(scriptname);
15749 }
15750 }
15751 if (j == FAIL)
15752 {
15753 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15754 goto erret;
15755 }
15756 }
15757
15758 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 if (fp == NULL)
15760 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015761
15762 if (fudi.fd_dict != NULL)
15763 {
15764 if (fudi.fd_di == NULL)
15765 {
15766 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015767 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015768 if (fudi.fd_di == NULL)
15769 {
15770 vim_free(fp);
15771 goto erret;
15772 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015773 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15774 {
15775 vim_free(fudi.fd_di);
15776 goto erret;
15777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015778 }
15779 else
15780 /* overwrite existing dict entry */
15781 clear_tv(&fudi.fd_di->di_tv);
15782 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015783 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015784 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015785 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015786 }
15787
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015789 STRCPY(fp->uf_name, name);
15790 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015792 fp->uf_args = newargs;
15793 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015794#ifdef FEAT_PROFILE
15795 fp->uf_tml_count = NULL;
15796 fp->uf_tml_total = NULL;
15797 fp->uf_tml_self = NULL;
15798 fp->uf_profiling = FALSE;
15799 if (prof_def_func())
15800 func_do_profile(fp);
15801#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015802 fp->uf_varargs = varargs;
15803 fp->uf_flags = flags;
15804 fp->uf_calls = 0;
15805 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015806 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807
15808erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 ga_clear_strings(&newargs);
15810 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015811ret_free:
15812 vim_free(skip_until);
15813 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816}
15817
15818/*
15819 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015820 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015822 * flags:
15823 * TFN_INT: internal function name OK
15824 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825 * Advances "pp" to just after the function name (if no error).
15826 */
15827 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015828trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 char_u **pp;
15830 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015831 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015832 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015834 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 char_u *start;
15836 char_u *end;
15837 int lead;
15838 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015840 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015841
15842 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015845
15846 /* Check for hard coded <SNR>: already translated function ID (from a user
15847 * command). */
15848 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15849 && (*pp)[2] == (int)KE_SNR)
15850 {
15851 *pp += 3;
15852 len = get_id_len(pp) + 3;
15853 return vim_strnsave(start, len);
15854 }
15855
15856 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15857 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015859 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015861
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015862 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015863 if (end == start)
15864 {
15865 if (!skip)
15866 EMSG(_("E129: Function name required"));
15867 goto theend;
15868 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015869 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015870 {
15871 /*
15872 * Report an invalid expression in braces, unless the expression
15873 * evaluation has been cancelled due to an aborting error, an
15874 * interrupt, or an exception.
15875 */
15876 if (!aborting())
15877 {
15878 if (end != NULL)
15879 EMSG2(_(e_invarg2), start);
15880 }
15881 else
15882 *pp = find_name_end(start, NULL, NULL, TRUE);
15883 goto theend;
15884 }
15885
15886 if (lv.ll_tv != NULL)
15887 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015888 if (fdp != NULL)
15889 {
15890 fdp->fd_dict = lv.ll_dict;
15891 fdp->fd_newkey = lv.ll_newkey;
15892 lv.ll_newkey = NULL;
15893 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015895 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15896 {
15897 name = vim_strsave(lv.ll_tv->vval.v_string);
15898 *pp = end;
15899 }
15900 else
15901 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015902 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15903 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015904 EMSG(_(e_funcref));
15905 else
15906 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015907 name = NULL;
15908 }
15909 goto theend;
15910 }
15911
15912 if (lv.ll_name == NULL)
15913 {
15914 /* Error found, but continue after the function name. */
15915 *pp = end;
15916 goto theend;
15917 }
15918
15919 if (lv.ll_exp_name != NULL)
15920 len = STRLEN(lv.ll_exp_name);
15921 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015922 {
15923 if (lead == 2) /* skip over "s:" */
15924 lv.ll_name += 2;
15925 len = (int)(end - lv.ll_name);
15926 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015927
15928 /*
15929 * Copy the function name to allocated memory.
15930 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15931 * Accept <SNR>123_name() outside a script.
15932 */
15933 if (skip)
15934 lead = 0; /* do nothing */
15935 else if (lead > 0)
15936 {
15937 lead = 3;
15938 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
15939 {
15940 if (current_SID <= 0)
15941 {
15942 EMSG(_(e_usingsid));
15943 goto theend;
15944 }
15945 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
15946 lead += (int)STRLEN(sid_buf);
15947 }
15948 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015949 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015950 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015951 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015952 goto theend;
15953 }
15954 name = alloc((unsigned)(len + lead + 1));
15955 if (name != NULL)
15956 {
15957 if (lead > 0)
15958 {
15959 name[0] = K_SPECIAL;
15960 name[1] = KS_EXTRA;
15961 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000015962 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015963 STRCPY(name + 3, sid_buf);
15964 }
15965 mch_memmove(name + lead, lv.ll_name, (size_t)len);
15966 name[len + lead] = NUL;
15967 }
15968 *pp = end;
15969
15970theend:
15971 clear_lval(&lv);
15972 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973}
15974
15975/*
15976 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
15977 * Return 2 if "p" starts with "s:".
15978 * Return 0 otherwise.
15979 */
15980 static int
15981eval_fname_script(p)
15982 char_u *p;
15983{
15984 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
15985 || STRNICMP(p + 1, "SNR>", 4) == 0))
15986 return 5;
15987 if (p[0] == 's' && p[1] == ':')
15988 return 2;
15989 return 0;
15990}
15991
15992/*
15993 * Return TRUE if "p" starts with "<SID>" or "s:".
15994 * Only works if eval_fname_script() returned non-zero for "p"!
15995 */
15996 static int
15997eval_fname_sid(p)
15998 char_u *p;
15999{
16000 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16001}
16002
16003/*
16004 * List the head of the function: "name(arg1, arg2)".
16005 */
16006 static void
16007list_func_head(fp, indent)
16008 ufunc_T *fp;
16009 int indent;
16010{
16011 int j;
16012
16013 msg_start();
16014 if (indent)
16015 MSG_PUTS(" ");
16016 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016017 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 {
16019 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016020 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 }
16022 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016023 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016025 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 {
16027 if (j)
16028 MSG_PUTS(", ");
16029 msg_puts(FUNCARG(fp, j));
16030 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016031 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 {
16033 if (j)
16034 MSG_PUTS(", ");
16035 MSG_PUTS("...");
16036 }
16037 msg_putchar(')');
16038}
16039
16040/*
16041 * Find a function by name, return pointer to it in ufuncs.
16042 * Return NULL for unknown function.
16043 */
16044 static ufunc_T *
16045find_func(name)
16046 char_u *name;
16047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016048 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016050 hi = hash_find(&func_hashtab, name);
16051 if (!HASHITEM_EMPTY(hi))
16052 return HI2UF(hi);
16053 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054}
16055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016056/*
16057 * Return TRUE if a function "name" exists.
16058 */
16059 static int
16060function_exists(name)
16061 char_u *name;
16062{
16063 char_u *p = name;
16064 int n = FALSE;
16065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016066 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016067 if (p != NULL)
16068 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016069 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016070 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016071 else
16072 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016073 vim_free(p);
16074 }
16075 return n;
16076}
16077
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016078/*
16079 * Return TRUE if "name" looks like a builtin function name: starts with a
16080 * lower case letter and doesn't contain a ':'.
16081 */
16082 static int
16083builtin_function(name)
16084 char_u *name;
16085{
16086 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
16087}
16088
Bram Moolenaar05159a02005-02-26 23:04:13 +000016089#if defined(FEAT_PROFILE) || defined(PROTO)
16090/*
16091 * Start profiling function "fp".
16092 */
16093 static void
16094func_do_profile(fp)
16095 ufunc_T *fp;
16096{
16097 fp->uf_tm_count = 0;
16098 profile_zero(&fp->uf_tm_self);
16099 profile_zero(&fp->uf_tm_total);
16100 if (fp->uf_tml_count == NULL)
16101 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16102 (sizeof(int) * fp->uf_lines.ga_len));
16103 if (fp->uf_tml_total == NULL)
16104 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16105 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16106 if (fp->uf_tml_self == NULL)
16107 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16108 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16109 fp->uf_tml_idx = -1;
16110 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16111 || fp->uf_tml_self == NULL)
16112 return; /* out of memory */
16113
16114 fp->uf_profiling = TRUE;
16115}
16116
16117/*
16118 * Dump the profiling results for all functions in file "fd".
16119 */
16120 void
16121func_dump_profile(fd)
16122 FILE *fd;
16123{
16124 hashitem_T *hi;
16125 int todo;
16126 ufunc_T *fp;
16127 int i;
16128
16129 todo = func_hashtab.ht_used;
16130 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16131 {
16132 if (!HASHITEM_EMPTY(hi))
16133 {
16134 --todo;
16135 fp = HI2UF(hi);
16136 if (fp->uf_profiling)
16137 {
16138 if (fp->uf_name[0] == K_SPECIAL)
16139 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16140 else
16141 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16142 if (fp->uf_tm_count == 1)
16143 fprintf(fd, "Called 1 time\n");
16144 else
16145 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16146 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16147 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16148 fprintf(fd, "\n");
16149 fprintf(fd, "count total (s) self (s)\n");
16150
16151 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16152 {
16153 if (fp->uf_tml_count[i] > 0)
16154 {
16155 fprintf(fd, "%5d ", fp->uf_tml_count[i]);
16156 if (profile_equal(&fp->uf_tml_total[i],
16157 &fp->uf_tml_self[i]))
16158 fprintf(fd, " ");
16159 else
16160 fprintf(fd, "%s ",
16161 profile_msg(&fp->uf_tml_total[i]));
16162 fprintf(fd, "%s ", profile_msg(&fp->uf_tml_self[i]));
16163 }
16164 else
16165 fprintf(fd, " ");
16166 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16167 }
16168 fprintf(fd, "\n");
16169 }
16170 }
16171 }
16172}
16173#endif
16174
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016175/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016176 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016177 * Return TRUE if a package was loaded.
16178 */
16179 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016180script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016181 char_u *name;
16182{
16183 char_u *p;
16184 char_u *scriptname;
16185 int ret = FALSE;
16186
16187 /* If there is no colon after name[1] there is no package name. */
16188 p = vim_strchr(name, ':');
16189 if (p == NULL || p <= name + 2)
16190 return FALSE;
16191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016192 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16193 scriptname = autoload_name(name);
16194 if (cmd_runtime(scriptname, FALSE) == OK)
16195 ret = TRUE;
16196
16197 vim_free(scriptname);
16198 return ret;
16199}
16200
16201/*
16202 * Return the autoload script name for a function or variable name.
16203 * Returns NULL when out of memory.
16204 */
16205 static char_u *
16206autoload_name(name)
16207 char_u *name;
16208{
16209 char_u *p;
16210 char_u *scriptname;
16211
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016212 /* Get the script file name: replace ':' with '/', append ".vim". */
16213 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16214 if (scriptname == NULL)
16215 return FALSE;
16216 STRCPY(scriptname, "autoload/");
16217 STRCAT(scriptname, name);
16218 *vim_strrchr(scriptname, ':') = NUL;
16219 STRCAT(scriptname, ".vim");
16220 while ((p = vim_strchr(scriptname, ':')) != NULL)
16221 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016222 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016223}
16224
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16226
16227/*
16228 * Function given to ExpandGeneric() to obtain the list of user defined
16229 * function names.
16230 */
16231 char_u *
16232get_user_func_name(xp, idx)
16233 expand_T *xp;
16234 int idx;
16235{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016236 static long_u done;
16237 static hashitem_T *hi;
16238 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239
16240 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016242 done = 0;
16243 hi = func_hashtab.ht_array;
16244 }
16245 if (done < func_hashtab.ht_used)
16246 {
16247 if (done++ > 0)
16248 ++hi;
16249 while (HASHITEM_EMPTY(hi))
16250 ++hi;
16251 fp = HI2UF(hi);
16252
16253 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16254 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255
16256 cat_func_name(IObuff, fp);
16257 if (xp->xp_context != EXPAND_USER_FUNC)
16258 {
16259 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016260 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 STRCAT(IObuff, ")");
16262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 return IObuff;
16264 }
16265 return NULL;
16266}
16267
16268#endif /* FEAT_CMDL_COMPL */
16269
16270/*
16271 * Copy the function name of "fp" to buffer "buf".
16272 * "buf" must be able to hold the function name plus three bytes.
16273 * Takes care of script-local function names.
16274 */
16275 static void
16276cat_func_name(buf, fp)
16277 char_u *buf;
16278 ufunc_T *fp;
16279{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016280 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 {
16282 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016283 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 }
16285 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016286 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287}
16288
16289/*
16290 * ":delfunction {name}"
16291 */
16292 void
16293ex_delfunction(eap)
16294 exarg_T *eap;
16295{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016296 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 char_u *p;
16298 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300
16301 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016302 name = trans_function_name(&p, eap->skip, 0, &fudi);
16303 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016305 {
16306 if (fudi.fd_dict != NULL && !eap->skip)
16307 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 if (!ends_excmd(*skipwhite(p)))
16311 {
16312 vim_free(name);
16313 EMSG(_(e_trailing));
16314 return;
16315 }
16316 eap->nextcmd = check_nextcmd(p);
16317 if (eap->nextcmd != NULL)
16318 *p = NUL;
16319
16320 if (!eap->skip)
16321 fp = find_func(name);
16322 vim_free(name);
16323
16324 if (!eap->skip)
16325 {
16326 if (fp == NULL)
16327 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016328 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 return;
16330 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016331 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 {
16333 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16334 return;
16335 }
16336
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016337 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016339 /* Delete the dict item that refers to the function, it will
16340 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016341 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016343 else
16344 func_free(fp);
16345 }
16346}
16347
16348/*
16349 * Free a function and remove it from the list of functions.
16350 */
16351 static void
16352func_free(fp)
16353 ufunc_T *fp;
16354{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016355 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016356
16357 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016358 ga_clear_strings(&(fp->uf_args));
16359 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016360#ifdef FEAT_PROFILE
16361 vim_free(fp->uf_tml_count);
16362 vim_free(fp->uf_tml_total);
16363 vim_free(fp->uf_tml_self);
16364#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016366 /* remove the function from the function hashtable */
16367 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16368 if (HASHITEM_EMPTY(hi))
16369 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016370 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016371 hash_remove(&func_hashtab, hi);
16372
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016373 vim_free(fp);
16374}
16375
16376/*
16377 * Unreference a Function: decrement the reference count and free it when it
16378 * becomes zero. Only for numbered functions.
16379 */
16380 static void
16381func_unref(name)
16382 char_u *name;
16383{
16384 ufunc_T *fp;
16385
16386 if (name != NULL && isdigit(*name))
16387 {
16388 fp = find_func(name);
16389 if (fp == NULL)
16390 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016391 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016392 {
16393 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016394 * when "uf_calls" becomes zero. */
16395 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016396 func_free(fp);
16397 }
16398 }
16399}
16400
16401/*
16402 * Count a reference to a Function.
16403 */
16404 static void
16405func_ref(name)
16406 char_u *name;
16407{
16408 ufunc_T *fp;
16409
16410 if (name != NULL && isdigit(*name))
16411 {
16412 fp = find_func(name);
16413 if (fp == NULL)
16414 EMSG2(_(e_intern2), "func_ref()");
16415 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016416 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 }
16418}
16419
16420/*
16421 * Call a user function.
16422 */
16423 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016424call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 ufunc_T *fp; /* pointer to function */
16426 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016427 typval_T *argvars; /* arguments */
16428 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 linenr_T firstline; /* first line of range */
16430 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432{
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 char_u *save_sourcing_name;
16434 linenr_T save_sourcing_lnum;
16435 scid_T save_current_SID;
16436 funccall_T fc;
16437 funccall_T *save_fcp = current_funccal;
16438 int save_did_emsg;
16439 static int depth = 0;
16440 dictitem_T *v;
16441 int fixvar_idx = 0; /* index in fixvar[] */
16442 int i;
16443 int ai;
16444 char_u numbuf[NUMBUFLEN];
16445 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016446#ifdef FEAT_PROFILE
16447 proftime_T wait_start;
16448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449
16450 /* If depth of calling is getting too high, don't execute the function */
16451 if (depth >= p_mfd)
16452 {
16453 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 rettv->v_type = VAR_NUMBER;
16455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 return;
16457 }
16458 ++depth;
16459
16460 line_breakcheck(); /* check for CTRL-C hit */
16461
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464 fc.rettv = rettv;
16465 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 fc.linenr = 0;
16467 fc.returned = FALSE;
16468 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016470 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 fc.dbg_tick = debug_tick;
16472
Bram Moolenaar33570922005-01-25 22:26:29 +000016473 /*
16474 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16475 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16476 * each argument variable and saves a lot of time.
16477 */
16478 /*
16479 * Init l: variables.
16480 */
16481 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016482 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016483 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 /* Set l:self to "selfdict". */
16485 v = &fc.fixvar[fixvar_idx++].var;
16486 STRCPY(v->di_key, "self");
16487 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16488 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16489 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016490 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016491 v->di_tv.vval.v_dict = selfdict;
16492 ++selfdict->dv_refcount;
16493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016494
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 /*
16496 * Init a: variables.
16497 * Set a:0 to "argcount".
16498 * Set a:000 to a list with room for the "..." arguments.
16499 */
16500 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16501 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016502 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016503 v = &fc.fixvar[fixvar_idx++].var;
16504 STRCPY(v->di_key, "000");
16505 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16506 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16507 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016508 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 v->di_tv.vval.v_list = &fc.l_varlist;
16510 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16511 fc.l_varlist.lv_refcount = 99999;
16512
16513 /*
16514 * Set a:firstline to "firstline" and a:lastline to "lastline".
16515 * Set a:name to named arguments.
16516 * Set a:N to the "..." arguments.
16517 */
16518 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16519 (varnumber_T)firstline);
16520 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16521 (varnumber_T)lastline);
16522 for (i = 0; i < argcount; ++i)
16523 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016524 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016525 if (ai < 0)
16526 /* named argument a:name */
16527 name = FUNCARG(fp, i);
16528 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016529 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016530 /* "..." argument a:1, a:2, etc. */
16531 sprintf((char *)numbuf, "%d", ai + 1);
16532 name = numbuf;
16533 }
16534 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16535 {
16536 v = &fc.fixvar[fixvar_idx++].var;
16537 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16538 }
16539 else
16540 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016541 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16542 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016543 if (v == NULL)
16544 break;
16545 v->di_flags = DI_FLAGS_RO;
16546 }
16547 STRCPY(v->di_key, name);
16548 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16549
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016550 /* Note: the values are copied directly to avoid alloc/free.
16551 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016553 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016554
16555 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16556 {
16557 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16558 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016559 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016560 }
16561 }
16562
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563 /* Don't redraw while executing the function. */
16564 ++RedrawingDisabled;
16565 save_sourcing_name = sourcing_name;
16566 save_sourcing_lnum = sourcing_lnum;
16567 sourcing_lnum = 1;
16568 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016569 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 if (sourcing_name != NULL)
16571 {
16572 if (save_sourcing_name != NULL
16573 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16574 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16575 else
16576 STRCPY(sourcing_name, "function ");
16577 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16578
16579 if (p_verbose >= 12)
16580 {
16581 ++no_wait_return;
16582 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16583 msg_str((char_u *)_("calling %s"), sourcing_name);
16584 if (p_verbose >= 14)
16585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016587 char_u numbuf[NUMBUFLEN];
16588 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589
16590 msg_puts((char_u *)"(");
16591 for (i = 0; i < argcount; ++i)
16592 {
16593 if (i > 0)
16594 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016595 if (argvars[i].v_type == VAR_NUMBER)
16596 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 else
16598 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016599 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016602 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 }
16604 }
16605 msg_puts((char_u *)")");
16606 }
16607 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16608 cmdline_row = msg_row;
16609 --no_wait_return;
16610 }
16611 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016612#ifdef FEAT_PROFILE
16613 if (do_profiling)
16614 {
16615 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16616 func_do_profile(fp);
16617 if (fp->uf_profiling
16618 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16619 {
16620 ++fp->uf_tm_count;
16621 profile_start(&fp->uf_tm_start);
16622 profile_zero(&fp->uf_tm_children);
16623 }
16624 script_prof_save(&wait_start);
16625 }
16626#endif
16627
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016629 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 save_did_emsg = did_emsg;
16631 did_emsg = FALSE;
16632
16633 /* call do_cmdline() to execute the lines */
16634 do_cmdline(NULL, get_func_line, (void *)&fc,
16635 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16636
16637 --RedrawingDisabled;
16638
16639 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016640 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 clear_tv(rettv);
16643 rettv->v_type = VAR_NUMBER;
16644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 }
16646
Bram Moolenaar05159a02005-02-26 23:04:13 +000016647#ifdef FEAT_PROFILE
16648 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16649 {
16650 profile_end(&fp->uf_tm_start);
16651 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16652 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16653 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16654 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16655 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16656 {
16657 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16658 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16659 }
16660 }
16661#endif
16662
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 /* when being verbose, mention the return value */
16664 if (p_verbose >= 12)
16665 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016666 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667
16668 ++no_wait_return;
16669 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16670
16671 /* Make sure the output fits in IObuff. */
16672 sn = sourcing_name;
16673 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16674 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16675
16676 if (aborting())
16677 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016683 char_u buf[MSG_BUF_LEN];
16684 char_u numbuf[NUMBUFLEN];
16685 char_u *tofree;
16686
16687 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16688 buf, MSG_BUF_LEN);
16689 smsg((char_u *)_("%s returning %s"), sn, buf);
16690 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 }
16692 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16693 cmdline_row = msg_row;
16694 --no_wait_return;
16695 }
16696
16697 vim_free(sourcing_name);
16698 sourcing_name = save_sourcing_name;
16699 sourcing_lnum = save_sourcing_lnum;
16700 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016701#ifdef FEAT_PROFILE
16702 if (do_profiling)
16703 script_prof_restore(&wait_start);
16704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705
16706 if (p_verbose >= 12 && sourcing_name != NULL)
16707 {
16708 ++no_wait_return;
16709 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16710 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16711 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16712 cmdline_row = msg_row;
16713 --no_wait_return;
16714 }
16715
16716 did_emsg |= save_did_emsg;
16717 current_funccal = save_fcp;
16718
Bram Moolenaar33570922005-01-25 22:26:29 +000016719 /* The a: variables typevals were not alloced, only free the allocated
16720 * variables. */
16721 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16722
16723 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 --depth;
16725}
16726
16727/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016728 * Add a number variable "name" to dict "dp" with value "nr".
16729 */
16730 static void
16731add_nr_var(dp, v, name, nr)
16732 dict_T *dp;
16733 dictitem_T *v;
16734 char *name;
16735 varnumber_T nr;
16736{
16737 STRCPY(v->di_key, name);
16738 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16739 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16740 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016741 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016742 v->di_tv.vval.v_number = nr;
16743}
16744
16745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 * ":return [expr]"
16747 */
16748 void
16749ex_return(eap)
16750 exarg_T *eap;
16751{
16752 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016753 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 int returning = FALSE;
16755
16756 if (current_funccal == NULL)
16757 {
16758 EMSG(_("E133: :return not inside a function"));
16759 return;
16760 }
16761
16762 if (eap->skip)
16763 ++emsg_skip;
16764
16765 eap->nextcmd = NULL;
16766 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 {
16769 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 }
16774 /* It's safer to return also on error. */
16775 else if (!eap->skip)
16776 {
16777 /*
16778 * Return unless the expression evaluation has been cancelled due to an
16779 * aborting error, an interrupt, or an exception.
16780 */
16781 if (!aborting())
16782 returning = do_return(eap, FALSE, TRUE, NULL);
16783 }
16784
16785 /* When skipping or the return gets pending, advance to the next command
16786 * in this line (!returning). Otherwise, ignore the rest of the line.
16787 * Following lines will be ignored by get_func_line(). */
16788 if (returning)
16789 eap->nextcmd = NULL;
16790 else if (eap->nextcmd == NULL) /* no argument */
16791 eap->nextcmd = check_nextcmd(arg);
16792
16793 if (eap->skip)
16794 --emsg_skip;
16795}
16796
16797/*
16798 * Return from a function. Possibly makes the return pending. Also called
16799 * for a pending return at the ":endtry" or after returning from an extra
16800 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016801 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016802 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 * FALSE when the return gets pending.
16804 */
16805 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016806do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 exarg_T *eap;
16808 int reanimate;
16809 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811{
16812 int idx;
16813 struct condstack *cstack = eap->cstack;
16814
16815 if (reanimate)
16816 /* Undo the return. */
16817 current_funccal->returned = FALSE;
16818
16819 /*
16820 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16821 * not in its finally clause (which then is to be executed next) is found.
16822 * In this case, make the ":return" pending for execution at the ":endtry".
16823 * Otherwise, return normally.
16824 */
16825 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16826 if (idx >= 0)
16827 {
16828 cstack->cs_pending[idx] = CSTP_RETURN;
16829
16830 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016831 /* A pending return again gets pending. "rettv" points to an
16832 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016834 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 else
16836 {
16837 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 {
16844 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016846 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 else
16848 EMSG(_(e_outofmem));
16849 }
16850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852
16853 if (reanimate)
16854 {
16855 /* The pending return value could be overwritten by a ":return"
16856 * without argument in a finally clause; reset the default
16857 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016858 current_funccal->rettv->v_type = VAR_NUMBER;
16859 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 }
16861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016862 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 }
16864 else
16865 {
16866 current_funccal->returned = TRUE;
16867
16868 /* If the return is carried out now, store the return value. For
16869 * a return immediately after reanimation, the value is already
16870 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016871 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016873 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016876 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 }
16878 }
16879
16880 return idx < 0;
16881}
16882
16883/*
16884 * Free the variable with a pending return value.
16885 */
16886 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016887discard_pending_return(rettv)
16888 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889{
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891}
16892
16893/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016894 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 * is an allocated string. Used by report_pending() for verbose messages.
16896 */
16897 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016898get_return_cmd(rettv)
16899 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016901 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016902 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016903 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016905 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016907 if (s == NULL)
16908 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016909
16910 STRCPY(IObuff, ":return ");
16911 STRNCPY(IObuff + 8, s, IOSIZE - 8);
16912 if (STRLEN(s) + 8 >= IOSIZE)
16913 STRCPY(IObuff + IOSIZE - 4, "...");
16914 vim_free(tofree);
16915 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916}
16917
16918/*
16919 * Get next function line.
16920 * Called by do_cmdline() to get the next line.
16921 * Returns allocated string, or NULL for end of function.
16922 */
16923/* ARGSUSED */
16924 char_u *
16925get_func_line(c, cookie, indent)
16926 int c; /* not used */
16927 void *cookie;
16928 int indent; /* not used */
16929{
Bram Moolenaar33570922005-01-25 22:26:29 +000016930 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016931 ufunc_T *fp = fcp->func;
16932 char_u *retval;
16933 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934
16935 /* If breakpoints have been added/deleted need to check for it. */
16936 if (fcp->dbg_tick != debug_tick)
16937 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016938 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 sourcing_lnum);
16940 fcp->dbg_tick = debug_tick;
16941 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016942#ifdef FEAT_PROFILE
16943 if (do_profiling)
16944 func_line_end(cookie);
16945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946
Bram Moolenaar05159a02005-02-26 23:04:13 +000016947 gap = &fp->uf_lines;
16948 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 retval = NULL;
16950 else if (fcp->returned || fcp->linenr >= gap->ga_len)
16951 retval = NULL;
16952 else
16953 {
16954 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
16955 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016956#ifdef FEAT_PROFILE
16957 if (do_profiling)
16958 func_line_start(cookie);
16959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 }
16961
16962 /* Did we encounter a breakpoint? */
16963 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
16964 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016965 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016967 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 sourcing_lnum);
16969 fcp->dbg_tick = debug_tick;
16970 }
16971
16972 return retval;
16973}
16974
Bram Moolenaar05159a02005-02-26 23:04:13 +000016975#if defined(FEAT_PROFILE) || defined(PROTO)
16976/*
16977 * Called when starting to read a function line.
16978 * "sourcing_lnum" must be correct!
16979 * When skipping lines it may not actually be executed, but we won't find out
16980 * until later and we need to store the time now.
16981 */
16982 void
16983func_line_start(cookie)
16984 void *cookie;
16985{
16986 funccall_T *fcp = (funccall_T *)cookie;
16987 ufunc_T *fp = fcp->func;
16988
16989 if (fp->uf_profiling && sourcing_lnum >= 1
16990 && sourcing_lnum <= fp->uf_lines.ga_len)
16991 {
16992 fp->uf_tml_idx = sourcing_lnum - 1;
16993 fp->uf_tml_execed = FALSE;
16994 profile_start(&fp->uf_tml_start);
16995 profile_zero(&fp->uf_tml_children);
16996 profile_get_wait(&fp->uf_tml_wait);
16997 }
16998}
16999
17000/*
17001 * Called when actually executing a function line.
17002 */
17003 void
17004func_line_exec(cookie)
17005 void *cookie;
17006{
17007 funccall_T *fcp = (funccall_T *)cookie;
17008 ufunc_T *fp = fcp->func;
17009
17010 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17011 fp->uf_tml_execed = TRUE;
17012}
17013
17014/*
17015 * Called when done with a function line.
17016 */
17017 void
17018func_line_end(cookie)
17019 void *cookie;
17020{
17021 funccall_T *fcp = (funccall_T *)cookie;
17022 ufunc_T *fp = fcp->func;
17023
17024 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17025 {
17026 if (fp->uf_tml_execed)
17027 {
17028 ++fp->uf_tml_count[fp->uf_tml_idx];
17029 profile_end(&fp->uf_tml_start);
17030 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17031 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17032 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17033 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17034 }
17035 fp->uf_tml_idx = -1;
17036 }
17037}
17038#endif
17039
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040/*
17041 * Return TRUE if the currently active function should be ended, because a
17042 * return was encountered or an error occured. Used inside a ":while".
17043 */
17044 int
17045func_has_ended(cookie)
17046 void *cookie;
17047{
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049
17050 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17051 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017052 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 || fcp->returned);
17054}
17055
17056/*
17057 * return TRUE if cookie indicates a function which "abort"s on errors.
17058 */
17059 int
17060func_has_abort(cookie)
17061 void *cookie;
17062{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017063 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064}
17065
17066#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17067typedef enum
17068{
17069 VAR_FLAVOUR_DEFAULT,
17070 VAR_FLAVOUR_SESSION,
17071 VAR_FLAVOUR_VIMINFO
17072} var_flavour_T;
17073
17074static var_flavour_T var_flavour __ARGS((char_u *varname));
17075
17076 static var_flavour_T
17077var_flavour(varname)
17078 char_u *varname;
17079{
17080 char_u *p = varname;
17081
17082 if (ASCII_ISUPPER(*p))
17083 {
17084 while (*(++p))
17085 if (ASCII_ISLOWER(*p))
17086 return VAR_FLAVOUR_SESSION;
17087 return VAR_FLAVOUR_VIMINFO;
17088 }
17089 else
17090 return VAR_FLAVOUR_DEFAULT;
17091}
17092#endif
17093
17094#if defined(FEAT_VIMINFO) || defined(PROTO)
17095/*
17096 * Restore global vars that start with a capital from the viminfo file
17097 */
17098 int
17099read_viminfo_varlist(virp, writing)
17100 vir_T *virp;
17101 int writing;
17102{
17103 char_u *tab;
17104 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
17107 if (!writing && (find_viminfo_parameter('!') != NULL))
17108 {
17109 tab = vim_strchr(virp->vir_line + 1, '\t');
17110 if (tab != NULL)
17111 {
17112 *tab++ = '\0'; /* isolate the variable name */
17113 if (*tab == 'S') /* string var */
17114 is_string = TRUE;
17115
17116 tab = vim_strchr(tab, '\t');
17117 if (tab != NULL)
17118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 if (is_string)
17120 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017121 tv.v_type = VAR_STRING;
17122 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 }
17125 else
17126 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017127 tv.v_type = VAR_NUMBER;
17128 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017130 set_var(virp->vir_line + 1, &tv, FALSE);
17131 if (is_string)
17132 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 }
17134 }
17135 }
17136
17137 return viminfo_readline(virp);
17138}
17139
17140/*
17141 * Write global vars that start with a capital to the viminfo file
17142 */
17143 void
17144write_viminfo_varlist(fp)
17145 FILE *fp;
17146{
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 hashitem_T *hi;
17148 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017149 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017151 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017152 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017153 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154
17155 if (find_viminfo_parameter('!') == NULL)
17156 return;
17157
17158 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017159
Bram Moolenaar33570922005-01-25 22:26:29 +000017160 todo = globvarht.ht_used;
17161 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017163 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017165 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017166 this_var = HI2DI(hi);
17167 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017169 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017170 {
17171 case VAR_STRING: s = "STR"; break;
17172 case VAR_NUMBER: s = "NUM"; break;
17173 default: continue;
17174 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017176 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17177 if (p != NULL)
17178 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017179 vim_free(tofree);
17180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 }
17182 }
17183}
17184#endif
17185
17186#if defined(FEAT_SESSION) || defined(PROTO)
17187 int
17188store_session_globals(fd)
17189 FILE *fd;
17190{
Bram Moolenaar33570922005-01-25 22:26:29 +000017191 hashitem_T *hi;
17192 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017193 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194 char_u *p, *t;
17195
Bram Moolenaar33570922005-01-25 22:26:29 +000017196 todo = globvarht.ht_used;
17197 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017199 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017201 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017202 this_var = HI2DI(hi);
17203 if ((this_var->di_tv.v_type == VAR_NUMBER
17204 || this_var->di_tv.v_type == VAR_STRING)
17205 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017206 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017207 /* Escape special characters with a backslash. Turn a LF and
17208 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017209 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017210 (char_u *)"\\\"\n\r");
17211 if (p == NULL) /* out of memory */
17212 break;
17213 for (t = p; *t != NUL; ++t)
17214 if (*t == '\n')
17215 *t = 'n';
17216 else if (*t == '\r')
17217 *t = 'r';
17218 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017219 this_var->di_key,
17220 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17221 : ' ',
17222 p,
17223 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17224 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017225 || put_eol(fd) == FAIL)
17226 {
17227 vim_free(p);
17228 return FAIL;
17229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 vim_free(p);
17231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 }
17233 }
17234 return OK;
17235}
17236#endif
17237
17238#endif /* FEAT_EVAL */
17239
17240#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17241
17242
17243#ifdef WIN3264
17244/*
17245 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17246 */
17247static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17248static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17249static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17250
17251/*
17252 * Get the short pathname of a file.
17253 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17254 */
17255 static int
17256get_short_pathname(fnamep, bufp, fnamelen)
17257 char_u **fnamep;
17258 char_u **bufp;
17259 int *fnamelen;
17260{
17261 int l,len;
17262 char_u *newbuf;
17263
17264 len = *fnamelen;
17265
17266 l = GetShortPathName(*fnamep, *fnamep, len);
17267 if (l > len - 1)
17268 {
17269 /* If that doesn't work (not enough space), then save the string
17270 * and try again with a new buffer big enough
17271 */
17272 newbuf = vim_strnsave(*fnamep, l);
17273 if (newbuf == NULL)
17274 return 0;
17275
17276 vim_free(*bufp);
17277 *fnamep = *bufp = newbuf;
17278
17279 l = GetShortPathName(*fnamep,*fnamep,l+1);
17280
17281 /* Really should always succeed, as the buffer is big enough */
17282 }
17283
17284 *fnamelen = l;
17285 return 1;
17286}
17287
17288/*
17289 * Create a short path name. Returns the length of the buffer it needs.
17290 * Doesn't copy over the end of the buffer passed in.
17291 */
17292 static int
17293shortpath_for_invalid_fname(fname, bufp, fnamelen)
17294 char_u **fname;
17295 char_u **bufp;
17296 int *fnamelen;
17297{
17298 char_u *s, *p, *pbuf2, *pbuf3;
17299 char_u ch;
17300 int l,len,len2,plen,slen;
17301
17302 /* Make a copy */
17303 len2 = *fnamelen;
17304 pbuf2 = vim_strnsave(*fname, len2);
17305 pbuf3 = NULL;
17306
17307 s = pbuf2 + len2 - 1; /* Find the end */
17308 slen = 1;
17309 plen = len2;
17310
17311 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017312 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 {
17314 --s;
17315 ++slen;
17316 --plen;
17317 }
17318
17319 do
17320 {
17321 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017322 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 {
17324 --s;
17325 ++slen;
17326 --plen;
17327 }
17328 if (s <= pbuf2)
17329 break;
17330
17331 /* Remeber the character that is about to be blatted */
17332 ch = *s;
17333 *s = 0; /* get_short_pathname requires a null-terminated string */
17334
17335 /* Try it in situ */
17336 p = pbuf2;
17337 if (!get_short_pathname(&p, &pbuf3, &plen))
17338 {
17339 vim_free(pbuf2);
17340 return -1;
17341 }
17342 *s = ch; /* Preserve the string */
17343 } while (plen == 0);
17344
17345 if (plen > 0)
17346 {
17347 /* Remeber the length of the new string. */
17348 *fnamelen = len = plen + slen;
17349 vim_free(*bufp);
17350 if (len > len2)
17351 {
17352 /* If there's not enough space in the currently allocated string,
17353 * then copy it to a buffer big enough.
17354 */
17355 *fname= *bufp = vim_strnsave(p, len);
17356 if (*fname == NULL)
17357 return -1;
17358 }
17359 else
17360 {
17361 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17362 *fname = *bufp = pbuf2;
17363 if (p != pbuf2)
17364 strncpy(*fname, p, plen);
17365 pbuf2 = NULL;
17366 }
17367 /* Concat the next bit */
17368 strncpy(*fname + plen, s, slen);
17369 (*fname)[len] = '\0';
17370 }
17371 vim_free(pbuf3);
17372 vim_free(pbuf2);
17373 return 0;
17374}
17375
17376/*
17377 * Get a pathname for a partial path.
17378 */
17379 static int
17380shortpath_for_partial(fnamep, bufp, fnamelen)
17381 char_u **fnamep;
17382 char_u **bufp;
17383 int *fnamelen;
17384{
17385 int sepcount, len, tflen;
17386 char_u *p;
17387 char_u *pbuf, *tfname;
17388 int hasTilde;
17389
17390 /* Count up the path seperators from the RHS.. so we know which part
17391 * of the path to return.
17392 */
17393 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017394 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 if (vim_ispathsep(*p))
17396 ++sepcount;
17397
17398 /* Need full path first (use expand_env() to remove a "~/") */
17399 hasTilde = (**fnamep == '~');
17400 if (hasTilde)
17401 pbuf = tfname = expand_env_save(*fnamep);
17402 else
17403 pbuf = tfname = FullName_save(*fnamep, FALSE);
17404
17405 len = tflen = STRLEN(tfname);
17406
17407 if (!get_short_pathname(&tfname, &pbuf, &len))
17408 return -1;
17409
17410 if (len == 0)
17411 {
17412 /* Don't have a valid filename, so shorten the rest of the
17413 * path if we can. This CAN give us invalid 8.3 filenames, but
17414 * there's not a lot of point in guessing what it might be.
17415 */
17416 len = tflen;
17417 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17418 return -1;
17419 }
17420
17421 /* Count the paths backward to find the beginning of the desired string. */
17422 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017423 {
17424#ifdef FEAT_MBYTE
17425 if (has_mbyte)
17426 p -= mb_head_off(tfname, p);
17427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 if (vim_ispathsep(*p))
17429 {
17430 if (sepcount == 0 || (hasTilde && sepcount == 1))
17431 break;
17432 else
17433 sepcount --;
17434 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 if (hasTilde)
17437 {
17438 --p;
17439 if (p >= tfname)
17440 *p = '~';
17441 else
17442 return -1;
17443 }
17444 else
17445 ++p;
17446
17447 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17448 vim_free(*bufp);
17449 *fnamelen = (int)STRLEN(p);
17450 *bufp = pbuf;
17451 *fnamep = p;
17452
17453 return 0;
17454}
17455#endif /* WIN3264 */
17456
17457/*
17458 * Adjust a filename, according to a string of modifiers.
17459 * *fnamep must be NUL terminated when called. When returning, the length is
17460 * determined by *fnamelen.
17461 * Returns valid flags.
17462 * When there is an error, *fnamep is set to NULL.
17463 */
17464 int
17465modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17466 char_u *src; /* string with modifiers */
17467 int *usedlen; /* characters after src that are used */
17468 char_u **fnamep; /* file name so far */
17469 char_u **bufp; /* buffer for allocated file name or NULL */
17470 int *fnamelen; /* length of fnamep */
17471{
17472 int valid = 0;
17473 char_u *tail;
17474 char_u *s, *p, *pbuf;
17475 char_u dirname[MAXPATHL];
17476 int c;
17477 int has_fullname = 0;
17478#ifdef WIN3264
17479 int has_shortname = 0;
17480#endif
17481
17482repeat:
17483 /* ":p" - full path/file_name */
17484 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17485 {
17486 has_fullname = 1;
17487
17488 valid |= VALID_PATH;
17489 *usedlen += 2;
17490
17491 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17492 if ((*fnamep)[0] == '~'
17493#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17494 && ((*fnamep)[1] == '/'
17495# ifdef BACKSLASH_IN_FILENAME
17496 || (*fnamep)[1] == '\\'
17497# endif
17498 || (*fnamep)[1] == NUL)
17499
17500#endif
17501 )
17502 {
17503 *fnamep = expand_env_save(*fnamep);
17504 vim_free(*bufp); /* free any allocated file name */
17505 *bufp = *fnamep;
17506 if (*fnamep == NULL)
17507 return -1;
17508 }
17509
17510 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017511 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 {
17513 if (vim_ispathsep(*p)
17514 && p[1] == '.'
17515 && (p[2] == NUL
17516 || vim_ispathsep(p[2])
17517 || (p[2] == '.'
17518 && (p[3] == NUL || vim_ispathsep(p[3])))))
17519 break;
17520 }
17521
17522 /* FullName_save() is slow, don't use it when not needed. */
17523 if (*p != NUL || !vim_isAbsName(*fnamep))
17524 {
17525 *fnamep = FullName_save(*fnamep, *p != NUL);
17526 vim_free(*bufp); /* free any allocated file name */
17527 *bufp = *fnamep;
17528 if (*fnamep == NULL)
17529 return -1;
17530 }
17531
17532 /* Append a path separator to a directory. */
17533 if (mch_isdir(*fnamep))
17534 {
17535 /* Make room for one or two extra characters. */
17536 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17537 vim_free(*bufp); /* free any allocated file name */
17538 *bufp = *fnamep;
17539 if (*fnamep == NULL)
17540 return -1;
17541 add_pathsep(*fnamep);
17542 }
17543 }
17544
17545 /* ":." - path relative to the current directory */
17546 /* ":~" - path relative to the home directory */
17547 /* ":8" - shortname path - postponed till after */
17548 while (src[*usedlen] == ':'
17549 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17550 {
17551 *usedlen += 2;
17552 if (c == '8')
17553 {
17554#ifdef WIN3264
17555 has_shortname = 1; /* Postpone this. */
17556#endif
17557 continue;
17558 }
17559 pbuf = NULL;
17560 /* Need full path first (use expand_env() to remove a "~/") */
17561 if (!has_fullname)
17562 {
17563 if (c == '.' && **fnamep == '~')
17564 p = pbuf = expand_env_save(*fnamep);
17565 else
17566 p = pbuf = FullName_save(*fnamep, FALSE);
17567 }
17568 else
17569 p = *fnamep;
17570
17571 has_fullname = 0;
17572
17573 if (p != NULL)
17574 {
17575 if (c == '.')
17576 {
17577 mch_dirname(dirname, MAXPATHL);
17578 s = shorten_fname(p, dirname);
17579 if (s != NULL)
17580 {
17581 *fnamep = s;
17582 if (pbuf != NULL)
17583 {
17584 vim_free(*bufp); /* free any allocated file name */
17585 *bufp = pbuf;
17586 pbuf = NULL;
17587 }
17588 }
17589 }
17590 else
17591 {
17592 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17593 /* Only replace it when it starts with '~' */
17594 if (*dirname == '~')
17595 {
17596 s = vim_strsave(dirname);
17597 if (s != NULL)
17598 {
17599 *fnamep = s;
17600 vim_free(*bufp);
17601 *bufp = s;
17602 }
17603 }
17604 }
17605 vim_free(pbuf);
17606 }
17607 }
17608
17609 tail = gettail(*fnamep);
17610 *fnamelen = (int)STRLEN(*fnamep);
17611
17612 /* ":h" - head, remove "/file_name", can be repeated */
17613 /* Don't remove the first "/" or "c:\" */
17614 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17615 {
17616 valid |= VALID_HEAD;
17617 *usedlen += 2;
17618 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017619 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 --tail;
17621 *fnamelen = (int)(tail - *fnamep);
17622#ifdef VMS
17623 if (*fnamelen > 0)
17624 *fnamelen += 1; /* the path separator is part of the path */
17625#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017626 while (tail > s && !after_pathsep(s, tail))
17627 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 }
17629
17630 /* ":8" - shortname */
17631 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17632 {
17633 *usedlen += 2;
17634#ifdef WIN3264
17635 has_shortname = 1;
17636#endif
17637 }
17638
17639#ifdef WIN3264
17640 /* Check shortname after we have done 'heads' and before we do 'tails'
17641 */
17642 if (has_shortname)
17643 {
17644 pbuf = NULL;
17645 /* Copy the string if it is shortened by :h */
17646 if (*fnamelen < (int)STRLEN(*fnamep))
17647 {
17648 p = vim_strnsave(*fnamep, *fnamelen);
17649 if (p == 0)
17650 return -1;
17651 vim_free(*bufp);
17652 *bufp = *fnamep = p;
17653 }
17654
17655 /* Split into two implementations - makes it easier. First is where
17656 * there isn't a full name already, second is where there is.
17657 */
17658 if (!has_fullname && !vim_isAbsName(*fnamep))
17659 {
17660 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17661 return -1;
17662 }
17663 else
17664 {
17665 int l;
17666
17667 /* Simple case, already have the full-name
17668 * Nearly always shorter, so try first time. */
17669 l = *fnamelen;
17670 if (!get_short_pathname(fnamep, bufp, &l))
17671 return -1;
17672
17673 if (l == 0)
17674 {
17675 /* Couldn't find the filename.. search the paths.
17676 */
17677 l = *fnamelen;
17678 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17679 return -1;
17680 }
17681 *fnamelen = l;
17682 }
17683 }
17684#endif /* WIN3264 */
17685
17686 /* ":t" - tail, just the basename */
17687 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17688 {
17689 *usedlen += 2;
17690 *fnamelen -= (int)(tail - *fnamep);
17691 *fnamep = tail;
17692 }
17693
17694 /* ":e" - extension, can be repeated */
17695 /* ":r" - root, without extension, can be repeated */
17696 while (src[*usedlen] == ':'
17697 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17698 {
17699 /* find a '.' in the tail:
17700 * - for second :e: before the current fname
17701 * - otherwise: The last '.'
17702 */
17703 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17704 s = *fnamep - 2;
17705 else
17706 s = *fnamep + *fnamelen - 1;
17707 for ( ; s > tail; --s)
17708 if (s[0] == '.')
17709 break;
17710 if (src[*usedlen + 1] == 'e') /* :e */
17711 {
17712 if (s > tail)
17713 {
17714 *fnamelen += (int)(*fnamep - (s + 1));
17715 *fnamep = s + 1;
17716#ifdef VMS
17717 /* cut version from the extension */
17718 s = *fnamep + *fnamelen - 1;
17719 for ( ; s > *fnamep; --s)
17720 if (s[0] == ';')
17721 break;
17722 if (s > *fnamep)
17723 *fnamelen = s - *fnamep;
17724#endif
17725 }
17726 else if (*fnamep <= tail)
17727 *fnamelen = 0;
17728 }
17729 else /* :r */
17730 {
17731 if (s > tail) /* remove one extension */
17732 *fnamelen = (int)(s - *fnamep);
17733 }
17734 *usedlen += 2;
17735 }
17736
17737 /* ":s?pat?foo?" - substitute */
17738 /* ":gs?pat?foo?" - global substitute */
17739 if (src[*usedlen] == ':'
17740 && (src[*usedlen + 1] == 's'
17741 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17742 {
17743 char_u *str;
17744 char_u *pat;
17745 char_u *sub;
17746 int sep;
17747 char_u *flags;
17748 int didit = FALSE;
17749
17750 flags = (char_u *)"";
17751 s = src + *usedlen + 2;
17752 if (src[*usedlen + 1] == 'g')
17753 {
17754 flags = (char_u *)"g";
17755 ++s;
17756 }
17757
17758 sep = *s++;
17759 if (sep)
17760 {
17761 /* find end of pattern */
17762 p = vim_strchr(s, sep);
17763 if (p != NULL)
17764 {
17765 pat = vim_strnsave(s, (int)(p - s));
17766 if (pat != NULL)
17767 {
17768 s = p + 1;
17769 /* find end of substitution */
17770 p = vim_strchr(s, sep);
17771 if (p != NULL)
17772 {
17773 sub = vim_strnsave(s, (int)(p - s));
17774 str = vim_strnsave(*fnamep, *fnamelen);
17775 if (sub != NULL && str != NULL)
17776 {
17777 *usedlen = (int)(p + 1 - src);
17778 s = do_string_sub(str, pat, sub, flags);
17779 if (s != NULL)
17780 {
17781 *fnamep = s;
17782 *fnamelen = (int)STRLEN(s);
17783 vim_free(*bufp);
17784 *bufp = s;
17785 didit = TRUE;
17786 }
17787 }
17788 vim_free(sub);
17789 vim_free(str);
17790 }
17791 vim_free(pat);
17792 }
17793 }
17794 /* after using ":s", repeat all the modifiers */
17795 if (didit)
17796 goto repeat;
17797 }
17798 }
17799
17800 return valid;
17801}
17802
17803/*
17804 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
17805 * "flags" can be "g" to do a global substitute.
17806 * Returns an allocated string, NULL for error.
17807 */
17808 char_u *
17809do_string_sub(str, pat, sub, flags)
17810 char_u *str;
17811 char_u *pat;
17812 char_u *sub;
17813 char_u *flags;
17814{
17815 int sublen;
17816 regmatch_T regmatch;
17817 int i;
17818 int do_all;
17819 char_u *tail;
17820 garray_T ga;
17821 char_u *ret;
17822 char_u *save_cpo;
17823
17824 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
17825 save_cpo = p_cpo;
17826 p_cpo = (char_u *)"";
17827
17828 ga_init2(&ga, 1, 200);
17829
17830 do_all = (flags[0] == 'g');
17831
17832 regmatch.rm_ic = p_ic;
17833 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17834 if (regmatch.regprog != NULL)
17835 {
17836 tail = str;
17837 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
17838 {
17839 /*
17840 * Get some space for a temporary buffer to do the substitution
17841 * into. It will contain:
17842 * - The text up to where the match is.
17843 * - The substituted text.
17844 * - The text after the match.
17845 */
17846 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
17847 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
17848 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
17849 {
17850 ga_clear(&ga);
17851 break;
17852 }
17853
17854 /* copy the text up to where the match is */
17855 i = (int)(regmatch.startp[0] - tail);
17856 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
17857 /* add the substituted text */
17858 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
17859 + ga.ga_len + i, TRUE, TRUE, FALSE);
17860 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 /* avoid getting stuck on a match with an empty string */
17862 if (tail == regmatch.endp[0])
17863 {
17864 if (*tail == NUL)
17865 break;
17866 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
17867 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 }
17869 else
17870 {
17871 tail = regmatch.endp[0];
17872 if (*tail == NUL)
17873 break;
17874 }
17875 if (!do_all)
17876 break;
17877 }
17878
17879 if (ga.ga_data != NULL)
17880 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
17881
17882 vim_free(regmatch.regprog);
17883 }
17884
17885 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
17886 ga_clear(&ga);
17887 p_cpo = save_cpo;
17888
17889 return ret;
17890}
17891
17892#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */