blob: aa3935034af34f0d75fbb88dd24032c33c7f9c16 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000110
111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
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 Moolenaar4debb442005-06-01 21:57:40 +0000191static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
267#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
272 int vv_len; /* length of name */
273 dictitem_T vv_di; /* value and name for key */
274 char vv_filler[16]; /* space for LONGEST name below!!! */
275 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
276} vimvars[VV_LEN] =
277{
278 /*
279 * The order here must match the VV_ defines in vim.h!
280 * Initializing a union does not work, leave tv.vval empty to get zero's.
281 */
282 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
283 {VV_NAME("count1", VAR_NUMBER), VV_RO},
284 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
285 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
286 {VV_NAME("warningmsg", VAR_STRING), 0},
287 {VV_NAME("statusmsg", VAR_STRING), 0},
288 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
289 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
290 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
291 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
292 {VV_NAME("termresponse", VAR_STRING), VV_RO},
293 {VV_NAME("fname", VAR_STRING), VV_RO},
294 {VV_NAME("lang", VAR_STRING), VV_RO},
295 {VV_NAME("lc_time", VAR_STRING), VV_RO},
296 {VV_NAME("ctype", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
298 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
299 {VV_NAME("fname_in", VAR_STRING), VV_RO},
300 {VV_NAME("fname_out", VAR_STRING), VV_RO},
301 {VV_NAME("fname_new", VAR_STRING), VV_RO},
302 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
303 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
304 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
307 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
308 {VV_NAME("progname", VAR_STRING), VV_RO},
309 {VV_NAME("servername", VAR_STRING), VV_RO},
310 {VV_NAME("dying", VAR_NUMBER), VV_RO},
311 {VV_NAME("exception", VAR_STRING), VV_RO},
312 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
313 {VV_NAME("register", VAR_STRING), VV_RO},
314 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
315 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000316 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
317 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000318 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000319 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
320 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000321 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
322 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
323 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
324 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
325 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000326};
327
328/* shorthand */
329#define vv_type vv_di.di_tv.v_type
330#define vv_nr vv_di.di_tv.vval.v_number
331#define vv_str vv_di.di_tv.vval.v_string
332#define vv_tv vv_di.di_tv
333
334/*
335 * The v: variables are stored in dictionary "vimvardict".
336 * "vimvars_var" is the variable that is used for the "l:" scope.
337 */
338static dict_T vimvardict;
339static dictitem_T vimvars_var;
340#define vimvarht vimvardict.dv_hashtab
341
342static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
343static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
349static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000350static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000351static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355static list_T *list_alloc __ARGS((void));
356static void list_unref __ARGS((list_T *l));
357static void list_free __ARGS((list_T *l));
358static listitem_T *listitem_alloc __ARGS((void));
359static void listitem_free __ARGS((listitem_T *item));
360static void listitem_remove __ARGS((list_T *l, listitem_T *item));
361static long list_len __ARGS((list_T *l));
362static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
363static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
364static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
365static int string_isa_number __ARGS((char_u *s));
366static listitem_T *list_find __ARGS((list_T *l, long n));
367static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000368static void list_append __ARGS((list_T *l, listitem_T *item));
369static int list_append_tv __ARGS((list_T *l, typval_T *tv));
370static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
371static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
372static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000373static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000374static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
375static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000376static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar33570922005-01-25 22:26:29 +0000378static void dict_unref __ARGS((dict_T *d));
379static void dict_free __ARGS((dict_T *d));
380static dictitem_T *dictitem_alloc __ARGS((char_u *key));
381static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
382static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
383static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000384static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int dict_add __ARGS((dict_T *d, dictitem_T *item));
386static long dict_len __ARGS((dict_T *d));
387static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
388static char_u *dict2string __ARGS((typval_T *tv));
389static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390
391static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
392static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
393static char_u *string_quote __ARGS((char_u *str, int function));
394static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int find_internal_func __ARGS((char_u *name));
396static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
397static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
398static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000399static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400
401static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
425static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000464static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000492static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000509static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513#ifdef vim_mkdir
514static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000521static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000539static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
545#ifdef HAVE_STRFTIME
546static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
547#endif
548static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000560static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577
578static win_T *find_win_by_nr __ARGS((typval_T *vp));
579static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
580static int get_env_len __ARGS((char_u **arg));
581static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000583static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
584#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
585#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
586 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000588static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000589static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
590static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static typval_T *alloc_tv __ARGS((void));
592static typval_T *alloc_string_tv __ARGS((char_u *string));
593static void free_tv __ARGS((typval_T *varp));
594static void clear_tv __ARGS((typval_T *varp));
595static void init_tv __ARGS((typval_T *varp));
596static long get_tv_number __ARGS((typval_T *varp));
597static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
598static char_u *get_tv_string __ARGS((typval_T *varp));
599static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
600static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000601static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
603static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
604static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
605static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
606static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
607static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
608static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000611static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
613static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
614static int eval_fname_script __ARGS((char_u *p));
615static int eval_fname_sid __ARGS((char_u *p));
616static void list_func_head __ARGS((ufunc_T *fp, int indent));
617static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
618static ufunc_T *find_func __ARGS((char_u *name));
619static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000620static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000621#ifdef FEAT_PROFILE
622static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000623static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
624static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
625static int
626# ifdef __BORLANDC__
627 _RTLENTRYF
628# endif
629 prof_total_cmp __ARGS((const void *s1, const void *s2));
630static int
631# ifdef __BORLANDC__
632 _RTLENTRYF
633# endif
634 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000635#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636static int script_autoload __ARGS((char_u *name));
637static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void func_free __ARGS((ufunc_T *fp));
639static void func_unref __ARGS((char_u *name));
640static void func_ref __ARGS((char_u *name));
641static 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));
642static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
643
644static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
645
646static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
647static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
648static char_u *skip_var_one __ARGS((char_u *arg));
649static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
650static void list_glob_vars __ARGS((void));
651static void list_buf_vars __ARGS((void));
652static void list_win_vars __ARGS((void));
653static void list_vim_vars __ARGS((void));
654static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
655static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
656static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000657static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void clear_lval __ARGS((lval_T *lp));
659static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
660static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
661static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
662static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
663static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000664static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000666static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
667static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000668static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000670/* Character used as separated in autoload function/variable names. */
671#define AUTOLOAD_CHAR '#'
672
Bram Moolenaar33570922005-01-25 22:26:29 +0000673/*
674 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000675 */
676 void
677eval_init()
678{
Bram Moolenaar33570922005-01-25 22:26:29 +0000679 int i;
680 struct vimvar *p;
681
682 init_var_dict(&globvardict, &globvars_var);
683 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000684 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000685 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000686
687 for (i = 0; i < VV_LEN; ++i)
688 {
689 p = &vimvars[i];
690 STRCPY(p->vv_di.di_key, p->vv_name);
691 if (p->vv_flags & VV_RO)
692 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
693 else if (p->vv_flags & VV_RO_SBX)
694 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
695 else
696 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000697
698 /* add to v: scope dict, unless the value is not always available */
699 if (p->vv_type != VAR_UNKNOWN)
700 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000701 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000702 /* add to compat scope dict */
703 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000704 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000705}
706
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 * Return the name of the executed function.
709 */
710 char_u *
711func_name(cookie)
712 void *cookie;
713{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000714 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715}
716
717/*
718 * Return the address holding the next breakpoint line for a funccall cookie.
719 */
720 linenr_T *
721func_breakpoint(cookie)
722 void *cookie;
723{
Bram Moolenaar33570922005-01-25 22:26:29 +0000724 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727/*
728 * Return the address holding the debug tick for a funccall cookie.
729 */
730 int *
731func_dbg_tick(cookie)
732 void *cookie;
733{
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735}
736
737/*
738 * Return the nesting level for a funccall cookie.
739 */
740 int
741func_level(cookie)
742 void *cookie;
743{
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746
747/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000748funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750/*
751 * Return TRUE when a function was ended by a ":return" command.
752 */
753 int
754current_func_returned()
755{
756 return current_funccal->returned;
757}
758
759
760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * Set an internal variable to a string value. Creates the variable if it does
762 * not already exist.
763 */
764 void
765set_internal_string_var(name, value)
766 char_u *name;
767 char_u *value;
768{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000769 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000770 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 val = vim_strsave(value);
773 if (val != NULL)
774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000775 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000776 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000778 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 }
782}
783
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static lval_T *redir_lval = NULL;
785static char_u *redir_endp = NULL;
786static char_u *redir_varname = NULL;
787
788/*
789 * Start recording command output to a variable
790 * Returns OK if successfully completed the setup. FAIL otherwise.
791 */
792 int
793var_redir_start(name, append)
794 char_u *name;
795 int append; /* append to an existing variable */
796{
797 int save_emsg;
798 int err;
799 typval_T tv;
800
801 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000802 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000803 {
804 EMSG(_(e_invarg));
805 return FAIL;
806 }
807
808 redir_varname = vim_strsave(name);
809 if (redir_varname == NULL)
810 return FAIL;
811
812 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
813 if (redir_lval == NULL)
814 {
815 var_redir_stop();
816 return FAIL;
817 }
818
819 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000820 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
821 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000822 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
823 {
824 if (redir_endp != NULL && *redir_endp != NUL)
825 /* Trailing characters are present after the variable name */
826 EMSG(_(e_trailing));
827 else
828 EMSG(_(e_invarg));
829 var_redir_stop();
830 return FAIL;
831 }
832
833 /* check if we can write to the variable: set it to or append an empty
834 * string */
835 save_emsg = did_emsg;
836 did_emsg = FALSE;
837 tv.v_type = VAR_STRING;
838 tv.vval.v_string = (char_u *)"";
839 if (append)
840 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
841 else
842 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
843 err = did_emsg;
844 did_emsg += save_emsg;
845 if (err)
846 {
847 var_redir_stop();
848 return FAIL;
849 }
850 if (redir_lval->ll_newkey != NULL)
851 {
852 /* Dictionary item was created, don't do it again. */
853 vim_free(redir_lval->ll_newkey);
854 redir_lval->ll_newkey = NULL;
855 }
856
857 return OK;
858}
859
860/*
861 * Append "value[len]" to the variable set by var_redir_start().
862 */
863 void
864var_redir_str(value, len)
865 char_u *value;
866 int len;
867{
868 char_u *val;
869 typval_T tv;
870 int save_emsg;
871 int err;
872
873 if (redir_lval == NULL)
874 return;
875
876 if (len == -1)
877 /* Append the entire string */
878 val = vim_strsave(value);
879 else
880 /* Append only the specified number of characters */
881 val = vim_strnsave(value, len);
882 if (val == NULL)
883 return;
884
885 tv.v_type = VAR_STRING;
886 tv.vval.v_string = val;
887
888 save_emsg = did_emsg;
889 did_emsg = FALSE;
890 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
891 err = did_emsg;
892 did_emsg += save_emsg;
893 if (err)
894 var_redir_stop();
895
896 vim_free(tv.vval.v_string);
897}
898
899/*
900 * Stop redirecting command output to a variable.
901 */
902 void
903var_redir_stop()
904{
905 if (redir_lval != NULL)
906 {
907 clear_lval(redir_lval);
908 vim_free(redir_lval);
909 redir_lval = NULL;
910 }
911 vim_free(redir_varname);
912 redir_varname = NULL;
913}
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915# if defined(FEAT_MBYTE) || defined(PROTO)
916 int
917eval_charconvert(enc_from, enc_to, fname_from, fname_to)
918 char_u *enc_from;
919 char_u *enc_to;
920 char_u *fname_from;
921 char_u *fname_to;
922{
923 int err = FALSE;
924
925 set_vim_var_string(VV_CC_FROM, enc_from, -1);
926 set_vim_var_string(VV_CC_TO, enc_to, -1);
927 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
928 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
929 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
930 err = TRUE;
931 set_vim_var_string(VV_CC_FROM, NULL, -1);
932 set_vim_var_string(VV_CC_TO, NULL, -1);
933 set_vim_var_string(VV_FNAME_IN, NULL, -1);
934 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
935
936 if (err)
937 return FAIL;
938 return OK;
939}
940# endif
941
942# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
943 int
944eval_printexpr(fname, args)
945 char_u *fname;
946 char_u *args;
947{
948 int err = FALSE;
949
950 set_vim_var_string(VV_FNAME_IN, fname, -1);
951 set_vim_var_string(VV_CMDARG, args, -1);
952 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
953 err = TRUE;
954 set_vim_var_string(VV_FNAME_IN, NULL, -1);
955 set_vim_var_string(VV_CMDARG, NULL, -1);
956
957 if (err)
958 {
959 mch_remove(fname);
960 return FAIL;
961 }
962 return OK;
963}
964# endif
965
966# if defined(FEAT_DIFF) || defined(PROTO)
967 void
968eval_diff(origfile, newfile, outfile)
969 char_u *origfile;
970 char_u *newfile;
971 char_u *outfile;
972{
973 int err = FALSE;
974
975 set_vim_var_string(VV_FNAME_IN, origfile, -1);
976 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
977 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
978 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
979 set_vim_var_string(VV_FNAME_IN, NULL, -1);
980 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
981 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
982}
983
984 void
985eval_patch(origfile, difffile, outfile)
986 char_u *origfile;
987 char_u *difffile;
988 char_u *outfile;
989{
990 int err;
991
992 set_vim_var_string(VV_FNAME_IN, origfile, -1);
993 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
994 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
995 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
996 set_vim_var_string(VV_FNAME_IN, NULL, -1);
997 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
998 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
999}
1000# endif
1001
1002/*
1003 * Top level evaluation function, returning a boolean.
1004 * Sets "error" to TRUE if there was an error.
1005 * Return TRUE or FALSE.
1006 */
1007 int
1008eval_to_bool(arg, error, nextcmd, skip)
1009 char_u *arg;
1010 int *error;
1011 char_u **nextcmd;
1012 int skip; /* only parse, don't execute */
1013{
Bram Moolenaar33570922005-01-25 22:26:29 +00001014 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 int retval = FALSE;
1016
1017 if (skip)
1018 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 else
1022 {
1023 *error = FALSE;
1024 if (!skip)
1025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 retval = (get_tv_number(&tv) != 0);
1027 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 }
1030 if (skip)
1031 --emsg_skip;
1032
1033 return retval;
1034}
1035
1036/*
1037 * Top level evaluation function, returning a string. If "skip" is TRUE,
1038 * only parsing to "nextcmd" is done, without reporting errors. Return
1039 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1040 */
1041 char_u *
1042eval_to_string_skip(arg, nextcmd, skip)
1043 char_u *arg;
1044 char_u **nextcmd;
1045 int skip; /* only parse, don't execute */
1046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 char_u *retval;
1049
1050 if (skip)
1051 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001052 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 retval = NULL;
1054 else
1055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 retval = vim_strsave(get_tv_string(&tv));
1057 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 if (skip)
1060 --emsg_skip;
1061
1062 return retval;
1063}
1064
1065/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001066 * Skip over an expression at "*pp".
1067 * Return FAIL for an error, OK otherwise.
1068 */
1069 int
1070skip_expr(pp)
1071 char_u **pp;
1072{
Bram Moolenaar33570922005-01-25 22:26:29 +00001073 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001074
1075 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001077}
1078
1079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * Top level evaluation function, returning a string.
1081 * Return pointer to allocated memory, or NULL for failure.
1082 */
1083 char_u *
1084eval_to_string(arg, nextcmd)
1085 char_u *arg;
1086 char_u **nextcmd;
1087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 char_u *retval;
1090
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 retval = NULL;
1093 else
1094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 retval = vim_strsave(get_tv_string(&tv));
1096 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098
1099 return retval;
1100}
1101
1102/*
1103 * Call eval_to_string() with "sandbox" set and not using local variables.
1104 */
1105 char_u *
1106eval_to_string_safe(arg, nextcmd)
1107 char_u *arg;
1108 char_u **nextcmd;
1109{
1110 char_u *retval;
1111 void *save_funccalp;
1112
1113 save_funccalp = save_funccal();
1114 ++sandbox;
1115 retval = eval_to_string(arg, nextcmd);
1116 --sandbox;
1117 restore_funccal(save_funccalp);
1118 return retval;
1119}
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121/*
1122 * Top level evaluation function, returning a number.
1123 * Evaluates "expr" silently.
1124 * Returns -1 for an error.
1125 */
1126 int
1127eval_to_number(expr)
1128 char_u *expr;
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001132 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 ++emsg_off;
1135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 retval = -1;
1138 else
1139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 retval = get_tv_number(&rettv);
1141 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 --emsg_off;
1144
1145 return retval;
1146}
1147
1148#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1149/*
1150 * Call some vimL function and return the result as a string
1151 * Uses argv[argc] for the function arguments.
1152 */
1153 char_u *
1154call_vim_function(func, argc, argv, safe)
1155 char_u *func;
1156 int argc;
1157 char_u **argv;
1158 int safe; /* use the sandbox */
1159{
1160 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T rettv;
1162 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 long n;
1164 int len;
1165 int i;
1166 int doesrange;
1167 void *save_funccalp = NULL;
1168
Bram Moolenaar33570922005-01-25 22:26:29 +00001169 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (argvars == NULL)
1171 return NULL;
1172
1173 for (i = 0; i < argc; i++)
1174 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001175 /* Pass a NULL or empty argument as an empty string */
1176 if (argv[i] == NULL || *argv[i] == NUL)
1177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001178 argvars[i].v_type = VAR_STRING;
1179 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001180 continue;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Recognize a number argument, the others must be strings. */
1184 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1185 if (len != 0 && len == (int)STRLEN(argv[i]))
1186 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001187 argvars[i].v_type = VAR_NUMBER;
1188 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else
1191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001192 argvars[i].v_type = VAR_STRING;
1193 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 }
1196
1197 if (safe)
1198 {
1199 save_funccalp = save_funccal();
1200 ++sandbox;
1201 }
1202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1204 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001206 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 vim_free(argvars);
1211
1212 if (safe)
1213 {
1214 --sandbox;
1215 restore_funccal(save_funccalp);
1216 }
1217 return retval;
1218}
1219#endif
1220
1221/*
1222 * Save the current function call pointer, and set it to NULL.
1223 * Used when executing autocommands and for ":source".
1224 */
1225 void *
1226save_funccal()
1227{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001228 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 current_funccal = NULL;
1231 return (void *)fc;
1232}
1233
1234 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001235restore_funccal(vfc)
1236 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001238 funccall_T *fc = (funccall_T *)vfc;
1239
1240 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241}
1242
Bram Moolenaar05159a02005-02-26 23:04:13 +00001243#if defined(FEAT_PROFILE) || defined(PROTO)
1244/*
1245 * Prepare profiling for entering a child or something else that is not
1246 * counted for the script/function itself.
1247 * Should always be called in pair with prof_child_exit().
1248 */
1249 void
1250prof_child_enter(tm)
1251 proftime_T *tm; /* place to store waittime */
1252{
1253 funccall_T *fc = current_funccal;
1254
1255 if (fc != NULL && fc->func->uf_profiling)
1256 profile_start(&fc->prof_child);
1257 script_prof_save(tm);
1258}
1259
1260/*
1261 * Take care of time spent in a child.
1262 * Should always be called after prof_child_enter().
1263 */
1264 void
1265prof_child_exit(tm)
1266 proftime_T *tm; /* where waittime was stored */
1267{
1268 funccall_T *fc = current_funccal;
1269
1270 if (fc != NULL && fc->func->uf_profiling)
1271 {
1272 profile_end(&fc->prof_child);
1273 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1274 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1275 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1276 }
1277 script_prof_restore(tm);
1278}
1279#endif
1280
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#ifdef FEAT_FOLDING
1283/*
1284 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1285 * it in "*cp". Doesn't give error messages.
1286 */
1287 int
1288eval_foldexpr(arg, cp)
1289 char_u *arg;
1290 int *cp;
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int retval;
1294 char_u *s;
1295
1296 ++emsg_off;
1297 ++sandbox;
1298 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 retval = 0;
1301 else
1302 {
1303 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (tv.v_type == VAR_NUMBER)
1305 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001306 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = 0;
1308 else
1309 {
1310 /* If the result is a string, check if there is a non-digit before
1311 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (!VIM_ISDIGIT(*s) && *s != '-')
1314 *cp = *s++;
1315 retval = atol((char *)s);
1316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 --emsg_off;
1320 --sandbox;
1321
1322 return retval;
1323}
1324#endif
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 * ":let" list all variable values
1328 * ":let var1 var2" list variable values
1329 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 * ":let var += expr" assignment command.
1331 * ":let var -= expr" assignment command.
1332 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 void
1336ex_let(eap)
1337 exarg_T *eap;
1338{
1339 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 int var_count = 0;
1344 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001345 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001347 expr = skip_var_list(arg, &var_count, &semicolon);
1348 if (expr == NULL)
1349 return;
1350 expr = vim_strchr(expr, '=');
1351 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001353 /*
1354 * ":let" without "=": list variables
1355 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356 if (*arg == '[')
1357 EMSG(_(e_invarg));
1358 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 /* ":let var1 var2" */
1360 arg = list_arg_vars(eap, arg);
1361 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001364 list_glob_vars();
1365 list_buf_vars();
1366 list_win_vars();
1367 list_vim_vars();
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 eap->nextcmd = check_nextcmd(arg);
1370 }
1371 else
1372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 op[0] = '=';
1374 op[1] = NUL;
1375 if (expr > arg)
1376 {
1377 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1378 op[0] = expr[-1]; /* +=, -= or .= */
1379 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001380 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (eap->skip)
1383 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001384 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (eap->skip)
1386 {
1387 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 --emsg_skip;
1390 }
1391 else if (i != FAIL)
1392 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 }
1398}
1399
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001400/*
1401 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1402 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 * When "nextchars" is not NULL it points to a string with characters that
1404 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1405 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001406 * Returns OK or FAIL;
1407 */
1408 static int
1409ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1410 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412 int copy; /* copy values from "tv", don't move */
1413 int semicolon; /* from skip_var_list() */
1414 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416{
1417 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 listitem_T *item;
1421 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422
1423 if (*arg != '[')
1424 {
1425 /*
1426 * ":let var = expr" or ":for var in list"
1427 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001428 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 return FAIL;
1430 return OK;
1431 }
1432
1433 /*
1434 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1435 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001436 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 {
1438 EMSG(_(e_listreq));
1439 return FAIL;
1440 }
1441
1442 i = list_len(l);
1443 if (semicolon == 0 && var_count < i)
1444 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001445 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001446 return FAIL;
1447 }
1448 if (var_count - semicolon > i)
1449 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001450 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001451 return FAIL;
1452 }
1453
1454 item = l->lv_first;
1455 while (*arg != ']')
1456 {
1457 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001459 item = item->li_next;
1460 if (arg == NULL)
1461 return FAIL;
1462
1463 arg = skipwhite(arg);
1464 if (*arg == ';')
1465 {
1466 /* Put the rest of the list (may be empty) in the var after ';'.
1467 * Create a new list for this. */
1468 l = list_alloc();
1469 if (l == NULL)
1470 return FAIL;
1471 while (item != NULL)
1472 {
1473 list_append_tv(l, &item->li_tv);
1474 item = item->li_next;
1475 }
1476
1477 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001478 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 ltv.vval.v_list = l;
1480 l->lv_refcount = 1;
1481
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001482 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1483 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 clear_tv(&ltv);
1485 if (arg == NULL)
1486 return FAIL;
1487 break;
1488 }
1489 else if (*arg != ',' && *arg != ']')
1490 {
1491 EMSG2(_(e_intern2), "ex_let_vars()");
1492 return FAIL;
1493 }
1494 }
1495
1496 return OK;
1497}
1498
1499/*
1500 * Skip over assignable variable "var" or list of variables "[var, var]".
1501 * Used for ":let varvar = expr" and ":for varvar in expr".
1502 * For "[var, var]" increment "*var_count" for each variable.
1503 * for "[var, var; var]" set "semicolon".
1504 * Return NULL for an error.
1505 */
1506 static char_u *
1507skip_var_list(arg, var_count, semicolon)
1508 char_u *arg;
1509 int *var_count;
1510 int *semicolon;
1511{
1512 char_u *p, *s;
1513
1514 if (*arg == '[')
1515 {
1516 /* "[var, var]": find the matching ']'. */
1517 p = arg;
1518 while (1)
1519 {
1520 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1521 s = skip_var_one(p);
1522 if (s == p)
1523 {
1524 EMSG2(_(e_invarg2), p);
1525 return NULL;
1526 }
1527 ++*var_count;
1528
1529 p = skipwhite(s);
1530 if (*p == ']')
1531 break;
1532 else if (*p == ';')
1533 {
1534 if (*semicolon == 1)
1535 {
1536 EMSG(_("Double ; in list of variables"));
1537 return NULL;
1538 }
1539 *semicolon = 1;
1540 }
1541 else if (*p != ',')
1542 {
1543 EMSG2(_(e_invarg2), p);
1544 return NULL;
1545 }
1546 }
1547 return p + 1;
1548 }
1549 else
1550 return skip_var_one(arg);
1551}
1552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001553/*
1554 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1555 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 static char_u *
1557skip_var_one(arg)
1558 char_u *arg;
1559{
1560 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1561 ++arg;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001562 return find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563}
1564
Bram Moolenaara7043832005-01-21 11:56:39 +00001565/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 * List variables for hashtab "ht" with prefix "prefix".
1567 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001568 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001570list_hashtable_vars(ht, prefix, empty)
1571 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001572 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001573 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001574{
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 hashitem_T *hi;
1576 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 int todo;
1578
1579 todo = ht->ht_used;
1580 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1581 {
1582 if (!HASHITEM_EMPTY(hi))
1583 {
1584 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001585 di = HI2DI(hi);
1586 if (empty || di->di_tv.v_type != VAR_STRING
1587 || di->di_tv.vval.v_string != NULL)
1588 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001589 }
1590 }
1591}
1592
1593/*
1594 * List global variables.
1595 */
1596 static void
1597list_glob_vars()
1598{
Bram Moolenaar33570922005-01-25 22:26:29 +00001599 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001600}
1601
1602/*
1603 * List buffer variables.
1604 */
1605 static void
1606list_buf_vars()
1607{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001608 char_u numbuf[NUMBUFLEN];
1609
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001611
1612 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1613 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001614}
1615
1616/*
1617 * List window variables.
1618 */
1619 static void
1620list_win_vars()
1621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001623}
1624
1625/*
1626 * List Vim variables.
1627 */
1628 static void
1629list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632}
1633
1634/*
1635 * List variables in "arg".
1636 */
1637 static char_u *
1638list_arg_vars(eap, arg)
1639 exarg_T *eap;
1640 char_u *arg;
1641{
1642 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001643 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001645 char_u *name_start;
1646 char_u *arg_subsc;
1647 char_u *tofree;
1648 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649
1650 while (!ends_excmd(*arg) && !got_int)
1651 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001652 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001654 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1656 {
1657 emsg_severe = TRUE;
1658 EMSG(_(e_trailing));
1659 break;
1660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664 /* get_name_len() takes care of expanding curly braces */
1665 name_start = name = arg;
1666 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1667 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001669 /* This is mainly to keep test 49 working: when expanding
1670 * curly braces fails overrule the exception error message. */
1671 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001673 emsg_severe = TRUE;
1674 EMSG2(_(e_invarg2), arg);
1675 break;
1676 }
1677 error = TRUE;
1678 }
1679 else
1680 {
1681 if (tofree != NULL)
1682 name = tofree;
1683 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 else
1686 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001687 /* handle d.key, l[idx], f(expr) */
1688 arg_subsc = arg;
1689 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001690 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001692 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001693 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001695 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001696 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001697 case 'g': list_glob_vars(); break;
1698 case 'b': list_buf_vars(); break;
1699 case 'w': list_win_vars(); break;
1700 case 'v': list_vim_vars(); break;
1701 default:
1702 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001703 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001704 }
1705 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001706 {
1707 char_u numbuf[NUMBUFLEN];
1708 char_u *tf;
1709 int c;
1710 char_u *s;
1711
1712 s = echo_string(&tv, &tf, numbuf);
1713 c = *arg;
1714 *arg = NUL;
1715 list_one_var_a((char_u *)"",
1716 arg == arg_subsc ? name : name_start,
1717 tv.v_type, s == NULL ? (char_u *)"" : s);
1718 *arg = c;
1719 vim_free(tf);
1720 }
1721 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 }
1724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001725
1726 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001728
1729 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 }
1731
1732 return arg;
1733}
1734
1735/*
1736 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1737 * Returns a pointer to the char just after the var name.
1738 * Returns NULL if there is an error.
1739 */
1740 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001743 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747{
1748 int c1;
1749 char_u *name;
1750 char_u *p;
1751 char_u *arg_end = NULL;
1752 int len;
1753 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001754 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755
1756 /*
1757 * ":let $VAR = expr": Set environment variable.
1758 */
1759 if (*arg == '$')
1760 {
1761 /* Find the end of the name. */
1762 ++arg;
1763 name = arg;
1764 len = get_env_len(&arg);
1765 if (len == 0)
1766 EMSG2(_(e_invarg2), name - 1);
1767 else
1768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 if (op != NULL && (*op == '+' || *op == '-'))
1770 EMSG2(_(e_letwrong), op);
1771 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 EMSG(_(e_letunexp));
1774 else
1775 {
1776 c1 = name[len];
1777 name[len] = NUL;
1778 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 if (op != NULL && *op == '.')
1780 {
1781 int mustfree = FALSE;
1782 char_u *s = vim_getenv(name, &mustfree);
1783
1784 if (s != NULL)
1785 {
1786 p = tofree = concat_str(s, p);
1787 if (mustfree)
1788 vim_free(s);
1789 }
1790 }
1791 if (p != NULL)
1792 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (STRICMP(name, "HOME") == 0)
1794 init_homedir();
1795 else if (didset_vim && STRICMP(name, "VIM") == 0)
1796 didset_vim = FALSE;
1797 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1798 didset_vimruntime = FALSE;
1799 name[len] = c1;
1800 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 }
1803 }
1804 }
1805
1806 /*
1807 * ":let &option = expr": Set option value.
1808 * ":let &l:option = expr": Set local option value.
1809 * ":let &g:option = expr": Set global option value.
1810 */
1811 else if (*arg == '&')
1812 {
1813 /* Find the end of the name. */
1814 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 if (p == NULL || (endchars != NULL
1816 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 EMSG(_(e_letunexp));
1818 else
1819 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 long n;
1821 int opt_type;
1822 long numval;
1823 char_u *stringval = NULL;
1824 char_u *s;
1825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 c1 = *p;
1827 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828
1829 n = get_tv_number(tv);
1830 s = get_tv_string(tv);
1831 if (op != NULL && *op != '=')
1832 {
1833 opt_type = get_option_value(arg, &numval,
1834 &stringval, opt_flags);
1835 if ((opt_type == 1 && *op == '.')
1836 || (opt_type == 0 && *op != '.'))
1837 EMSG2(_(e_letwrong), op);
1838 else
1839 {
1840 if (opt_type == 1) /* number */
1841 {
1842 if (*op == '+')
1843 n = numval + n;
1844 else
1845 n = numval - n;
1846 }
1847 else if (opt_type == 0 && stringval != NULL) /* string */
1848 {
1849 s = concat_str(stringval, s);
1850 vim_free(stringval);
1851 stringval = s;
1852 }
1853 }
1854 }
1855 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 *p = c1;
1857 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001858 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 }
1860 }
1861
1862 /*
1863 * ":let @r = expr": Set register contents.
1864 */
1865 else if (*arg == '@')
1866 {
1867 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 if (op != NULL && (*op == '+' || *op == '-'))
1869 EMSG2(_(e_letwrong), op);
1870 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 EMSG(_(e_letunexp));
1873 else
1874 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 char_u *tofree = NULL;
1876 char_u *s;
1877
1878 p = get_tv_string(tv);
1879 if (op != NULL && *op == '.')
1880 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00001881 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 if (s != NULL)
1883 {
1884 p = tofree = concat_str(s, p);
1885 vim_free(s);
1886 }
1887 }
1888 if (p != NULL)
1889 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 }
1893 }
1894
1895 /*
1896 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001899 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001901 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001903 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1907 EMSG(_(e_letunexp));
1908 else
1909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 arg_end = p;
1912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 }
1916
1917 else
1918 EMSG2(_(e_invarg2), arg);
1919
1920 return arg_end;
1921}
1922
1923/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001924 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1925 */
1926 static int
1927check_changedtick(arg)
1928 char_u *arg;
1929{
1930 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1931 {
1932 EMSG2(_(e_readonlyvar), arg);
1933 return TRUE;
1934 }
1935 return FALSE;
1936}
1937
1938/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 * Get an lval: variable, Dict item or List item that can be assigned a value
1940 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1941 * "name.key", "name.key[expr]" etc.
1942 * Indexing only works if "name" is an existing List or Dictionary.
1943 * "name" points to the start of the name.
1944 * If "rettv" is not NULL it points to the value to be assigned.
1945 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1946 * wrong; must end in space or cmd separator.
1947 *
1948 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001949 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 */
1952 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001953get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 typval_T *rettv;
1956 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 int unlet;
1958 int skip;
1959 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001960 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 char_u *expr_start, *expr_end;
1964 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 dictitem_T *v;
1966 typval_T var1;
1967 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976
1977 if (skip)
1978 {
1979 /* When skipping just find the end of the name. */
1980 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001981 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 }
1983
1984 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001985 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 if (expr_start != NULL)
1987 {
1988 /* Don't expand the name when we already know there is an error. */
1989 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1990 && *p != '[' && *p != '.')
1991 {
1992 EMSG(_(e_trailing));
1993 return NULL;
1994 }
1995
1996 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1997 if (lp->ll_exp_name == NULL)
1998 {
1999 /* Report an invalid expression in braces, unless the
2000 * expression evaluation has been cancelled due to an
2001 * aborting error, an interrupt, or an exception. */
2002 if (!aborting() && !quiet)
2003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002004 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 EMSG2(_(e_invarg2), name);
2006 return NULL;
2007 }
2008 }
2009 lp->ll_name = lp->ll_exp_name;
2010 }
2011 else
2012 lp->ll_name = name;
2013
2014 /* Without [idx] or .key we are done. */
2015 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2016 return p;
2017
2018 cc = *p;
2019 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002020 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (v == NULL && !quiet)
2022 EMSG2(_(e_undefvar), lp->ll_name);
2023 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 if (v == NULL)
2025 return NULL;
2026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 /*
2028 * Loop until no more [idx] or .key is following.
2029 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2034 && !(lp->ll_tv->v_type == VAR_DICT
2035 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 if (!quiet)
2038 EMSG(_("E689: Can only index a List or Dictionary"));
2039 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (!quiet)
2044 EMSG(_("E708: [:] must come last"));
2045 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002047
Bram Moolenaar8c711452005-01-14 21:53:12 +00002048 len = -1;
2049 if (*p == '.')
2050 {
2051 key = p + 1;
2052 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2053 ;
2054 if (len == 0)
2055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (!quiet)
2057 EMSG(_(e_emptykey));
2058 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 }
2060 p = key + len;
2061 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002062 else
2063 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002065 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 if (*p == ':')
2067 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002068 else
2069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 empty1 = FALSE;
2071 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 }
2074
2075 /* Optionally get the second index [ :expr]. */
2076 if (*p == ':')
2077 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002081 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002082 if (!empty1)
2083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002085 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 if (rettv != NULL && (rettv->v_type != VAR_LIST
2087 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089 if (!quiet)
2090 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 if (!empty1)
2092 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002094 }
2095 p = skipwhite(p + 1);
2096 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002098 else
2099 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 if (!empty1)
2104 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 }
2107 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002112
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002114 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (!quiet)
2116 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 if (!empty1)
2118 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 }
2123
2124 /* Skip to past ']'. */
2125 ++p;
2126 }
2127
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 {
2130 if (len == -1)
2131 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 key = get_tv_string(&var1);
2134 if (*key == NUL)
2135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (!quiet)
2137 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 }
2141 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 lp->ll_list = NULL;
2143 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002144 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002147 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002148 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 if (len == -1)
2153 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002155 }
2156 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002160 if (len == -1)
2161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002163 p = NULL;
2164 break;
2165 }
2166 if (len == -1)
2167 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002169 }
2170 else
2171 {
2172 /*
2173 * Get the number and item for the only or first index of the List.
2174 */
2175 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002177 else
2178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 clear_tv(&var1);
2181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002182 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 lp->ll_list = lp->ll_tv->vval.v_list;
2184 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2185 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (!quiet)
2188 EMSGN(_(e_listidx), lp->ll_n1);
2189 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 }
2193
2194 /*
2195 * May need to find the item or absolute index for the second
2196 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 * When no index given: "lp->ll_empty2" is TRUE.
2198 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002199 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 if (ni == NULL)
2208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (!quiet)
2210 EMSGN(_(e_listidx), lp->ll_n2);
2211 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 }
2215
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2217 if (lp->ll_n1 < 0)
2218 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2219 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002220 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 if (!quiet)
2222 EMSGN(_(e_listidx), lp->ll_n2);
2223 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002224 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002225 }
2226
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 }
2230
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 return p;
2232}
2233
2234/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002235 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 */
2237 static void
2238clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002239 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240{
2241 vim_free(lp->ll_exp_name);
2242 vim_free(lp->ll_newkey);
2243}
2244
2245/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002246 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 */
2250 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002254 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257{
2258 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002259 listitem_T *ni;
2260 listitem_T *ri;
2261 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262
2263 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 cc = *endp;
2268 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 if (op != NULL && *op != '=')
2270 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002271 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002273 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2275 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 {
2277 if (tv_op(&tv, rettv, op) == OK)
2278 set_var(lp->ll_name, &tv, FALSE);
2279 clear_tv(&tv);
2280 }
2281 }
2282 else
2283 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002287 else if (tv_check_lock(lp->ll_newkey == NULL
2288 ? lp->ll_tv->v_lock
2289 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2290 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 else if (lp->ll_range)
2292 {
2293 /*
2294 * Assign the List values to the list items.
2295 */
2296 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002297 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 if (op != NULL && *op != '=')
2299 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2300 else
2301 {
2302 clear_tv(&lp->ll_li->li_tv);
2303 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2304 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 ri = ri->li_next;
2306 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2307 break;
2308 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 /* Need to add an empty item. */
2311 ni = listitem_alloc();
2312 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 ri = NULL;
2315 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002318 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 ni->li_tv.vval.v_number = 0;
2320 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002321 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 lp->ll_li = lp->ll_li->li_next;
2323 ++lp->ll_n1;
2324 }
2325 if (ri != NULL)
2326 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 else if (lp->ll_empty2
2328 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 : lp->ll_n1 != lp->ll_n2)
2330 EMSG(_("E711: List value has not enough items"));
2331 }
2332 else
2333 {
2334 /*
2335 * Assign to a List or Dictionary item.
2336 */
2337 if (lp->ll_newkey != NULL)
2338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 if (op != NULL && *op != '=')
2340 {
2341 EMSG2(_(e_letwrong), op);
2342 return;
2343 }
2344
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002346 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 if (di == NULL)
2348 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002349 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2350 {
2351 vim_free(di);
2352 return;
2353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002355 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 else if (op != NULL && *op != '=')
2357 {
2358 tv_op(lp->ll_tv, rettv, op);
2359 return;
2360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 /*
2365 * Assign the value to the variable or list item.
2366 */
2367 if (copy)
2368 copy_tv(rettv, lp->ll_tv);
2369 else
2370 {
2371 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002372 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
2375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376}
2377
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2380 * Returns OK or FAIL.
2381 */
2382 static int
2383tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002384 typval_T *tv1;
2385 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 char_u *op;
2387{
2388 long n;
2389 char_u numbuf[NUMBUFLEN];
2390 char_u *s;
2391
2392 /* Can't do anything with a Funcref or a Dict on the right. */
2393 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2394 {
2395 switch (tv1->v_type)
2396 {
2397 case VAR_DICT:
2398 case VAR_FUNC:
2399 break;
2400
2401 case VAR_LIST:
2402 if (*op != '+' || tv2->v_type != VAR_LIST)
2403 break;
2404 /* List += List */
2405 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2406 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2407 return OK;
2408
2409 case VAR_NUMBER:
2410 case VAR_STRING:
2411 if (tv2->v_type == VAR_LIST)
2412 break;
2413 if (*op == '+' || *op == '-')
2414 {
2415 /* nr += nr or nr -= nr*/
2416 n = get_tv_number(tv1);
2417 if (*op == '+')
2418 n += get_tv_number(tv2);
2419 else
2420 n -= get_tv_number(tv2);
2421 clear_tv(tv1);
2422 tv1->v_type = VAR_NUMBER;
2423 tv1->vval.v_number = n;
2424 }
2425 else
2426 {
2427 /* str .= str */
2428 s = get_tv_string(tv1);
2429 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2430 clear_tv(tv1);
2431 tv1->v_type = VAR_STRING;
2432 tv1->vval.v_string = s;
2433 }
2434 return OK;
2435 }
2436 }
2437
2438 EMSG2(_(e_letwrong), op);
2439 return FAIL;
2440}
2441
2442/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443 * Add a watcher to a list.
2444 */
2445 static void
2446list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 list_T *l;
2448 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449{
2450 lw->lw_next = l->lv_watch;
2451 l->lv_watch = lw;
2452}
2453
2454/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002455 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002456 * No warning when it isn't found...
2457 */
2458 static void
2459list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002460 list_T *l;
2461 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002462{
Bram Moolenaar33570922005-01-25 22:26:29 +00002463 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464
2465 lwp = &l->lv_watch;
2466 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2467 {
2468 if (lw == lwrem)
2469 {
2470 *lwp = lw->lw_next;
2471 break;
2472 }
2473 lwp = &lw->lw_next;
2474 }
2475}
2476
2477/*
2478 * Just before removing an item from a list: advance watchers to the next
2479 * item.
2480 */
2481 static void
2482list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 list_T *l;
2484 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485{
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487
2488 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2489 if (lw->lw_item == item)
2490 lw->lw_item = item->li_next;
2491}
2492
2493/*
2494 * Evaluate the expression used in a ":for var in expr" command.
2495 * "arg" points to "var".
2496 * Set "*errp" to TRUE for an error, FALSE otherwise;
2497 * Return a pointer that holds the info. Null when there is an error.
2498 */
2499 void *
2500eval_for_line(arg, errp, nextcmdp, skip)
2501 char_u *arg;
2502 int *errp;
2503 char_u **nextcmdp;
2504 int skip;
2505{
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 typval_T tv;
2509 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510
2511 *errp = TRUE; /* default: there is an error */
2512
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514 if (fi == NULL)
2515 return NULL;
2516
2517 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2518 if (expr == NULL)
2519 return fi;
2520
2521 expr = skipwhite(expr);
2522 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2523 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002524 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525 return fi;
2526 }
2527
2528 if (skip)
2529 ++emsg_skip;
2530 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2531 {
2532 *errp = FALSE;
2533 if (!skip)
2534 {
2535 l = tv.vval.v_list;
2536 if (tv.v_type != VAR_LIST || l == NULL)
2537 EMSG(_(e_listreq));
2538 else
2539 {
2540 fi->fi_list = l;
2541 list_add_watch(l, &fi->fi_lw);
2542 fi->fi_lw.lw_item = l->lv_first;
2543 }
2544 }
2545 }
2546 if (skip)
2547 --emsg_skip;
2548
2549 return fi;
2550}
2551
2552/*
2553 * Use the first item in a ":for" list. Advance to the next.
2554 * Assign the values to the variable (list). "arg" points to the first one.
2555 * Return TRUE when a valid item was found, FALSE when at end of list or
2556 * something wrong.
2557 */
2558 int
2559next_for_item(fi_void, arg)
2560 void *fi_void;
2561 char_u *arg;
2562{
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002564 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002566
2567 item = fi->fi_lw.lw_item;
2568 if (item == NULL)
2569 result = FALSE;
2570 else
2571 {
2572 fi->fi_lw.lw_item = item->li_next;
2573 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2574 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2575 }
2576 return result;
2577}
2578
2579/*
2580 * Free the structure used to store info used by ":for".
2581 */
2582 void
2583free_for_info(fi_void)
2584 void *fi_void;
2585{
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002587
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002588 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002589 list_rem_watch(fi->fi_list, &fi->fi_lw);
2590 vim_free(fi);
2591}
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2594
2595 void
2596set_context_for_expression(xp, arg, cmdidx)
2597 expand_T *xp;
2598 char_u *arg;
2599 cmdidx_T cmdidx;
2600{
2601 int got_eq = FALSE;
2602 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002603 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002605 if (cmdidx == CMD_let)
2606 {
2607 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002608 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002609 {
2610 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002611 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002612 {
2613 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002614 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002615 if (vim_iswhite(*p))
2616 break;
2617 }
2618 return;
2619 }
2620 }
2621 else
2622 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2623 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 while ((xp->xp_pattern = vim_strpbrk(arg,
2625 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2626 {
2627 c = *xp->xp_pattern;
2628 if (c == '&')
2629 {
2630 c = xp->xp_pattern[1];
2631 if (c == '&')
2632 {
2633 ++xp->xp_pattern;
2634 xp->xp_context = cmdidx != CMD_let || got_eq
2635 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2636 }
2637 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002640 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2641 xp->xp_pattern += 2;
2642
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645 else if (c == '$')
2646 {
2647 /* environment variable */
2648 xp->xp_context = EXPAND_ENV_VARS;
2649 }
2650 else if (c == '=')
2651 {
2652 got_eq = TRUE;
2653 xp->xp_context = EXPAND_EXPRESSION;
2654 }
2655 else if (c == '<'
2656 && xp->xp_context == EXPAND_FUNCTIONS
2657 && vim_strchr(xp->xp_pattern, '(') == NULL)
2658 {
2659 /* Function name can start with "<SNR>" */
2660 break;
2661 }
2662 else if (cmdidx != CMD_let || got_eq)
2663 {
2664 if (c == '"') /* string */
2665 {
2666 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2667 if (c == '\\' && xp->xp_pattern[1] != NUL)
2668 ++xp->xp_pattern;
2669 xp->xp_context = EXPAND_NOTHING;
2670 }
2671 else if (c == '\'') /* literal string */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2675 /* skip */ ;
2676 xp->xp_context = EXPAND_NOTHING;
2677 }
2678 else if (c == '|')
2679 {
2680 if (xp->xp_pattern[1] == '|')
2681 {
2682 ++xp->xp_pattern;
2683 xp->xp_context = EXPAND_EXPRESSION;
2684 }
2685 else
2686 xp->xp_context = EXPAND_COMMANDS;
2687 }
2688 else
2689 xp->xp_context = EXPAND_EXPRESSION;
2690 }
2691 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002692 /* Doesn't look like something valid, expand as an expression
2693 * anyway. */
2694 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 arg = xp->xp_pattern;
2696 if (*arg != NUL)
2697 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2698 /* skip */ ;
2699 }
2700 xp->xp_pattern = arg;
2701}
2702
2703#endif /* FEAT_CMDL_COMPL */
2704
2705/*
2706 * ":1,25call func(arg1, arg2)" function call.
2707 */
2708 void
2709ex_call(eap)
2710 exarg_T *eap;
2711{
2712 char_u *arg = eap->arg;
2713 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002717 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 linenr_T lnum;
2719 int doesrange;
2720 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002721 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002723 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2724 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 if (tofree == NULL)
2726 return;
2727
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002728 /* Increase refcount on dictionary, it could get deleted when evaluating
2729 * the arguments. */
2730 if (fudi.fd_dict != NULL)
2731 ++fudi.fd_dict->dv_refcount;
2732
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002733 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2734 len = STRLEN(tofree);
2735 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar532c7802005-01-27 14:44:31 +00002737 /* Skip white space to allow ":call func ()". Not good, but required for
2738 * backward compatibility. */
2739 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002740 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 if (*startarg != '(')
2743 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002744 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 goto end;
2746 }
2747
2748 /*
2749 * When skipping, evaluate the function once, to find the end of the
2750 * arguments.
2751 * When the function takes a range, this is discovered after the first
2752 * call, and the loop is broken.
2753 */
2754 if (eap->skip)
2755 {
2756 ++emsg_skip;
2757 lnum = eap->line2; /* do it once, also with an invalid range */
2758 }
2759 else
2760 lnum = eap->line1;
2761 for ( ; lnum <= eap->line2; ++lnum)
2762 {
2763 if (!eap->skip && eap->addr_count > 0)
2764 {
2765 curwin->w_cursor.lnum = lnum;
2766 curwin->w_cursor.col = 0;
2767 }
2768 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002770 eap->line1, eap->line2, &doesrange,
2771 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 failed = TRUE;
2774 break;
2775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (doesrange || eap->skip)
2778 break;
2779 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002780 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002782 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (aborting())
2784 break;
2785 }
2786 if (eap->skip)
2787 --emsg_skip;
2788
2789 if (!failed)
2790 {
2791 /* Check for trailing illegal characters and a following command. */
2792 if (!ends_excmd(*arg))
2793 {
2794 emsg_severe = TRUE;
2795 EMSG(_(e_trailing));
2796 }
2797 else
2798 eap->nextcmd = check_nextcmd(arg);
2799 }
2800
2801end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002802 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804}
2805
2806/*
2807 * ":unlet[!] var1 ... " command.
2808 */
2809 void
2810ex_unlet(eap)
2811 exarg_T *eap;
2812{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002813 ex_unletlock(eap, eap->arg, 0);
2814}
2815
2816/*
2817 * ":lockvar" and ":unlockvar" commands
2818 */
2819 void
2820ex_lockvar(eap)
2821 exarg_T *eap;
2822{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002824 int deep = 2;
2825
2826 if (eap->forceit)
2827 deep = -1;
2828 else if (vim_isdigit(*arg))
2829 {
2830 deep = getdigits(&arg);
2831 arg = skipwhite(arg);
2832 }
2833
2834 ex_unletlock(eap, arg, deep);
2835}
2836
2837/*
2838 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2839 */
2840 static void
2841ex_unletlock(eap, argstart, deep)
2842 exarg_T *eap;
2843 char_u *argstart;
2844 int deep;
2845{
2846 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 do
2852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002854 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
2855 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lv.ll_name == NULL)
2857 error = TRUE; /* error but continue parsing */
2858 if (name_end == NULL || (!vim_iswhite(*name_end)
2859 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 if (name_end != NULL)
2862 {
2863 emsg_severe = TRUE;
2864 EMSG(_(e_trailing));
2865 }
2866 if (!(eap->skip || error))
2867 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 break;
2869 }
2870
2871 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002872 {
2873 if (eap->cmdidx == CMD_unlet)
2874 {
2875 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2876 error = TRUE;
2877 }
2878 else
2879 {
2880 if (do_lock_var(&lv, name_end, deep,
2881 eap->cmdidx == CMD_lockvar) == FAIL)
2882 error = TRUE;
2883 }
2884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (!eap->skip)
2887 clear_lval(&lv);
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 arg = skipwhite(name_end);
2890 } while (!ends_excmd(*arg));
2891
2892 eap->nextcmd = check_nextcmd(arg);
2893}
2894
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 int forceit;
2900{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 int ret = OK;
2902 int cc;
2903
2904 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *name_end;
2907 *name_end = NUL;
2908
2909 /* Normal name or expanded name. */
2910 if (check_changedtick(lp->ll_name))
2911 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002915 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2917 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 else if (lp->ll_range)
2919 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921
2922 /* Delete a range of List items. */
2923 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2924 {
2925 li = lp->ll_li->li_next;
2926 listitem_remove(lp->ll_list, lp->ll_li);
2927 lp->ll_li = li;
2928 ++lp->ll_n1;
2929 }
2930 }
2931 else
2932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 /* unlet a List item. */
2935 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002938 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 }
2940
2941 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002942}
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944/*
2945 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 */
2948 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002949do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 hashtab_T *ht;
2954 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002955 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 ht = find_var_ht(name, &varname);
2958 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 hi = hash_find(ht, varname);
2961 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002962 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 if (var_check_ro(HI2DI(hi)->di_flags, name))
2964 return FAIL;
2965 delete_var(ht, hi);
2966 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 if (forceit)
2970 return OK;
2971 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 return FAIL;
2973}
2974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975/*
2976 * Lock or unlock variable indicated by "lp".
2977 * "deep" is the levels to go (-1 for unlimited);
2978 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2979 */
2980 static int
2981do_lock_var(lp, name_end, deep, lock)
2982 lval_T *lp;
2983 char_u *name_end;
2984 int deep;
2985 int lock;
2986{
2987 int ret = OK;
2988 int cc;
2989 dictitem_T *di;
2990
2991 if (deep == 0) /* nothing to do */
2992 return OK;
2993
2994 if (lp->ll_tv == NULL)
2995 {
2996 cc = *name_end;
2997 *name_end = NUL;
2998
2999 /* Normal name or expanded name. */
3000 if (check_changedtick(lp->ll_name))
3001 ret = FAIL;
3002 else
3003 {
3004 di = find_var(lp->ll_name, NULL);
3005 if (di == NULL)
3006 ret = FAIL;
3007 else
3008 {
3009 if (lock)
3010 di->di_flags |= DI_FLAGS_LOCK;
3011 else
3012 di->di_flags &= ~DI_FLAGS_LOCK;
3013 item_lock(&di->di_tv, deep, lock);
3014 }
3015 }
3016 *name_end = cc;
3017 }
3018 else if (lp->ll_range)
3019 {
3020 listitem_T *li = lp->ll_li;
3021
3022 /* (un)lock a range of List items. */
3023 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3024 {
3025 item_lock(&li->li_tv, deep, lock);
3026 li = li->li_next;
3027 ++lp->ll_n1;
3028 }
3029 }
3030 else if (lp->ll_list != NULL)
3031 /* (un)lock a List item. */
3032 item_lock(&lp->ll_li->li_tv, deep, lock);
3033 else
3034 /* un(lock) a Dictionary item. */
3035 item_lock(&lp->ll_di->di_tv, deep, lock);
3036
3037 return ret;
3038}
3039
3040/*
3041 * Lock or unlock an item. "deep" is nr of levels to go.
3042 */
3043 static void
3044item_lock(tv, deep, lock)
3045 typval_T *tv;
3046 int deep;
3047 int lock;
3048{
3049 static int recurse = 0;
3050 list_T *l;
3051 listitem_T *li;
3052 dict_T *d;
3053 hashitem_T *hi;
3054 int todo;
3055
3056 if (recurse >= DICT_MAXNEST)
3057 {
3058 EMSG(_("E743: variable nested too deep for (un)lock"));
3059 return;
3060 }
3061 if (deep == 0)
3062 return;
3063 ++recurse;
3064
3065 /* lock/unlock the item itself */
3066 if (lock)
3067 tv->v_lock |= VAR_LOCKED;
3068 else
3069 tv->v_lock &= ~VAR_LOCKED;
3070
3071 switch (tv->v_type)
3072 {
3073 case VAR_LIST:
3074 if ((l = tv->vval.v_list) != NULL)
3075 {
3076 if (lock)
3077 l->lv_lock |= VAR_LOCKED;
3078 else
3079 l->lv_lock &= ~VAR_LOCKED;
3080 if (deep < 0 || deep > 1)
3081 /* recursive: lock/unlock the items the List contains */
3082 for (li = l->lv_first; li != NULL; li = li->li_next)
3083 item_lock(&li->li_tv, deep - 1, lock);
3084 }
3085 break;
3086 case VAR_DICT:
3087 if ((d = tv->vval.v_dict) != NULL)
3088 {
3089 if (lock)
3090 d->dv_lock |= VAR_LOCKED;
3091 else
3092 d->dv_lock &= ~VAR_LOCKED;
3093 if (deep < 0 || deep > 1)
3094 {
3095 /* recursive: lock/unlock the items the List contains */
3096 todo = d->dv_hashtab.ht_used;
3097 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3098 {
3099 if (!HASHITEM_EMPTY(hi))
3100 {
3101 --todo;
3102 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3103 }
3104 }
3105 }
3106 }
3107 }
3108 --recurse;
3109}
3110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3112/*
3113 * Delete all "menutrans_" variables.
3114 */
3115 void
3116del_menutrans_vars()
3117{
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003119 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 hash_lock(&globvarht);
3122 todo = globvarht.ht_used;
3123 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003124 {
3125 if (!HASHITEM_EMPTY(hi))
3126 {
3127 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3129 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003130 }
3131 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134#endif
3135
3136#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3137
3138/*
3139 * Local string buffer for the next two functions to store a variable name
3140 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3141 * get_user_var_name().
3142 */
3143
3144static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3145
3146static char_u *varnamebuf = NULL;
3147static int varnamebuflen = 0;
3148
3149/*
3150 * Function to concatenate a prefix and a variable name.
3151 */
3152 static char_u *
3153cat_prefix_varname(prefix, name)
3154 int prefix;
3155 char_u *name;
3156{
3157 int len;
3158
3159 len = (int)STRLEN(name) + 3;
3160 if (len > varnamebuflen)
3161 {
3162 vim_free(varnamebuf);
3163 len += 10; /* some additional space */
3164 varnamebuf = alloc(len);
3165 if (varnamebuf == NULL)
3166 {
3167 varnamebuflen = 0;
3168 return NULL;
3169 }
3170 varnamebuflen = len;
3171 }
3172 *varnamebuf = prefix;
3173 varnamebuf[1] = ':';
3174 STRCPY(varnamebuf + 2, name);
3175 return varnamebuf;
3176}
3177
3178/*
3179 * Function given to ExpandGeneric() to obtain the list of user defined
3180 * (global/buffer/window/built-in) variable names.
3181 */
3182/*ARGSUSED*/
3183 char_u *
3184get_user_var_name(xp, idx)
3185 expand_T *xp;
3186 int idx;
3187{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003188 static long_u gdone;
3189 static long_u bdone;
3190 static long_u wdone;
3191 static int vidx;
3192 static hashitem_T *hi;
3193 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003196 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003197
3198 /* Global variables */
3199 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003201 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003203 else
3204 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003205 while (HASHITEM_EMPTY(hi))
3206 ++hi;
3207 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3208 return cat_prefix_varname('g', hi->hi_key);
3209 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003211
3212 /* b: variables */
3213 ht = &curbuf->b_vars.dv_hashtab;
3214 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003216 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003218 else
3219 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003220 while (HASHITEM_EMPTY(hi))
3221 ++hi;
3222 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003226 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return (char_u *)"b:changedtick";
3228 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003229
3230 /* w: variables */
3231 ht = &curwin->w_vars.dv_hashtab;
3232 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003234 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003236 else
3237 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003238 while (HASHITEM_EMPTY(hi))
3239 ++hi;
3240 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003242
3243 /* v: variables */
3244 if (vidx < VV_LEN)
3245 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
3247 vim_free(varnamebuf);
3248 varnamebuf = NULL;
3249 varnamebuflen = 0;
3250 return NULL;
3251}
3252
3253#endif /* FEAT_CMDL_COMPL */
3254
3255/*
3256 * types for expressions.
3257 */
3258typedef enum
3259{
3260 TYPE_UNKNOWN = 0
3261 , TYPE_EQUAL /* == */
3262 , TYPE_NEQUAL /* != */
3263 , TYPE_GREATER /* > */
3264 , TYPE_GEQUAL /* >= */
3265 , TYPE_SMALLER /* < */
3266 , TYPE_SEQUAL /* <= */
3267 , TYPE_MATCH /* =~ */
3268 , TYPE_NOMATCH /* !~ */
3269} exptype_T;
3270
3271/*
3272 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3275 */
3276
3277/*
3278 * Handle zero level expression.
3279 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 * Return OK or FAIL.
3282 */
3283 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003284eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 char_u **nextcmd;
3288 int evaluate;
3289{
3290 int ret;
3291 char_u *p;
3292
3293 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (ret == FAIL || !ends_excmd(*p))
3296 {
3297 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003298 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 /*
3300 * Report the invalid expression unless the expression evaluation has
3301 * been cancelled due to an aborting error, an interrupt, or an
3302 * exception.
3303 */
3304 if (!aborting())
3305 EMSG2(_(e_invexpr2), arg);
3306 ret = FAIL;
3307 }
3308 if (nextcmd != NULL)
3309 *nextcmd = check_nextcmd(p);
3310
3311 return ret;
3312}
3313
3314/*
3315 * Handle top level expression:
3316 * expr1 ? expr0 : expr0
3317 *
3318 * "arg" must point to the first non-white of the expression.
3319 * "arg" is advanced to the next non-white after the recognized expression.
3320 *
3321 * Return OK or FAIL.
3322 */
3323 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 int evaluate;
3328{
3329 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
3332 /*
3333 * Get the first variable.
3334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return FAIL;
3337
3338 if ((*arg)[0] == '?')
3339 {
3340 result = FALSE;
3341 if (evaluate)
3342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003343 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
3347
3348 /*
3349 * Get the second variable.
3350 */
3351 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 return FAIL;
3354
3355 /*
3356 * Check for the ":".
3357 */
3358 if ((*arg)[0] != ':')
3359 {
3360 EMSG(_("E109: Missing ':' after '?'"));
3361 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 return FAIL;
3364 }
3365
3366 /*
3367 * Get the third variable.
3368 */
3369 *arg = skipwhite(*arg + 1);
3370 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3371 {
3372 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return FAIL;
3375 }
3376 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379
3380 return OK;
3381}
3382
3383/*
3384 * Handle first level expression:
3385 * expr2 || expr2 || expr2 logical OR
3386 *
3387 * "arg" must point to the first non-white of the expression.
3388 * "arg" is advanced to the next non-white after the recognized expression.
3389 *
3390 * Return OK or FAIL.
3391 */
3392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 int evaluate;
3397{
Bram Moolenaar33570922005-01-25 22:26:29 +00003398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 long result;
3400 int first;
3401
3402 /*
3403 * Get the first variable.
3404 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return FAIL;
3407
3408 /*
3409 * Repeat until there is no following "||".
3410 */
3411 first = TRUE;
3412 result = FALSE;
3413 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3414 {
3415 if (evaluate && first)
3416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003417 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 first = FALSE;
3421 }
3422
3423 /*
3424 * Get the second variable.
3425 */
3426 *arg = skipwhite(*arg + 2);
3427 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3428 return FAIL;
3429
3430 /*
3431 * Compute the result.
3432 */
3433 if (evaluate && !result)
3434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 if (evaluate)
3440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 rettv->v_type = VAR_NUMBER;
3442 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 }
3445
3446 return OK;
3447}
3448
3449/*
3450 * Handle second level expression:
3451 * expr3 && expr3 && expr3 logical AND
3452 *
3453 * "arg" must point to the first non-white of the expression.
3454 * "arg" is advanced to the next non-white after the recognized expression.
3455 *
3456 * Return OK or FAIL.
3457 */
3458 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int evaluate;
3463{
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 long result;
3466 int first;
3467
3468 /*
3469 * Get the first variable.
3470 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473
3474 /*
3475 * Repeat until there is no following "&&".
3476 */
3477 first = TRUE;
3478 result = TRUE;
3479 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3480 {
3481 if (evaluate && first)
3482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 first = FALSE;
3487 }
3488
3489 /*
3490 * Get the second variable.
3491 */
3492 *arg = skipwhite(*arg + 2);
3493 if (eval4(arg, &var2, evaluate && result) == FAIL)
3494 return FAIL;
3495
3496 /*
3497 * Compute the result.
3498 */
3499 if (evaluate && result)
3500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 if (evaluate)
3506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003507 rettv->v_type = VAR_NUMBER;
3508 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 }
3511
3512 return OK;
3513}
3514
3515/*
3516 * Handle third level expression:
3517 * var1 == var2
3518 * var1 =~ var2
3519 * var1 != var2
3520 * var1 !~ var2
3521 * var1 > var2
3522 * var1 >= var2
3523 * var1 < var2
3524 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003525 * var1 is var2
3526 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 *
3528 * "arg" must point to the first non-white of the expression.
3529 * "arg" is advanced to the next non-white after the recognized expression.
3530 *
3531 * Return OK or FAIL.
3532 */
3533 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 int evaluate;
3538{
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_u *p;
3541 int i;
3542 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003543 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 int len = 2;
3545 long n1, n2;
3546 char_u *s1, *s2;
3547 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3548 regmatch_T regmatch;
3549 int ic;
3550 char_u *save_cpo;
3551
3552 /*
3553 * Get the first variable.
3554 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 return FAIL;
3557
3558 p = *arg;
3559 switch (p[0])
3560 {
3561 case '=': if (p[1] == '=')
3562 type = TYPE_EQUAL;
3563 else if (p[1] == '~')
3564 type = TYPE_MATCH;
3565 break;
3566 case '!': if (p[1] == '=')
3567 type = TYPE_NEQUAL;
3568 else if (p[1] == '~')
3569 type = TYPE_NOMATCH;
3570 break;
3571 case '>': if (p[1] != '=')
3572 {
3573 type = TYPE_GREATER;
3574 len = 1;
3575 }
3576 else
3577 type = TYPE_GEQUAL;
3578 break;
3579 case '<': if (p[1] != '=')
3580 {
3581 type = TYPE_SMALLER;
3582 len = 1;
3583 }
3584 else
3585 type = TYPE_SEQUAL;
3586 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003587 case 'i': if (p[1] == 's')
3588 {
3589 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3590 len = 5;
3591 if (!vim_isIDc(p[len]))
3592 {
3593 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3594 type_is = TRUE;
3595 }
3596 }
3597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /*
3601 * If there is a comparitive operator, use it.
3602 */
3603 if (type != TYPE_UNKNOWN)
3604 {
3605 /* extra question mark appended: ignore case */
3606 if (p[len] == '?')
3607 {
3608 ic = TRUE;
3609 ++len;
3610 }
3611 /* extra '#' appended: match case */
3612 else if (p[len] == '#')
3613 {
3614 ic = FALSE;
3615 ++len;
3616 }
3617 /* nothing appened: use 'ignorecase' */
3618 else
3619 ic = p_ic;
3620
3621 /*
3622 * Get the second variable.
3623 */
3624 *arg = skipwhite(p + len);
3625 if (eval5(arg, &var2, evaluate) == FAIL)
3626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return FAIL;
3629 }
3630
3631 if (evaluate)
3632 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003633 if (type_is && rettv->v_type != var2.v_type)
3634 {
3635 /* For "is" a different type always means FALSE, for "notis"
3636 * it means TRUE. */
3637 n1 = (type == TYPE_NEQUAL);
3638 }
3639 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3640 {
3641 if (type_is)
3642 {
3643 n1 = (rettv->v_type == var2.v_type
3644 && rettv->vval.v_list == var2.vval.v_list);
3645 if (type == TYPE_NEQUAL)
3646 n1 = !n1;
3647 }
3648 else if (rettv->v_type != var2.v_type
3649 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3650 {
3651 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003652 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003653 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003654 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003655 clear_tv(rettv);
3656 clear_tv(&var2);
3657 return FAIL;
3658 }
3659 else
3660 {
3661 /* Compare two Lists for being equal or unequal. */
3662 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3663 if (type == TYPE_NEQUAL)
3664 n1 = !n1;
3665 }
3666 }
3667
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003668 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3669 {
3670 if (type_is)
3671 {
3672 n1 = (rettv->v_type == var2.v_type
3673 && rettv->vval.v_dict == var2.vval.v_dict);
3674 if (type == TYPE_NEQUAL)
3675 n1 = !n1;
3676 }
3677 else if (rettv->v_type != var2.v_type
3678 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3679 {
3680 if (rettv->v_type != var2.v_type)
3681 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3682 else
3683 EMSG(_("E736: Invalid operation for Dictionary"));
3684 clear_tv(rettv);
3685 clear_tv(&var2);
3686 return FAIL;
3687 }
3688 else
3689 {
3690 /* Compare two Dictionaries for being equal or unequal. */
3691 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3692 if (type == TYPE_NEQUAL)
3693 n1 = !n1;
3694 }
3695 }
3696
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003697 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3698 {
3699 if (rettv->v_type != var2.v_type
3700 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3701 {
3702 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003703 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003704 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003705 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003706 clear_tv(rettv);
3707 clear_tv(&var2);
3708 return FAIL;
3709 }
3710 else
3711 {
3712 /* Compare two Funcrefs for being equal or unequal. */
3713 if (rettv->vval.v_string == NULL
3714 || var2.vval.v_string == NULL)
3715 n1 = FALSE;
3716 else
3717 n1 = STRCMP(rettv->vval.v_string,
3718 var2.vval.v_string) == 0;
3719 if (type == TYPE_NEQUAL)
3720 n1 = !n1;
3721 }
3722 }
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 /*
3725 * If one of the two variables is a number, compare as a number.
3726 * When using "=~" or "!~", always compare as string.
3727 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003728 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003731 n1 = get_tv_number(rettv);
3732 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 switch (type)
3734 {
3735 case TYPE_EQUAL: n1 = (n1 == n2); break;
3736 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3737 case TYPE_GREATER: n1 = (n1 > n2); break;
3738 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3739 case TYPE_SMALLER: n1 = (n1 < n2); break;
3740 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3741 case TYPE_UNKNOWN:
3742 case TYPE_MATCH:
3743 case TYPE_NOMATCH: break; /* avoid gcc warning */
3744 }
3745 }
3746 else
3747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003748 s1 = get_tv_string_buf(rettv, buf1);
3749 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3751 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3752 else
3753 i = 0;
3754 n1 = FALSE;
3755 switch (type)
3756 {
3757 case TYPE_EQUAL: n1 = (i == 0); break;
3758 case TYPE_NEQUAL: n1 = (i != 0); break;
3759 case TYPE_GREATER: n1 = (i > 0); break;
3760 case TYPE_GEQUAL: n1 = (i >= 0); break;
3761 case TYPE_SMALLER: n1 = (i < 0); break;
3762 case TYPE_SEQUAL: n1 = (i <= 0); break;
3763
3764 case TYPE_MATCH:
3765 case TYPE_NOMATCH:
3766 /* avoid 'l' flag in 'cpoptions' */
3767 save_cpo = p_cpo;
3768 p_cpo = (char_u *)"";
3769 regmatch.regprog = vim_regcomp(s2,
3770 RE_MAGIC + RE_STRING);
3771 regmatch.rm_ic = ic;
3772 if (regmatch.regprog != NULL)
3773 {
3774 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3775 vim_free(regmatch.regprog);
3776 if (type == TYPE_NOMATCH)
3777 n1 = !n1;
3778 }
3779 p_cpo = save_cpo;
3780 break;
3781
3782 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3783 }
3784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 clear_tv(rettv);
3786 clear_tv(&var2);
3787 rettv->v_type = VAR_NUMBER;
3788 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791
3792 return OK;
3793}
3794
3795/*
3796 * Handle fourth level expression:
3797 * + number addition
3798 * - number subtraction
3799 * . string concatenation
3800 *
3801 * "arg" must point to the first non-white of the expression.
3802 * "arg" is advanced to the next non-white after the recognized expression.
3803 *
3804 * Return OK or FAIL.
3805 */
3806 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int evaluate;
3811{
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 typval_T var2;
3813 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 int op;
3815 long n1, n2;
3816 char_u *s1, *s2;
3817 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3818 char_u *p;
3819
3820 /*
3821 * Get the first variable.
3822 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 return FAIL;
3825
3826 /*
3827 * Repeat computing, until no '+', '-' or '.' is following.
3828 */
3829 for (;;)
3830 {
3831 op = **arg;
3832 if (op != '+' && op != '-' && op != '.')
3833 break;
3834
3835 /*
3836 * Get the second variable.
3837 */
3838 *arg = skipwhite(*arg + 1);
3839 if (eval6(arg, &var2, evaluate) == FAIL)
3840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 return FAIL;
3843 }
3844
3845 if (evaluate)
3846 {
3847 /*
3848 * Compute the result.
3849 */
3850 if (op == '.')
3851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 s1 = get_tv_string_buf(rettv, buf1);
3853 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003854 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 clear_tv(rettv);
3856 rettv->v_type = VAR_STRING;
3857 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003859 else if (op == '+' && rettv->v_type == VAR_LIST
3860 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003861 {
3862 /* concatenate Lists */
3863 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3864 &var3) == FAIL)
3865 {
3866 clear_tv(rettv);
3867 clear_tv(&var2);
3868 return FAIL;
3869 }
3870 clear_tv(rettv);
3871 *rettv = var3;
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 else
3874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 n1 = get_tv_number(rettv);
3876 n2 = get_tv_number(&var2);
3877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (op == '+')
3879 n1 = n1 + n2;
3880 else
3881 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 rettv->v_type = VAR_NUMBER;
3883 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887 }
3888 return OK;
3889}
3890
3891/*
3892 * Handle fifth level expression:
3893 * * number multiplication
3894 * / number division
3895 * % number modulo
3896 *
3897 * "arg" must point to the first non-white of the expression.
3898 * "arg" is advanced to the next non-white after the recognized expression.
3899 *
3900 * Return OK or FAIL.
3901 */
3902 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 int evaluate;
3907{
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 int op;
3910 long n1, n2;
3911
3912 /*
3913 * Get the first variable.
3914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 return FAIL;
3917
3918 /*
3919 * Repeat computing, until no '*', '/' or '%' is following.
3920 */
3921 for (;;)
3922 {
3923 op = **arg;
3924 if (op != '*' && op != '/' && op != '%')
3925 break;
3926
3927 if (evaluate)
3928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003929 n1 = get_tv_number(rettv);
3930 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 else
3933 n1 = 0;
3934
3935 /*
3936 * Get the second variable.
3937 */
3938 *arg = skipwhite(*arg + 1);
3939 if (eval7(arg, &var2, evaluate) == FAIL)
3940 return FAIL;
3941
3942 if (evaluate)
3943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 n2 = get_tv_number(&var2);
3945 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 /*
3948 * Compute the result.
3949 */
3950 if (op == '*')
3951 n1 = n1 * n2;
3952 else if (op == '/')
3953 {
3954 if (n2 == 0) /* give an error message? */
3955 n1 = 0x7fffffffL;
3956 else
3957 n1 = n1 / n2;
3958 }
3959 else
3960 {
3961 if (n2 == 0) /* give an error message? */
3962 n1 = 0;
3963 else
3964 n1 = n1 % n2;
3965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966 rettv->v_type = VAR_NUMBER;
3967 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970
3971 return OK;
3972}
3973
3974/*
3975 * Handle sixth level expression:
3976 * number number constant
3977 * "string" string contstant
3978 * 'string' literal string contstant
3979 * &option-name option value
3980 * @r register contents
3981 * identifier variable value
3982 * function() function call
3983 * $VAR environment variable
3984 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003985 * [expr, expr] List
3986 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 *
3988 * Also handle:
3989 * ! in front logical NOT
3990 * - in front unary minus
3991 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003992 * trailing [] subscript in String or List
3993 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 *
3995 * "arg" must point to the first non-white of the expression.
3996 * "arg" is advanced to the next non-white after the recognized expression.
3997 *
3998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int evaluate;
4005{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 long n;
4007 int len;
4008 char_u *s;
4009 int val;
4010 char_u *start_leader, *end_leader;
4011 int ret = OK;
4012 char_u *alias;
4013
4014 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004016 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020 /*
4021 * Skip '!' and '-' characters. They are handled later.
4022 */
4023 start_leader = *arg;
4024 while (**arg == '!' || **arg == '-' || **arg == '+')
4025 *arg = skipwhite(*arg + 1);
4026 end_leader = *arg;
4027
4028 switch (**arg)
4029 {
4030 /*
4031 * Number constant.
4032 */
4033 case '0':
4034 case '1':
4035 case '2':
4036 case '3':
4037 case '4':
4038 case '5':
4039 case '6':
4040 case '7':
4041 case '8':
4042 case '9':
4043 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4044 *arg += len;
4045 if (evaluate)
4046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 rettv->v_type = VAR_NUMBER;
4048 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 break;
4051
4052 /*
4053 * String constant: "string".
4054 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 break;
4057
4058 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004059 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 break;
4063
4064 /*
4065 * List: [expr, expr]
4066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 break;
4069
4070 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 * Dictionary: {key: val, key: val}
4072 */
4073 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4074 break;
4075
4076 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004077 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004079 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 break;
4081
4082 /*
4083 * Environment variable: $VAR.
4084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 break;
4087
4088 /*
4089 * Register contents: @r.
4090 */
4091 case '@': ++*arg;
4092 if (evaluate)
4093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 rettv->v_type = VAR_STRING;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00004095 rettv->vval.v_string = get_reg_contents(**arg,
4096 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 if (**arg != NUL)
4099 ++*arg;
4100 break;
4101
4102 /*
4103 * nested expression: (expression).
4104 */
4105 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (**arg == ')')
4108 ++*arg;
4109 else if (ret == OK)
4110 {
4111 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 ret = FAIL;
4114 }
4115 break;
4116
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004120
4121 if (ret == NOTDONE)
4122 {
4123 /*
4124 * Must be a variable or function name.
4125 * Can also be a curly-braces kind of name: {expr}.
4126 */
4127 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004128 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004129 if (alias != NULL)
4130 s = alias;
4131
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004132 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004133 ret = FAIL;
4134 else
4135 {
4136 if (**arg == '(') /* recursive! */
4137 {
4138 /* If "s" is the name of a variable of type VAR_FUNC
4139 * use its contents. */
4140 s = deref_func_name(s, &len);
4141
4142 /* Invoke the function. */
4143 ret = get_func_tv(s, len, rettv, arg,
4144 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004145 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 /* Stop the expression evaluation when immediately
4147 * aborting on error, or when an interrupt occurred or
4148 * an exception was thrown but not caught. */
4149 if (aborting())
4150 {
4151 if (ret == OK)
4152 clear_tv(rettv);
4153 ret = FAIL;
4154 }
4155 }
4156 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004157 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004158 else
4159 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 }
4161
4162 if (alias != NULL)
4163 vim_free(alias);
4164 }
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 *arg = skipwhite(*arg);
4167
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004168 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4169 * expr(expr). */
4170 if (ret == OK)
4171 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4175 */
4176 if (ret == OK && evaluate && end_leader > start_leader)
4177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 while (end_leader > start_leader)
4180 {
4181 --end_leader;
4182 if (*end_leader == '!')
4183 val = !val;
4184 else if (*end_leader == '-')
4185 val = -val;
4186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
4188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191
4192 return ret;
4193}
4194
4195/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004196 * Evaluate an "[expr]" or "[expr:expr]" index.
4197 * "*arg" points to the '['.
4198 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4199 */
4200 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004201eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004202 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004204 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004205 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206{
4207 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004208 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 long len = -1;
4211 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004213 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004217 if (verbose)
4218 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004219 return FAIL;
4220 }
4221
Bram Moolenaar8c711452005-01-14 21:53:12 +00004222 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004224 /*
4225 * dict.name
4226 */
4227 key = *arg + 1;
4228 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4229 ;
4230 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004232 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004233 }
4234 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004236 /*
4237 * something[idx]
4238 *
4239 * Get the (first) variable from inside the [].
4240 */
4241 *arg = skipwhite(*arg + 1);
4242 if (**arg == ':')
4243 empty1 = TRUE;
4244 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4245 return FAIL;
4246
4247 /*
4248 * Get the second variable from inside the [:].
4249 */
4250 if (**arg == ':')
4251 {
4252 range = TRUE;
4253 *arg = skipwhite(*arg + 1);
4254 if (**arg == ']')
4255 empty2 = TRUE;
4256 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4257 {
4258 clear_tv(&var1);
4259 return FAIL;
4260 }
4261 }
4262
4263 /* Check for the ']'. */
4264 if (**arg != ']')
4265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004266 if (verbose)
4267 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004268 clear_tv(&var1);
4269 if (range)
4270 clear_tv(&var2);
4271 return FAIL;
4272 }
4273 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 }
4275
4276 if (evaluate)
4277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 n1 = 0;
4279 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 n1 = get_tv_number(&var1);
4282 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004283 }
4284 if (range)
4285 {
4286 if (empty2)
4287 n2 = -1;
4288 else
4289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 n2 = get_tv_number(&var2);
4291 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 }
4293 }
4294
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004296 {
4297 case VAR_NUMBER:
4298 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004300 len = (long)STRLEN(s);
4301 if (range)
4302 {
4303 /* The resulting variable is a substring. If the indexes
4304 * are out of range the result is empty. */
4305 if (n1 < 0)
4306 {
4307 n1 = len + n1;
4308 if (n1 < 0)
4309 n1 = 0;
4310 }
4311 if (n2 < 0)
4312 n2 = len + n2;
4313 else if (n2 >= len)
4314 n2 = len;
4315 if (n1 >= len || n2 < 0 || n1 > n2)
4316 s = NULL;
4317 else
4318 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4319 }
4320 else
4321 {
4322 /* The resulting variable is a string of a single
4323 * character. If the index is too big or negative the
4324 * result is empty. */
4325 if (n1 >= len || n1 < 0)
4326 s = NULL;
4327 else
4328 s = vim_strnsave(s + n1, 1);
4329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
4331 rettv->v_type = VAR_STRING;
4332 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004333 break;
4334
4335 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004337 if (n1 < 0)
4338 n1 = len + n1;
4339 if (!empty1 && (n1 < 0 || n1 >= len))
4340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004341 if (verbose)
4342 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004343 return FAIL;
4344 }
4345 if (range)
4346 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004347 list_T *l;
4348 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004349
4350 if (n2 < 0)
4351 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004353 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004354 if (verbose)
4355 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004356 return FAIL;
4357 }
4358 l = list_alloc();
4359 if (l == NULL)
4360 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004362 n1 <= n2; ++n1)
4363 {
4364 if (list_append_tv(l, &item->li_tv) == FAIL)
4365 {
4366 list_free(l);
4367 return FAIL;
4368 }
4369 item = item->li_next;
4370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
4372 rettv->v_type = VAR_LIST;
4373 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004374 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004375 }
4376 else
4377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
4381 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004382 }
4383 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384
4385 case VAR_DICT:
4386 if (range)
4387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004388 if (verbose)
4389 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 if (len == -1)
4391 clear_tv(&var1);
4392 return FAIL;
4393 }
4394 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004396
4397 if (len == -1)
4398 {
4399 key = get_tv_string(&var1);
4400 if (*key == NUL)
4401 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004402 if (verbose)
4403 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004404 clear_tv(&var1);
4405 return FAIL;
4406 }
4407 }
4408
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004409 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004411 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004412 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004413 if (len == -1)
4414 clear_tv(&var1);
4415 if (item == NULL)
4416 return FAIL;
4417
4418 copy_tv(&item->di_tv, &var1);
4419 clear_tv(rettv);
4420 *rettv = var1;
4421 }
4422 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 }
4424 }
4425
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426 return OK;
4427}
4428
4429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 * Get an option value.
4431 * "arg" points to the '&' or '+' before the option name.
4432 * "arg" is advanced to character after the option name.
4433 * Return OK or FAIL.
4434 */
4435 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004438 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 int evaluate;
4440{
4441 char_u *option_end;
4442 long numval;
4443 char_u *stringval;
4444 int opt_type;
4445 int c;
4446 int working = (**arg == '+'); /* has("+option") */
4447 int ret = OK;
4448 int opt_flags;
4449
4450 /*
4451 * Isolate the option name and find its value.
4452 */
4453 option_end = find_option_end(arg, &opt_flags);
4454 if (option_end == NULL)
4455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 EMSG2(_("E112: Option name missing: %s"), *arg);
4458 return FAIL;
4459 }
4460
4461 if (!evaluate)
4462 {
4463 *arg = option_end;
4464 return OK;
4465 }
4466
4467 c = *option_end;
4468 *option_end = NUL;
4469 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 if (opt_type == -3) /* invalid name */
4473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 EMSG2(_("E113: Unknown option: %s"), *arg);
4476 ret = FAIL;
4477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 if (opt_type == -2) /* hidden string option */
4481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 rettv->v_type = VAR_STRING;
4483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else if (opt_type == -1) /* hidden number option */
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 rettv->v_type = VAR_NUMBER;
4488 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 else if (opt_type == 1) /* number option */
4491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004492 rettv->v_type = VAR_NUMBER;
4493 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 else /* string option */
4496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004497 rettv->v_type = VAR_STRING;
4498 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 }
4501 else if (working && (opt_type == -2 || opt_type == -1))
4502 ret = FAIL;
4503
4504 *option_end = c; /* put back for error messages */
4505 *arg = option_end;
4506
4507 return ret;
4508}
4509
4510/*
4511 * Allocate a variable for a string constant.
4512 * Return OK or FAIL.
4513 */
4514 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 int evaluate;
4519{
4520 char_u *p;
4521 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 int extra = 0;
4523
4524 /*
4525 * Find the end of the string, skipping backslashed characters.
4526 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004527 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 if (*p == '\\' && p[1] != NUL)
4530 {
4531 ++p;
4532 /* A "\<x>" form occupies at least 4 characters, and produces up
4533 * to 6 characters: reserve space for 2 extra */
4534 if (*p == '<')
4535 extra += 2;
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
4539 if (*p != '"')
4540 {
4541 EMSG2(_("E114: Missing quote: %s"), *arg);
4542 return FAIL;
4543 }
4544
4545 /* If only parsing, set *arg and return here */
4546 if (!evaluate)
4547 {
4548 *arg = p + 1;
4549 return OK;
4550 }
4551
4552 /*
4553 * Copy the string into allocated memory, handling backslashed
4554 * characters.
4555 */
4556 name = alloc((unsigned)(p - *arg + extra));
4557 if (name == NULL)
4558 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 rettv->v_type = VAR_STRING;
4560 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 if (*p == '\\')
4565 {
4566 switch (*++p)
4567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 case 'b': *name++ = BS; ++p; break;
4569 case 'e': *name++ = ESC; ++p; break;
4570 case 'f': *name++ = FF; ++p; break;
4571 case 'n': *name++ = NL; ++p; break;
4572 case 'r': *name++ = CAR; ++p; break;
4573 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 case 'X': /* hex: "\x1", "\x12" */
4576 case 'x':
4577 case 'u': /* Unicode: "\u0023" */
4578 case 'U':
4579 if (vim_isxdigit(p[1]))
4580 {
4581 int n, nr;
4582 int c = toupper(*p);
4583
4584 if (c == 'X')
4585 n = 2;
4586 else
4587 n = 4;
4588 nr = 0;
4589 while (--n >= 0 && vim_isxdigit(p[1]))
4590 {
4591 ++p;
4592 nr = (nr << 4) + hex2nr(*p);
4593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_MBYTE
4596 /* For "\u" store the number according to
4597 * 'encoding'. */
4598 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 else
4601#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 break;
4605
4606 /* octal: "\1", "\12", "\123" */
4607 case '0':
4608 case '1':
4609 case '2':
4610 case '3':
4611 case '4':
4612 case '5':
4613 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 case '7': *name = *p++ - '0';
4615 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 *name = (*name << 3) + *p++ - '0';
4618 if (*p >= '0' && *p <= '7')
4619 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623
4624 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (extra != 0)
4627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630 }
4631 /* FALLTHROUGH */
4632
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 break;
4635 }
4636 }
4637 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 *arg = p + 1;
4643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return OK;
4645}
4646
4647/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 * Return OK or FAIL.
4650 */
4651 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 int evaluate;
4656{
4657 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004659 int reduce = 0;
4660
4661 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004663 */
4664 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004667 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004669 break;
4670 ++reduce;
4671 ++p;
4672 }
4673 }
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004678 return FAIL;
4679 }
4680
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004682 if (!evaluate)
4683 {
4684 *arg = p + 1;
4685 return OK;
4686 }
4687
4688 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004690 */
4691 str = alloc((unsigned)((p - *arg) - reduce));
4692 if (str == NULL)
4693 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 rettv->v_type = VAR_STRING;
4695 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004696
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004702 break;
4703 ++p;
4704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004708 *arg = p + 1;
4709
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004710 return OK;
4711}
4712
4713/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 * Allocate a variable for a List and fill it from "*arg".
4715 * Return OK or FAIL.
4716 */
4717 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004720 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 int evaluate;
4722{
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 list_T *l = NULL;
4724 typval_T tv;
4725 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726
4727 if (evaluate)
4728 {
4729 l = list_alloc();
4730 if (l == NULL)
4731 return FAIL;
4732 }
4733
4734 *arg = skipwhite(*arg + 1);
4735 while (**arg != ']' && **arg != NUL)
4736 {
4737 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4738 goto failret;
4739 if (evaluate)
4740 {
4741 item = listitem_alloc();
4742 if (item != NULL)
4743 {
4744 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004745 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 list_append(l, item);
4747 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004748 else
4749 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 }
4751
4752 if (**arg == ']')
4753 break;
4754 if (**arg != ',')
4755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 goto failret;
4758 }
4759 *arg = skipwhite(*arg + 1);
4760 }
4761
4762 if (**arg != ']')
4763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765failret:
4766 if (evaluate)
4767 list_free(l);
4768 return FAIL;
4769 }
4770
4771 *arg = skipwhite(*arg + 1);
4772 if (evaluate)
4773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 rettv->v_type = VAR_LIST;
4775 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 ++l->lv_refcount;
4777 }
4778
4779 return OK;
4780}
4781
4782/*
4783 * Allocate an empty header for a list.
4784 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786list_alloc()
4787{
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789}
4790
4791/*
4792 * Unreference a list: decrement the reference count and free it when it
4793 * becomes zero.
4794 */
4795 static void
4796list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004797 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798{
4799 if (l != NULL && --l->lv_refcount <= 0)
4800 list_free(l);
4801}
4802
4803/*
4804 * Free a list, including all items it points to.
4805 * Ignores the reference count.
4806 */
4807 static void
4808list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004809 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810{
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 listitem_T *item;
4812 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813
4814 for (item = l->lv_first; item != NULL; item = next)
4815 {
4816 next = item->li_next;
4817 listitem_free(item);
4818 }
4819 vim_free(l);
4820}
4821
4822/*
4823 * Allocate a list item.
4824 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004825 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826listitem_alloc()
4827{
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829}
4830
4831/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004832 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 */
4834 static void
4835listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004836 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 vim_free(item);
4840}
4841
4842/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004843 * Remove a list item from a List and free it. Also clears the value.
4844 */
4845 static void
4846listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004847 list_T *l;
4848 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004849{
4850 list_remove(l, item, item);
4851 listitem_free(item);
4852}
4853
4854/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 * Get the number of items in a list.
4856 */
4857 static long
4858list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004859 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 if (l == NULL)
4862 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004863 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864}
4865
4866/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004867 * Return TRUE when two lists have exactly the same values.
4868 */
4869 static int
4870list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 list_T *l1;
4872 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004873 int ic; /* ignore case for strings */
4874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004876
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004877 if (list_len(l1) != list_len(l2))
4878 return FALSE;
4879
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004880 for (item1 = l1->lv_first, item2 = l2->lv_first;
4881 item1 != NULL && item2 != NULL;
4882 item1 = item1->li_next, item2 = item2->li_next)
4883 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4884 return FALSE;
4885 return item1 == NULL && item2 == NULL;
4886}
4887
4888/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004889 * Return TRUE when two dictionaries have exactly the same key/values.
4890 */
4891 static int
4892dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004893 dict_T *d1;
4894 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004895 int ic; /* ignore case for strings */
4896{
Bram Moolenaar33570922005-01-25 22:26:29 +00004897 hashitem_T *hi;
4898 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004899 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004900
4901 if (dict_len(d1) != dict_len(d2))
4902 return FALSE;
4903
Bram Moolenaar33570922005-01-25 22:26:29 +00004904 todo = d1->dv_hashtab.ht_used;
4905 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004907 if (!HASHITEM_EMPTY(hi))
4908 {
4909 item2 = dict_find(d2, hi->hi_key, -1);
4910 if (item2 == NULL)
4911 return FALSE;
4912 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4913 return FALSE;
4914 --todo;
4915 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004916 }
4917 return TRUE;
4918}
4919
4920/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004921 * Return TRUE if "tv1" and "tv2" have the same value.
4922 * Compares the items just like "==" would compare them.
4923 */
4924 static int
4925tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004926 typval_T *tv1;
4927 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004928 int ic; /* ignore case */
4929{
4930 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4931
4932 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4933 {
4934 /* recursive! */
4935 if (tv1->v_type != tv2->v_type
4936 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4937 return FALSE;
4938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004939 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4940 {
4941 /* recursive! */
4942 if (tv1->v_type != tv2->v_type
4943 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4944 return FALSE;
4945 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004946 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4947 {
4948 if (tv1->v_type != tv2->v_type
4949 || tv1->vval.v_string == NULL
4950 || tv2->vval.v_string == NULL
4951 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4952 return FALSE;
4953 }
4954 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4955 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4957 * Don't consider "4x" to be equal to 4. */
4958 if ((tv1->v_type == VAR_STRING
4959 && !string_isa_number(tv1->vval.v_string))
4960 || (tv2->v_type == VAR_STRING
4961 && !string_isa_number(tv2->vval.v_string)))
4962 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004963 if (get_tv_number(tv1) != get_tv_number(tv2))
4964 return FALSE;
4965 }
4966 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4967 get_tv_string_buf(tv2, buf2)) != 0)
4968 return FALSE;
4969 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4970 get_tv_string_buf(tv2, buf2)) != 0)
4971 return FALSE;
4972 return TRUE;
4973}
4974
4975/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004976 * Return TRUE if "tv" is a number without other non-white characters.
4977 */
4978 static int
4979string_isa_number(s)
4980 char_u *s;
4981{
4982 int len;
4983
4984 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4985 return len > 0 && *skipwhite(s + len) == NUL;
4986}
4987
4988/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 * Locate item with index "n" in list "l" and return it.
4990 * A negative index is counted from the end; -1 is the last item.
4991 * Returns NULL when "n" is out of range.
4992 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004993 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004995 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 long n;
4997{
Bram Moolenaar33570922005-01-25 22:26:29 +00004998 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004999 long idx;
5000
5001 if (l == NULL)
5002 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005003
5004 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005006 n = l->lv_len + n;
5007
5008 /* Check for index out of range. */
5009 if (n < 0 || n >= l->lv_len)
5010 return NULL;
5011
5012 /* When there is a cached index may start search from there. */
5013 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005015 if (n < l->lv_idx / 2)
5016 {
5017 /* closest to the start of the list */
5018 item = l->lv_first;
5019 idx = 0;
5020 }
5021 else if (n > (l->lv_idx + l->lv_len) / 2)
5022 {
5023 /* closest to the end of the list */
5024 item = l->lv_last;
5025 idx = l->lv_len - 1;
5026 }
5027 else
5028 {
5029 /* closest to the cached index */
5030 item = l->lv_idx_item;
5031 idx = l->lv_idx;
5032 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 }
5034 else
5035 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005036 if (n < l->lv_len / 2)
5037 {
5038 /* closest to the start of the list */
5039 item = l->lv_first;
5040 idx = 0;
5041 }
5042 else
5043 {
5044 /* closest to the end of the list */
5045 item = l->lv_last;
5046 idx = l->lv_len - 1;
5047 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005048 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005049
5050 while (n > idx)
5051 {
5052 /* search forward */
5053 item = item->li_next;
5054 ++idx;
5055 }
5056 while (n < idx)
5057 {
5058 /* search backward */
5059 item = item->li_prev;
5060 --idx;
5061 }
5062
5063 /* cache the used index */
5064 l->lv_idx = idx;
5065 l->lv_idx_item = item;
5066
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005067 return item;
5068}
5069
5070/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005071 * Locate "item" list "l" and return its index.
5072 * Returns -1 when "item" is not in the list.
5073 */
5074 static long
5075list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 list_T *l;
5077 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005078{
5079 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005081
5082 if (l == NULL)
5083 return -1;
5084 idx = 0;
5085 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5086 ++idx;
5087 if (li == NULL)
5088 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005089 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005090}
5091
5092/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005093 * Append item "item" to the end of list "l".
5094 */
5095 static void
5096list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005097 list_T *l;
5098 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099{
5100 if (l->lv_last == NULL)
5101 {
5102 /* empty list */
5103 l->lv_first = item;
5104 l->lv_last = item;
5105 item->li_prev = NULL;
5106 }
5107 else
5108 {
5109 l->lv_last->li_next = item;
5110 item->li_prev = l->lv_last;
5111 l->lv_last = item;
5112 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005113 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 item->li_next = NULL;
5115}
5116
5117/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005118 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005119 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 */
5121 static int
5122list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 list_T *l;
5124 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005126 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127
Bram Moolenaar05159a02005-02-26 23:04:13 +00005128 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005130 copy_tv(tv, &li->li_tv);
5131 list_append(l, li);
5132 return OK;
5133}
5134
5135/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005136 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005137 * Return FAIL when out of memory.
5138 */
5139 int
5140list_append_dict(list, dict)
5141 list_T *list;
5142 dict_T *dict;
5143{
5144 listitem_T *li = listitem_alloc();
5145
5146 if (li == NULL)
5147 return FAIL;
5148 li->li_tv.v_type = VAR_DICT;
5149 li->li_tv.v_lock = 0;
5150 li->li_tv.vval.v_dict = dict;
5151 list_append(list, li);
5152 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 return OK;
5154}
5155
5156/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005157 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005158 * If "item" is NULL append at the end.
5159 * Return FAIL when out of memory.
5160 */
5161 static int
5162list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 list_T *l;
5164 typval_T *tv;
5165 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005166{
Bram Moolenaar33570922005-01-25 22:26:29 +00005167 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005168
5169 if (ni == NULL)
5170 return FAIL;
5171 copy_tv(tv, &ni->li_tv);
5172 if (item == NULL)
5173 /* Append new item at end of list. */
5174 list_append(l, ni);
5175 else
5176 {
5177 /* Insert new item before existing item. */
5178 ni->li_prev = item->li_prev;
5179 ni->li_next = item;
5180 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005181 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005182 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005183 ++l->lv_idx;
5184 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005185 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005186 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005187 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005188 l->lv_idx_item = NULL;
5189 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005190 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005191 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192 }
5193 return OK;
5194}
5195
5196/*
5197 * Extend "l1" with "l2".
5198 * If "bef" is NULL append at the end, otherwise insert before this item.
5199 * Returns FAIL when out of memory.
5200 */
5201 static int
5202list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 list_T *l1;
5204 list_T *l2;
5205 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005206{
Bram Moolenaar33570922005-01-25 22:26:29 +00005207 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005208
5209 for (item = l2->lv_first; item != NULL; item = item->li_next)
5210 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5211 return FAIL;
5212 return OK;
5213}
5214
5215/*
5216 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5217 * Return FAIL when out of memory.
5218 */
5219 static int
5220list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005221 list_T *l1;
5222 list_T *l2;
5223 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005224{
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005226
5227 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005228 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005229 if (l == NULL)
5230 return FAIL;
5231 tv->v_type = VAR_LIST;
5232 tv->vval.v_list = l;
5233
5234 /* append all items from the second list */
5235 return list_extend(l, l2, NULL);
5236}
5237
5238/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005241 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 * Returns NULL when out of memory.
5243 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005245list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005246 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005248 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249{
Bram Moolenaar33570922005-01-25 22:26:29 +00005250 list_T *copy;
5251 listitem_T *item;
5252 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253
5254 if (orig == NULL)
5255 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256
5257 copy = list_alloc();
5258 if (copy != NULL)
5259 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005260 if (copyID != 0)
5261 {
5262 /* Do this before adding the items, because one of the items may
5263 * refer back to this list. */
5264 orig->lv_copyID = copyID;
5265 orig->lv_copylist = copy;
5266 }
5267 for (item = orig->lv_first; item != NULL && !got_int;
5268 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 {
5270 ni = listitem_alloc();
5271 if (ni == NULL)
5272 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005273 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005274 {
5275 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5276 {
5277 vim_free(ni);
5278 break;
5279 }
5280 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 list_append(copy, ni);
5284 }
5285 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005286 if (item != NULL)
5287 {
5288 list_unref(copy);
5289 copy = NULL;
5290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 return copy;
5294}
5295
5296/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005298 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005301list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 list_T *l;
5303 listitem_T *item;
5304 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305{
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005308 /* notify watchers */
5309 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005311 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312 list_fix_watch(l, ip);
5313 if (ip == item2)
5314 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005316
5317 if (item2->li_next == NULL)
5318 l->lv_last = item->li_prev;
5319 else
5320 item2->li_next->li_prev = item->li_prev;
5321 if (item->li_prev == NULL)
5322 l->lv_first = item2->li_next;
5323 else
5324 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005325 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326}
5327
5328/*
5329 * Return an allocated string with the string representation of a list.
5330 * May return NULL.
5331 */
5332 static char_u *
5333list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335{
5336 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337
5338 if (tv->vval.v_list == NULL)
5339 return NULL;
5340 ga_init2(&ga, (int)sizeof(char), 80);
5341 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005342 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5343 {
5344 vim_free(ga.ga_data);
5345 return NULL;
5346 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 ga_append(&ga, ']');
5348 ga_append(&ga, NUL);
5349 return (char_u *)ga.ga_data;
5350}
5351
5352/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005353 * Join list "l" into a string in "*gap", using separator "sep".
5354 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005355 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005356 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005357 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005358list_join(gap, l, sep, echo)
5359 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005361 char_u *sep;
5362 int echo;
5363{
5364 int first = TRUE;
5365 char_u *tofree;
5366 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005368 char_u *s;
5369
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005370 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005371 {
5372 if (first)
5373 first = FALSE;
5374 else
5375 ga_concat(gap, sep);
5376
5377 if (echo)
5378 s = echo_string(&item->li_tv, &tofree, numbuf);
5379 else
5380 s = tv2string(&item->li_tv, &tofree, numbuf);
5381 if (s != NULL)
5382 ga_concat(gap, s);
5383 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005384 if (s == NULL)
5385 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005386 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005387 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005388}
5389
5390/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 * Allocate an empty header for a dictionary.
5392 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005393 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394dict_alloc()
5395{
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005397
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005399 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005400 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005401 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005402 d->dv_lock = 0;
5403 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005404 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005405 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005406 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407}
5408
5409/*
5410 * Unreference a Dictionary: decrement the reference count and free it when it
5411 * becomes zero.
5412 */
5413 static void
5414dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416{
5417 if (d != NULL && --d->dv_refcount <= 0)
5418 dict_free(d);
5419}
5420
5421/*
5422 * Free a Dictionary, including all items it contains.
5423 * Ignores the reference count.
5424 */
5425 static void
5426dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005429 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005432 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005433 * hashtab. Must not try to resize the hashtab! */
5434 todo = d->dv_hashtab.ht_used;
5435 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005437 if (!HASHITEM_EMPTY(hi))
5438 {
5439 dictitem_free(HI2DI(hi));
5440 --todo;
5441 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 vim_free(d);
5445}
5446
5447/*
5448 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005449 * The "key" is copied to the new item.
5450 * Note that the value of the item "di_tv" still needs to be initialized!
5451 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005454dictitem_alloc(key)
5455 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456{
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005458
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005460 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005462 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005463 di->di_flags = 0;
5464 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005465 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466}
5467
5468/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005469 * Make a copy of a Dictionary item.
5470 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005471 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005472dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005474{
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005476
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005478 if (di != NULL)
5479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005480 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005481 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005482 copy_tv(&org->di_tv, &di->di_tv);
5483 }
5484 return di;
5485}
5486
5487/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005488 * Remove item "item" from Dictionary "dict" and free it.
5489 */
5490 static void
5491dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dict_T *dict;
5493 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005494{
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005496
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005498 if (HASHITEM_EMPTY(hi))
5499 EMSG2(_(e_intern2), "dictitem_remove()");
5500 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005502 dictitem_free(item);
5503}
5504
5505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 * Free a dict item. Also clears the value.
5507 */
5508 static void
5509dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005512 clear_tv(&item->di_tv);
5513 vim_free(item);
5514}
5515
5516/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005517 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5518 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005519 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520 * Returns NULL when out of memory.
5521 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005523dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005525 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005526 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005527{
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 dict_T *copy;
5529 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005530 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005531 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005532
5533 if (orig == NULL)
5534 return NULL;
5535
5536 copy = dict_alloc();
5537 if (copy != NULL)
5538 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005539 if (copyID != 0)
5540 {
5541 orig->dv_copyID = copyID;
5542 orig->dv_copydict = copy;
5543 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005545 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005547 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 --todo;
5550
5551 di = dictitem_alloc(hi->hi_key);
5552 if (di == NULL)
5553 break;
5554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005555 {
5556 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5557 copyID) == FAIL)
5558 {
5559 vim_free(di);
5560 break;
5561 }
5562 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005563 else
5564 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5565 if (dict_add(copy, di) == FAIL)
5566 {
5567 dictitem_free(di);
5568 break;
5569 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005570 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005571 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005572
Bram Moolenaare9a41262005-01-15 22:18:47 +00005573 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005574 if (todo > 0)
5575 {
5576 dict_unref(copy);
5577 copy = NULL;
5578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005579 }
5580
5581 return copy;
5582}
5583
5584/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005586 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005588 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005590 dict_T *d;
5591 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592{
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594}
5595
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005597 * Add a number or string entry to dictionary "d".
5598 * When "str" is NULL use number "nr", otherwise use "str".
5599 * Returns FAIL when out of memory and when key already exists.
5600 */
5601 int
5602dict_add_nr_str(d, key, nr, str)
5603 dict_T *d;
5604 char *key;
5605 long nr;
5606 char_u *str;
5607{
5608 dictitem_T *item;
5609
5610 item = dictitem_alloc((char_u *)key);
5611 if (item == NULL)
5612 return FAIL;
5613 item->di_tv.v_lock = 0;
5614 if (str == NULL)
5615 {
5616 item->di_tv.v_type = VAR_NUMBER;
5617 item->di_tv.vval.v_number = nr;
5618 }
5619 else
5620 {
5621 item->di_tv.v_type = VAR_STRING;
5622 item->di_tv.vval.v_string = vim_strsave(str);
5623 }
5624 if (dict_add(d, item) == FAIL)
5625 {
5626 dictitem_free(item);
5627 return FAIL;
5628 }
5629 return OK;
5630}
5631
5632/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005633 * Get the number of items in a Dictionary.
5634 */
5635 static long
5636dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 if (d == NULL)
5640 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642}
5643
5644/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 * Find item "key[len]" in Dictionary "d".
5646 * If "len" is negative use strlen(key).
5647 * Returns NULL when not found.
5648 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005650dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005651 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 char_u *key;
5653 int len;
5654{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005655#define AKEYLEN 200
5656 char_u buf[AKEYLEN];
5657 char_u *akey;
5658 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005661 if (len < 0)
5662 akey = key;
5663 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005665 tofree = akey = vim_strnsave(key, len);
5666 if (akey == NULL)
5667 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005668 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005669 else
5670 {
5671 /* Avoid a malloc/free by using buf[]. */
5672 STRNCPY(buf, key, len);
5673 buf[len] = NUL;
5674 akey = buf;
5675 }
5676
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005678 vim_free(tofree);
5679 if (HASHITEM_EMPTY(hi))
5680 return NULL;
5681 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682}
5683
5684/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005685 * Get a string item from a dictionary in allocated memory.
5686 * Returns NULL if the entry doesn't exist or out of memory.
5687 */
5688 char_u *
5689get_dict_string(d, key)
5690 dict_T *d;
5691 char_u *key;
5692{
5693 dictitem_T *di;
5694
5695 di = dict_find(d, key, -1);
5696 if (di == NULL)
5697 return NULL;
5698 return vim_strsave(get_tv_string(&di->di_tv));
5699}
5700
5701/*
5702 * Get a number item from a dictionary.
5703 * Returns 0 if the entry doesn't exist or out of memory.
5704 */
5705 long
5706get_dict_number(d, key)
5707 dict_T *d;
5708 char_u *key;
5709{
5710 dictitem_T *di;
5711
5712 di = dict_find(d, key, -1);
5713 if (di == NULL)
5714 return 0;
5715 return get_tv_number(&di->di_tv);
5716}
5717
5718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 * Return an allocated string with the string representation of a Dictionary.
5720 * May return NULL.
5721 */
5722 static char_u *
5723dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725{
5726 garray_T ga;
5727 int first = TRUE;
5728 char_u *tofree;
5729 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005733 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005735 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 return NULL;
5737 ga_init2(&ga, (int)sizeof(char), 80);
5738 ga_append(&ga, '{');
5739
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005741 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005745 --todo;
5746
5747 if (first)
5748 first = FALSE;
5749 else
5750 ga_concat(&ga, (char_u *)", ");
5751
5752 tofree = string_quote(hi->hi_key, FALSE);
5753 if (tofree != NULL)
5754 {
5755 ga_concat(&ga, tofree);
5756 vim_free(tofree);
5757 }
5758 ga_concat(&ga, (char_u *)": ");
5759 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5760 if (s != NULL)
5761 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005763 if (s == NULL)
5764 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005767 if (todo > 0)
5768 {
5769 vim_free(ga.ga_data);
5770 return NULL;
5771 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772
5773 ga_append(&ga, '}');
5774 ga_append(&ga, NUL);
5775 return (char_u *)ga.ga_data;
5776}
5777
5778/*
5779 * Allocate a variable for a Dictionary and fill it from "*arg".
5780 * Return OK or FAIL. Returns NOTDONE for {expr}.
5781 */
5782 static int
5783get_dict_tv(arg, rettv, evaluate)
5784 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 int evaluate;
5787{
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 dict_T *d = NULL;
5789 typval_T tvkey;
5790 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005794 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795
5796 /*
5797 * First check if it's not a curly-braces thing: {expr}.
5798 * Must do this without evaluating, otherwise a function may be called
5799 * twice. Unfortunately this means we need to call eval1() twice for the
5800 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005801 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 if (*start != '}')
5804 {
5805 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5806 return FAIL;
5807 if (*start == '}')
5808 return NOTDONE;
5809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810
5811 if (evaluate)
5812 {
5813 d = dict_alloc();
5814 if (d == NULL)
5815 return FAIL;
5816 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005817 tvkey.v_type = VAR_UNKNOWN;
5818 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819
5820 *arg = skipwhite(*arg + 1);
5821 while (**arg != '}' && **arg != NUL)
5822 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005823 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 goto failret;
5825 if (**arg != ':')
5826 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005827 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005828 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 goto failret;
5830 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005831 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*key == NUL)
5833 {
5834 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005835 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 goto failret;
5837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838
5839 *arg = skipwhite(*arg + 1);
5840 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5841 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005842 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 goto failret;
5844 }
5845 if (evaluate)
5846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005847 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 if (item != NULL)
5849 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00005850 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 clear_tv(&tv);
5853 goto failret;
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 item = dictitem_alloc(key);
5856 clear_tv(&tvkey);
5857 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005860 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005861 if (dict_add(d, item) == FAIL)
5862 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 }
5864 }
5865
5866 if (**arg == '}')
5867 break;
5868 if (**arg != ',')
5869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005870 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 goto failret;
5872 }
5873 *arg = skipwhite(*arg + 1);
5874 }
5875
5876 if (**arg != '}')
5877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005878 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879failret:
5880 if (evaluate)
5881 dict_free(d);
5882 return FAIL;
5883 }
5884
5885 *arg = skipwhite(*arg + 1);
5886 if (evaluate)
5887 {
5888 rettv->v_type = VAR_DICT;
5889 rettv->vval.v_dict = d;
5890 ++d->dv_refcount;
5891 }
5892
5893 return OK;
5894}
5895
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 * Return a string with the string representation of a variable.
5898 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005899 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * May return NULL;
5902 */
5903 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005909 static int recurse = 0;
5910 char_u *r = NULL;
5911
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005914 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915 *tofree = NULL;
5916 return NULL;
5917 }
5918 ++recurse;
5919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 switch (tv->v_type)
5921 {
5922 case VAR_FUNC:
5923 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005924 r = tv->vval.v_string;
5925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 case VAR_LIST:
5927 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005928 r = *tofree;
5929 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 case VAR_DICT:
5931 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 r = *tofree;
5933 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934 case VAR_STRING:
5935 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 *tofree = NULL;
5937 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005939 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005941 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005943
5944 --recurse;
5945 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946}
5947
5948/*
5949 * Return a string with the string representation of a variable.
5950 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5951 * "numbuf" is used for a number.
5952 * Puts quotes around strings, so that they can be parsed back by eval().
5953 * May return NULL;
5954 */
5955 static char_u *
5956tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 char_u **tofree;
5959 char_u *numbuf;
5960{
5961 switch (tv->v_type)
5962 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 case VAR_FUNC:
5964 *tofree = string_quote(tv->vval.v_string, TRUE);
5965 return *tofree;
5966 case VAR_STRING:
5967 *tofree = string_quote(tv->vval.v_string, FALSE);
5968 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005970 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977}
5978
5979/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 * Return string "str" in ' quotes, doubling ' characters.
5981 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005982 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983 */
5984 static char_u *
5985string_quote(str, function)
5986 char_u *str;
5987 int function;
5988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990 char_u *p, *r, *s;
5991
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 len = (function ? 13 : 3);
5993 if (str != NULL)
5994 {
5995 len += STRLEN(str);
5996 for (p = str; *p != NUL; mb_ptr_adv(p))
5997 if (*p == '\'')
5998 ++len;
5999 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006000 s = r = alloc(len);
6001 if (r != NULL)
6002 {
6003 if (function)
6004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006005 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006006 r += 10;
6007 }
6008 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 if (str != NULL)
6011 for (p = str; *p != NUL; )
6012 {
6013 if (*p == '\'')
6014 *r++ = '\'';
6015 MB_COPY_CHAR(p, r);
6016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006017 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 if (function)
6019 *r++ = ')';
6020 *r++ = NUL;
6021 }
6022 return s;
6023}
6024
6025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 * Get the value of an environment variable.
6027 * "arg" is pointing to the '$'. It is advanced to after the name.
6028 * If the environment variable was not set, silently assume it is empty.
6029 * Always return OK.
6030 */
6031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006032get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 int evaluate;
6036{
6037 char_u *string = NULL;
6038 int len;
6039 int cc;
6040 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006041 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042
6043 ++*arg;
6044 name = *arg;
6045 len = get_env_len(arg);
6046 if (evaluate)
6047 {
6048 if (len != 0)
6049 {
6050 cc = name[len];
6051 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006052 /* first try vim_getenv(), fast for normal environment vars */
6053 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006055 {
6056 if (!mustfree)
6057 string = vim_strsave(string);
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 else
6060 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006061 if (mustfree)
6062 vim_free(string);
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 /* next try expanding things like $VIM and ${HOME} */
6065 string = expand_env_save(name - 1);
6066 if (string != NULL && *string == '$')
6067 {
6068 vim_free(string);
6069 string = NULL;
6070 }
6071 }
6072 name[len] = cc;
6073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 rettv->v_type = VAR_STRING;
6075 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077
6078 return OK;
6079}
6080
6081/*
6082 * Array with names and number of arguments of all internal functions
6083 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6084 */
6085static struct fst
6086{
6087 char *f_name; /* function name */
6088 char f_min_argc; /* minimal number of arguments */
6089 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092} functions[] =
6093{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006094 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {"append", 2, 2, f_append},
6096 {"argc", 0, 0, f_argc},
6097 {"argidx", 0, 0, f_argidx},
6098 {"argv", 1, 1, f_argv},
6099 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006100 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {"bufexists", 1, 1, f_bufexists},
6102 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6103 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6104 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6105 {"buflisted", 1, 1, f_buflisted},
6106 {"bufloaded", 1, 1, f_bufloaded},
6107 {"bufname", 1, 1, f_bufname},
6108 {"bufnr", 1, 1, f_bufnr},
6109 {"bufwinnr", 1, 1, f_bufwinnr},
6110 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006111 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {"char2nr", 1, 1, f_char2nr},
6114 {"cindent", 1, 1, f_cindent},
6115 {"col", 1, 1, f_col},
6116 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006118 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {"cscope_connection",0,3, f_cscope_connection},
6120 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006121 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {"delete", 1, 1, f_delete},
6123 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006124 {"diff_filler", 1, 1, f_diff_filler},
6125 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006126 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006128 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {"eventhandler", 0, 0, f_eventhandler},
6130 {"executable", 1, 1, f_executable},
6131 {"exists", 1, 1, f_exists},
6132 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6135 {"filereadable", 1, 1, f_filereadable},
6136 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006137 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006138 {"finddir", 1, 3, f_finddir},
6139 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 {"fnamemodify", 2, 2, f_fnamemodify},
6141 {"foldclosed", 1, 1, f_foldclosed},
6142 {"foldclosedend", 1, 1, f_foldclosedend},
6143 {"foldlevel", 1, 1, f_foldlevel},
6144 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006145 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006148 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {"getbufvar", 2, 2, f_getbufvar},
6150 {"getchar", 0, 1, f_getchar},
6151 {"getcharmod", 0, 0, f_getcharmod},
6152 {"getcmdline", 0, 0, f_getcmdline},
6153 {"getcmdpos", 0, 0, f_getcmdpos},
6154 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006155 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006156 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {"getfsize", 1, 1, f_getfsize},
6158 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006159 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006160 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006161 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006162 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {"getregtype", 0, 1, f_getregtype},
6164 {"getwinposx", 0, 0, f_getwinposx},
6165 {"getwinposy", 0, 0, f_getwinposy},
6166 {"getwinvar", 2, 2, f_getwinvar},
6167 {"glob", 1, 1, f_glob},
6168 {"globpath", 2, 2, f_globpath},
6169 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 {"hasmapto", 1, 2, f_hasmapto},
6172 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6173 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6174 {"histadd", 2, 2, f_histadd},
6175 {"histdel", 1, 2, f_histdel},
6176 {"histget", 1, 2, f_histget},
6177 {"histnr", 1, 1, f_histnr},
6178 {"hlID", 1, 1, f_hlID},
6179 {"hlexists", 1, 1, f_hlexists},
6180 {"hostname", 0, 0, f_hostname},
6181 {"iconv", 3, 3, f_iconv},
6182 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006183 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {"input", 1, 2, f_input},
6185 {"inputdialog", 1, 3, f_inputdialog},
6186 {"inputrestore", 0, 0, f_inputrestore},
6187 {"inputsave", 0, 0, f_inputsave},
6188 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006191 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006192 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006193 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006194 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {"libcall", 3, 3, f_libcall},
6198 {"libcallnr", 3, 3, f_libcallnr},
6199 {"line", 1, 1, f_line},
6200 {"line2byte", 1, 1, f_line2byte},
6201 {"lispindent", 1, 1, f_lispindent},
6202 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006203 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {"maparg", 1, 2, f_maparg},
6205 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006206 {"match", 2, 4, f_match},
6207 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006208 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006209 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006210 {"max", 1, 1, f_max},
6211 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006212#ifdef vim_mkdir
6213 {"mkdir", 1, 3, f_mkdir},
6214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {"mode", 0, 0, f_mode},
6216 {"nextnonblank", 1, 1, f_nextnonblank},
6217 {"nr2char", 1, 1, f_nr2char},
6218 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006219 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006220 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {"remote_expr", 2, 3, f_remote_expr},
6222 {"remote_foreground", 1, 1, f_remote_foreground},
6223 {"remote_peek", 1, 2, f_remote_peek},
6224 {"remote_read", 1, 1, f_remote_read},
6225 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006226 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006228 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006230 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {"search", 1, 2, f_search},
6232 {"searchpair", 3, 5, f_searchpair},
6233 {"server2client", 2, 2, f_server2client},
6234 {"serverlist", 0, 0, f_serverlist},
6235 {"setbufvar", 3, 3, f_setbufvar},
6236 {"setcmdpos", 1, 1, f_setcmdpos},
6237 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006238 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {"setreg", 2, 3, f_setreg},
6240 {"setwinvar", 3, 3, f_setwinvar},
6241 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006242 {"sort", 1, 2, f_sort},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006243 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244#ifdef HAVE_STRFTIME
6245 {"strftime", 1, 2, f_strftime},
6246#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006247 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {"strlen", 1, 1, f_strlen},
6250 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006251 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {"strtrans", 1, 1, f_strtrans},
6253 {"submatch", 1, 1, f_submatch},
6254 {"substitute", 4, 4, f_substitute},
6255 {"synID", 3, 3, f_synID},
6256 {"synIDattr", 2, 3, f_synIDattr},
6257 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006258 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006259 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {"tempname", 0, 0, f_tempname},
6261 {"tolower", 1, 1, f_tolower},
6262 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006263 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006265 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {"virtcol", 1, 1, f_virtcol},
6267 {"visualmode", 0, 1, f_visualmode},
6268 {"winbufnr", 1, 1, f_winbufnr},
6269 {"wincol", 0, 0, f_wincol},
6270 {"winheight", 1, 1, f_winheight},
6271 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006272 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {"winrestcmd", 0, 0, f_winrestcmd},
6274 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006275 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276};
6277
6278#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6279
6280/*
6281 * Function given to ExpandGeneric() to obtain the list of internal
6282 * or user defined function names.
6283 */
6284 char_u *
6285get_function_name(xp, idx)
6286 expand_T *xp;
6287 int idx;
6288{
6289 static int intidx = -1;
6290 char_u *name;
6291
6292 if (idx == 0)
6293 intidx = -1;
6294 if (intidx < 0)
6295 {
6296 name = get_user_func_name(xp, idx);
6297 if (name != NULL)
6298 return name;
6299 }
6300 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6301 {
6302 STRCPY(IObuff, functions[intidx].f_name);
6303 STRCAT(IObuff, "(");
6304 if (functions[intidx].f_max_argc == 0)
6305 STRCAT(IObuff, ")");
6306 return IObuff;
6307 }
6308
6309 return NULL;
6310}
6311
6312/*
6313 * Function given to ExpandGeneric() to obtain the list of internal or
6314 * user defined variable or function names.
6315 */
6316/*ARGSUSED*/
6317 char_u *
6318get_expr_name(xp, idx)
6319 expand_T *xp;
6320 int idx;
6321{
6322 static int intidx = -1;
6323 char_u *name;
6324
6325 if (idx == 0)
6326 intidx = -1;
6327 if (intidx < 0)
6328 {
6329 name = get_function_name(xp, idx);
6330 if (name != NULL)
6331 return name;
6332 }
6333 return get_user_var_name(xp, ++intidx);
6334}
6335
6336#endif /* FEAT_CMDL_COMPL */
6337
6338/*
6339 * Find internal function in table above.
6340 * Return index, or -1 if not found
6341 */
6342 static int
6343find_internal_func(name)
6344 char_u *name; /* name of the function */
6345{
6346 int first = 0;
6347 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6348 int cmp;
6349 int x;
6350
6351 /*
6352 * Find the function name in the table. Binary search.
6353 */
6354 while (first <= last)
6355 {
6356 x = first + ((unsigned)(last - first) >> 1);
6357 cmp = STRCMP(name, functions[x].f_name);
6358 if (cmp < 0)
6359 last = x - 1;
6360 else if (cmp > 0)
6361 first = x + 1;
6362 else
6363 return x;
6364 }
6365 return -1;
6366}
6367
6368/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6370 * name it contains, otherwise return "name".
6371 */
6372 static char_u *
6373deref_func_name(name, lenp)
6374 char_u *name;
6375 int *lenp;
6376{
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 int cc;
6379
6380 cc = name[*lenp];
6381 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006382 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 {
6388 *lenp = 0;
6389 return (char_u *)""; /* just in case */
6390 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 *lenp = STRLEN(v->di_tv.vval.v_string);
6392 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 }
6394
6395 return name;
6396}
6397
6398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 * Allocate a variable for the result of a function.
6400 * Return OK or FAIL.
6401 */
6402 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6404 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 char_u *name; /* name of the function */
6406 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 char_u **arg; /* argument, pointing to the '(' */
6409 linenr_T firstline; /* first line of range */
6410 linenr_T lastline; /* last line of range */
6411 int *doesrange; /* return: function handled range */
6412 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 char_u *argp;
6416 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 int argcount = 0; /* number of arguments found */
6419
6420 /*
6421 * Get the arguments.
6422 */
6423 argp = *arg;
6424 while (argcount < MAX_FUNC_ARGS)
6425 {
6426 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6427 if (*argp == ')' || *argp == ',' || *argp == NUL)
6428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6430 {
6431 ret = FAIL;
6432 break;
6433 }
6434 ++argcount;
6435 if (*argp != ',')
6436 break;
6437 }
6438 if (*argp == ')')
6439 ++argp;
6440 else
6441 ret = FAIL;
6442
6443 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006444 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 {
6448 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006455 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
6457 *arg = skipwhite(argp);
6458 return ret;
6459}
6460
6461
6462/*
6463 * Call a function with its resolved parameters
6464 * Return OK or FAIL.
6465 */
6466 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 char_u *name; /* name of the function */
6470 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 linenr_T firstline; /* first line of range */
6475 linenr_T lastline; /* last line of range */
6476 int *doesrange; /* return: function handled range */
6477 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481#define ERROR_UNKNOWN 0
6482#define ERROR_TOOMANY 1
6483#define ERROR_TOOFEW 2
6484#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485#define ERROR_DICT 4
6486#define ERROR_NONE 5
6487#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 int error = ERROR_NONE;
6489 int i;
6490 int llen;
6491 ufunc_T *fp;
6492 int cc;
6493#define FLEN_FIXED 40
6494 char_u fname_buf[FLEN_FIXED + 1];
6495 char_u *fname;
6496
6497 /*
6498 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6499 * Change <SNR>123_name() to K_SNR 123_name().
6500 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6501 */
6502 cc = name[len];
6503 name[len] = NUL;
6504 llen = eval_fname_script(name);
6505 if (llen > 0)
6506 {
6507 fname_buf[0] = K_SPECIAL;
6508 fname_buf[1] = KS_EXTRA;
6509 fname_buf[2] = (int)KE_SNR;
6510 i = 3;
6511 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6512 {
6513 if (current_SID <= 0)
6514 error = ERROR_SCRIPT;
6515 else
6516 {
6517 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6518 i = (int)STRLEN(fname_buf);
6519 }
6520 }
6521 if (i + STRLEN(name + llen) < FLEN_FIXED)
6522 {
6523 STRCPY(fname_buf + i, name + llen);
6524 fname = fname_buf;
6525 }
6526 else
6527 {
6528 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6529 if (fname == NULL)
6530 error = ERROR_OTHER;
6531 else
6532 {
6533 mch_memmove(fname, fname_buf, (size_t)i);
6534 STRCPY(fname + i, name + llen);
6535 }
6536 }
6537 }
6538 else
6539 fname = name;
6540
6541 *doesrange = FALSE;
6542
6543
6544 /* execute the function if no errors detected and executing */
6545 if (evaluate && error == ERROR_NONE)
6546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 error = ERROR_UNKNOWN;
6549
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006550 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
6552 /*
6553 * User defined function.
6554 */
6555 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006558 /* Trigger FuncUndefined event, may load the function. */
6559 if (fp == NULL
6560 && apply_autocmds(EVENT_FUNCUNDEFINED,
6561 fname, fname, TRUE, NULL)
6562 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006564 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 fp = find_func(fname);
6566 }
6567#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006568 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006569 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006570 {
6571 /* loaded a package, search for the function again */
6572 fp = find_func(fname);
6573 }
6574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (fp != NULL)
6576 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006577 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006579 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006581 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006583 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 else
6586 {
6587 /*
6588 * Call the user function.
6589 * Save and restore search patterns, script variables and
6590 * redo buffer.
6591 */
6592 save_search_patterns();
6593 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006594 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006595 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006596 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006597 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6598 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6599 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006600 /* Function was unreferenced while being used, free it
6601 * now. */
6602 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 restoreRedobuff();
6604 restore_search_patterns();
6605 error = ERROR_NONE;
6606 }
6607 }
6608 }
6609 else
6610 {
6611 /*
6612 * Find the function name in the table, call its implementation.
6613 */
6614 i = find_internal_func(fname);
6615 if (i >= 0)
6616 {
6617 if (argcount < functions[i].f_min_argc)
6618 error = ERROR_TOOFEW;
6619 else if (argcount > functions[i].f_max_argc)
6620 error = ERROR_TOOMANY;
6621 else
6622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 error = ERROR_NONE;
6626 }
6627 }
6628 }
6629 /*
6630 * The function call (or "FuncUndefined" autocommand sequence) might
6631 * have been aborted by an error, an interrupt, or an explicitly thrown
6632 * exception that has not been caught so far. This situation can be
6633 * tested for by calling aborting(). For an error in an internal
6634 * function or for the "E132" error in call_user_func(), however, the
6635 * throw point at which the "force_abort" flag (temporarily reset by
6636 * emsg()) is normally updated has not been reached yet. We need to
6637 * update that flag first to make aborting() reliable.
6638 */
6639 update_force_abort();
6640 }
6641 if (error == ERROR_NONE)
6642 ret = OK;
6643
6644 /*
6645 * Report an error unless the argument evaluation or function call has been
6646 * cancelled due to an aborting error, an interrupt, or an exception.
6647 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 if (!aborting())
6649 {
6650 switch (error)
6651 {
6652 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006653 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 break;
6655 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657 break;
6658 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 name);
6661 break;
6662 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 name);
6665 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006667 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 name);
6669 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006670 }
6671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 name[len] = cc;
6674 if (fname != name && fname != fname_buf)
6675 vim_free(fname);
6676
6677 return ret;
6678}
6679
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006680/*
6681 * Give an error message with a function name. Handle <SNR> things.
6682 */
6683 static void
6684emsg_funcname(msg, name)
6685 char *msg;
6686 char_u *name;
6687{
6688 char_u *p;
6689
6690 if (*name == K_SPECIAL)
6691 p = concat_str((char_u *)"<SNR>", name + 3);
6692 else
6693 p = name;
6694 EMSG2(_(msg), p);
6695 if (p != name)
6696 vim_free(p);
6697}
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699/*********************************************
6700 * Implementation of the built-in functions
6701 */
6702
6703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006704 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 */
6706 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006707f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 typval_T *argvars;
6709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
Bram Moolenaar33570922005-01-25 22:26:29 +00006711 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006714 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006716 if ((l = argvars[0].vval.v_list) != NULL
6717 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6718 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006719 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720 }
6721 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006722 EMSG(_(e_listreq));
6723}
6724
6725/*
6726 * "append(lnum, string/list)" function
6727 */
6728 static void
6729f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006730 typval_T *argvars;
6731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006732{
6733 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 list_T *l = NULL;
6735 listitem_T *li = NULL;
6736 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006737 long added = 0;
6738
6739 rettv->vval.v_number = 1; /* Default: Failed */
6740 lnum = get_tv_lnum(argvars);
6741 if (lnum >= 0
6742 && lnum <= curbuf->b_ml.ml_line_count
6743 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006745 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006747 l = argvars[1].vval.v_list;
6748 if (l == NULL)
6749 return;
6750 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006752 for (;;)
6753 {
6754 if (l == NULL)
6755 tv = &argvars[1]; /* append a string */
6756 else if (li == NULL)
6757 break; /* end of list */
6758 else
6759 tv = &li->li_tv; /* append item from list */
6760 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6761 ++added;
6762 if (l == NULL)
6763 break;
6764 li = li->li_next;
6765 }
6766
6767 appended_lines_mark(lnum, added);
6768 if (curwin->w_cursor.lnum > lnum)
6769 curwin->w_cursor.lnum += added;
6770 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772}
6773
6774/*
6775 * "argc()" function
6776 */
6777/* ARGSUSED */
6778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006779f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 typval_T *argvars;
6781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006783 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
6785
6786/*
6787 * "argidx()" function
6788 */
6789/* ARGSUSED */
6790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 typval_T *argvars;
6793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796}
6797
6798/*
6799 * "argv(nr)" function
6800 */
6801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006802f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006803 typval_T *argvars;
6804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int idx;
6807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006808 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 rettv->vval.v_string = NULL;
6813 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816/*
6817 * "browse(save, title, initdir, default)" function
6818 */
6819/* ARGSUSED */
6820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006821f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 typval_T *argvars;
6823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
6825#ifdef FEAT_BROWSE
6826 int save;
6827 char_u *title;
6828 char_u *initdir;
6829 char_u *defname;
6830 char_u buf[NUMBUFLEN];
6831 char_u buf2[NUMBUFLEN];
6832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 save = get_tv_number(&argvars[0]);
6834 title = get_tv_string(&argvars[1]);
6835 initdir = get_tv_string_buf(&argvars[2], buf);
6836 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006838 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006839 do_browse(save ? BROWSE_SAVE : 0,
6840 title, defname, NULL, initdir, NULL, curbuf);
6841#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006843#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006845}
6846
6847/*
6848 * "browsedir(title, initdir)" function
6849 */
6850/* ARGSUSED */
6851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006853 typval_T *argvars;
6854 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006855{
6856#ifdef FEAT_BROWSE
6857 char_u *title;
6858 char_u *initdir;
6859 char_u buf[NUMBUFLEN];
6860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006861 title = get_tv_string(&argvars[0]);
6862 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006864 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006865 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870}
6871
Bram Moolenaar33570922005-01-25 22:26:29 +00006872static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874/*
6875 * Find a buffer by number or exact name.
6876 */
6877 static buf_T *
6878find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
6881 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006883 if (avar->v_type == VAR_NUMBER)
6884 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006885 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006887 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006888 if (buf == NULL)
6889 {
6890 /* No full path name match, try a match with a URL or a "nofile"
6891 * buffer, these don't use the full path. */
6892 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6893 if (buf->b_fname != NULL
6894 && (path_with_url(buf->b_fname)
6895#ifdef FEAT_QUICKFIX
6896 || bt_nofile(buf)
6897#endif
6898 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006899 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006900 break;
6901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
6903 return buf;
6904}
6905
6906/*
6907 * "bufexists(expr)" function
6908 */
6909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 typval_T *argvars;
6912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915}
6916
6917/*
6918 * "buflisted(expr)" function
6919 */
6920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006921f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 typval_T *argvars;
6923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 buf_T *buf;
6926
6927 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006928 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929}
6930
6931/*
6932 * "bufloaded(expr)" function
6933 */
6934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 typval_T *argvars;
6937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 buf_T *buf;
6940
6941 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943}
6944
Bram Moolenaar33570922005-01-25 22:26:29 +00006945static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947/*
6948 * Get buffer by number or pattern.
6949 */
6950 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006951get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006954 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 int save_magic;
6956 char_u *save_cpo;
6957 buf_T *buf;
6958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006959 if (tv->v_type == VAR_NUMBER)
6960 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006961 if (tv->v_type != VAR_STRING)
6962 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (name == NULL || *name == NUL)
6964 return curbuf;
6965 if (name[0] == '$' && name[1] == NUL)
6966 return lastbuf;
6967
6968 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6969 save_magic = p_magic;
6970 p_magic = TRUE;
6971 save_cpo = p_cpo;
6972 p_cpo = (char_u *)"";
6973
6974 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6975 TRUE, FALSE));
6976
6977 p_magic = save_magic;
6978 p_cpo = save_cpo;
6979
6980 /* If not found, try expanding the name, like done for bufexists(). */
6981 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006982 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 return buf;
6985}
6986
6987/*
6988 * "bufname(expr)" function
6989 */
6990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006991f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006992 typval_T *argvars;
6993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 buf_T *buf;
6996
6997 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006998 buf = get_buf_tv(&argvars[0]);
6999 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007001 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 --emsg_off;
7005}
7006
7007/*
7008 * "bufnr(expr)" function
7009 */
7010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007011f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 typval_T *argvars;
7013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 buf_T *buf;
7016
7017 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007018 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007020 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007022 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 --emsg_off;
7024}
7025
7026/*
7027 * "bufwinnr(nr)" function
7028 */
7029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 typval_T *argvars;
7032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034#ifdef FEAT_WINDOWS
7035 win_T *wp;
7036 int winnr = 0;
7037#endif
7038 buf_T *buf;
7039
7040 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007041 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042#ifdef FEAT_WINDOWS
7043 for (wp = firstwin; wp; wp = wp->w_next)
7044 {
7045 ++winnr;
7046 if (wp->w_buffer == buf)
7047 break;
7048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007049 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007051 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#endif
7053 --emsg_off;
7054}
7055
7056/*
7057 * "byte2line(byte)" function
7058 */
7059/*ARGSUSED*/
7060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007061f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 typval_T *argvars;
7063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#else
7068 long boff = 0;
7069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007070 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007074 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 (linenr_T)0, &boff);
7076#endif
7077}
7078
7079/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007080 * "byteidx()" function
7081 */
7082/*ARGSUSED*/
7083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 typval_T *argvars;
7086 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007087{
7088#ifdef FEAT_MBYTE
7089 char_u *t;
7090#endif
7091 char_u *str;
7092 long idx;
7093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007094 str = get_tv_string(&argvars[0]);
7095 idx = get_tv_number(&argvars[1]);
7096 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007097 if (idx < 0)
7098 return;
7099
7100#ifdef FEAT_MBYTE
7101 t = str;
7102 for ( ; idx > 0; idx--)
7103 {
7104 if (*t == NUL) /* EOL reached */
7105 return;
7106 t += mb_ptr2len_check(t);
7107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007108 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007109#else
7110 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007111 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007112#endif
7113}
7114
7115/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007116 * "call(func, arglist)" function
7117 */
7118 static void
7119f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 typval_T *argvars;
7121 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007122{
7123 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007125 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007127 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007129
7130 rettv->vval.v_number = 0;
7131 if (argvars[1].v_type != VAR_LIST)
7132 {
7133 EMSG(_(e_listreq));
7134 return;
7135 }
7136 if (argvars[1].vval.v_list == NULL)
7137 return;
7138
7139 if (argvars[0].v_type == VAR_FUNC)
7140 func = argvars[0].vval.v_string;
7141 else
7142 func = get_tv_string(&argvars[0]);
7143
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 if (argvars[2].v_type != VAR_UNKNOWN)
7145 {
7146 if (argvars[2].v_type != VAR_DICT)
7147 {
7148 EMSG(_(e_dictreq));
7149 return;
7150 }
7151 selfdict = argvars[2].vval.v_dict;
7152 }
7153
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007154 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7155 item = item->li_next)
7156 {
7157 if (argc == MAX_FUNC_ARGS)
7158 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007159 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007160 break;
7161 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007162 /* Make a copy of each argument. This is needed to be able to set
7163 * v_lock to VAR_FIXED in the copy without changing the original list.
7164 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007165 copy_tv(&item->li_tv, &argv[argc++]);
7166 }
7167
7168 if (item == NULL)
7169 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7171 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007172
7173 /* Free the arguments. */
7174 while (argc > 0)
7175 clear_tv(&argv[--argc]);
7176}
7177
7178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 * "char2nr(string)" function
7180 */
7181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007182f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 typval_T *argvars;
7184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186#ifdef FEAT_MBYTE
7187 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188 rettv->vval.v_number =
7189 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 else
7191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007192 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195/*
7196 * "cindent(lnum)" function
7197 */
7198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007199f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 typval_T *argvars;
7201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203#ifdef FEAT_CINDENT
7204 pos_T pos;
7205 linenr_T lnum;
7206
7207 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007208 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7210 {
7211 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007212 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 curwin->w_cursor = pos;
7214 }
7215 else
7216#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
7220/*
7221 * "col(string)" function
7222 */
7223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 typval_T *argvars;
7226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 colnr_T col = 0;
7229 pos_T *fp;
7230
7231 fp = var2fpos(&argvars[0], FALSE);
7232 if (fp != NULL)
7233 {
7234 if (fp->col == MAXCOL)
7235 {
7236 /* '> can be MAXCOL, get the length of the line then */
7237 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7238 col = STRLEN(ml_get(fp->lnum)) + 1;
7239 else
7240 col = MAXCOL;
7241 }
7242 else
7243 {
7244 col = fp->col + 1;
7245#ifdef FEAT_VIRTUALEDIT
7246 /* col(".") when the cursor is on the NUL at the end of the line
7247 * because of "coladd" can be seen as an extra column. */
7248 if (virtual_active() && fp == &curwin->w_cursor)
7249 {
7250 char_u *p = ml_get_cursor();
7251
7252 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7253 curwin->w_virtcol - curwin->w_cursor.coladd))
7254 {
7255# ifdef FEAT_MBYTE
7256 int l;
7257
7258 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7259 col += l;
7260# else
7261 if (*p != NUL && p[1] == NUL)
7262 ++col;
7263# endif
7264 }
7265 }
7266#endif
7267 }
7268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
7272/*
7273 * "confirm(message, buttons[, default [, type]])" function
7274 */
7275/*ARGSUSED*/
7276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 typval_T *argvars;
7279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280{
7281#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7282 char_u *message;
7283 char_u *buttons = NULL;
7284 char_u buf[NUMBUFLEN];
7285 char_u buf2[NUMBUFLEN];
7286 int def = 1;
7287 int type = VIM_GENERIC;
7288 int c;
7289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007290 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007293 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007294 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007296 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007299 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007300 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 switch (TOUPPER_ASC(c))
7302 {
7303 case 'E': type = VIM_ERROR; break;
7304 case 'Q': type = VIM_QUESTION; break;
7305 case 'I': type = VIM_INFO; break;
7306 case 'W': type = VIM_WARNING; break;
7307 case 'G': type = VIM_GENERIC; break;
7308 }
7309 }
7310 }
7311 }
7312
7313 if (buttons == NULL || *buttons == NUL)
7314 buttons = (char_u *)_("&Ok");
7315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007316 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 def, NULL);
7318#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#endif
7321}
7322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323/*
7324 * "copy()" function
7325 */
7326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007327f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 typval_T *argvars;
7329 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007330{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007331 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007335 * "count()" function
7336 */
7337 static void
7338f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 typval_T *argvars;
7340 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007341{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007342 long n = 0;
7343 int ic = FALSE;
7344
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007346 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 listitem_T *li;
7348 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007350
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 if ((l = argvars[0].vval.v_list) != NULL)
7352 {
7353 li = l->lv_first;
7354 if (argvars[2].v_type != VAR_UNKNOWN)
7355 {
7356 ic = get_tv_number(&argvars[2]);
7357 if (argvars[3].v_type != VAR_UNKNOWN)
7358 {
7359 idx = get_tv_number(&argvars[3]);
7360 li = list_find(l, idx);
7361 if (li == NULL)
7362 EMSGN(_(e_listidx), idx);
7363 }
7364 }
7365
7366 for ( ; li != NULL; li = li->li_next)
7367 if (tv_equal(&li->li_tv, &argvars[1], ic))
7368 ++n;
7369 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 else if (argvars[0].v_type == VAR_DICT)
7372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 int todo;
7374 dict_T *d;
7375 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 if ((d = argvars[0].vval.v_dict) != NULL)
7378 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 if (argvars[2].v_type != VAR_UNKNOWN)
7380 {
7381 ic = get_tv_number(&argvars[2]);
7382 if (argvars[3].v_type != VAR_UNKNOWN)
7383 EMSG(_(e_invarg));
7384 }
7385
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 todo = d->dv_hashtab.ht_used;
7387 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 {
7389 if (!HASHITEM_EMPTY(hi))
7390 {
7391 --todo;
7392 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7393 ++n;
7394 }
7395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 }
7397 }
7398 else
7399 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007400 rettv->vval.v_number = n;
7401}
7402
7403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7405 *
7406 * Checks the existence of a cscope connection.
7407 */
7408/*ARGSUSED*/
7409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007410f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 typval_T *argvars;
7412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414#ifdef FEAT_CSCOPE
7415 int num = 0;
7416 char_u *dbpath = NULL;
7417 char_u *prepend = NULL;
7418 char_u buf[NUMBUFLEN];
7419
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420 if (argvars[0].v_type != VAR_UNKNOWN
7421 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 num = (int)get_tv_number(&argvars[0]);
7424 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#endif
7433}
7434
7435/*
7436 * "cursor(lnum, col)" function
7437 *
7438 * Moves the cursor to the specified line and column
7439 */
7440/*ARGSUSED*/
7441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 typval_T *argvars;
7444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 long line, col;
7447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007448 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (line > 0)
7450 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007451 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 if (col > 0)
7453 curwin->w_cursor.col = col - 1;
7454#ifdef FEAT_VIRTUALEDIT
7455 curwin->w_cursor.coladd = 0;
7456#endif
7457
7458 /* Make sure the cursor is in a valid position. */
7459 check_cursor();
7460#ifdef FEAT_MBYTE
7461 /* Correct cursor for multi-byte character. */
7462 if (has_mbyte)
7463 mb_adjust_cursor();
7464#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007465
7466 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467}
7468
7469/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007470 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 */
7472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007473f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 typval_T *argvars;
7475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007477 static int copyID = 0;
7478 int noref = 0;
7479
7480 if (argvars[1].v_type != VAR_UNKNOWN)
7481 noref = get_tv_number(&argvars[1]);
7482 if (noref < 0 || noref > 1)
7483 EMSG(_(e_invarg));
7484 else
7485 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486}
7487
7488/*
7489 * "delete()" function
7490 */
7491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007492f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 typval_T *argvars;
7494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007499 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500}
7501
7502/*
7503 * "did_filetype()" function
7504 */
7505/*ARGSUSED*/
7506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 typval_T *argvars;
7509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007514 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#endif
7516}
7517
7518/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007519 * "diff_filler()" function
7520 */
7521/*ARGSUSED*/
7522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007523f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 typval_T *argvars;
7525 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007526{
7527#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007529#endif
7530}
7531
7532/*
7533 * "diff_hlID()" function
7534 */
7535/*ARGSUSED*/
7536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007537f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 typval_T *argvars;
7539 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007540{
7541#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007542 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007543 static linenr_T prev_lnum = 0;
7544 static int changedtick = 0;
7545 static int fnum = 0;
7546 static int change_start = 0;
7547 static int change_end = 0;
7548 static enum hlf_value hlID = 0;
7549 int filler_lines;
7550 int col;
7551
7552 if (lnum != prev_lnum
7553 || changedtick != curbuf->b_changedtick
7554 || fnum != curbuf->b_fnum)
7555 {
7556 /* New line, buffer, change: need to get the values. */
7557 filler_lines = diff_check(curwin, lnum);
7558 if (filler_lines < 0)
7559 {
7560 if (filler_lines == -1)
7561 {
7562 change_start = MAXCOL;
7563 change_end = -1;
7564 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7565 hlID = HLF_ADD; /* added line */
7566 else
7567 hlID = HLF_CHD; /* changed line */
7568 }
7569 else
7570 hlID = HLF_ADD; /* added line */
7571 }
7572 else
7573 hlID = (enum hlf_value)0;
7574 prev_lnum = lnum;
7575 changedtick = curbuf->b_changedtick;
7576 fnum = curbuf->b_fnum;
7577 }
7578
7579 if (hlID == HLF_CHD || hlID == HLF_TXD)
7580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007582 if (col >= change_start && col <= change_end)
7583 hlID = HLF_TXD; /* changed text */
7584 else
7585 hlID = HLF_CHD; /* changed line */
7586 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007588#endif
7589}
7590
7591/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007592 * "empty({expr})" function
7593 */
7594 static void
7595f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 typval_T *argvars;
7597 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007598{
7599 int n;
7600
7601 switch (argvars[0].v_type)
7602 {
7603 case VAR_STRING:
7604 case VAR_FUNC:
7605 n = argvars[0].vval.v_string == NULL
7606 || *argvars[0].vval.v_string == NUL;
7607 break;
7608 case VAR_NUMBER:
7609 n = argvars[0].vval.v_number == 0;
7610 break;
7611 case VAR_LIST:
7612 n = argvars[0].vval.v_list == NULL
7613 || argvars[0].vval.v_list->lv_first == NULL;
7614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 case VAR_DICT:
7616 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007619 default:
7620 EMSG2(_(e_intern2), "f_empty()");
7621 n = 0;
7622 }
7623
7624 rettv->vval.v_number = n;
7625}
7626
7627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 * "escape({string}, {chars})" function
7629 */
7630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007631f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 typval_T *argvars;
7633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634{
7635 char_u buf[NUMBUFLEN];
7636
Bram Moolenaar758711c2005-02-02 23:11:38 +00007637 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7638 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007643 * "eval()" function
7644 */
7645/*ARGSUSED*/
7646 static void
7647f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 typval_T *argvars;
7649 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650{
7651 char_u *s;
7652
7653 s = get_tv_string(&argvars[0]);
7654 s = skipwhite(s);
7655
7656 if (eval1(&s, rettv, TRUE) == FAIL)
7657 rettv->vval.v_number = 0;
7658 else if (*s != NUL)
7659 EMSG(_(e_trailing));
7660}
7661
7662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * "eventhandler()" function
7664 */
7665/*ARGSUSED*/
7666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672}
7673
7674/*
7675 * "executable()" function
7676 */
7677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683}
7684
7685/*
7686 * "exists()" function
7687 */
7688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 typval_T *argvars;
7691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 char_u *p;
7694 char_u *name;
7695 int n = FALSE;
7696 int len = 0;
7697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (*p == '$') /* environment variable */
7700 {
7701 /* first try "normal" environment variables (fast) */
7702 if (mch_getenv(p + 1) != NULL)
7703 n = TRUE;
7704 else
7705 {
7706 /* try expanding things like $VIM and ${HOME} */
7707 p = expand_env_save(p);
7708 if (p != NULL && *p != '$')
7709 n = TRUE;
7710 vim_free(p);
7711 }
7712 }
7713 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 else if (*p == '*') /* internal or user defined function */
7716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719 else if (*p == ':')
7720 {
7721 n = cmd_exists(p + 1);
7722 }
7723 else if (*p == '#')
7724 {
7725#ifdef FEAT_AUTOCMD
7726 name = p + 1;
7727 p = vim_strchr(name, '#');
7728 if (p != NULL)
7729 n = au_exists(name, p, p + 1);
7730 else
7731 n = au_exists(name, name + STRLEN(name), NULL);
7732#endif
7733 }
7734 else /* internal variable */
7735 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007736 char_u *tofree;
7737 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007739 /* get_name_len() takes care of expanding curly braces */
7740 name = p;
7741 len = get_name_len(&p, &tofree, TRUE, FALSE);
7742 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007744 if (tofree != NULL)
7745 name = tofree;
7746 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7747 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007749 /* handle d.key, l[idx], f(expr) */
7750 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7751 if (n)
7752 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007756 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761
7762/*
7763 * "expand()" function
7764 */
7765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 typval_T *argvars;
7768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770 char_u *s;
7771 int len;
7772 char_u *errormsg;
7773 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7774 expand_T xpc;
7775
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->v_type = VAR_STRING;
7777 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 if (*s == '%' || *s == '#' || *s == '<')
7779 {
7780 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 --emsg_off;
7783 }
7784 else
7785 {
7786 /* When the optional second argument is non-zero, don't remove matches
7787 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 flags |= WILD_KEEP_ALL;
7790 ExpandInit(&xpc);
7791 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 ExpandCleanup(&xpc);
7794 }
7795}
7796
7797/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007800 */
7801 static void
7802f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007805{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007806 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007808 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 list_T *l1, *l2;
7810 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007812
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 l1 = argvars[0].vval.v_list;
7814 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007815 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7816 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 {
7818 if (argvars[2].v_type != VAR_UNKNOWN)
7819 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007820 before = get_tv_number(&argvars[2]);
7821 if (before == l1->lv_len)
7822 item = NULL;
7823 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007825 item = list_find(l1, before);
7826 if (item == NULL)
7827 {
7828 EMSGN(_(e_listidx), before);
7829 return;
7830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 }
7832 }
7833 else
7834 item = NULL;
7835 list_extend(l1, l2, item);
7836
7837 ++l1->lv_refcount;
7838 copy_tv(&argvars[0], rettv);
7839 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7842 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 dict_T *d1, *d2;
7844 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 char_u *action;
7846 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007848 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849
7850 d1 = argvars[0].vval.v_dict;
7851 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007852 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7853 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 {
7855 /* Check the third argument. */
7856 if (argvars[2].v_type != VAR_UNKNOWN)
7857 {
7858 static char *(av[]) = {"keep", "force", "error"};
7859
7860 action = get_tv_string(&argvars[2]);
7861 for (i = 0; i < 3; ++i)
7862 if (STRCMP(action, av[i]) == 0)
7863 break;
7864 if (i == 3)
7865 {
7866 EMSGN(_(e_invarg2), action);
7867 return;
7868 }
7869 }
7870 else
7871 action = (char_u *)"force";
7872
7873 /* Go over all entries in the second dict and add them to the
7874 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 todo = d2->dv_hashtab.ht_used;
7876 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007878 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007880 --todo;
7881 di1 = dict_find(d1, hi2->hi_key, -1);
7882 if (di1 == NULL)
7883 {
7884 di1 = dictitem_copy(HI2DI(hi2));
7885 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7886 dictitem_free(di1);
7887 }
7888 else if (*action == 'e')
7889 {
7890 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7891 break;
7892 }
7893 else if (*action == 'f')
7894 {
7895 clear_tv(&di1->di_tv);
7896 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 }
7899 }
7900
7901 ++d1->dv_refcount;
7902 copy_tv(&argvars[0], rettv);
7903 }
7904 }
7905 else
7906 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907}
7908
7909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 * "filereadable()" function
7911 */
7912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007914 typval_T *argvars;
7915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 FILE *fd;
7918 char_u *p;
7919 int n;
7920
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7923 {
7924 n = TRUE;
7925 fclose(fd);
7926 }
7927 else
7928 n = FALSE;
7929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931}
7932
7933/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00007934 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 * rights to write into.
7936 */
7937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 typval_T *argvars;
7940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00007942 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
Bram Moolenaar33570922005-01-25 22:26:29 +00007945static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007946
7947 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007948findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007951 int dir;
7952{
7953#ifdef FEAT_SEARCHPATH
7954 char_u *fname;
7955 char_u *fresult = NULL;
7956 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7957 char_u *p;
7958 char_u pathbuf[NUMBUFLEN];
7959 int count = 1;
7960 int first = TRUE;
7961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007967 if (*p != NUL)
7968 path = p;
7969
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007972 }
7973
7974 do
7975 {
7976 vim_free(fresult);
7977 fresult = find_file_in_path_option(first ? fname : NULL,
7978 first ? (int)STRLEN(fname) : 0,
7979 0, first, path, dir, NULL);
7980 first = FALSE;
7981 } while (--count > 0 && fresult != NULL);
7982
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007984#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007986#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007988}
7989
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007990static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7991static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007992static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7993static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994
7995/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007996 * Prepare v: variable "idx" to be used.
7997 * Save the current typeval in "save_tv".
7998 * When not used yet add the variable to the v: hashtable.
7999 */
8000 static void
8001prepare_vimvar(idx, save_tv)
8002 int idx;
8003 typval_T *save_tv;
8004{
8005 *save_tv = vimvars[idx].vv_tv;
8006 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8007 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8008}
8009
8010/*
8011 * Restore v: variable "idx" to typeval "save_tv".
8012 * When no longer defined, remove the variable from the v: hashtable.
8013 */
8014 static void
8015restore_vimvar(idx, save_tv)
8016 int idx;
8017 typval_T *save_tv;
8018{
8019 hashitem_T *hi;
8020
8021 clear_tv(&vimvars[idx].vv_tv);
8022 vimvars[idx].vv_tv = *save_tv;
8023 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8024 {
8025 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8026 if (HASHITEM_EMPTY(hi))
8027 EMSG2(_(e_intern2), "restore_vimvar()");
8028 else
8029 hash_remove(&vimvarht, hi);
8030 }
8031}
8032
8033/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 * Implementation of map() and filter().
8035 */
8036 static void
8037filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 typval_T *argvars;
8039 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040 int map;
8041{
8042 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008043 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 listitem_T *li, *nli;
8045 list_T *l = NULL;
8046 dictitem_T *di;
8047 hashtab_T *ht;
8048 hashitem_T *hi;
8049 dict_T *d = NULL;
8050 typval_T save_val;
8051 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008052 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008053 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008054 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8055
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008056
8057 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008058 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008059 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008060 if ((l = argvars[0].vval.v_list) == NULL
8061 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 return;
8063 }
8064 else if (argvars[0].v_type == VAR_DICT)
8065 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008066 if ((d = argvars[0].vval.v_dict) == NULL
8067 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 return;
8069 }
8070 else
8071 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008072 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008073 return;
8074 }
8075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008076 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008077 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078
8079 if (argvars[0].v_type == VAR_DICT)
8080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008081 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008083
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008085 hash_lock(ht);
8086 todo = ht->ht_used;
8087 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008089 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008091 --todo;
8092 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008093 if (tv_check_lock(di->di_tv.v_lock, msg))
8094 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008095 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008096 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8097 break;
8098 if (!map && rem)
8099 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008101 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008102 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008103 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008104
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008105 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 else
8108 {
8109 for (li = l->lv_first; li != NULL; li = nli)
8110 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008111 if (tv_check_lock(li->li_tv.v_lock, msg))
8112 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 nli = li->li_next;
8114 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8115 break;
8116 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008117 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 }
8119 }
8120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008121 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008122
8123 copy_tv(&argvars[0], rettv);
8124}
8125
8126 static int
8127filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008129 char_u *expr;
8130 int map;
8131 int *remp;
8132{
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 char_u *s;
8135
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 s = expr;
8138 if (eval1(&s, &rettv, TRUE) == FAIL)
8139 return FAIL;
8140 if (*s != NUL) /* check for trailing chars after expr */
8141 {
8142 EMSG2(_(e_invexpr2), s);
8143 return FAIL;
8144 }
8145 if (map)
8146 {
8147 /* map(): replace the list item value */
8148 clear_tv(tv);
8149 *tv = rettv;
8150 }
8151 else
8152 {
8153 /* filter(): when expr is zero remove the item */
8154 *remp = (get_tv_number(&rettv) == 0);
8155 clear_tv(&rettv);
8156 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008159}
8160
8161/*
8162 * "filter()" function
8163 */
8164 static void
8165f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008166 typval_T *argvars;
8167 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008168{
8169 filter_map(argvars, rettv, FALSE);
8170}
8171
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008173 * "finddir({fname}[, {path}[, {count}]])" function
8174 */
8175 static void
8176f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 typval_T *argvars;
8178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008179{
8180 findfilendir(argvars, rettv, TRUE);
8181}
8182
8183/*
8184 * "findfile({fname}[, {path}[, {count}]])" function
8185 */
8186 static void
8187f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008188 typval_T *argvars;
8189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008190{
8191 findfilendir(argvars, rettv, FALSE);
8192}
8193
8194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 * "fnamemodify({fname}, {mods})" function
8196 */
8197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008198f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 typval_T *argvars;
8200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
8202 char_u *fname;
8203 char_u *mods;
8204 int usedlen = 0;
8205 int len;
8206 char_u *fbuf = NULL;
8207 char_u buf[NUMBUFLEN];
8208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209 fname = get_tv_string(&argvars[0]);
8210 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 len = (int)STRLEN(fname);
8212
8213 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 vim_free(fbuf);
8221}
8222
Bram Moolenaar33570922005-01-25 22:26:29 +00008223static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
8225/*
8226 * "foldclosed()" function
8227 */
8228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 typval_T *argvars;
8231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 int end;
8233{
8234#ifdef FEAT_FOLDING
8235 linenr_T lnum;
8236 linenr_T first, last;
8237
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8240 {
8241 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8242 {
8243 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 return;
8248 }
8249 }
8250#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252}
8253
8254/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008255 * "foldclosed()" function
8256 */
8257 static void
8258f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 typval_T *argvars;
8260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008261{
8262 foldclosed_both(argvars, rettv, FALSE);
8263}
8264
8265/*
8266 * "foldclosedend()" function
8267 */
8268 static void
8269f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 typval_T *argvars;
8271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272{
8273 foldclosed_both(argvars, rettv, TRUE);
8274}
8275
8276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 * "foldlevel()" function
8278 */
8279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 typval_T *argvars;
8282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284#ifdef FEAT_FOLDING
8285 linenr_T lnum;
8286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 else
8291#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
8295/*
8296 * "foldtext()" function
8297 */
8298/*ARGSUSED*/
8299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 typval_T *argvars;
8302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304#ifdef FEAT_FOLDING
8305 linenr_T lnum;
8306 char_u *s;
8307 char_u *r;
8308 int len;
8309 char *txt;
8310#endif
8311
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008312 rettv->v_type = VAR_STRING;
8313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8316 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8317 <= curbuf->b_ml.ml_line_count
8318 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
8320 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008321 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8322 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {
8324 if (!linewhite(lnum))
8325 break;
8326 ++lnum;
8327 }
8328
8329 /* Find interesting text in this line. */
8330 s = skipwhite(ml_get(lnum));
8331 /* skip C comment-start */
8332 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008335 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008337 {
8338 s = skipwhite(ml_get(lnum + 1));
8339 if (*s == '*')
8340 s = skipwhite(s + 1);
8341 }
8342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 txt = _("+-%s%3ld lines: ");
8344 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 + 20 /* for %3ld */
8347 + STRLEN(s))); /* concatenated */
8348 if (r != NULL)
8349 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8351 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8352 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 len = (int)STRLEN(r);
8354 STRCAT(r, s);
8355 /* remove 'foldmarker' and 'commentstring' */
8356 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
8359 }
8360#endif
8361}
8362
8363/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008364 * "foldtextresult(lnum)" function
8365 */
8366/*ARGSUSED*/
8367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008371{
8372#ifdef FEAT_FOLDING
8373 linenr_T lnum;
8374 char_u *text;
8375 char_u buf[51];
8376 foldinfo_T foldinfo;
8377 int fold_count;
8378#endif
8379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 rettv->v_type = VAR_STRING;
8381 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008382#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008384 fold_count = foldedCount(curwin, lnum, &foldinfo);
8385 if (fold_count > 0)
8386 {
8387 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8388 &foldinfo, buf);
8389 if (text == buf)
8390 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008392 }
8393#endif
8394}
8395
8396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 * "foreground()" function
8398 */
8399/*ARGSUSED*/
8400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *argvars;
8403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008405 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#ifdef FEAT_GUI
8407 if (gui.in_use)
8408 gui_mch_set_foreground();
8409#else
8410# ifdef WIN32
8411 win32_set_foreground();
8412# endif
8413#endif
8414}
8415
8416/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008417 * "function()" function
8418 */
8419/*ARGSUSED*/
8420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 typval_T *argvars;
8423 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424{
8425 char_u *s;
8426
Bram Moolenaara7043832005-01-21 11:56:39 +00008427 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008429 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008430 EMSG2(_(e_invarg2), s);
8431 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008432 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008433 else
8434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435 rettv->vval.v_string = vim_strsave(s);
8436 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 }
8438}
8439
8440/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008441 * "get()" function
8442 */
8443 static void
8444f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008445 typval_T *argvars;
8446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008447{
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 listitem_T *li;
8449 list_T *l;
8450 dictitem_T *di;
8451 dict_T *d;
8452 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008453
Bram Moolenaare9a41262005-01-15 22:18:47 +00008454 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008455 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008456 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008457 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008458 li = list_find(l, get_tv_number(&argvars[1]));
8459 if (li != NULL)
8460 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008463 else if (argvars[0].v_type == VAR_DICT)
8464 {
8465 if ((d = argvars[0].vval.v_dict) != NULL)
8466 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008467 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008468 if (di != NULL)
8469 tv = &di->di_tv;
8470 }
8471 }
8472 else
8473 EMSG2(_(e_listdictarg), "get()");
8474
8475 if (tv == NULL)
8476 {
8477 if (argvars[2].v_type == VAR_UNKNOWN)
8478 rettv->vval.v_number = 0;
8479 else
8480 copy_tv(&argvars[2], rettv);
8481 }
8482 else
8483 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008484}
8485
8486/*
8487 * "getbufvar()" function
8488 */
8489 static void
8490f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008493{
8494 buf_T *buf;
8495 buf_T *save_curbuf;
8496 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008498
8499 ++emsg_off;
8500 buf = get_buf_tv(&argvars[0]);
8501 varname = get_tv_string(&argvars[1]);
8502
8503 rettv->v_type = VAR_STRING;
8504 rettv->vval.v_string = NULL;
8505
8506 if (buf != NULL && varname != NULL)
8507 {
8508 if (*varname == '&') /* buffer-local-option */
8509 {
8510 /* set curbuf to be our buf, temporarily */
8511 save_curbuf = curbuf;
8512 curbuf = buf;
8513
8514 get_option_tv(&varname, rettv, TRUE);
8515
8516 /* restore previous notion of curbuf */
8517 curbuf = save_curbuf;
8518 }
8519 else
8520 {
8521 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008522 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008523 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008525 }
8526 }
8527
8528 --emsg_off;
8529}
8530
8531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 * "getchar()" function
8533 */
8534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *argvars;
8537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 varnumber_T n;
8540
8541 ++no_mapping;
8542 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 /* getchar(): blocking wait. */
8545 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 /* getchar(1): only check if char avail */
8548 n = vpeekc();
8549 else if (vpeekc() == NUL)
8550 /* getchar(0) and no char avail: return zero */
8551 n = 0;
8552 else
8553 /* getchar(0) and char avail: return char */
8554 n = safe_vgetc();
8555 --no_mapping;
8556 --allow_keys;
8557
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008558 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 if (IS_SPECIAL(n) || mod_mask != 0)
8560 {
8561 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8562 int i = 0;
8563
8564 /* Turn a special key into three bytes, plus modifier. */
8565 if (mod_mask != 0)
8566 {
8567 temp[i++] = K_SPECIAL;
8568 temp[i++] = KS_MODIFIER;
8569 temp[i++] = mod_mask;
8570 }
8571 if (IS_SPECIAL(n))
8572 {
8573 temp[i++] = K_SPECIAL;
8574 temp[i++] = K_SECOND(n);
8575 temp[i++] = K_THIRD(n);
8576 }
8577#ifdef FEAT_MBYTE
8578 else if (has_mbyte)
8579 i += (*mb_char2bytes)(n, temp + i);
8580#endif
8581 else
8582 temp[i++] = n;
8583 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->v_type = VAR_STRING;
8585 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587}
8588
8589/*
8590 * "getcharmod()" function
8591 */
8592/*ARGSUSED*/
8593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *argvars;
8596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599}
8600
8601/*
8602 * "getcmdline()" function
8603 */
8604/*ARGSUSED*/
8605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 typval_T *argvars;
8608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 rettv->v_type = VAR_STRING;
8611 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612}
8613
8614/*
8615 * "getcmdpos()" function
8616 */
8617/*ARGSUSED*/
8618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008619f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008620 typval_T *argvars;
8621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624}
8625
8626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 * "getcwd()" function
8628 */
8629/*ARGSUSED*/
8630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 typval_T *argvars;
8633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 char_u cwd[MAXPATHL];
8636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 else
8641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#endif
8646 }
8647}
8648
8649/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008650 * "getfontname()" function
8651 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008652/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 typval_T *argvars;
8656 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008657{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658 rettv->v_type = VAR_STRING;
8659 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008660#ifdef FEAT_GUI
8661 if (gui.in_use)
8662 {
8663 GuiFont font;
8664 char_u *name = NULL;
8665
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008667 {
8668 /* Get the "Normal" font. Either the name saved by
8669 * hl_set_font_name() or from the font ID. */
8670 font = gui.norm_font;
8671 name = hl_get_font_name();
8672 }
8673 else
8674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008676 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8677 return;
8678 font = gui_mch_get_font(name, FALSE);
8679 if (font == NOFONT)
8680 return; /* Invalid font name, return empty string. */
8681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008684 gui_mch_free_font(font);
8685 }
8686#endif
8687}
8688
8689/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008690 * "getfperm({fname})" function
8691 */
8692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *argvars;
8695 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008696{
8697 char_u *fname;
8698 struct stat st;
8699 char_u *perm = NULL;
8700 char_u flags[] = "rwx";
8701 int i;
8702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008704
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008706 if (mch_stat((char *)fname, &st) >= 0)
8707 {
8708 perm = vim_strsave((char_u *)"---------");
8709 if (perm != NULL)
8710 {
8711 for (i = 0; i < 9; i++)
8712 {
8713 if (st.st_mode & (1 << (8 - i)))
8714 perm[i] = flags[i % 3];
8715 }
8716 }
8717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008719}
8720
8721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 * "getfsize({fname})" function
8723 */
8724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 char_u *fname;
8730 struct stat st;
8731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 if (mch_stat((char *)fname, &st) >= 0)
8737 {
8738 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745}
8746
8747/*
8748 * "getftime({fname})" function
8749 */
8750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 typval_T *argvars;
8753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 char_u *fname;
8756 struct stat st;
8757
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
8760 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764}
8765
8766/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008767 * "getftype({fname})" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008773{
8774 char_u *fname;
8775 struct stat st;
8776 char_u *type = NULL;
8777 char *t;
8778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008782 if (mch_lstat((char *)fname, &st) >= 0)
8783 {
8784#ifdef S_ISREG
8785 if (S_ISREG(st.st_mode))
8786 t = "file";
8787 else if (S_ISDIR(st.st_mode))
8788 t = "dir";
8789# ifdef S_ISLNK
8790 else if (S_ISLNK(st.st_mode))
8791 t = "link";
8792# endif
8793# ifdef S_ISBLK
8794 else if (S_ISBLK(st.st_mode))
8795 t = "bdev";
8796# endif
8797# ifdef S_ISCHR
8798 else if (S_ISCHR(st.st_mode))
8799 t = "cdev";
8800# endif
8801# ifdef S_ISFIFO
8802 else if (S_ISFIFO(st.st_mode))
8803 t = "fifo";
8804# endif
8805# ifdef S_ISSOCK
8806 else if (S_ISSOCK(st.st_mode))
8807 t = "fifo";
8808# endif
8809 else
8810 t = "other";
8811#else
8812# ifdef S_IFMT
8813 switch (st.st_mode & S_IFMT)
8814 {
8815 case S_IFREG: t = "file"; break;
8816 case S_IFDIR: t = "dir"; break;
8817# ifdef S_IFLNK
8818 case S_IFLNK: t = "link"; break;
8819# endif
8820# ifdef S_IFBLK
8821 case S_IFBLK: t = "bdev"; break;
8822# endif
8823# ifdef S_IFCHR
8824 case S_IFCHR: t = "cdev"; break;
8825# endif
8826# ifdef S_IFIFO
8827 case S_IFIFO: t = "fifo"; break;
8828# endif
8829# ifdef S_IFSOCK
8830 case S_IFSOCK: t = "socket"; break;
8831# endif
8832 default: t = "other";
8833 }
8834# else
8835 if (mch_isdir(fname))
8836 t = "dir";
8837 else
8838 t = "file";
8839# endif
8840#endif
8841 type = vim_strsave((char_u *)t);
8842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008844}
8845
8846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008847 * "getline(lnum)" function
8848 */
8849 static void
8850f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 typval_T *argvars;
8852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008853{
8854 linenr_T lnum;
8855 linenr_T end;
8856 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 list_T *l;
8858 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008859
8860 lnum = get_tv_lnum(argvars);
8861
8862 if (argvars[1].v_type == VAR_UNKNOWN)
8863 {
8864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8865 p = ml_get(lnum);
8866 else
8867 p = (char_u *)"";
8868
8869 rettv->v_type = VAR_STRING;
8870 rettv->vval.v_string = vim_strsave(p);
8871 }
8872 else
8873 {
8874 end = get_tv_lnum(&argvars[1]);
8875 if (end < lnum)
8876 {
8877 EMSG(_(e_invrange));
8878 rettv->vval.v_number = 0;
8879 }
8880 else
8881 {
8882 l = list_alloc();
8883 if (l != NULL)
8884 {
8885 if (lnum < 1)
8886 lnum = 1;
8887 if (end > curbuf->b_ml.ml_line_count)
8888 end = curbuf->b_ml.ml_line_count;
8889 while (lnum <= end)
8890 {
8891 li = listitem_alloc();
8892 if (li == NULL)
8893 break;
8894 list_append(l, li);
8895 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008896 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008897 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8898 }
8899 rettv->vval.v_list = l;
8900 rettv->v_type = VAR_LIST;
8901 ++l->lv_refcount;
8902 }
8903 }
8904 }
8905}
8906
8907/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00008908 * "getqflist()" function
8909 */
8910/*ARGSUSED*/
8911 static void
8912f_getqflist(argvars, rettv)
8913 typval_T *argvars;
8914 typval_T *rettv;
8915{
8916#ifdef FEAT_QUICKFIX
8917 list_T *l;
8918#endif
8919
8920 rettv->vval.v_number = FALSE;
8921#ifdef FEAT_QUICKFIX
8922 l = list_alloc();
8923 if (l != NULL)
8924 {
8925 if (get_errorlist(l) != FAIL)
8926 {
8927 rettv->vval.v_list = l;
8928 rettv->v_type = VAR_LIST;
8929 ++l->lv_refcount;
8930 }
8931 else
8932 list_free(l);
8933 }
8934#endif
8935}
8936
8937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 * "getreg()" function
8939 */
8940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 typval_T *argvars;
8943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944{
8945 char_u *strregname;
8946 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008947 int arg2 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008949 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008952 if (argvars[1].v_type != VAR_UNKNOWN)
8953 arg2 = get_tv_number(&argvars[1]);
8954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008956 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 regname = (strregname == NULL ? '"' : *strregname);
8958 if (regname == 0)
8959 regname = '"';
8960
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->v_type = VAR_STRING;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008962 rettv->vval.v_string = get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963}
8964
8965/*
8966 * "getregtype()" function
8967 */
8968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008970 typval_T *argvars;
8971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 char_u *strregname;
8974 int regname;
8975 char_u buf[NUMBUFLEN + 2];
8976 long reglen = 0;
8977
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008978 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 else
8981 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983
8984 regname = (strregname == NULL ? '"' : *strregname);
8985 if (regname == 0)
8986 regname = '"';
8987
8988 buf[0] = NUL;
8989 buf[1] = NUL;
8990 switch (get_reg_type(regname, &reglen))
8991 {
8992 case MLINE: buf[0] = 'V'; break;
8993 case MCHAR: buf[0] = 'v'; break;
8994#ifdef FEAT_VISUAL
8995 case MBLOCK:
8996 buf[0] = Ctrl_V;
8997 sprintf((char *)buf + 1, "%ld", reglen + 1);
8998 break;
8999#endif
9000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 rettv->v_type = VAR_STRING;
9002 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003}
9004
9005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * "getwinposx()" function
9007 */
9008/*ARGSUSED*/
9009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *argvars;
9012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015#ifdef FEAT_GUI
9016 if (gui.in_use)
9017 {
9018 int x, y;
9019
9020 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
9023#endif
9024}
9025
9026/*
9027 * "getwinposy()" function
9028 */
9029/*ARGSUSED*/
9030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 typval_T *argvars;
9033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036#ifdef FEAT_GUI
9037 if (gui.in_use)
9038 {
9039 int x, y;
9040
9041 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044#endif
9045}
9046
9047/*
9048 * "getwinvar()" function
9049 */
9050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 typval_T *argvars;
9053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 win_T *win, *oldcurwin;
9056 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009057 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 ++emsg_off;
9060 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->v_type = VAR_STRING;
9064 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 if (win != NULL && varname != NULL)
9067 {
9068 if (*varname == '&') /* window-local-option */
9069 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009070 /* Set curwin to be our win, temporarily. Also set curbuf, so
9071 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 oldcurwin = curwin;
9073 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009074 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
9078 /* restore previous notion of curwin */
9079 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009080 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
9082 else
9083 {
9084 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009085 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
9089 }
9090
9091 --emsg_off;
9092}
9093
9094/*
9095 * "glob()" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 expand_T xpc;
9103
9104 ExpandInit(&xpc);
9105 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 rettv->v_type = VAR_STRING;
9107 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9109 ExpandCleanup(&xpc);
9110}
9111
9112/*
9113 * "globpath()" function
9114 */
9115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *argvars;
9118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 char_u buf1[NUMBUFLEN];
9121
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->v_type = VAR_STRING;
9123 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9124 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * "has()" function
9129 */
9130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 int i;
9136 char_u *name;
9137 int n = FALSE;
9138 static char *(has_list[]) =
9139 {
9140#ifdef AMIGA
9141 "amiga",
9142# ifdef FEAT_ARP
9143 "arp",
9144# endif
9145#endif
9146#ifdef __BEOS__
9147 "beos",
9148#endif
9149#ifdef MSDOS
9150# ifdef DJGPP
9151 "dos32",
9152# else
9153 "dos16",
9154# endif
9155#endif
9156#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9157 "mac",
9158#endif
9159#if defined(MACOS_X_UNIX)
9160 "macunix",
9161#endif
9162#ifdef OS2
9163 "os2",
9164#endif
9165#ifdef __QNX__
9166 "qnx",
9167#endif
9168#ifdef RISCOS
9169 "riscos",
9170#endif
9171#ifdef UNIX
9172 "unix",
9173#endif
9174#ifdef VMS
9175 "vms",
9176#endif
9177#ifdef WIN16
9178 "win16",
9179#endif
9180#ifdef WIN32
9181 "win32",
9182#endif
9183#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9184 "win32unix",
9185#endif
9186#ifdef WIN64
9187 "win64",
9188#endif
9189#ifdef EBCDIC
9190 "ebcdic",
9191#endif
9192#ifndef CASE_INSENSITIVE_FILENAME
9193 "fname_case",
9194#endif
9195#ifdef FEAT_ARABIC
9196 "arabic",
9197#endif
9198#ifdef FEAT_AUTOCMD
9199 "autocmd",
9200#endif
9201#ifdef FEAT_BEVAL
9202 "balloon_eval",
9203#endif
9204#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9205 "builtin_terms",
9206# ifdef ALL_BUILTIN_TCAPS
9207 "all_builtin_terms",
9208# endif
9209#endif
9210#ifdef FEAT_BYTEOFF
9211 "byte_offset",
9212#endif
9213#ifdef FEAT_CINDENT
9214 "cindent",
9215#endif
9216#ifdef FEAT_CLIENTSERVER
9217 "clientserver",
9218#endif
9219#ifdef FEAT_CLIPBOARD
9220 "clipboard",
9221#endif
9222#ifdef FEAT_CMDL_COMPL
9223 "cmdline_compl",
9224#endif
9225#ifdef FEAT_CMDHIST
9226 "cmdline_hist",
9227#endif
9228#ifdef FEAT_COMMENTS
9229 "comments",
9230#endif
9231#ifdef FEAT_CRYPT
9232 "cryptv",
9233#endif
9234#ifdef FEAT_CSCOPE
9235 "cscope",
9236#endif
9237#ifdef DEBUG
9238 "debug",
9239#endif
9240#ifdef FEAT_CON_DIALOG
9241 "dialog_con",
9242#endif
9243#ifdef FEAT_GUI_DIALOG
9244 "dialog_gui",
9245#endif
9246#ifdef FEAT_DIFF
9247 "diff",
9248#endif
9249#ifdef FEAT_DIGRAPHS
9250 "digraphs",
9251#endif
9252#ifdef FEAT_DND
9253 "dnd",
9254#endif
9255#ifdef FEAT_EMACS_TAGS
9256 "emacs_tags",
9257#endif
9258 "eval", /* always present, of course! */
9259#ifdef FEAT_EX_EXTRA
9260 "ex_extra",
9261#endif
9262#ifdef FEAT_SEARCH_EXTRA
9263 "extra_search",
9264#endif
9265#ifdef FEAT_FKMAP
9266 "farsi",
9267#endif
9268#ifdef FEAT_SEARCHPATH
9269 "file_in_path",
9270#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009271#if defined(UNIX) && !defined(USE_SYSTEM)
9272 "filterpipe",
9273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#ifdef FEAT_FIND_ID
9275 "find_in_path",
9276#endif
9277#ifdef FEAT_FOLDING
9278 "folding",
9279#endif
9280#ifdef FEAT_FOOTER
9281 "footer",
9282#endif
9283#if !defined(USE_SYSTEM) && defined(UNIX)
9284 "fork",
9285#endif
9286#ifdef FEAT_GETTEXT
9287 "gettext",
9288#endif
9289#ifdef FEAT_GUI
9290 "gui",
9291#endif
9292#ifdef FEAT_GUI_ATHENA
9293# ifdef FEAT_GUI_NEXTAW
9294 "gui_neXtaw",
9295# else
9296 "gui_athena",
9297# endif
9298#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009299#ifdef FEAT_GUI_KDE
9300 "gui_kde",
9301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#ifdef FEAT_GUI_GTK
9303 "gui_gtk",
9304# ifdef HAVE_GTK2
9305 "gui_gtk2",
9306# endif
9307#endif
9308#ifdef FEAT_GUI_MAC
9309 "gui_mac",
9310#endif
9311#ifdef FEAT_GUI_MOTIF
9312 "gui_motif",
9313#endif
9314#ifdef FEAT_GUI_PHOTON
9315 "gui_photon",
9316#endif
9317#ifdef FEAT_GUI_W16
9318 "gui_win16",
9319#endif
9320#ifdef FEAT_GUI_W32
9321 "gui_win32",
9322#endif
9323#ifdef FEAT_HANGULIN
9324 "hangul_input",
9325#endif
9326#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9327 "iconv",
9328#endif
9329#ifdef FEAT_INS_EXPAND
9330 "insert_expand",
9331#endif
9332#ifdef FEAT_JUMPLIST
9333 "jumplist",
9334#endif
9335#ifdef FEAT_KEYMAP
9336 "keymap",
9337#endif
9338#ifdef FEAT_LANGMAP
9339 "langmap",
9340#endif
9341#ifdef FEAT_LIBCALL
9342 "libcall",
9343#endif
9344#ifdef FEAT_LINEBREAK
9345 "linebreak",
9346#endif
9347#ifdef FEAT_LISP
9348 "lispindent",
9349#endif
9350#ifdef FEAT_LISTCMDS
9351 "listcmds",
9352#endif
9353#ifdef FEAT_LOCALMAP
9354 "localmap",
9355#endif
9356#ifdef FEAT_MENU
9357 "menu",
9358#endif
9359#ifdef FEAT_SESSION
9360 "mksession",
9361#endif
9362#ifdef FEAT_MODIFY_FNAME
9363 "modify_fname",
9364#endif
9365#ifdef FEAT_MOUSE
9366 "mouse",
9367#endif
9368#ifdef FEAT_MOUSESHAPE
9369 "mouseshape",
9370#endif
9371#if defined(UNIX) || defined(VMS)
9372# ifdef FEAT_MOUSE_DEC
9373 "mouse_dec",
9374# endif
9375# ifdef FEAT_MOUSE_GPM
9376 "mouse_gpm",
9377# endif
9378# ifdef FEAT_MOUSE_JSB
9379 "mouse_jsbterm",
9380# endif
9381# ifdef FEAT_MOUSE_NET
9382 "mouse_netterm",
9383# endif
9384# ifdef FEAT_MOUSE_PTERM
9385 "mouse_pterm",
9386# endif
9387# ifdef FEAT_MOUSE_XTERM
9388 "mouse_xterm",
9389# endif
9390#endif
9391#ifdef FEAT_MBYTE
9392 "multi_byte",
9393#endif
9394#ifdef FEAT_MBYTE_IME
9395 "multi_byte_ime",
9396#endif
9397#ifdef FEAT_MULTI_LANG
9398 "multi_lang",
9399#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009400#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009401#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009402 "mzscheme",
9403#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405#ifdef FEAT_OLE
9406 "ole",
9407#endif
9408#ifdef FEAT_OSFILETYPE
9409 "osfiletype",
9410#endif
9411#ifdef FEAT_PATH_EXTRA
9412 "path_extra",
9413#endif
9414#ifdef FEAT_PERL
9415#ifndef DYNAMIC_PERL
9416 "perl",
9417#endif
9418#endif
9419#ifdef FEAT_PYTHON
9420#ifndef DYNAMIC_PYTHON
9421 "python",
9422#endif
9423#endif
9424#ifdef FEAT_POSTSCRIPT
9425 "postscript",
9426#endif
9427#ifdef FEAT_PRINTER
9428 "printer",
9429#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009430#ifdef FEAT_PROFILE
9431 "profile",
9432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#ifdef FEAT_QUICKFIX
9434 "quickfix",
9435#endif
9436#ifdef FEAT_RIGHTLEFT
9437 "rightleft",
9438#endif
9439#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9440 "ruby",
9441#endif
9442#ifdef FEAT_SCROLLBIND
9443 "scrollbind",
9444#endif
9445#ifdef FEAT_CMDL_INFO
9446 "showcmd",
9447 "cmdline_info",
9448#endif
9449#ifdef FEAT_SIGNS
9450 "signs",
9451#endif
9452#ifdef FEAT_SMARTINDENT
9453 "smartindent",
9454#endif
9455#ifdef FEAT_SNIFF
9456 "sniff",
9457#endif
9458#ifdef FEAT_STL_OPT
9459 "statusline",
9460#endif
9461#ifdef FEAT_SUN_WORKSHOP
9462 "sun_workshop",
9463#endif
9464#ifdef FEAT_NETBEANS_INTG
9465 "netbeans_intg",
9466#endif
9467#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009468 "spell",
9469#endif
9470#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 "syntax",
9472#endif
9473#if defined(USE_SYSTEM) || !defined(UNIX)
9474 "system",
9475#endif
9476#ifdef FEAT_TAG_BINS
9477 "tag_binary",
9478#endif
9479#ifdef FEAT_TAG_OLDSTATIC
9480 "tag_old_static",
9481#endif
9482#ifdef FEAT_TAG_ANYWHITE
9483 "tag_any_white",
9484#endif
9485#ifdef FEAT_TCL
9486# ifndef DYNAMIC_TCL
9487 "tcl",
9488# endif
9489#endif
9490#ifdef TERMINFO
9491 "terminfo",
9492#endif
9493#ifdef FEAT_TERMRESPONSE
9494 "termresponse",
9495#endif
9496#ifdef FEAT_TEXTOBJ
9497 "textobjects",
9498#endif
9499#ifdef HAVE_TGETENT
9500 "tgetent",
9501#endif
9502#ifdef FEAT_TITLE
9503 "title",
9504#endif
9505#ifdef FEAT_TOOLBAR
9506 "toolbar",
9507#endif
9508#ifdef FEAT_USR_CMDS
9509 "user-commands", /* was accidentally included in 5.4 */
9510 "user_commands",
9511#endif
9512#ifdef FEAT_VIMINFO
9513 "viminfo",
9514#endif
9515#ifdef FEAT_VERTSPLIT
9516 "vertsplit",
9517#endif
9518#ifdef FEAT_VIRTUALEDIT
9519 "virtualedit",
9520#endif
9521#ifdef FEAT_VISUAL
9522 "visual",
9523#endif
9524#ifdef FEAT_VISUALEXTRA
9525 "visualextra",
9526#endif
9527#ifdef FEAT_VREPLACE
9528 "vreplace",
9529#endif
9530#ifdef FEAT_WILDIGN
9531 "wildignore",
9532#endif
9533#ifdef FEAT_WILDMENU
9534 "wildmenu",
9535#endif
9536#ifdef FEAT_WINDOWS
9537 "windows",
9538#endif
9539#ifdef FEAT_WAK
9540 "winaltkeys",
9541#endif
9542#ifdef FEAT_WRITEBACKUP
9543 "writebackup",
9544#endif
9545#ifdef FEAT_XIM
9546 "xim",
9547#endif
9548#ifdef FEAT_XFONTSET
9549 "xfontset",
9550#endif
9551#ifdef USE_XSMP
9552 "xsmp",
9553#endif
9554#ifdef USE_XSMP_INTERACT
9555 "xsmp_interact",
9556#endif
9557#ifdef FEAT_XCLIPBOARD
9558 "xterm_clipboard",
9559#endif
9560#ifdef FEAT_XTERM_SAVE
9561 "xterm_save",
9562#endif
9563#if defined(UNIX) && defined(FEAT_X11)
9564 "X11",
9565#endif
9566 NULL
9567 };
9568
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 for (i = 0; has_list[i] != NULL; ++i)
9571 if (STRICMP(name, has_list[i]) == 0)
9572 {
9573 n = TRUE;
9574 break;
9575 }
9576
9577 if (n == FALSE)
9578 {
9579 if (STRNICMP(name, "patch", 5) == 0)
9580 n = has_patch(atoi((char *)name + 5));
9581 else if (STRICMP(name, "vim_starting") == 0)
9582 n = (starting != 0);
9583#ifdef DYNAMIC_TCL
9584 else if (STRICMP(name, "tcl") == 0)
9585 n = tcl_enabled(FALSE);
9586#endif
9587#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9588 else if (STRICMP(name, "iconv") == 0)
9589 n = iconv_enabled(FALSE);
9590#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009591#ifdef DYNAMIC_MZSCHEME
9592 else if (STRICMP(name, "mzscheme") == 0)
9593 n = mzscheme_enabled(FALSE);
9594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#ifdef DYNAMIC_RUBY
9596 else if (STRICMP(name, "ruby") == 0)
9597 n = ruby_enabled(FALSE);
9598#endif
9599#ifdef DYNAMIC_PYTHON
9600 else if (STRICMP(name, "python") == 0)
9601 n = python_enabled(FALSE);
9602#endif
9603#ifdef DYNAMIC_PERL
9604 else if (STRICMP(name, "perl") == 0)
9605 n = perl_enabled(FALSE);
9606#endif
9607#ifdef FEAT_GUI
9608 else if (STRICMP(name, "gui_running") == 0)
9609 n = (gui.in_use || gui.starting);
9610# ifdef FEAT_GUI_W32
9611 else if (STRICMP(name, "gui_win32s") == 0)
9612 n = gui_is_win32s();
9613# endif
9614# ifdef FEAT_BROWSE
9615 else if (STRICMP(name, "browse") == 0)
9616 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9617# endif
9618#endif
9619#ifdef FEAT_SYN_HL
9620 else if (STRICMP(name, "syntax_items") == 0)
9621 n = syntax_present(curbuf);
9622#endif
9623#if defined(WIN3264)
9624 else if (STRICMP(name, "win95") == 0)
9625 n = mch_windows95();
9626#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009627#ifdef FEAT_NETBEANS_INTG
9628 else if (STRICMP(name, "netbeans_enabled") == 0)
9629 n = usingNetbeans;
9630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 }
9632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634}
9635
9636/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637 * "has_key()" function
9638 */
9639 static void
9640f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009641 typval_T *argvars;
9642 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643{
9644 rettv->vval.v_number = 0;
9645 if (argvars[0].v_type != VAR_DICT)
9646 {
9647 EMSG(_(e_dictreq));
9648 return;
9649 }
9650 if (argvars[0].vval.v_dict == NULL)
9651 return;
9652
9653 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009654 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009655}
9656
9657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 * "hasmapto()" function
9659 */
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009662 typval_T *argvars;
9663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 char_u *name;
9666 char_u *mode;
9667 char_u buf[NUMBUFLEN];
9668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009670 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 mode = (char_u *)"nvo";
9672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674
9675 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679}
9680
9681/*
9682 * "histadd()" function
9683 */
9684/*ARGSUSED*/
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690#ifdef FEAT_CMDHIST
9691 int histype;
9692 char_u *str;
9693 char_u buf[NUMBUFLEN];
9694#endif
9695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 if (check_restricted() || check_secure())
9698 return;
9699#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (histype >= 0)
9702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 if (*str != NUL)
9705 {
9706 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 return;
9709 }
9710 }
9711#endif
9712}
9713
9714/*
9715 * "histdel()" function
9716 */
9717/*ARGSUSED*/
9718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009720 typval_T *argvars;
9721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723#ifdef FEAT_CMDHIST
9724 int n;
9725 char_u buf[NUMBUFLEN];
9726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009727 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009730 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9733 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 else
9735 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9737 get_tv_string_buf(&argvars[1], buf));
9738 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741#endif
9742}
9743
9744/*
9745 * "histget()" function
9746 */
9747/*ARGSUSED*/
9748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 typval_T *argvars;
9751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753#ifdef FEAT_CMDHIST
9754 int type;
9755 int idx;
9756
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009758 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 idx = get_history_idx(type);
9760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 idx = (int)get_tv_number(&argvars[1]);
9762 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767}
9768
9769/*
9770 * "histnr()" function
9771 */
9772/*ARGSUSED*/
9773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 int i;
9779
9780#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 if (i >= HIST_CMD && i < HIST_COUNT)
9783 i = get_history_idx(i);
9784 else
9785#endif
9786 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * "highlightID(name)" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799}
9800
9801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009802 * "highlight_exists()" function
9803 */
9804 static void
9805f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009808{
9809 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9810}
9811
9812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 * "hostname()" function
9814 */
9815/*ARGSUSED*/
9816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 typval_T *argvars;
9819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821 char_u hostname[256];
9822
9823 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824 rettv->v_type = VAR_STRING;
9825 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828/*
9829 * iconv() function
9830 */
9831/*ARGSUSED*/
9832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 typval_T *argvars;
9835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837#ifdef FEAT_MBYTE
9838 char_u buf1[NUMBUFLEN];
9839 char_u buf2[NUMBUFLEN];
9840 char_u *from, *to, *str;
9841 vimconv_T vimconv;
9842#endif
9843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->v_type = VAR_STRING;
9845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846
9847#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 str = get_tv_string(&argvars[0]);
9849 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9850 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 vimconv.vc_type = CONV_NONE;
9852 convert_setup(&vimconv, from, to);
9853
9854 /* If the encodings are equal, no conversion needed. */
9855 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
9860 convert_setup(&vimconv, NULL, NULL);
9861 vim_free(from);
9862 vim_free(to);
9863#endif
9864}
9865
9866/*
9867 * "indent()" function
9868 */
9869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 linenr_T lnum;
9875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881}
9882
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009883/*
9884 * "index()" function
9885 */
9886 static void
9887f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *argvars;
9889 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009890{
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 list_T *l;
9892 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009893 long idx = 0;
9894 int ic = FALSE;
9895
9896 rettv->vval.v_number = -1;
9897 if (argvars[0].v_type != VAR_LIST)
9898 {
9899 EMSG(_(e_listreq));
9900 return;
9901 }
9902 l = argvars[0].vval.v_list;
9903 if (l != NULL)
9904 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009905 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009907 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009908 /* Start at specified item. Use the cached index that list_find()
9909 * sets, so that a negative number also works. */
9910 item = list_find(l, get_tv_number(&argvars[2]));
9911 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009912 if (argvars[3].v_type != VAR_UNKNOWN)
9913 ic = get_tv_number(&argvars[3]);
9914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915
Bram Moolenaar758711c2005-02-02 23:11:38 +00009916 for ( ; item != NULL; item = item->li_next, ++idx)
9917 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918 {
9919 rettv->vval.v_number = idx;
9920 break;
9921 }
9922 }
9923}
9924
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925static int inputsecret_flag = 0;
9926
9927/*
9928 * "input()" function
9929 * Also handles inputsecret() when inputsecret is set.
9930 */
9931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009933 typval_T *argvars;
9934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 char_u *p = NULL;
9938 int c;
9939 char_u buf[NUMBUFLEN];
9940 int cmd_silent_save = cmd_silent;
9941
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
9944#ifdef NO_CONSOLE_INPUT
9945 /* While starting up, there is no place to enter text. */
9946 if (no_console_input())
9947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 return;
9950 }
9951#endif
9952
9953 cmd_silent = FALSE; /* Want to see the prompt. */
9954 if (prompt != NULL)
9955 {
9956 /* Only the part of the message after the last NL is considered as
9957 * prompt for the command line */
9958 p = vim_strrchr(prompt, '\n');
9959 if (p == NULL)
9960 p = prompt;
9961 else
9962 {
9963 ++p;
9964 c = *p;
9965 *p = NUL;
9966 msg_start();
9967 msg_clr_eos();
9968 msg_puts_attr(prompt, echo_attr);
9969 msg_didout = FALSE;
9970 msg_starthere();
9971 *p = c;
9972 }
9973 cmdline_row = msg_row;
9974 }
9975
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9981
9982 /* since the user typed this, no need to wait for return */
9983 need_wait_return = FALSE;
9984 msg_didout = FALSE;
9985 cmd_silent = cmd_silent_save;
9986}
9987
9988/*
9989 * "inputdialog()" function
9990 */
9991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009993 typval_T *argvars;
9994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995{
9996#if defined(FEAT_GUI_TEXTDIALOG)
9997 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9998 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9999 {
10000 char_u *message;
10001 char_u buf[NUMBUFLEN];
10002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010004 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 IObuff[IOSIZE - 1] = NUL;
10008 }
10009 else
10010 IObuff[0] = NUL;
10011 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
10012 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 else
10015 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010016 if (argvars[1].v_type != VAR_UNKNOWN
10017 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018 rettv->vval.v_string = vim_strsave(
10019 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else
10026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028}
10029
10030static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10031
10032/*
10033 * "inputrestore()" function
10034 */
10035/*ARGSUSED*/
10036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010038 typval_T *argvars;
10039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
10041 if (ga_userinput.ga_len > 0)
10042 {
10043 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10045 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
10048 else if (p_verbose > 1)
10049 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010050 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053}
10054
10055/*
10056 * "inputsave()" function
10057 */
10058/*ARGSUSED*/
10059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010061 typval_T *argvars;
10062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
10064 /* Add an entry to the stack of typehead storage. */
10065 if (ga_grow(&ga_userinput, 1) == OK)
10066 {
10067 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10068 + ga_userinput.ga_len);
10069 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 }
10072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074}
10075
10076/*
10077 * "inputsecret()" function
10078 */
10079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010081 typval_T *argvars;
10082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083{
10084 ++cmdline_star;
10085 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 --cmdline_star;
10088 --inputsecret_flag;
10089}
10090
10091/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010092 * "insert()" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010098{
10099 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010100 listitem_T *item;
10101 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010102
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010103 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010104 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010105 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010106 else if ((l = argvars[0].vval.v_list) != NULL
10107 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010108 {
10109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010111
Bram Moolenaar758711c2005-02-02 23:11:38 +000010112 if (before == l->lv_len)
10113 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010114 else
10115 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010116 item = list_find(l, before);
10117 if (item == NULL)
10118 {
10119 EMSGN(_(e_listidx), before);
10120 l = NULL;
10121 }
10122 }
10123 if (l != NULL)
10124 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010125 list_insert_tv(l, &argvars[1], item);
10126 ++l->lv_refcount;
10127 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010128 }
10129 }
10130}
10131
10132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 * "isdirectory()" function
10134 */
10135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010137 typval_T *argvars;
10138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141}
10142
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010143/*
10144 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10145 * it refers to a List or Dictionary that is locked.
10146 */
10147 static int
10148tv_islocked(tv)
10149 typval_T *tv;
10150{
10151 return (tv->v_lock & VAR_LOCKED)
10152 || (tv->v_type == VAR_LIST
10153 && tv->vval.v_list != NULL
10154 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10155 || (tv->v_type == VAR_DICT
10156 && tv->vval.v_dict != NULL
10157 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10158}
10159
10160/*
10161 * "islocked()" function
10162 */
10163 static void
10164f_islocked(argvars, rettv)
10165 typval_T *argvars;
10166 typval_T *rettv;
10167{
10168 lval_T lv;
10169 char_u *end;
10170 dictitem_T *di;
10171
10172 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010173 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10174 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010175 if (end != NULL && lv.ll_name != NULL)
10176 {
10177 if (*end != NUL)
10178 EMSG(_(e_trailing));
10179 else
10180 {
10181 if (lv.ll_tv == NULL)
10182 {
10183 if (check_changedtick(lv.ll_name))
10184 rettv->vval.v_number = 1; /* always locked */
10185 else
10186 {
10187 di = find_var(lv.ll_name, NULL);
10188 if (di != NULL)
10189 {
10190 /* Consider a variable locked when:
10191 * 1. the variable itself is locked
10192 * 2. the value of the variable is locked.
10193 * 3. the List or Dict value is locked.
10194 */
10195 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10196 || tv_islocked(&di->di_tv));
10197 }
10198 }
10199 }
10200 else if (lv.ll_range)
10201 EMSG(_("E745: Range not allowed"));
10202 else if (lv.ll_newkey != NULL)
10203 EMSG2(_(e_dictkey), lv.ll_newkey);
10204 else if (lv.ll_list != NULL)
10205 /* List item. */
10206 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10207 else
10208 /* Dictionary item. */
10209 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10210 }
10211 }
10212
10213 clear_lval(&lv);
10214}
10215
Bram Moolenaar33570922005-01-25 22:26:29 +000010216static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010217
10218/*
10219 * Turn a dict into a list:
10220 * "what" == 0: list of keys
10221 * "what" == 1: list of values
10222 * "what" == 2: list of items
10223 */
10224 static void
10225dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 typval_T *argvars;
10227 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010228 int what;
10229{
Bram Moolenaar33570922005-01-25 22:26:29 +000010230 list_T *l;
10231 list_T *l2;
10232 dictitem_T *di;
10233 hashitem_T *hi;
10234 listitem_T *li;
10235 listitem_T *li2;
10236 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010237 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010238
10239 rettv->vval.v_number = 0;
10240 if (argvars[0].v_type != VAR_DICT)
10241 {
10242 EMSG(_(e_dictreq));
10243 return;
10244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010245 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010246 return;
10247
10248 l = list_alloc();
10249 if (l == NULL)
10250 return;
10251 rettv->v_type = VAR_LIST;
10252 rettv->vval.v_list = l;
10253 ++l->lv_refcount;
10254
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 todo = d->dv_hashtab.ht_used;
10256 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010258 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010259 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010260 --todo;
10261 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010262
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010263 li = listitem_alloc();
10264 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010265 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010266 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010267
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010268 if (what == 0)
10269 {
10270 /* keys() */
10271 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010272 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010273 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10274 }
10275 else if (what == 1)
10276 {
10277 /* values() */
10278 copy_tv(&di->di_tv, &li->li_tv);
10279 }
10280 else
10281 {
10282 /* items() */
10283 l2 = list_alloc();
10284 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010285 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010286 li->li_tv.vval.v_list = l2;
10287 if (l2 == NULL)
10288 break;
10289 ++l2->lv_refcount;
10290
10291 li2 = listitem_alloc();
10292 if (li2 == NULL)
10293 break;
10294 list_append(l2, li2);
10295 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010296 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010297 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10298
10299 li2 = listitem_alloc();
10300 if (li2 == NULL)
10301 break;
10302 list_append(l2, li2);
10303 copy_tv(&di->di_tv, &li2->li_tv);
10304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010305 }
10306 }
10307}
10308
10309/*
10310 * "items(dict)" function
10311 */
10312 static void
10313f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T *argvars;
10315 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010316{
10317 dict_list(argvars, rettv, 2);
10318}
10319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010321 * "join()" function
10322 */
10323 static void
10324f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010325 typval_T *argvars;
10326 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010327{
10328 garray_T ga;
10329 char_u *sep;
10330
10331 rettv->vval.v_number = 0;
10332 if (argvars[0].v_type != VAR_LIST)
10333 {
10334 EMSG(_(e_listreq));
10335 return;
10336 }
10337 if (argvars[0].vval.v_list == NULL)
10338 return;
10339 if (argvars[1].v_type == VAR_UNKNOWN)
10340 sep = (char_u *)" ";
10341 else
10342 sep = get_tv_string(&argvars[1]);
10343
10344 ga_init2(&ga, (int)sizeof(char), 80);
10345 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10346 ga_append(&ga, NUL);
10347
10348 rettv->v_type = VAR_STRING;
10349 rettv->vval.v_string = (char_u *)ga.ga_data;
10350}
10351
10352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010353 * "keys()" function
10354 */
10355 static void
10356f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 typval_T *argvars;
10358 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010359{
10360 dict_list(argvars, rettv, 0);
10361}
10362
10363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 * "last_buffer_nr()" function.
10365 */
10366/*ARGSUSED*/
10367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010369 typval_T *argvars;
10370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
10372 int n = 0;
10373 buf_T *buf;
10374
10375 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10376 if (n < buf->b_fnum)
10377 n = buf->b_fnum;
10378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010380}
10381
10382/*
10383 * "len()" function
10384 */
10385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 typval_T *argvars;
10388 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010389{
10390 switch (argvars[0].v_type)
10391 {
10392 case VAR_STRING:
10393 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 rettv->vval.v_number = (varnumber_T)STRLEN(
10395 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396 break;
10397 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010399 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010400 case VAR_DICT:
10401 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10402 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010403 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010404 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010405 break;
10406 }
10407}
10408
Bram Moolenaar33570922005-01-25 22:26:29 +000010409static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010410
10411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010413 typval_T *argvars;
10414 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010415 int type;
10416{
10417#ifdef FEAT_LIBCALL
10418 char_u *string_in;
10419 char_u **string_result;
10420 int nr_result;
10421#endif
10422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010424 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010426 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010428
10429 if (check_restricted() || check_secure())
10430 return;
10431
10432#ifdef FEAT_LIBCALL
10433 /* The first two args must be strings, otherwise its meaningless */
10434 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10435 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010436 string_in = NULL;
10437 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010438 string_in = argvars[2].vval.v_string;
10439 if (type == VAR_NUMBER)
10440 string_result = NULL;
10441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010442 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010443 if (mch_libcall(argvars[0].vval.v_string,
10444 argvars[1].vval.v_string,
10445 string_in,
10446 argvars[2].vval.v_number,
10447 string_result,
10448 &nr_result) == OK
10449 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010451 }
10452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453}
10454
10455/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010456 * "libcall()" function
10457 */
10458 static void
10459f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010462{
10463 libcall_common(argvars, rettv, VAR_STRING);
10464}
10465
10466/*
10467 * "libcallnr()" function
10468 */
10469 static void
10470f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010473{
10474 libcall_common(argvars, rettv, VAR_NUMBER);
10475}
10476
10477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 * "line(string)" function
10479 */
10480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010482 typval_T *argvars;
10483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 linenr_T lnum = 0;
10486 pos_T *fp;
10487
10488 fp = var2fpos(&argvars[0], TRUE);
10489 if (fp != NULL)
10490 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
10493
10494/*
10495 * "line2byte(lnum)" function
10496 */
10497/*ARGSUSED*/
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505#else
10506 linenr_T lnum;
10507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10513 if (rettv->vval.v_number >= 0)
10514 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515#endif
10516}
10517
10518/*
10519 * "lispindent(lnum)" function
10520 */
10521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010523 typval_T *argvars;
10524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526#ifdef FEAT_LISP
10527 pos_T pos;
10528 linenr_T lnum;
10529
10530 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10533 {
10534 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 curwin->w_cursor = pos;
10537 }
10538 else
10539#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541}
10542
10543/*
10544 * "localtime()" function
10545 */
10546/*ARGSUSED*/
10547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 typval_T *argvars;
10550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553}
10554
Bram Moolenaar33570922005-01-25 22:26:29 +000010555static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556
10557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010559 typval_T *argvars;
10560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 int exact;
10562{
10563 char_u *keys;
10564 char_u *which;
10565 char_u buf[NUMBUFLEN];
10566 char_u *keys_buf = NULL;
10567 char_u *rhs;
10568 int mode;
10569 garray_T ga;
10570
10571 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 rettv->v_type = VAR_STRING;
10573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 if (*keys == NUL)
10577 return;
10578
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010579 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 else
10582 which = (char_u *)"";
10583 mode = get_map_mode(&which, 0);
10584
10585 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000010586 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 vim_free(keys_buf);
10588 if (rhs != NULL)
10589 {
10590 ga_init(&ga);
10591 ga.ga_itemsize = 1;
10592 ga.ga_growsize = 40;
10593
10594 while (*rhs != NUL)
10595 ga_concat(&ga, str2special(&rhs, FALSE));
10596
10597 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 }
10600}
10601
10602/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010603 * "map()" function
10604 */
10605 static void
10606f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 typval_T *argvars;
10608 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010609{
10610 filter_map(argvars, rettv, TRUE);
10611}
10612
10613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010614 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 */
10616 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010617f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010618 typval_T *argvars;
10619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010621 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622}
10623
10624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010625 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 */
10627 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010628f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 typval_T *argvars;
10630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633}
10634
Bram Moolenaar33570922005-01-25 22:26:29 +000010635static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636
10637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010639 typval_T *argvars;
10640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 int type;
10642{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643 char_u *str = NULL;
10644 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 char_u *pat;
10646 regmatch_T regmatch;
10647 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010648 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 char_u *save_cpo;
10650 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010651 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010652 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 list_T *l = NULL;
10654 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010655 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010656 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
10658 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10659 save_cpo = p_cpo;
10660 p_cpo = (char_u *)"";
10661
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010662 rettv->vval.v_number = -1;
10663 if (type == 3)
10664 {
10665 /* return empty list when there are no matches */
10666 if ((rettv->vval.v_list = list_alloc()) == NULL)
10667 goto theend;
10668 rettv->v_type = VAR_LIST;
10669 ++rettv->vval.v_list->lv_refcount;
10670 }
10671 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->v_type = VAR_STRING;
10674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010677 if (argvars[0].v_type == VAR_LIST)
10678 {
10679 if ((l = argvars[0].vval.v_list) == NULL)
10680 goto theend;
10681 li = l->lv_first;
10682 }
10683 else
10684 expr = str = get_tv_string(&argvars[0]);
10685
10686 pat = get_tv_string_buf(&argvars[1], patbuf);
10687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010688 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010691 if (l != NULL)
10692 {
10693 li = list_find(l, start);
10694 if (li == NULL)
10695 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010696 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010697 }
10698 else
10699 {
10700 if (start < 0)
10701 start = 0;
10702 if (start > (long)STRLEN(str))
10703 goto theend;
10704 str += start;
10705 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010706
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010707 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 }
10710
10711 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10712 if (regmatch.regprog != NULL)
10713 {
10714 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010715
10716 while (1)
10717 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010718 if (l != NULL)
10719 {
10720 if (li == NULL)
10721 {
10722 match = FALSE;
10723 break;
10724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010725 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010726 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010727 if (str == NULL)
10728 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010729 }
10730
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010731 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010732
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010733 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010734 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010735 if (l == NULL && !match)
10736 break;
10737
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010738 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010739 if (l != NULL)
10740 {
10741 li = li->li_next;
10742 ++idx;
10743 }
10744 else
10745 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010746#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010747 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010748#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010749 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010750#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010751 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010752 }
10753
10754 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010756 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010758 int i;
10759
10760 /* return list with matched string and submatches */
10761 for (i = 0; i < NSUBEXP; ++i)
10762 {
10763 if (regmatch.endp[i] == NULL)
10764 break;
10765 li = listitem_alloc();
10766 if (li == NULL)
10767 break;
10768 li->li_tv.v_type = VAR_STRING;
10769 li->li_tv.v_lock = 0;
10770 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10771 (int)(regmatch.endp[i] - regmatch.startp[i]));
10772 list_append(rettv->vval.v_list, li);
10773 }
10774 }
10775 else if (type == 2)
10776 {
10777 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010778 if (l != NULL)
10779 copy_tv(&li->li_tv, rettv);
10780 else
10781 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010783 }
10784 else if (l != NULL)
10785 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 else
10787 {
10788 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 (varnumber_T)(regmatch.startp[0] - str);
10791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 }
10796 }
10797 vim_free(regmatch.regprog);
10798 }
10799
10800theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010801 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 p_cpo = save_cpo;
10803}
10804
10805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010806 * "match()" function
10807 */
10808 static void
10809f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010810 typval_T *argvars;
10811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010812{
10813 find_some_match(argvars, rettv, 1);
10814}
10815
10816/*
10817 * "matchend()" function
10818 */
10819 static void
10820f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010821 typval_T *argvars;
10822 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010823{
10824 find_some_match(argvars, rettv, 0);
10825}
10826
10827/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010828 * "matchlist()" function
10829 */
10830 static void
10831f_matchlist(argvars, rettv)
10832 typval_T *argvars;
10833 typval_T *rettv;
10834{
10835 find_some_match(argvars, rettv, 3);
10836}
10837
10838/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010839 * "matchstr()" function
10840 */
10841 static void
10842f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010843 typval_T *argvars;
10844 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010845{
10846 find_some_match(argvars, rettv, 2);
10847}
10848
Bram Moolenaar33570922005-01-25 22:26:29 +000010849static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010850
10851 static void
10852max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010853 typval_T *argvars;
10854 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010855 int domax;
10856{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010857 long n = 0;
10858 long i;
10859
10860 if (argvars[0].v_type == VAR_LIST)
10861 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010862 list_T *l;
10863 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010865 l = argvars[0].vval.v_list;
10866 if (l != NULL)
10867 {
10868 li = l->lv_first;
10869 if (li != NULL)
10870 {
10871 n = get_tv_number(&li->li_tv);
10872 while (1)
10873 {
10874 li = li->li_next;
10875 if (li == NULL)
10876 break;
10877 i = get_tv_number(&li->li_tv);
10878 if (domax ? i > n : i < n)
10879 n = i;
10880 }
10881 }
10882 }
10883 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010884 else if (argvars[0].v_type == VAR_DICT)
10885 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010887 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010888 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010889 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010890
10891 d = argvars[0].vval.v_dict;
10892 if (d != NULL)
10893 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 todo = d->dv_hashtab.ht_used;
10895 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010897 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010899 --todo;
10900 i = get_tv_number(&HI2DI(hi)->di_tv);
10901 if (first)
10902 {
10903 n = i;
10904 first = FALSE;
10905 }
10906 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 n = i;
10908 }
10909 }
10910 }
10911 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010912 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010913 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010914 rettv->vval.v_number = n;
10915}
10916
10917/*
10918 * "max()" function
10919 */
10920 static void
10921f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *argvars;
10923 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010924{
10925 max_min(argvars, rettv, TRUE);
10926}
10927
10928/*
10929 * "min()" function
10930 */
10931 static void
10932f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *argvars;
10934 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010935{
10936 max_min(argvars, rettv, FALSE);
10937}
10938
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010939static int mkdir_recurse __ARGS((char_u *dir, int prot));
10940
10941/*
10942 * Create the directory in which "dir" is located, and higher levels when
10943 * needed.
10944 */
10945 static int
10946mkdir_recurse(dir, prot)
10947 char_u *dir;
10948 int prot;
10949{
10950 char_u *p;
10951 char_u *updir;
10952 int r = FAIL;
10953
10954 /* Get end of directory name in "dir".
10955 * We're done when it's "/" or "c:/". */
10956 p = gettail_sep(dir);
10957 if (p <= get_past_head(dir))
10958 return OK;
10959
10960 /* If the directory exists we're done. Otherwise: create it.*/
10961 updir = vim_strnsave(dir, (int)(p - dir));
10962 if (updir == NULL)
10963 return FAIL;
10964 if (mch_isdir(updir))
10965 r = OK;
10966 else if (mkdir_recurse(updir, prot) == OK)
10967 r = vim_mkdir_emsg(updir, prot);
10968 vim_free(updir);
10969 return r;
10970}
10971
10972#ifdef vim_mkdir
10973/*
10974 * "mkdir()" function
10975 */
10976 static void
10977f_mkdir(argvars, rettv)
10978 typval_T *argvars;
10979 typval_T *rettv;
10980{
10981 char_u *dir;
10982 char_u buf[NUMBUFLEN];
10983 int prot = 0755;
10984
10985 rettv->vval.v_number = FAIL;
10986 if (check_restricted() || check_secure())
10987 return;
10988
10989 dir = get_tv_string_buf(&argvars[0], buf);
10990 if (argvars[1].v_type != VAR_UNKNOWN)
10991 {
10992 if (argvars[2].v_type != VAR_UNKNOWN)
10993 prot = get_tv_number(&argvars[2]);
10994 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
10995 mkdir_recurse(dir, prot);
10996 }
10997 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
10998}
10999#endif
11000
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 * "mode()" function
11003 */
11004/*ARGSUSED*/
11005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011007 typval_T *argvars;
11008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
11010 char_u buf[2];
11011
11012#ifdef FEAT_VISUAL
11013 if (VIsual_active)
11014 {
11015 if (VIsual_select)
11016 buf[0] = VIsual_mode + 's' - 'v';
11017 else
11018 buf[0] = VIsual_mode;
11019 }
11020 else
11021#endif
11022 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11023 buf[0] = 'r';
11024 else if (State & INSERT)
11025 {
11026 if (State & REPLACE_FLAG)
11027 buf[0] = 'R';
11028 else
11029 buf[0] = 'i';
11030 }
11031 else if (State & CMDLINE)
11032 buf[0] = 'c';
11033 else
11034 buf[0] = 'n';
11035
11036 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 rettv->vval.v_string = vim_strsave(buf);
11038 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039}
11040
11041/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011042 * "nextnonblank()" function
11043 */
11044 static void
11045f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011046 typval_T *argvars;
11047 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011048{
11049 linenr_T lnum;
11050
11051 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11052 {
11053 if (lnum > curbuf->b_ml.ml_line_count)
11054 {
11055 lnum = 0;
11056 break;
11057 }
11058 if (*skipwhite(ml_get(lnum)) != NUL)
11059 break;
11060 }
11061 rettv->vval.v_number = lnum;
11062}
11063
11064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 * "nr2char()" function
11066 */
11067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011069 typval_T *argvars;
11070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071{
11072 char_u buf[NUMBUFLEN];
11073
11074#ifdef FEAT_MBYTE
11075 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 else
11078#endif
11079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 buf[1] = NUL;
11082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 rettv->v_type = VAR_STRING;
11084 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011085}
11086
11087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088 * "prevnonblank()" function
11089 */
11090 static void
11091f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 typval_T *argvars;
11093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094{
11095 linenr_T lnum;
11096
11097 lnum = get_tv_lnum(argvars);
11098 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11099 lnum = 0;
11100 else
11101 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11102 --lnum;
11103 rettv->vval.v_number = lnum;
11104}
11105
Bram Moolenaar8c711452005-01-14 21:53:12 +000011106/*
11107 * "range()" function
11108 */
11109 static void
11110f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *argvars;
11112 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011113{
11114 long start;
11115 long end;
11116 long stride = 1;
11117 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 list_T *l;
11119 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011120
11121 start = get_tv_number(&argvars[0]);
11122 if (argvars[1].v_type == VAR_UNKNOWN)
11123 {
11124 end = start - 1;
11125 start = 0;
11126 }
11127 else
11128 {
11129 end = get_tv_number(&argvars[1]);
11130 if (argvars[2].v_type != VAR_UNKNOWN)
11131 stride = get_tv_number(&argvars[2]);
11132 }
11133
11134 rettv->vval.v_number = 0;
11135 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011136 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011137 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011138 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011139 else
11140 {
11141 l = list_alloc();
11142 if (l != NULL)
11143 {
11144 rettv->v_type = VAR_LIST;
11145 rettv->vval.v_list = l;
11146 ++l->lv_refcount;
11147
11148 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11149 {
11150 li = listitem_alloc();
11151 if (li == NULL)
11152 break;
11153 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011154 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011155 li->li_tv.vval.v_number = i;
11156 list_append(l, li);
11157 }
11158 }
11159 }
11160}
11161
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011162/*
11163 * "readfile()" function
11164 */
11165 static void
11166f_readfile(argvars, rettv)
11167 typval_T *argvars;
11168 typval_T *rettv;
11169{
11170 int binary = FALSE;
11171 char_u *fname;
11172 FILE *fd;
11173 list_T *l;
11174 listitem_T *li;
11175#define FREAD_SIZE 200 /* optimized for text lines */
11176 char_u buf[FREAD_SIZE];
11177 int readlen; /* size of last fread() */
11178 int buflen; /* nr of valid chars in buf[] */
11179 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11180 int tolist; /* first byte in buf[] still to be put in list */
11181 int chop; /* how many CR to chop off */
11182 char_u *prev = NULL; /* previously read bytes, if any */
11183 int prevlen = 0; /* length of "prev" if not NULL */
11184 char_u *s;
11185 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011186 long maxline = MAXLNUM;
11187 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011188
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011189 if (argvars[1].v_type != VAR_UNKNOWN)
11190 {
11191 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11192 binary = TRUE;
11193 if (argvars[2].v_type != VAR_UNKNOWN)
11194 maxline = get_tv_number(&argvars[2]);
11195 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011196
11197 l = list_alloc();
11198 if (l == NULL)
11199 return;
11200 rettv->v_type = VAR_LIST;
11201 rettv->vval.v_list = l;
11202 l->lv_refcount = 1;
11203
11204 /* Always open the file in binary mode, library functions have a mind of
11205 * their own about CR-LF conversion. */
11206 fname = get_tv_string(&argvars[0]);
11207 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11208 {
11209 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11210 return;
11211 }
11212
11213 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011214 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011215 {
11216 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11217 buflen = filtd + readlen;
11218 tolist = 0;
11219 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11220 {
11221 if (buf[filtd] == '\n' || readlen <= 0)
11222 {
11223 /* Only when in binary mode add an empty list item when the
11224 * last line ends in a '\n'. */
11225 if (!binary && readlen == 0 && filtd == 0)
11226 break;
11227
11228 /* Found end-of-line or end-of-file: add a text line to the
11229 * list. */
11230 chop = 0;
11231 if (!binary)
11232 while (filtd - chop - 1 >= tolist
11233 && buf[filtd - chop - 1] == '\r')
11234 ++chop;
11235 len = filtd - tolist - chop;
11236 if (prev == NULL)
11237 s = vim_strnsave(buf + tolist, len);
11238 else
11239 {
11240 s = alloc((unsigned)(prevlen + len + 1));
11241 if (s != NULL)
11242 {
11243 mch_memmove(s, prev, prevlen);
11244 vim_free(prev);
11245 prev = NULL;
11246 mch_memmove(s + prevlen, buf + tolist, len);
11247 s[prevlen + len] = NUL;
11248 }
11249 }
11250 tolist = filtd + 1;
11251
11252 li = listitem_alloc();
11253 if (li == NULL)
11254 {
11255 vim_free(s);
11256 break;
11257 }
11258 li->li_tv.v_type = VAR_STRING;
11259 li->li_tv.v_lock = 0;
11260 li->li_tv.vval.v_string = s;
11261 list_append(l, li);
11262
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011263 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011264 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011265 if (readlen <= 0)
11266 break;
11267 }
11268 else if (buf[filtd] == NUL)
11269 buf[filtd] = '\n';
11270 }
11271 if (readlen <= 0)
11272 break;
11273
11274 if (tolist == 0)
11275 {
11276 /* "buf" is full, need to move text to an allocated buffer */
11277 if (prev == NULL)
11278 {
11279 prev = vim_strnsave(buf, buflen);
11280 prevlen = buflen;
11281 }
11282 else
11283 {
11284 s = alloc((unsigned)(prevlen + buflen));
11285 if (s != NULL)
11286 {
11287 mch_memmove(s, prev, prevlen);
11288 mch_memmove(s + prevlen, buf, buflen);
11289 vim_free(prev);
11290 prev = s;
11291 prevlen += buflen;
11292 }
11293 }
11294 filtd = 0;
11295 }
11296 else
11297 {
11298 mch_memmove(buf, buf + tolist, buflen - tolist);
11299 filtd -= tolist;
11300 }
11301 }
11302
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011303 /*
11304 * For a negative line count use only the lines at the end of the file,
11305 * free the rest.
11306 */
11307 if (maxline < 0)
11308 while (cnt > -maxline)
11309 {
11310 listitem_remove(l, l->lv_first);
11311 --cnt;
11312 }
11313
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011314 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011315 fclose(fd);
11316}
11317
11318
Bram Moolenaar0d660222005-01-07 21:51:51 +000011319#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11320static void make_connection __ARGS((void));
11321static int check_connection __ARGS((void));
11322
11323 static void
11324make_connection()
11325{
11326 if (X_DISPLAY == NULL
11327# ifdef FEAT_GUI
11328 && !gui.in_use
11329# endif
11330 )
11331 {
11332 x_force_connect = TRUE;
11333 setup_term_clip();
11334 x_force_connect = FALSE;
11335 }
11336}
11337
11338 static int
11339check_connection()
11340{
11341 make_connection();
11342 if (X_DISPLAY == NULL)
11343 {
11344 EMSG(_("E240: No connection to Vim server"));
11345 return FAIL;
11346 }
11347 return OK;
11348}
11349#endif
11350
11351#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011352static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011353
11354 static void
11355remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011356 typval_T *argvars;
11357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358 int expr;
11359{
11360 char_u *server_name;
11361 char_u *keys;
11362 char_u *r = NULL;
11363 char_u buf[NUMBUFLEN];
11364# ifdef WIN32
11365 HWND w;
11366# else
11367 Window w;
11368# endif
11369
11370 if (check_restricted() || check_secure())
11371 return;
11372
11373# ifdef FEAT_X11
11374 if (check_connection() == FAIL)
11375 return;
11376# endif
11377
11378 server_name = get_tv_string(&argvars[0]);
11379 keys = get_tv_string_buf(&argvars[1], buf);
11380# ifdef WIN32
11381 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11382# else
11383 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11384 < 0)
11385# endif
11386 {
11387 if (r != NULL)
11388 EMSG(r); /* sending worked but evaluation failed */
11389 else
11390 EMSG2(_("E241: Unable to send to %s"), server_name);
11391 return;
11392 }
11393
11394 rettv->vval.v_string = r;
11395
11396 if (argvars[2].v_type != VAR_UNKNOWN)
11397 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000011399 char_u str[30];
Bram Moolenaar0d660222005-01-07 21:51:51 +000011400
11401 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 v.di_tv.v_type = VAR_STRING;
11403 v.di_tv.vval.v_string = vim_strsave(str);
11404 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11405 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011406 }
11407}
11408#endif
11409
11410/*
11411 * "remote_expr()" function
11412 */
11413/*ARGSUSED*/
11414 static void
11415f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 typval_T *argvars;
11417 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011418{
11419 rettv->v_type = VAR_STRING;
11420 rettv->vval.v_string = NULL;
11421#ifdef FEAT_CLIENTSERVER
11422 remote_common(argvars, rettv, TRUE);
11423#endif
11424}
11425
11426/*
11427 * "remote_foreground()" function
11428 */
11429/*ARGSUSED*/
11430 static void
11431f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011432 typval_T *argvars;
11433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011434{
11435 rettv->vval.v_number = 0;
11436#ifdef FEAT_CLIENTSERVER
11437# ifdef WIN32
11438 /* On Win32 it's done in this application. */
11439 serverForeground(get_tv_string(&argvars[0]));
11440# else
11441 /* Send a foreground() expression to the server. */
11442 argvars[1].v_type = VAR_STRING;
11443 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11444 argvars[2].v_type = VAR_UNKNOWN;
11445 remote_common(argvars, rettv, TRUE);
11446 vim_free(argvars[1].vval.v_string);
11447# endif
11448#endif
11449}
11450
11451/*ARGSUSED*/
11452 static void
11453f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011456{
11457#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011459 char_u *s = NULL;
11460# ifdef WIN32
11461 int n = 0;
11462# endif
11463
11464 if (check_restricted() || check_secure())
11465 {
11466 rettv->vval.v_number = -1;
11467 return;
11468 }
11469# ifdef WIN32
11470 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11471 if (n == 0)
11472 rettv->vval.v_number = -1;
11473 else
11474 {
11475 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11476 rettv->vval.v_number = (s != NULL);
11477 }
11478# else
11479 rettv->vval.v_number = 0;
11480 if (check_connection() == FAIL)
11481 return;
11482
11483 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11484 serverStrToWin(get_tv_string(&argvars[0])), &s);
11485# endif
11486
11487 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11488 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011489 v.di_tv.v_type = VAR_STRING;
11490 v.di_tv.vval.v_string = vim_strsave(s);
11491 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11492 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 }
11494#else
11495 rettv->vval.v_number = -1;
11496#endif
11497}
11498
11499/*ARGSUSED*/
11500 static void
11501f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011504{
11505 char_u *r = NULL;
11506
11507#ifdef FEAT_CLIENTSERVER
11508 if (!check_restricted() && !check_secure())
11509 {
11510# ifdef WIN32
11511 /* The server's HWND is encoded in the 'id' parameter */
11512 int n = 0;
11513
11514 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11515 if (n != 0)
11516 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11517 if (r == NULL)
11518# else
11519 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11520 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11521# endif
11522 EMSG(_("E277: Unable to read a server reply"));
11523 }
11524#endif
11525 rettv->v_type = VAR_STRING;
11526 rettv->vval.v_string = r;
11527}
11528
11529/*
11530 * "remote_send()" function
11531 */
11532/*ARGSUSED*/
11533 static void
11534f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011535 typval_T *argvars;
11536 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011537{
11538 rettv->v_type = VAR_STRING;
11539 rettv->vval.v_string = NULL;
11540#ifdef FEAT_CLIENTSERVER
11541 remote_common(argvars, rettv, FALSE);
11542#endif
11543}
11544
11545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011546 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011547 */
11548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011552{
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 list_T *l;
11554 listitem_T *item, *item2;
11555 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011556 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011557 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011558 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011559 dict_T *d;
11560 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011561
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011562 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011563 if (argvars[0].v_type == VAR_DICT)
11564 {
11565 if (argvars[2].v_type != VAR_UNKNOWN)
11566 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011567 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011568 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011569 {
11570 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011571 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011572 if (di == NULL)
11573 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011574 else
11575 {
11576 *rettv = di->di_tv;
11577 init_tv(&di->di_tv);
11578 dictitem_remove(d, di);
11579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011580 }
11581 }
11582 else if (argvars[0].v_type != VAR_LIST)
11583 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011584 else if ((l = argvars[0].vval.v_list) != NULL
11585 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011588 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011589 if (item == NULL)
11590 EMSGN(_(e_listidx), idx);
11591 else
11592 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011593 if (argvars[2].v_type == VAR_UNKNOWN)
11594 {
11595 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011596 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011597 *rettv = item->li_tv;
11598 vim_free(item);
11599 }
11600 else
11601 {
11602 /* Remove range of items, return list with values. */
11603 end = get_tv_number(&argvars[2]);
11604 item2 = list_find(l, end);
11605 if (item2 == NULL)
11606 EMSGN(_(e_listidx), end);
11607 else
11608 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011609 int cnt = 0;
11610
11611 for (li = item; li != NULL; li = li->li_next)
11612 {
11613 ++cnt;
11614 if (li == item2)
11615 break;
11616 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011617 if (li == NULL) /* didn't find "item2" after "item" */
11618 EMSG(_(e_invrange));
11619 else
11620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011621 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011622 l = list_alloc();
11623 if (l != NULL)
11624 {
11625 rettv->v_type = VAR_LIST;
11626 rettv->vval.v_list = l;
11627 l->lv_first = item;
11628 l->lv_last = item2;
11629 l->lv_refcount = 1;
11630 item->li_prev = NULL;
11631 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011632 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011633 }
11634 }
11635 }
11636 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011637 }
11638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639}
11640
11641/*
11642 * "rename({from}, {to})" function
11643 */
11644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011646 typval_T *argvars;
11647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649 char_u buf[NUMBUFLEN];
11650
11651 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11655 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656}
11657
11658/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011659 * "repeat()" function
11660 */
11661/*ARGSUSED*/
11662 static void
11663f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *argvars;
11665 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011666{
11667 char_u *p;
11668 int n;
11669 int slen;
11670 int len;
11671 char_u *r;
11672 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011673 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011674
11675 n = get_tv_number(&argvars[1]);
11676 if (argvars[0].v_type == VAR_LIST)
11677 {
11678 l = list_alloc();
11679 if (l != NULL && argvars[0].vval.v_list != NULL)
11680 {
11681 l->lv_refcount = 1;
11682 while (n-- > 0)
11683 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11684 break;
11685 }
11686 rettv->v_type = VAR_LIST;
11687 rettv->vval.v_list = l;
11688 }
11689 else
11690 {
11691 p = get_tv_string(&argvars[0]);
11692 rettv->v_type = VAR_STRING;
11693 rettv->vval.v_string = NULL;
11694
11695 slen = (int)STRLEN(p);
11696 len = slen * n;
11697 if (len <= 0)
11698 return;
11699
11700 r = alloc(len + 1);
11701 if (r != NULL)
11702 {
11703 for (i = 0; i < n; i++)
11704 mch_memmove(r + i * slen, p, (size_t)slen);
11705 r[len] = NUL;
11706 }
11707
11708 rettv->vval.v_string = r;
11709 }
11710}
11711
11712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 * "resolve()" function
11714 */
11715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *argvars;
11718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719{
11720 char_u *p;
11721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723#ifdef FEAT_SHORTCUT
11724 {
11725 char_u *v = NULL;
11726
11727 v = mch_resolve_shortcut(p);
11728 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 }
11733#else
11734# ifdef HAVE_READLINK
11735 {
11736 char_u buf[MAXPATHL + 1];
11737 char_u *cpy;
11738 int len;
11739 char_u *remain = NULL;
11740 char_u *q;
11741 int is_relative_to_current = FALSE;
11742 int has_trailing_pathsep = FALSE;
11743 int limit = 100;
11744
11745 p = vim_strsave(p);
11746
11747 if (p[0] == '.' && (vim_ispathsep(p[1])
11748 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11749 is_relative_to_current = TRUE;
11750
11751 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011752 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 has_trailing_pathsep = TRUE;
11754
11755 q = getnextcomp(p);
11756 if (*q != NUL)
11757 {
11758 /* Separate the first path component in "p", and keep the
11759 * remainder (beginning with the path separator). */
11760 remain = vim_strsave(q - 1);
11761 q[-1] = NUL;
11762 }
11763
11764 for (;;)
11765 {
11766 for (;;)
11767 {
11768 len = readlink((char *)p, (char *)buf, MAXPATHL);
11769 if (len <= 0)
11770 break;
11771 buf[len] = NUL;
11772
11773 if (limit-- == 0)
11774 {
11775 vim_free(p);
11776 vim_free(remain);
11777 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 goto fail;
11780 }
11781
11782 /* Ensure that the result will have a trailing path separator
11783 * if the argument has one. */
11784 if (remain == NULL && has_trailing_pathsep)
11785 add_pathsep(buf);
11786
11787 /* Separate the first path component in the link value and
11788 * concatenate the remainders. */
11789 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11790 if (*q != NUL)
11791 {
11792 if (remain == NULL)
11793 remain = vim_strsave(q - 1);
11794 else
11795 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011796 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 if (cpy != NULL)
11798 {
11799 STRCAT(cpy, remain);
11800 vim_free(remain);
11801 remain = cpy;
11802 }
11803 }
11804 q[-1] = NUL;
11805 }
11806
11807 q = gettail(p);
11808 if (q > p && *q == NUL)
11809 {
11810 /* Ignore trailing path separator. */
11811 q[-1] = NUL;
11812 q = gettail(p);
11813 }
11814 if (q > p && !mch_isFullName(buf))
11815 {
11816 /* symlink is relative to directory of argument */
11817 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11818 if (cpy != NULL)
11819 {
11820 STRCPY(cpy, p);
11821 STRCPY(gettail(cpy), buf);
11822 vim_free(p);
11823 p = cpy;
11824 }
11825 }
11826 else
11827 {
11828 vim_free(p);
11829 p = vim_strsave(buf);
11830 }
11831 }
11832
11833 if (remain == NULL)
11834 break;
11835
11836 /* Append the first path component of "remain" to "p". */
11837 q = getnextcomp(remain + 1);
11838 len = q - remain - (*q != NUL);
11839 cpy = vim_strnsave(p, STRLEN(p) + len);
11840 if (cpy != NULL)
11841 {
11842 STRNCAT(cpy, remain, len);
11843 vim_free(p);
11844 p = cpy;
11845 }
11846 /* Shorten "remain". */
11847 if (*q != NUL)
11848 STRCPY(remain, q - 1);
11849 else
11850 {
11851 vim_free(remain);
11852 remain = NULL;
11853 }
11854 }
11855
11856 /* If the result is a relative path name, make it explicitly relative to
11857 * the current directory if and only if the argument had this form. */
11858 if (!vim_ispathsep(*p))
11859 {
11860 if (is_relative_to_current
11861 && *p != NUL
11862 && !(p[0] == '.'
11863 && (p[1] == NUL
11864 || vim_ispathsep(p[1])
11865 || (p[1] == '.'
11866 && (p[2] == NUL
11867 || vim_ispathsep(p[2]))))))
11868 {
11869 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011870 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 if (cpy != NULL)
11872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 vim_free(p);
11874 p = cpy;
11875 }
11876 }
11877 else if (!is_relative_to_current)
11878 {
11879 /* Strip leading "./". */
11880 q = p;
11881 while (q[0] == '.' && vim_ispathsep(q[1]))
11882 q += 2;
11883 if (q > p)
11884 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11885 }
11886 }
11887
11888 /* Ensure that the result will have no trailing path separator
11889 * if the argument had none. But keep "/" or "//". */
11890 if (!has_trailing_pathsep)
11891 {
11892 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011893 if (after_pathsep(p, q))
11894 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 }
11896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 }
11899# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901# endif
11902#endif
11903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905
11906#ifdef HAVE_READLINK
11907fail:
11908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910}
11911
11912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011913 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 */
11915 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *argvars;
11918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
Bram Moolenaar33570922005-01-25 22:26:29 +000011920 list_T *l;
11921 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922
Bram Moolenaar0d660222005-01-07 21:51:51 +000011923 rettv->vval.v_number = 0;
11924 if (argvars[0].v_type != VAR_LIST)
11925 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011926 else if ((l = argvars[0].vval.v_list) != NULL
11927 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928 {
11929 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011930 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011931 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011932 while (li != NULL)
11933 {
11934 ni = li->li_prev;
11935 list_append(l, li);
11936 li = ni;
11937 }
11938 rettv->vval.v_list = l;
11939 rettv->v_type = VAR_LIST;
11940 ++l->lv_refcount;
11941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942}
11943
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011944#define SP_NOMOVE 1 /* don't move cursor */
11945#define SP_REPEAT 2 /* repeat to find outer pair */
11946#define SP_RETCOUNT 4 /* return matchcount */
11947
Bram Moolenaar33570922005-01-25 22:26:29 +000011948static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011949
11950/*
11951 * Get flags for a search function.
11952 * Possibly sets "p_ws".
11953 * Returns BACKWARD, FORWARD or zero (for an error).
11954 */
11955 static int
11956get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011958 int *flagsp;
11959{
11960 int dir = FORWARD;
11961 char_u *flags;
11962 char_u nbuf[NUMBUFLEN];
11963 int mask;
11964
11965 if (varp->v_type != VAR_UNKNOWN)
11966 {
11967 flags = get_tv_string_buf(varp, nbuf);
11968 while (*flags != NUL)
11969 {
11970 switch (*flags)
11971 {
11972 case 'b': dir = BACKWARD; break;
11973 case 'w': p_ws = TRUE; break;
11974 case 'W': p_ws = FALSE; break;
11975 default: mask = 0;
11976 if (flagsp != NULL)
11977 switch (*flags)
11978 {
11979 case 'n': mask = SP_NOMOVE; break;
11980 case 'r': mask = SP_REPEAT; break;
11981 case 'm': mask = SP_RETCOUNT; break;
11982 }
11983 if (mask == 0)
11984 {
11985 EMSG2(_(e_invarg2), flags);
11986 dir = 0;
11987 }
11988 else
11989 *flagsp |= mask;
11990 }
11991 if (dir == 0)
11992 break;
11993 ++flags;
11994 }
11995 }
11996 return dir;
11997}
11998
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999/*
12000 * "search()" function
12001 */
12002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012004 typval_T *argvars;
12005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006{
12007 char_u *pat;
12008 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012009 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 int save_p_ws = p_ws;
12011 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012012 int flags = 0;
12013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012017 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12018 if (dir == 0)
12019 goto theend;
12020 if ((flags & ~SP_NOMOVE) != 0)
12021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012023 goto theend;
12024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012026 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12028 SEARCH_KEEP, RE_SEARCH) != FAIL)
12029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 curwin->w_cursor = pos;
12032 /* "/$" will put the cursor after the end of the line, may need to
12033 * correct that here */
12034 check_cursor();
12035 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012036
12037 /* If 'n' flag is used: restore cursor position. */
12038 if (flags & SP_NOMOVE)
12039 curwin->w_cursor = save_cursor;
12040theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 p_ws = save_p_ws;
12042}
12043
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044/*
12045 * "searchpair()" function
12046 */
12047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T *argvars;
12050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 char_u *spat, *mpat, *epat;
12053 char_u *skip;
12054 char_u *pat, *pat2, *pat3;
12055 pos_T pos;
12056 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012057 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 pos_T save_cursor;
12059 pos_T save_pos;
12060 int save_p_ws = p_ws;
12061 char_u *save_cpo;
12062 int dir;
12063 int flags = 0;
12064 char_u nbuf1[NUMBUFLEN];
12065 char_u nbuf2[NUMBUFLEN];
12066 char_u nbuf3[NUMBUFLEN];
12067 int n;
12068 int r;
12069 int nest = 1;
12070 int err;
12071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073
12074 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12075 save_cpo = p_cpo;
12076 p_cpo = (char_u *)"";
12077
12078 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 spat = get_tv_string(&argvars[0]);
12080 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12081 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082
12083 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12084 * start/middle/end (pat3, for the top pair). */
12085 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12086 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12087 if (pat2 == NULL || pat3 == NULL)
12088 goto theend;
12089 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12090 if (*mpat == NUL)
12091 STRCPY(pat3, pat2);
12092 else
12093 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12094 spat, epat, mpat);
12095
12096 /* Handle the optional fourth argument: flags */
12097 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012098 if (dir == 0)
12099 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100
12101 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012102 if (argvars[3].v_type == VAR_UNKNOWN
12103 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 skip = (char_u *)"";
12105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107
12108 save_cursor = curwin->w_cursor;
12109 pos = curwin->w_cursor;
12110 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012111 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 pat = pat3;
12113 for (;;)
12114 {
12115 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12116 SEARCH_KEEP, RE_SEARCH);
12117 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12118 /* didn't find it or found the first match again: FAIL */
12119 break;
12120
12121 if (firstpos.lnum == 0)
12122 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012123 if (equalpos(pos, foundpos))
12124 {
12125 /* Found the same position again. Can happen with a pattern that
12126 * has "\zs" at the end and searching backwards. Advance one
12127 * character and try again. */
12128 if (dir == BACKWARD)
12129 decl(&pos);
12130 else
12131 incl(&pos);
12132 }
12133 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134
12135 /* If the skip pattern matches, ignore this match. */
12136 if (*skip != NUL)
12137 {
12138 save_pos = curwin->w_cursor;
12139 curwin->w_cursor = pos;
12140 r = eval_to_bool(skip, &err, NULL, FALSE);
12141 curwin->w_cursor = save_pos;
12142 if (err)
12143 {
12144 /* Evaluating {skip} caused an error, break here. */
12145 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 break;
12148 }
12149 if (r)
12150 continue;
12151 }
12152
12153 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12154 {
12155 /* Found end when searching backwards or start when searching
12156 * forward: nested pair. */
12157 ++nest;
12158 pat = pat2; /* nested, don't search for middle */
12159 }
12160 else
12161 {
12162 /* Found end when searching forward or start when searching
12163 * backward: end of (nested) pair; or found middle in outer pair. */
12164 if (--nest == 1)
12165 pat = pat3; /* outer level, search for middle */
12166 }
12167
12168 if (nest == 0)
12169 {
12170 /* Found the match: return matchcount or line number. */
12171 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 curwin->w_cursor = pos;
12176 if (!(flags & SP_REPEAT))
12177 break;
12178 nest = 1; /* search for next unmatched */
12179 }
12180 }
12181
12182 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 curwin->w_cursor = save_cursor;
12185
12186theend:
12187 vim_free(pat2);
12188 vim_free(pat3);
12189 p_ws = save_p_ws;
12190 p_cpo = save_cpo;
12191}
12192
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193/*ARGSUSED*/
12194 static void
12195f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012196 typval_T *argvars;
12197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012199#ifdef FEAT_CLIENTSERVER
12200 char_u buf[NUMBUFLEN];
12201 char_u *server = get_tv_string(&argvars[0]);
12202 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204 rettv->vval.v_number = -1;
12205 if (check_restricted() || check_secure())
12206 return;
12207# ifdef FEAT_X11
12208 if (check_connection() == FAIL)
12209 return;
12210# endif
12211
12212 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214 EMSG(_("E258: Unable to send to client"));
12215 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217 rettv->vval.v_number = 0;
12218#else
12219 rettv->vval.v_number = -1;
12220#endif
12221}
12222
12223/*ARGSUSED*/
12224 static void
12225f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012226 typval_T *argvars;
12227 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012228{
12229 char_u *r = NULL;
12230
12231#ifdef FEAT_CLIENTSERVER
12232# ifdef WIN32
12233 r = serverGetVimNames();
12234# else
12235 make_connection();
12236 if (X_DISPLAY != NULL)
12237 r = serverGetVimNames(X_DISPLAY);
12238# endif
12239#endif
12240 rettv->v_type = VAR_STRING;
12241 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242}
12243
12244/*
12245 * "setbufvar()" function
12246 */
12247/*ARGSUSED*/
12248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012250 typval_T *argvars;
12251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252{
12253 buf_T *buf;
12254#ifdef FEAT_AUTOCMD
12255 aco_save_T aco;
12256#else
12257 buf_T *save_curbuf;
12258#endif
12259 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012260 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 char_u nbuf[NUMBUFLEN];
12262
12263 if (check_restricted() || check_secure())
12264 return;
12265 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 buf = get_buf_tv(&argvars[0]);
12267 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 varp = &argvars[2];
12269
12270 if (buf != NULL && varname != NULL && varp != NULL)
12271 {
12272 /* set curbuf to be our buf, temporarily */
12273#ifdef FEAT_AUTOCMD
12274 aucmd_prepbuf(&aco, buf);
12275#else
12276 save_curbuf = curbuf;
12277 curbuf = buf;
12278#endif
12279
12280 if (*varname == '&')
12281 {
12282 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 set_option_value(varname, get_tv_number(varp),
12284 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 }
12286 else
12287 {
12288 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12289 if (bufvarname != NULL)
12290 {
12291 STRCPY(bufvarname, "b:");
12292 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012293 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 vim_free(bufvarname);
12295 }
12296 }
12297
12298 /* reset notion of buffer */
12299#ifdef FEAT_AUTOCMD
12300 aucmd_restbuf(&aco);
12301#else
12302 curbuf = save_curbuf;
12303#endif
12304 }
12305 --emsg_off;
12306}
12307
12308/*
12309 * "setcmdpos()" function
12310 */
12311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012313 typval_T *argvars;
12314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 rettv->vval.v_number = set_cmdline_pos(
12317 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318}
12319
12320/*
12321 * "setline()" function
12322 */
12323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012325 typval_T *argvars;
12326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327{
12328 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000012329 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012330 list_T *l = NULL;
12331 listitem_T *li = NULL;
12332 long added = 0;
12333 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012335 lnum = get_tv_lnum(&argvars[0]);
12336 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012338 l = argvars[1].vval.v_list;
12339 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012341 else
12342 line = get_tv_string(&argvars[1]);
12343
12344 rettv->vval.v_number = 0; /* OK */
12345 for (;;)
12346 {
12347 if (l != NULL)
12348 {
12349 /* list argument, get next string */
12350 if (li == NULL)
12351 break;
12352 line = get_tv_string(&li->li_tv);
12353 li = li->li_next;
12354 }
12355
12356 rettv->vval.v_number = 1; /* FAIL */
12357 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
12358 break;
12359 if (lnum <= curbuf->b_ml.ml_line_count)
12360 {
12361 /* existing line, replace it */
12362 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
12363 {
12364 changed_bytes(lnum, 0);
12365 check_cursor_col();
12366 rettv->vval.v_number = 0; /* OK */
12367 }
12368 }
12369 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
12370 {
12371 /* lnum is one past the last line, append the line */
12372 ++added;
12373 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
12374 rettv->vval.v_number = 0; /* OK */
12375 }
12376
12377 if (l == NULL) /* only one string argument */
12378 break;
12379 ++lnum;
12380 }
12381
12382 if (added > 0)
12383 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384}
12385
12386/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012387 * "setqflist()" function
12388 */
12389/*ARGSUSED*/
12390 static void
12391f_setqflist(argvars, rettv)
12392 typval_T *argvars;
12393 typval_T *rettv;
12394{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012395 char_u *act;
12396 int action = ' ';
12397
Bram Moolenaar2641f772005-03-25 21:58:17 +000012398 rettv->vval.v_number = -1;
12399
12400#ifdef FEAT_QUICKFIX
12401 if (argvars[0].v_type != VAR_LIST)
12402 EMSG(_(e_listreq));
12403 else
12404 {
12405 list_T *l = argvars[0].vval.v_list;
12406
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012407 if (argvars[1].v_type == VAR_STRING)
12408 {
12409 act = get_tv_string(&argvars[1]);
12410 if (*act == 'a' || *act == 'r')
12411 action = *act;
12412 }
12413
12414 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012415 rettv->vval.v_number = 0;
12416 }
12417#endif
12418}
12419
12420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 * "setreg()" function
12422 */
12423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012425 typval_T *argvars;
12426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427{
12428 int regname;
12429 char_u *strregname;
12430 char_u *stropt;
12431 int append;
12432 char_u yank_type;
12433 long block_len;
12434
12435 block_len = -1;
12436 yank_type = MAUTO;
12437 append = FALSE;
12438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 strregname = get_tv_string(argvars);
12440 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441
12442 regname = (strregname == NULL ? '"' : *strregname);
12443 if (regname == 0 || regname == '@')
12444 regname = '"';
12445 else if (regname == '=')
12446 return;
12447
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451 switch (*stropt)
12452 {
12453 case 'a': case 'A': /* append */
12454 append = TRUE;
12455 break;
12456 case 'v': case 'c': /* character-wise selection */
12457 yank_type = MCHAR;
12458 break;
12459 case 'V': case 'l': /* line-wise selection */
12460 yank_type = MLINE;
12461 break;
12462#ifdef FEAT_VISUAL
12463 case 'b': case Ctrl_V: /* block-wise selection */
12464 yank_type = MBLOCK;
12465 if (VIM_ISDIGIT(stropt[1]))
12466 {
12467 ++stropt;
12468 block_len = getdigits(&stropt) - 1;
12469 --stropt;
12470 }
12471 break;
12472#endif
12473 }
12474 }
12475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479}
12480
12481
12482/*
12483 * "setwinvar(expr)" function
12484 */
12485/*ARGSUSED*/
12486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012488 typval_T *argvars;
12489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490{
12491 win_T *win;
12492#ifdef FEAT_WINDOWS
12493 win_T *save_curwin;
12494#endif
12495 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 char_u nbuf[NUMBUFLEN];
12498
12499 if (check_restricted() || check_secure())
12500 return;
12501 ++emsg_off;
12502 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504 varp = &argvars[2];
12505
12506 if (win != NULL && varname != NULL && varp != NULL)
12507 {
12508#ifdef FEAT_WINDOWS
12509 /* set curwin to be our win, temporarily */
12510 save_curwin = curwin;
12511 curwin = win;
12512 curbuf = curwin->w_buffer;
12513#endif
12514
12515 if (*varname == '&')
12516 {
12517 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 set_option_value(varname, get_tv_number(varp),
12519 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520 }
12521 else
12522 {
12523 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12524 if (winvarname != NULL)
12525 {
12526 STRCPY(winvarname, "w:");
12527 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012528 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 vim_free(winvarname);
12530 }
12531 }
12532
12533#ifdef FEAT_WINDOWS
12534 /* Restore current window, if it's still valid (autocomands can make
12535 * it invalid). */
12536 if (win_valid(save_curwin))
12537 {
12538 curwin = save_curwin;
12539 curbuf = curwin->w_buffer;
12540 }
12541#endif
12542 }
12543 --emsg_off;
12544}
12545
12546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 */
12549 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012551 typval_T *argvars;
12552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012554 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556 p = get_tv_string(&argvars[0]);
12557 rettv->vval.v_string = vim_strsave(p);
12558 simplify_filename(rettv->vval.v_string); /* simplify in place */
12559 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560}
12561
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562static int
12563#ifdef __BORLANDC__
12564 _RTLENTRYF
12565#endif
12566 item_compare __ARGS((const void *s1, const void *s2));
12567static int
12568#ifdef __BORLANDC__
12569 _RTLENTRYF
12570#endif
12571 item_compare2 __ARGS((const void *s1, const void *s2));
12572
12573static int item_compare_ic;
12574static char_u *item_compare_func;
12575#define ITEM_COMPARE_FAIL 999
12576
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580 static int
12581#ifdef __BORLANDC__
12582_RTLENTRYF
12583#endif
12584item_compare(s1, s2)
12585 const void *s1;
12586 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012588 char_u *p1, *p2;
12589 char_u *tofree1, *tofree2;
12590 int res;
12591 char_u numbuf1[NUMBUFLEN];
12592 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593
Bram Moolenaar33570922005-01-25 22:26:29 +000012594 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12595 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012596 if (item_compare_ic)
12597 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012599 res = STRCMP(p1, p2);
12600 vim_free(tofree1);
12601 vim_free(tofree2);
12602 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603}
12604
12605 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606#ifdef __BORLANDC__
12607_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609item_compare2(s1, s2)
12610 const void *s1;
12611 const void *s2;
12612{
12613 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012614 typval_T rettv;
12615 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012618 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12619 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12621 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012622
12623 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12624 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012625 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012626 clear_tv(&argv[0]);
12627 clear_tv(&argv[1]);
12628
12629 if (res == FAIL)
12630 res = ITEM_COMPARE_FAIL;
12631 else
12632 res = get_tv_number(&rettv);
12633 clear_tv(&rettv);
12634 return res;
12635}
12636
12637/*
12638 * "sort({list})" function
12639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012642 typval_T *argvars;
12643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644{
Bram Moolenaar33570922005-01-25 22:26:29 +000012645 list_T *l;
12646 listitem_T *li;
12647 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012648 long len;
12649 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650
Bram Moolenaar0d660222005-01-07 21:51:51 +000012651 rettv->vval.v_number = 0;
12652 if (argvars[0].v_type != VAR_LIST)
12653 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 else
12655 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012657 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658 return;
12659 rettv->vval.v_list = l;
12660 rettv->v_type = VAR_LIST;
12661 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663 len = list_len(l);
12664 if (len <= 1)
12665 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 item_compare_ic = FALSE;
12668 item_compare_func = NULL;
12669 if (argvars[1].v_type != VAR_UNKNOWN)
12670 {
12671 if (argvars[1].v_type == VAR_FUNC)
12672 item_compare_func = argvars[0].vval.v_string;
12673 else
12674 {
12675 i = get_tv_number(&argvars[1]);
12676 if (i == 1)
12677 item_compare_ic = TRUE;
12678 else
12679 item_compare_func = get_tv_string(&argvars[1]);
12680 }
12681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682
Bram Moolenaar0d660222005-01-07 21:51:51 +000012683 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012685 if (ptrs == NULL)
12686 return;
12687 i = 0;
12688 for (li = l->lv_first; li != NULL; li = li->li_next)
12689 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690
Bram Moolenaar0d660222005-01-07 21:51:51 +000012691 /* test the compare function */
12692 if (item_compare_func != NULL
12693 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12694 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012695 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012697 {
12698 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012700 item_compare_func == NULL ? item_compare : item_compare2);
12701
12702 /* Clear the List and append the items in the sorted order. */
12703 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012704 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012705 for (i = 0; i < len; ++i)
12706 list_append(l, ptrs[i]);
12707 }
12708
12709 vim_free(ptrs);
12710 }
12711}
12712
12713 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012714f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 typval_T *argvars;
12716 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012717{
12718 char_u *str;
12719 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012720 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 regmatch_T regmatch;
12722 char_u patbuf[NUMBUFLEN];
12723 char_u *save_cpo;
12724 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 listitem_T *ni;
12726 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012727 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012728 int keepempty = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012729
12730 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12731 save_cpo = p_cpo;
12732 p_cpo = (char_u *)"";
12733
12734 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012735 if (argvars[1].v_type != VAR_UNKNOWN)
12736 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012737 pat = get_tv_string_buf(&argvars[1], patbuf);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012738 if (argvars[2].v_type != VAR_UNKNOWN)
12739 keepempty = get_tv_number(&argvars[2]);
12740 }
12741 if (pat == NULL || *pat == NUL)
12742 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000012743
12744 l = list_alloc();
12745 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012747 rettv->v_type = VAR_LIST;
12748 rettv->vval.v_list = l;
12749 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750
Bram Moolenaar0d660222005-01-07 21:51:51 +000012751 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12752 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012754 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012755 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012756 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012757 if (*str == NUL)
12758 match = FALSE; /* empty item at the end */
12759 else
12760 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761 if (match)
12762 end = regmatch.startp[0];
12763 else
12764 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012765 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
12766 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767 {
12768 ni = listitem_alloc();
12769 if (ni == NULL)
12770 break;
12771 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012772 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012773 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12774 list_append(l, ni);
12775 }
12776 if (!match)
12777 break;
12778 /* Advance to just after the match. */
12779 if (regmatch.endp[0] > str)
12780 col = 0;
12781 else
12782 {
12783 /* Don't get stuck at the same match. */
12784#ifdef FEAT_MBYTE
12785 col = mb_ptr2len_check(regmatch.endp[0]);
12786#else
12787 col = 1;
12788#endif
12789 }
12790 str = regmatch.endp[0];
12791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792
Bram Moolenaar0d660222005-01-07 21:51:51 +000012793 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795
Bram Moolenaar0d660222005-01-07 21:51:51 +000012796 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797}
12798
12799#ifdef HAVE_STRFTIME
12800/*
12801 * "strftime({format}[, {time}])" function
12802 */
12803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 typval_T *argvars;
12806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807{
12808 char_u result_buf[256];
12809 struct tm *curtime;
12810 time_t seconds;
12811 char_u *p;
12812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012816 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 seconds = time(NULL);
12818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 curtime = localtime(&seconds);
12821 /* MSVC returns NULL for an invalid value of seconds. */
12822 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 else
12825 {
12826# ifdef FEAT_MBYTE
12827 vimconv_T conv;
12828 char_u *enc;
12829
12830 conv.vc_type = CONV_NONE;
12831 enc = enc_locale();
12832 convert_setup(&conv, p_enc, enc);
12833 if (conv.vc_type != CONV_NONE)
12834 p = string_convert(&conv, p, NULL);
12835# endif
12836 if (p != NULL)
12837 (void)strftime((char *)result_buf, sizeof(result_buf),
12838 (char *)p, curtime);
12839 else
12840 result_buf[0] = NUL;
12841
12842# ifdef FEAT_MBYTE
12843 if (conv.vc_type != CONV_NONE)
12844 vim_free(p);
12845 convert_setup(&conv, enc, p_enc);
12846 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 else
12849# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851
12852# ifdef FEAT_MBYTE
12853 /* Release conversion descriptors */
12854 convert_setup(&conv, NULL, NULL);
12855 vim_free(enc);
12856# endif
12857 }
12858}
12859#endif
12860
12861/*
12862 * "stridx()" function
12863 */
12864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 typval_T *argvars;
12867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868{
12869 char_u buf[NUMBUFLEN];
12870 char_u *needle;
12871 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012872 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 if (argvars[2].v_type != VAR_UNKNOWN)
12881 {
12882 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012883 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012884 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012885 if (start_idx >= 0)
12886 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 }
12888
12889 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12890 if (pos != NULL)
12891 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892}
12893
12894/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012895 * "string()" function
12896 */
12897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012899 typval_T *argvars;
12900 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012901{
12902 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012903 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012906 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012907 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909}
12910
12911/*
12912 * "strlen()" function
12913 */
12914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->vval.v_number = (varnumber_T)(STRLEN(
12920 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921}
12922
12923/*
12924 * "strpart()" function
12925 */
12926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012928 typval_T *argvars;
12929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930{
12931 char_u *p;
12932 int n;
12933 int len;
12934 int slen;
12935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 slen = (int)STRLEN(p);
12938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942 else
12943 len = slen - n; /* default len: all bytes that are available. */
12944
12945 /*
12946 * Only return the overlap between the specified part and the actual
12947 * string.
12948 */
12949 if (n < 0)
12950 {
12951 len += n;
12952 n = 0;
12953 }
12954 else if (n > slen)
12955 n = slen;
12956 if (len < 0)
12957 len = 0;
12958 else if (n + len > slen)
12959 len = slen - n;
12960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 rettv->v_type = VAR_STRING;
12962 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963}
12964
12965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 * "strridx()" function
12967 */
12968 static void
12969f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012970 typval_T *argvars;
12971 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012972{
12973 char_u buf[NUMBUFLEN];
12974 char_u *needle;
12975 char_u *haystack;
12976 char_u *rest;
12977 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012978 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979
12980 needle = get_tv_string(&argvars[1]);
12981 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012982 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012983 if (argvars[2].v_type != VAR_UNKNOWN)
12984 {
12985 /* Third argument: upper limit for index */
12986 end_idx = get_tv_number(&argvars[2]);
12987 if (end_idx < 0)
12988 {
12989 /* can never find a match */
12990 rettv->vval.v_number = -1;
12991 return;
12992 }
12993 }
12994 else
12995 end_idx = haystack_len;
12996
Bram Moolenaar0d660222005-01-07 21:51:51 +000012997 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012998 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012999 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013000 lastmatch = haystack + end_idx;
13001 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013003 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013004 for (rest = haystack; *rest != '\0'; ++rest)
13005 {
13006 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013007 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008 break;
13009 lastmatch = rest;
13010 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013011 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013012
13013 if (lastmatch == NULL)
13014 rettv->vval.v_number = -1;
13015 else
13016 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13017}
13018
13019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 * "strtrans()" function
13021 */
13022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 typval_T *argvars;
13025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013027 rettv->v_type = VAR_STRING;
13028 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029}
13030
13031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032 * "submatch()" function
13033 */
13034 static void
13035f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013036 typval_T *argvars;
13037 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013038{
13039 rettv->v_type = VAR_STRING;
13040 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
13041}
13042
13043/*
13044 * "substitute()" function
13045 */
13046 static void
13047f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *argvars;
13049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013050{
13051 char_u patbuf[NUMBUFLEN];
13052 char_u subbuf[NUMBUFLEN];
13053 char_u flagsbuf[NUMBUFLEN];
13054
13055 rettv->v_type = VAR_STRING;
13056 rettv->vval.v_string = do_string_sub(
13057 get_tv_string(&argvars[0]),
13058 get_tv_string_buf(&argvars[1], patbuf),
13059 get_tv_string_buf(&argvars[2], subbuf),
13060 get_tv_string_buf(&argvars[3], flagsbuf));
13061}
13062
13063/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013064 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 */
13066/*ARGSUSED*/
13067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013069 typval_T *argvars;
13070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071{
13072 int id = 0;
13073#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013074 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 long col;
13076 int trans;
13077
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013078 lnum = get_tv_lnum(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 col = get_tv_number(&argvars[1]) - 1;
13080 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13083 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13084 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085#endif
13086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013087 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088}
13089
13090/*
13091 * "synIDattr(id, what [, mode])" function
13092 */
13093/*ARGSUSED*/
13094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013096 typval_T *argvars;
13097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098{
13099 char_u *p = NULL;
13100#ifdef FEAT_SYN_HL
13101 int id;
13102 char_u *what;
13103 char_u *mode;
13104 char_u modebuf[NUMBUFLEN];
13105 int modec;
13106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 id = get_tv_number(&argvars[0]);
13108 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 modec = TOLOWER_ASC(mode[0]);
13113 if (modec != 't' && modec != 'c'
13114#ifdef FEAT_GUI
13115 && modec != 'g'
13116#endif
13117 )
13118 modec = 0; /* replace invalid with current */
13119 }
13120 else
13121 {
13122#ifdef FEAT_GUI
13123 if (gui.in_use)
13124 modec = 'g';
13125 else
13126#endif
13127 if (t_colors > 1)
13128 modec = 'c';
13129 else
13130 modec = 't';
13131 }
13132
13133
13134 switch (TOLOWER_ASC(what[0]))
13135 {
13136 case 'b':
13137 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13138 p = highlight_color(id, what, modec);
13139 else /* bold */
13140 p = highlight_has_attr(id, HL_BOLD, modec);
13141 break;
13142
13143 case 'f': /* fg[#] */
13144 p = highlight_color(id, what, modec);
13145 break;
13146
13147 case 'i':
13148 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13149 p = highlight_has_attr(id, HL_INVERSE, modec);
13150 else /* italic */
13151 p = highlight_has_attr(id, HL_ITALIC, modec);
13152 break;
13153
13154 case 'n': /* name */
13155 p = get_highlight_name(NULL, id - 1);
13156 break;
13157
13158 case 'r': /* reverse */
13159 p = highlight_has_attr(id, HL_INVERSE, modec);
13160 break;
13161
13162 case 's': /* standout */
13163 p = highlight_has_attr(id, HL_STANDOUT, modec);
13164 break;
13165
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013166 case 'u':
13167 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13168 /* underline */
13169 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13170 else
13171 /* undercurl */
13172 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 break;
13174 }
13175
13176 if (p != NULL)
13177 p = vim_strsave(p);
13178#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179 rettv->v_type = VAR_STRING;
13180 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181}
13182
13183/*
13184 * "synIDtrans(id)" function
13185 */
13186/*ARGSUSED*/
13187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013189 typval_T *argvars;
13190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191{
13192 int id;
13193
13194#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196
13197 if (id > 0)
13198 id = syn_get_final_id(id);
13199 else
13200#endif
13201 id = 0;
13202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204}
13205
13206/*
13207 * "system()" function
13208 */
13209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013211 typval_T *argvars;
13212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013214 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013216 char_u *infile = NULL;
13217 char_u buf[NUMBUFLEN];
13218 int err = FALSE;
13219 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013221 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013222 {
13223 /*
13224 * Write the string to a temp file, to be used for input of the shell
13225 * command.
13226 */
13227 if ((infile = vim_tempname('i')) == NULL)
13228 {
13229 EMSG(_(e_notmp));
13230 return;
13231 }
13232
13233 fd = mch_fopen((char *)infile, WRITEBIN);
13234 if (fd == NULL)
13235 {
13236 EMSG2(_(e_notopen), infile);
13237 goto done;
13238 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013240 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13241 err = TRUE;
13242 if (fclose(fd) != 0)
13243 err = TRUE;
13244 if (err)
13245 {
13246 EMSG(_("E677: Error writing temp file"));
13247 goto done;
13248 }
13249 }
13250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013252
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253#ifdef USE_CR
13254 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013255 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 {
13257 char_u *s;
13258
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013259 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 {
13261 if (*s == CAR)
13262 *s = NL;
13263 }
13264 }
13265#else
13266# ifdef USE_CRNL
13267 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013268 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 {
13270 char_u *s, *d;
13271
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013272 d = res;
13273 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 {
13275 if (s[0] == CAR && s[1] == NL)
13276 ++s;
13277 *d++ = *s;
13278 }
13279 *d = NUL;
13280 }
13281# endif
13282#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013283
13284done:
13285 if (infile != NULL)
13286 {
13287 mch_remove(infile);
13288 vim_free(infile);
13289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290 rettv->v_type = VAR_STRING;
13291 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292}
13293
13294/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013295 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013296 */
13297 static void
13298f_taglist(argvars, rettv)
13299 typval_T *argvars;
13300 typval_T *rettv;
13301{
13302 char_u *tag_pattern;
13303 list_T *l;
13304
13305 tag_pattern = get_tv_string(&argvars[0]);
13306
13307 rettv->vval.v_number = FALSE;
13308
13309 l = list_alloc();
13310 if (l != NULL)
13311 {
13312 if (get_tags(l, tag_pattern) != FAIL)
13313 {
13314 rettv->vval.v_list = l;
13315 rettv->v_type = VAR_LIST;
13316 ++l->lv_refcount;
13317 }
13318 else
13319 list_free(l);
13320 }
13321}
13322
13323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 * "tempname()" function
13325 */
13326/*ARGSUSED*/
13327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 typval_T *argvars;
13330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
13332 static int x = 'A';
13333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->v_type = VAR_STRING;
13335 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336
13337 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13338 * names. Skip 'I' and 'O', they are used for shell redirection. */
13339 do
13340 {
13341 if (x == 'Z')
13342 x = '0';
13343 else if (x == '9')
13344 x = 'A';
13345 else
13346 {
13347#ifdef EBCDIC
13348 if (x == 'I')
13349 x = 'J';
13350 else if (x == 'R')
13351 x = 'S';
13352 else
13353#endif
13354 ++x;
13355 }
13356 } while (x == 'I' || x == 'O');
13357}
13358
13359/*
13360 * "tolower(string)" function
13361 */
13362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 typval_T *argvars;
13365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366{
13367 char_u *p;
13368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369 p = vim_strsave(get_tv_string(&argvars[0]));
13370 rettv->v_type = VAR_STRING;
13371 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372
13373 if (p != NULL)
13374 while (*p != NUL)
13375 {
13376#ifdef FEAT_MBYTE
13377 int l;
13378
13379 if (enc_utf8)
13380 {
13381 int c, lc;
13382
13383 c = utf_ptr2char(p);
13384 lc = utf_tolower(c);
13385 l = utf_ptr2len_check(p);
13386 /* TODO: reallocate string when byte count changes. */
13387 if (utf_char2len(lc) == l)
13388 utf_char2bytes(lc, p);
13389 p += l;
13390 }
13391 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13392 p += l; /* skip multi-byte character */
13393 else
13394#endif
13395 {
13396 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13397 ++p;
13398 }
13399 }
13400}
13401
13402/*
13403 * "toupper(string)" function
13404 */
13405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 typval_T *argvars;
13408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409{
13410 char_u *p;
13411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412 p = vim_strsave(get_tv_string(&argvars[0]));
13413 rettv->v_type = VAR_STRING;
13414 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415
13416 if (p != NULL)
13417 while (*p != NUL)
13418 {
13419#ifdef FEAT_MBYTE
13420 int l;
13421
13422 if (enc_utf8)
13423 {
13424 int c, uc;
13425
13426 c = utf_ptr2char(p);
13427 uc = utf_toupper(c);
13428 l = utf_ptr2len_check(p);
13429 /* TODO: reallocate string when byte count changes. */
13430 if (utf_char2len(uc) == l)
13431 utf_char2bytes(uc, p);
13432 p += l;
13433 }
13434 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13435 p += l; /* skip multi-byte character */
13436 else
13437#endif
13438 {
13439 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13440 p++;
13441 }
13442 }
13443}
13444
13445/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013446 * "tr(string, fromstr, tostr)" function
13447 */
13448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013452{
13453 char_u *instr;
13454 char_u *fromstr;
13455 char_u *tostr;
13456 char_u *p;
13457#ifdef FEAT_MBYTE
13458 int inlen;
13459 int fromlen;
13460 int tolen;
13461 int idx;
13462 char_u *cpstr;
13463 int cplen;
13464 int first = TRUE;
13465#endif
13466 char_u buf[NUMBUFLEN];
13467 char_u buf2[NUMBUFLEN];
13468 garray_T ga;
13469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470 instr = get_tv_string(&argvars[0]);
13471 fromstr = get_tv_string_buf(&argvars[1], buf);
13472 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013473
13474 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 rettv->v_type = VAR_STRING;
13476 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013477 ga_init2(&ga, (int)sizeof(char), 80);
13478
13479#ifdef FEAT_MBYTE
13480 if (!has_mbyte)
13481#endif
13482 /* not multi-byte: fromstr and tostr must be the same length */
13483 if (STRLEN(fromstr) != STRLEN(tostr))
13484 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013485#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013486error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013487#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013488 EMSG2(_(e_invarg2), fromstr);
13489 ga_clear(&ga);
13490 return;
13491 }
13492
13493 /* fromstr and tostr have to contain the same number of chars */
13494 while (*instr != NUL)
13495 {
13496#ifdef FEAT_MBYTE
13497 if (has_mbyte)
13498 {
13499 inlen = mb_ptr2len_check(instr);
13500 cpstr = instr;
13501 cplen = inlen;
13502 idx = 0;
13503 for (p = fromstr; *p != NUL; p += fromlen)
13504 {
13505 fromlen = mb_ptr2len_check(p);
13506 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13507 {
13508 for (p = tostr; *p != NUL; p += tolen)
13509 {
13510 tolen = mb_ptr2len_check(p);
13511 if (idx-- == 0)
13512 {
13513 cplen = tolen;
13514 cpstr = p;
13515 break;
13516 }
13517 }
13518 if (*p == NUL) /* tostr is shorter than fromstr */
13519 goto error;
13520 break;
13521 }
13522 ++idx;
13523 }
13524
13525 if (first && cpstr == instr)
13526 {
13527 /* Check that fromstr and tostr have the same number of
13528 * (multi-byte) characters. Done only once when a character
13529 * of instr doesn't appear in fromstr. */
13530 first = FALSE;
13531 for (p = tostr; *p != NUL; p += tolen)
13532 {
13533 tolen = mb_ptr2len_check(p);
13534 --idx;
13535 }
13536 if (idx != 0)
13537 goto error;
13538 }
13539
13540 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013541 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013542 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013543
13544 instr += inlen;
13545 }
13546 else
13547#endif
13548 {
13549 /* When not using multi-byte chars we can do it faster. */
13550 p = vim_strchr(fromstr, *instr);
13551 if (p != NULL)
13552 ga_append(&ga, tostr[p - fromstr]);
13553 else
13554 ga_append(&ga, *instr);
13555 ++instr;
13556 }
13557 }
13558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013560}
13561
13562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 * "type(expr)" function
13564 */
13565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013570 int n;
13571
13572 switch (argvars[0].v_type)
13573 {
13574 case VAR_NUMBER: n = 0; break;
13575 case VAR_STRING: n = 1; break;
13576 case VAR_FUNC: n = 2; break;
13577 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013578 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013579 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13580 }
13581 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582}
13583
13584/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013585 * "values(dict)" function
13586 */
13587 static void
13588f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013589 typval_T *argvars;
13590 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013591{
13592 dict_list(argvars, rettv, 1);
13593}
13594
13595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 * "virtcol(string)" function
13597 */
13598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013600 typval_T *argvars;
13601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602{
13603 colnr_T vcol = 0;
13604 pos_T *fp;
13605
13606 fp = var2fpos(&argvars[0], FALSE);
13607 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13608 {
13609 getvvcol(curwin, fp, NULL, NULL, &vcol);
13610 ++vcol;
13611 }
13612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614}
13615
13616/*
13617 * "visualmode()" function
13618 */
13619/*ARGSUSED*/
13620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *argvars;
13623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
13625#ifdef FEAT_VISUAL
13626 char_u str[2];
13627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 str[0] = curbuf->b_visual_mode_eval;
13630 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632
13633 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013634 if ((argvars[0].v_type == VAR_NUMBER
13635 && argvars[0].vval.v_number != 0)
13636 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 curbuf->b_visual_mode_eval = NUL;
13639#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#endif
13642}
13643
13644/*
13645 * "winbufnr(nr)" function
13646 */
13647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651{
13652 win_T *wp;
13653
13654 wp = find_win_by_nr(&argvars[0]);
13655 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013656 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659}
13660
13661/*
13662 * "wincol()" function
13663 */
13664/*ARGSUSED*/
13665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 typval_T *argvars;
13668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669{
13670 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672}
13673
13674/*
13675 * "winheight(nr)" function
13676 */
13677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 typval_T *argvars;
13680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
13682 win_T *wp;
13683
13684 wp = find_win_by_nr(&argvars[0]);
13685 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689}
13690
13691/*
13692 * "winline()" function
13693 */
13694/*ARGSUSED*/
13695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013696f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013697 typval_T *argvars;
13698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
13700 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
13704/*
13705 * "winnr()" function
13706 */
13707/* ARGSUSED */
13708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *argvars;
13711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712{
13713 int nr = 1;
13714#ifdef FEAT_WINDOWS
13715 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013716 win_T *twin = curwin;
13717 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013719 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013722 if (STRCMP(arg, "$") == 0)
13723 twin = lastwin;
13724 else if (STRCMP(arg, "#") == 0)
13725 {
13726 twin = prevwin;
13727 if (prevwin == NULL)
13728 nr = 0;
13729 }
13730 else
13731 {
13732 EMSG2(_(e_invexpr2), arg);
13733 nr = 0;
13734 }
13735 }
13736
13737 if (nr > 0)
13738 for (wp = firstwin; wp != twin; wp = wp->w_next)
13739 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742}
13743
13744/*
13745 * "winrestcmd()" function
13746 */
13747/* ARGSUSED */
13748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 typval_T *argvars;
13751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752{
13753#ifdef FEAT_WINDOWS
13754 win_T *wp;
13755 int winnr = 1;
13756 garray_T ga;
13757 char_u buf[50];
13758
13759 ga_init2(&ga, (int)sizeof(char), 70);
13760 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13761 {
13762 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13763 ga_concat(&ga, buf);
13764# ifdef FEAT_VERTSPLIT
13765 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13766 ga_concat(&ga, buf);
13767# endif
13768 ++winnr;
13769 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013770 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013772 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777}
13778
13779/*
13780 * "winwidth(nr)" function
13781 */
13782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *argvars;
13785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786{
13787 win_T *wp;
13788
13789 wp = find_win_by_nr(&argvars[0]);
13790 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 else
13793#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797#endif
13798}
13799
13800 static win_T *
13801find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013802 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803{
13804#ifdef FEAT_WINDOWS
13805 win_T *wp;
13806#endif
13807 int nr;
13808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013809 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810
13811#ifdef FEAT_WINDOWS
13812 if (nr == 0)
13813 return curwin;
13814
13815 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13816 if (--nr <= 0)
13817 break;
13818 return wp;
13819#else
13820 if (nr == 0 || nr == 1)
13821 return curwin;
13822 return NULL;
13823#endif
13824}
13825
13826/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013827 * "writefile()" function
13828 */
13829 static void
13830f_writefile(argvars, rettv)
13831 typval_T *argvars;
13832 typval_T *rettv;
13833{
13834 int binary = FALSE;
13835 char_u *fname;
13836 FILE *fd;
13837 listitem_T *li;
13838 char_u *s;
13839 int ret = 0;
13840 int c;
13841
13842 if (argvars[0].v_type != VAR_LIST)
13843 {
13844 EMSG2(_(e_listarg), "writefile()");
13845 return;
13846 }
13847 if (argvars[0].vval.v_list == NULL)
13848 return;
13849
13850 if (argvars[2].v_type != VAR_UNKNOWN
13851 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13852 binary = TRUE;
13853
13854 /* Always open the file in binary mode, library functions have a mind of
13855 * their own about CR-LF conversion. */
13856 fname = get_tv_string(&argvars[1]);
13857 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13858 {
13859 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13860 ret = -1;
13861 }
13862 else
13863 {
13864 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13865 li = li->li_next)
13866 {
13867 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13868 {
13869 if (*s == '\n')
13870 c = putc(NUL, fd);
13871 else
13872 c = putc(*s, fd);
13873 if (c == EOF)
13874 {
13875 ret = -1;
13876 break;
13877 }
13878 }
13879 if (!binary || li->li_next != NULL)
13880 if (putc('\n', fd) == EOF)
13881 {
13882 ret = -1;
13883 break;
13884 }
13885 if (ret < 0)
13886 {
13887 EMSG(_(e_write));
13888 break;
13889 }
13890 }
13891 fclose(fd);
13892 }
13893
13894 rettv->vval.v_number = ret;
13895}
13896
13897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 * Translate a String variable into a position.
13899 */
13900 static pos_T *
13901var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013902 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 int lnum; /* TRUE when $ is last line */
13904{
13905 char_u *name;
13906 static pos_T pos;
13907 pos_T *pp;
13908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 if (name[0] == '.') /* cursor */
13911 return &curwin->w_cursor;
13912 if (name[0] == '\'') /* mark */
13913 {
13914 pp = getmark(name[1], FALSE);
13915 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13916 return NULL;
13917 return pp;
13918 }
13919 if (name[0] == '$') /* last column or line */
13920 {
13921 if (lnum)
13922 {
13923 pos.lnum = curbuf->b_ml.ml_line_count;
13924 pos.col = 0;
13925 }
13926 else
13927 {
13928 pos.lnum = curwin->w_cursor.lnum;
13929 pos.col = (colnr_T)STRLEN(ml_get_curline());
13930 }
13931 return &pos;
13932 }
13933 return NULL;
13934}
13935
13936/*
13937 * Get the length of an environment variable name.
13938 * Advance "arg" to the first character after the name.
13939 * Return 0 for error.
13940 */
13941 static int
13942get_env_len(arg)
13943 char_u **arg;
13944{
13945 char_u *p;
13946 int len;
13947
13948 for (p = *arg; vim_isIDc(*p); ++p)
13949 ;
13950 if (p == *arg) /* no name found */
13951 return 0;
13952
13953 len = (int)(p - *arg);
13954 *arg = p;
13955 return len;
13956}
13957
13958/*
13959 * Get the length of the name of a function or internal variable.
13960 * "arg" is advanced to the first non-white character after the name.
13961 * Return 0 if something is wrong.
13962 */
13963 static int
13964get_id_len(arg)
13965 char_u **arg;
13966{
13967 char_u *p;
13968 int len;
13969
13970 /* Find the end of the name. */
13971 for (p = *arg; eval_isnamec(*p); ++p)
13972 ;
13973 if (p == *arg) /* no name found */
13974 return 0;
13975
13976 len = (int)(p - *arg);
13977 *arg = skipwhite(p);
13978
13979 return len;
13980}
13981
13982/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013983 * Get the length of the name of a variable or function.
13984 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013986 * Return -1 if curly braces expansion failed.
13987 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 * If the name contains 'magic' {}'s, expand them and return the
13989 * expanded name in an allocated string via 'alias' - caller must free.
13990 */
13991 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013992get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 char_u **arg;
13994 char_u **alias;
13995 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013996 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997{
13998 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 char_u *p;
14000 char_u *expr_start;
14001 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002
14003 *alias = NULL; /* default to no alias */
14004
14005 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14006 && (*arg)[2] == (int)KE_SNR)
14007 {
14008 /* hard coded <SNR>, already translated */
14009 *arg += 3;
14010 return get_id_len(arg) + 3;
14011 }
14012 len = eval_fname_script(*arg);
14013 if (len > 0)
14014 {
14015 /* literal "<SID>", "s:" or "<SNR>" */
14016 *arg += len;
14017 }
14018
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014022 p = find_name_end(*arg, &expr_start, &expr_end,
14023 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 if (expr_start != NULL)
14025 {
14026 char_u *temp_string;
14027
14028 if (!evaluate)
14029 {
14030 len += (int)(p - *arg);
14031 *arg = skipwhite(p);
14032 return len;
14033 }
14034
14035 /*
14036 * Include any <SID> etc in the expanded string:
14037 * Thus the -len here.
14038 */
14039 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14040 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014041 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 *alias = temp_string;
14043 *arg = skipwhite(p);
14044 return (int)STRLEN(temp_string);
14045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046
14047 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014048 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 EMSG2(_(e_invexpr2), *arg);
14050
14051 return len;
14052}
14053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054/*
14055 * Find the end of a variable or function name, taking care of magic braces.
14056 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14057 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014058 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059 * Return a pointer to just after the name. Equal to "arg" if there is no
14060 * valid name.
14061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014063find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 char_u *arg;
14065 char_u **expr_start;
14066 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014067 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 int mb_nest = 0;
14070 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 char_u *p;
14072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014073 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 *expr_start = NULL;
14076 *expr_end = NULL;
14077 }
14078
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014079 /* Quick check for valid starting character. */
14080 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14081 return arg;
14082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 for (p = arg; *p != NUL
14084 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014085 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014086 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 || mb_nest != 0
14088 || br_nest != 0); ++p)
14089 {
14090 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 if (*p == '[')
14093 ++br_nest;
14094 else if (*p == ']')
14095 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 if (*p == '{')
14100 {
14101 mb_nest++;
14102 if (expr_start != NULL && *expr_start == NULL)
14103 *expr_start = p;
14104 }
14105 else if (*p == '}')
14106 {
14107 mb_nest--;
14108 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14109 *expr_end = p;
14110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 }
14113
14114 return p;
14115}
14116
14117/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014118 * Expands out the 'magic' {}'s in a variable/function name.
14119 * Note that this can call itself recursively, to deal with
14120 * constructs like foo{bar}{baz}{bam}
14121 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14122 * "in_start" ^
14123 * "expr_start" ^
14124 * "expr_end" ^
14125 * "in_end" ^
14126 *
14127 * Returns a new allocated string, which the caller must free.
14128 * Returns NULL for failure.
14129 */
14130 static char_u *
14131make_expanded_name(in_start, expr_start, expr_end, in_end)
14132 char_u *in_start;
14133 char_u *expr_start;
14134 char_u *expr_end;
14135 char_u *in_end;
14136{
14137 char_u c1;
14138 char_u *retval = NULL;
14139 char_u *temp_result;
14140 char_u *nextcmd = NULL;
14141
14142 if (expr_end == NULL || in_end == NULL)
14143 return NULL;
14144 *expr_start = NUL;
14145 *expr_end = NUL;
14146 c1 = *in_end;
14147 *in_end = NUL;
14148
14149 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14150 if (temp_result != NULL && nextcmd == NULL)
14151 {
14152 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14153 + (in_end - expr_end) + 1));
14154 if (retval != NULL)
14155 {
14156 STRCPY(retval, in_start);
14157 STRCAT(retval, temp_result);
14158 STRCAT(retval, expr_end + 1);
14159 }
14160 }
14161 vim_free(temp_result);
14162
14163 *in_end = c1; /* put char back for error messages */
14164 *expr_start = '{';
14165 *expr_end = '}';
14166
14167 if (retval != NULL)
14168 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014169 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014170 if (expr_start != NULL)
14171 {
14172 /* Further expansion! */
14173 temp_result = make_expanded_name(retval, expr_start,
14174 expr_end, temp_result);
14175 vim_free(retval);
14176 retval = temp_result;
14177 }
14178 }
14179
14180 return retval;
14181}
14182
14183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014185 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 */
14187 static int
14188eval_isnamec(c)
14189 int c;
14190{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014191 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14192}
14193
14194/*
14195 * Return TRUE if character "c" can be used as the first character in a
14196 * variable or function name (excluding '{' and '}').
14197 */
14198 static int
14199eval_isnamec1(c)
14200 int c;
14201{
14202 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203}
14204
14205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 * Set number v: variable to "val".
14207 */
14208 void
14209set_vim_var_nr(idx, val)
14210 int idx;
14211 long val;
14212{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014213 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214}
14215
14216/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014217 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 */
14219 long
14220get_vim_var_nr(idx)
14221 int idx;
14222{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014223 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014226#if defined(FEAT_AUTOCMD) || defined(PROTO)
14227/*
14228 * Get string v: variable value. Uses a static buffer, can only be used once.
14229 */
14230 char_u *
14231get_vim_var_str(idx)
14232 int idx;
14233{
14234 return get_tv_string(&vimvars[idx].vv_tv);
14235}
14236#endif
14237
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238/*
14239 * Set v:count, v:count1 and v:prevcount.
14240 */
14241 void
14242set_vcount(count, count1)
14243 long count;
14244 long count1;
14245{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014246 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14247 vimvars[VV_COUNT].vv_nr = count;
14248 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249}
14250
14251/*
14252 * Set string v: variable to a copy of "val".
14253 */
14254 void
14255set_vim_var_string(idx, val, len)
14256 int idx;
14257 char_u *val;
14258 int len; /* length of "val" to use or -1 (whole string) */
14259{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014260 /* Need to do this (at least) once, since we can't initialize a union.
14261 * Will always be invoked when "v:progname" is set. */
14262 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14263
Bram Moolenaare9a41262005-01-15 22:18:47 +000014264 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014266 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014268 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014270 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271}
14272
14273/*
14274 * Set v:register if needed.
14275 */
14276 void
14277set_reg_var(c)
14278 int c;
14279{
14280 char_u regname;
14281
14282 if (c == 0 || c == ' ')
14283 regname = '"';
14284 else
14285 regname = c;
14286 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014287 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 set_vim_var_string(VV_REG, &regname, 1);
14289}
14290
14291/*
14292 * Get or set v:exception. If "oldval" == NULL, return the current value.
14293 * Otherwise, restore the value to "oldval" and return NULL.
14294 * Must always be called in pairs to save and restore v:exception! Does not
14295 * take care of memory allocations.
14296 */
14297 char_u *
14298v_exception(oldval)
14299 char_u *oldval;
14300{
14301 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014302 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
Bram Moolenaare9a41262005-01-15 22:18:47 +000014304 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 return NULL;
14306}
14307
14308/*
14309 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14310 * Otherwise, restore the value to "oldval" and return NULL.
14311 * Must always be called in pairs to save and restore v:throwpoint! Does not
14312 * take care of memory allocations.
14313 */
14314 char_u *
14315v_throwpoint(oldval)
14316 char_u *oldval;
14317{
14318 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014319 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320
Bram Moolenaare9a41262005-01-15 22:18:47 +000014321 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 return NULL;
14323}
14324
14325#if defined(FEAT_AUTOCMD) || defined(PROTO)
14326/*
14327 * Set v:cmdarg.
14328 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14329 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14330 * Must always be called in pairs!
14331 */
14332 char_u *
14333set_cmdarg(eap, oldarg)
14334 exarg_T *eap;
14335 char_u *oldarg;
14336{
14337 char_u *oldval;
14338 char_u *newval;
14339 unsigned len;
14340
Bram Moolenaare9a41262005-01-15 22:18:47 +000014341 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014342 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014344 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014345 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014346 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347 }
14348
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014349 if (eap->force_bin == FORCE_BIN)
14350 len = 6;
14351 else if (eap->force_bin == FORCE_NOBIN)
14352 len = 8;
14353 else
14354 len = 0;
14355 if (eap->force_ff != 0)
14356 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14357# ifdef FEAT_MBYTE
14358 if (eap->force_enc != 0)
14359 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14360# endif
14361
14362 newval = alloc(len + 1);
14363 if (newval == NULL)
14364 return NULL;
14365
14366 if (eap->force_bin == FORCE_BIN)
14367 sprintf((char *)newval, " ++bin");
14368 else if (eap->force_bin == FORCE_NOBIN)
14369 sprintf((char *)newval, " ++nobin");
14370 else
14371 *newval = NUL;
14372 if (eap->force_ff != 0)
14373 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14374 eap->cmd + eap->force_ff);
14375# ifdef FEAT_MBYTE
14376 if (eap->force_enc != 0)
14377 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14378 eap->cmd + eap->force_enc);
14379# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014380 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014381 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382}
14383#endif
14384
14385/*
14386 * Get the value of internal variable "name".
14387 * Return OK or FAIL.
14388 */
14389 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014390get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 char_u *name;
14392 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014394 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395{
14396 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *tv = NULL;
14398 typval_T atv;
14399 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
14402 /* truncate the name, so that we can use strcmp() */
14403 cc = name[len];
14404 name[len] = NUL;
14405
14406 /*
14407 * Check for "b:changedtick".
14408 */
14409 if (STRCMP(name, "b:changedtick") == 0)
14410 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014411 atv.v_type = VAR_NUMBER;
14412 atv.vval.v_number = curbuf->b_changedtick;
14413 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 }
14415
14416 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 * Check for user-defined variables.
14418 */
14419 else
14420 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014421 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014423 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 }
14425
Bram Moolenaare9a41262005-01-15 22:18:47 +000014426 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014428 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 ret = FAIL;
14431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014433 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434
14435 name[len] = cc;
14436
14437 return ret;
14438}
14439
14440/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014441 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14442 * Also handle function call with Funcref variable: func(expr)
14443 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14444 */
14445 static int
14446handle_subscript(arg, rettv, evaluate, verbose)
14447 char_u **arg;
14448 typval_T *rettv;
14449 int evaluate; /* do more than finding the end */
14450 int verbose; /* give error messages */
14451{
14452 int ret = OK;
14453 dict_T *selfdict = NULL;
14454 char_u *s;
14455 int len;
14456
14457 while (ret == OK
14458 && (**arg == '['
14459 || (**arg == '.' && rettv->v_type == VAR_DICT)
14460 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14461 && !vim_iswhite(*(*arg - 1)))
14462 {
14463 if (**arg == '(')
14464 {
14465 s = rettv->vval.v_string;
14466
14467 /* Invoke the function. Recursive! */
14468 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14469 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14470 &len, evaluate, selfdict);
14471
14472 /* Stop the expression evaluation when immediately aborting on
14473 * error, or when an interrupt occurred or an exception was thrown
14474 * but not caught. */
14475 if (aborting())
14476 {
14477 if (ret == OK)
14478 clear_tv(rettv);
14479 ret = FAIL;
14480 }
14481 dict_unref(selfdict);
14482 selfdict = NULL;
14483 }
14484 else /* **arg == '[' || **arg == '.' */
14485 {
14486 dict_unref(selfdict);
14487 if (rettv->v_type == VAR_DICT)
14488 {
14489 selfdict = rettv->vval.v_dict;
14490 if (selfdict != NULL)
14491 ++selfdict->dv_refcount;
14492 }
14493 else
14494 selfdict = NULL;
14495 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14496 {
14497 clear_tv(rettv);
14498 ret = FAIL;
14499 }
14500 }
14501 }
14502 dict_unref(selfdict);
14503 return ret;
14504}
14505
14506/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014507 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14508 * value).
14509 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014512{
Bram Moolenaar33570922005-01-25 22:26:29 +000014513 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014514}
14515
14516/*
14517 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 * The string "s" must have been allocated, it is consumed.
14519 * Return NULL for out of memory, the variable otherwise.
14520 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 char_u *s;
14524{
Bram Moolenaar33570922005-01-25 22:26:29 +000014525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014527 rettv = alloc_tv();
14528 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 rettv->v_type = VAR_STRING;
14531 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 }
14533 else
14534 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536}
14537
14538/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014539 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 */
14541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544{
14545 if (varp != NULL)
14546 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014547 switch (varp->v_type)
14548 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014549 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014550 func_unref(varp->vval.v_string);
14551 /*FALLTHROUGH*/
14552 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014553 vim_free(varp->vval.v_string);
14554 break;
14555 case VAR_LIST:
14556 list_unref(varp->vval.v_list);
14557 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014558 case VAR_DICT:
14559 dict_unref(varp->vval.v_dict);
14560 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014561 case VAR_NUMBER:
14562 case VAR_UNKNOWN:
14563 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014564 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014565 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014566 break;
14567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 vim_free(varp);
14569 }
14570}
14571
14572/*
14573 * Free the memory for a variable value and set the value to NULL or 0.
14574 */
14575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578{
14579 if (varp != NULL)
14580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014581 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014583 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014584 func_unref(varp->vval.v_string);
14585 /*FALLTHROUGH*/
14586 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014587 vim_free(varp->vval.v_string);
14588 varp->vval.v_string = NULL;
14589 break;
14590 case VAR_LIST:
14591 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014592 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014593 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014594 case VAR_DICT:
14595 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014596 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014597 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014599 varp->vval.v_number = 0;
14600 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 case VAR_UNKNOWN:
14602 break;
14603 default:
14604 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014606 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607 }
14608}
14609
14610/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 * Set the value of a variable to NULL without freeing items.
14612 */
14613 static void
14614init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014615 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616{
14617 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014618 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014619}
14620
14621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 * Get the number value of a variable.
14623 * If it is a String variable, uses vim_str2nr().
14624 */
14625 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014629 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014633 case VAR_NUMBER:
14634 n = (long)(varp->vval.v_number);
14635 break;
14636 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014637 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014638 break;
14639 case VAR_STRING:
14640 if (varp->vval.v_string != NULL)
14641 vim_str2nr(varp->vval.v_string, NULL, NULL,
14642 TRUE, TRUE, &n, NULL);
14643 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014644 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014645 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014646 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014647 case VAR_DICT:
14648 EMSG(_("E728: Using a Dictionary as a number"));
14649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014650 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014651 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014654 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655}
14656
14657/*
14658 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14659 */
14660 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014662 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663{
Bram Moolenaar33570922005-01-25 22:26:29 +000014664 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 linenr_T lnum;
14666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 if (lnum == 0) /* no valid number, try using line() */
14669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 rettv.v_type = VAR_NUMBER;
14671 f_line(argvars, &rettv);
14672 lnum = rettv.vval.v_number;
14673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 }
14675 return lnum;
14676}
14677
14678/*
14679 * Get the string value of a variable.
14680 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014681 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14682 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 * If the String variable has never been set, return an empty string.
14684 * Never returns NULL;
14685 */
14686 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689{
14690 static char_u mybuf[NUMBUFLEN];
14691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693}
14694
14695 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014697 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014698 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014700 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014702 case VAR_NUMBER:
14703 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14704 return buf;
14705 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014706 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014707 break;
14708 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014709 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014710 break;
14711 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014712 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014713 break;
14714 case VAR_STRING:
14715 if (varp->vval.v_string != NULL)
14716 return varp->vval.v_string;
14717 break;
14718 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014720 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014722 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723}
14724
14725/*
14726 * Find variable "name" in the list of variables.
14727 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014728 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014729 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014730 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014732 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014733find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014735 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739
Bram Moolenaara7043832005-01-21 11:56:39 +000014740 ht = find_var_ht(name, &varname);
14741 if (htp != NULL)
14742 *htp = ht;
14743 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014745 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746}
14747
14748/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014749 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014750 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014753find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014755 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014756 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014757{
Bram Moolenaar33570922005-01-25 22:26:29 +000014758 hashitem_T *hi;
14759
14760 if (*varname == NUL)
14761 {
14762 /* Must be something like "s:", otherwise "ht" would be NULL. */
14763 switch (varname[-2])
14764 {
14765 case 's': return &SCRIPT_SV(current_SID).sv_var;
14766 case 'g': return &globvars_var;
14767 case 'v': return &vimvars_var;
14768 case 'b': return &curbuf->b_bufvar;
14769 case 'w': return &curwin->w_winvar;
14770 case 'l': return &current_funccal->l_vars_var;
14771 case 'a': return &current_funccal->l_avars_var;
14772 }
14773 return NULL;
14774 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014775
14776 hi = hash_find(ht, varname);
14777 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014778 {
14779 /* For global variables we may try auto-loading the script. If it
14780 * worked find the variable again. */
14781 if (ht == &globvarht && !writing
14782 && script_autoload(varname) && !aborting())
14783 hi = hash_find(ht, varname);
14784 if (HASHITEM_EMPTY(hi))
14785 return NULL;
14786 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014788}
14789
14790/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014792 * Set "varname" to the start of name without ':'.
14793 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014794 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014795find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 char_u *name;
14797 char_u **varname;
14798{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000014799 hashitem_T *hi;
14800
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 if (name[1] != ':')
14802 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014803 /* The name must not start with a colon or #. */
14804 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 return NULL;
14806 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014807
14808 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000014809 hi = hash_find(&compat_hashtab, name);
14810 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000014811 return &compat_hashtab;
14812
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 return &globvarht; /* global variable */
14815 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 }
14817 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014818 if (*name == 'g') /* global variable */
14819 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014820 /* There must be no ':' or '#' in the rest of the name, unless g: is used
14821 */
14822 if (vim_strchr(name + 2, ':') != NULL
14823 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014824 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014826 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014828 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014829 if (*name == 'v') /* v: variable */
14830 return &vimvarht;
14831 if (*name == 'a' && current_funccal != NULL) /* function argument */
14832 return &current_funccal->l_avars.dv_hashtab;
14833 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14834 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 if (*name == 's' /* script variable */
14836 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14837 return &SCRIPT_VARS(current_SID);
14838 return NULL;
14839}
14840
14841/*
14842 * Get the string value of a (global/local) variable.
14843 * Returns NULL when it doesn't exist.
14844 */
14845 char_u *
14846get_var_value(name)
14847 char_u *name;
14848{
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850
Bram Moolenaara7043832005-01-21 11:56:39 +000014851 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 if (v == NULL)
14853 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014854 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855}
14856
14857/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014858 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 * sourcing this script and when executing functions defined in the script.
14860 */
14861 void
14862new_script_vars(id)
14863 scid_T id;
14864{
Bram Moolenaara7043832005-01-21 11:56:39 +000014865 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 hashtab_T *ht;
14867 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014868
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14870 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014871 /* Re-allocating ga_data means that an ht_array pointing to
14872 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014874 for (i = 1; i <= ga_scripts.ga_len; ++i)
14875 {
14876 ht = &SCRIPT_VARS(i);
14877 if (ht->ht_mask == HT_INIT_SIZE - 1)
14878 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 sv = &SCRIPT_SV(i);
14880 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014881 }
14882
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 while (ga_scripts.ga_len < id)
14884 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014885 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14886 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 }
14889 }
14890}
14891
14892/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14894 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 */
14896 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014897init_var_dict(dict, dict_var)
14898 dict_T *dict;
14899 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900{
Bram Moolenaar33570922005-01-25 22:26:29 +000014901 hash_init(&dict->dv_hashtab);
14902 dict->dv_refcount = 99999;
14903 dict_var->di_tv.vval.v_dict = dict;
14904 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014905 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014906 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14907 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908}
14909
14910/*
14911 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014912 * Frees all allocated variables and the value they contain.
14913 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 */
14915 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014916vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014917 hashtab_T *ht;
14918{
14919 vars_clear_ext(ht, TRUE);
14920}
14921
14922/*
14923 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14924 */
14925 static void
14926vars_clear_ext(ht, free_val)
14927 hashtab_T *ht;
14928 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929{
Bram Moolenaara7043832005-01-21 11:56:39 +000014930 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014931 hashitem_T *hi;
14932 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933
Bram Moolenaar33570922005-01-25 22:26:29 +000014934 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014935 todo = ht->ht_used;
14936 for (hi = ht->ht_array; todo > 0; ++hi)
14937 {
14938 if (!HASHITEM_EMPTY(hi))
14939 {
14940 --todo;
14941
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014943 * ht_array might change then. hash_clear() takes care of it
14944 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 v = HI2DI(hi);
14946 if (free_val)
14947 clear_tv(&v->di_tv);
14948 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14949 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014950 }
14951 }
14952 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953}
14954
Bram Moolenaara7043832005-01-21 11:56:39 +000014955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014956 * Delete a variable from hashtab "ht" at item "hi".
14957 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014960delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014961 hashtab_T *ht;
14962 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963{
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014965
14966 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 clear_tv(&di->di_tv);
14968 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969}
14970
14971/*
14972 * List the value of one internal variable.
14973 */
14974 static void
14975list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 char_u *prefix;
14978{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014979 char_u *tofree;
14980 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014981 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014982
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 s = echo_string(&v->di_tv, &tofree, numbuf);
14984 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014985 s == NULL ? (char_u *)"" : s);
14986 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987}
14988
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 static void
14990list_one_var_a(prefix, name, type, string)
14991 char_u *prefix;
14992 char_u *name;
14993 int type;
14994 char_u *string;
14995{
14996 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14997 if (name != NULL) /* "a:" vars don't have a name stored */
14998 msg_puts(name);
14999 msg_putchar(' ');
15000 msg_advance(22);
15001 if (type == VAR_NUMBER)
15002 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015003 else if (type == VAR_FUNC)
15004 msg_putchar('*');
15005 else if (type == VAR_LIST)
15006 {
15007 msg_putchar('[');
15008 if (*string == '[')
15009 ++string;
15010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011 else if (type == VAR_DICT)
15012 {
15013 msg_putchar('{');
15014 if (*string == '{')
15015 ++string;
15016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 else
15018 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015019
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015021
15022 if (type == VAR_FUNC)
15023 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024}
15025
15026/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 * If the variable already exists, the value is updated.
15029 * Otherwise the variable is created.
15030 */
15031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015035 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036{
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015039 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015042 {
15043 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15044 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15045 ? name[2] : name[0]))
15046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015047 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015048 return;
15049 }
15050 if (function_exists(name))
15051 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015052 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15053 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015054 return;
15055 }
15056 }
15057
Bram Moolenaara7043832005-01-21 11:56:39 +000015058 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015059 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015060 {
15061 EMSG2(_("E461: Illegal variable name: %s"), name);
15062 return;
15063 }
15064
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015065 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015068 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015069 if (var_check_ro(v->di_flags, name)
15070 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 return;
15072 if (v->di_tv.v_type != tv->v_type
15073 && !((v->di_tv.v_type == VAR_STRING
15074 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 && (tv->v_type == VAR_STRING
15076 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015077 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015078 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015079 return;
15080 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015081
15082 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015083 * Handle setting internal v: variables separately: we don't change
15084 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 */
15086 if (ht == &vimvarht)
15087 {
15088 if (v->di_tv.v_type == VAR_STRING)
15089 {
15090 vim_free(v->di_tv.vval.v_string);
15091 if (copy || tv->v_type != VAR_STRING)
15092 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15093 else
15094 {
15095 /* Take over the string to avoid an extra alloc/free. */
15096 v->di_tv.vval.v_string = tv->vval.v_string;
15097 tv->vval.v_string = NULL;
15098 }
15099 }
15100 else if (v->di_tv.v_type != VAR_NUMBER)
15101 EMSG2(_(e_intern2), "set_var()");
15102 else
15103 v->di_tv.vval.v_number = get_tv_number(tv);
15104 return;
15105 }
15106
15107 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 }
15109 else /* add a new variable */
15110 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015111 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15112 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015113 if (v == NULL)
15114 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015116 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015118 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 return;
15120 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015121 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015126 else
15127 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015129 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132}
15133
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015134/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15136 * Also give an error message.
15137 */
15138 static int
15139var_check_ro(flags, name)
15140 int flags;
15141 char_u *name;
15142{
15143 if (flags & DI_FLAGS_RO)
15144 {
15145 EMSG2(_(e_readonlyvar), name);
15146 return TRUE;
15147 }
15148 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15149 {
15150 EMSG2(_(e_readonlysbx), name);
15151 return TRUE;
15152 }
15153 return FALSE;
15154}
15155
15156/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015157 * Return TRUE if typeval "tv" is set to be locked (immutable).
15158 * Also give an error message, using "name".
15159 */
15160 static int
15161tv_check_lock(lock, name)
15162 int lock;
15163 char_u *name;
15164{
15165 if (lock & VAR_LOCKED)
15166 {
15167 EMSG2(_("E741: Value is locked: %s"),
15168 name == NULL ? (char_u *)_("Unknown") : name);
15169 return TRUE;
15170 }
15171 if (lock & VAR_FIXED)
15172 {
15173 EMSG2(_("E742: Cannot change value of %s"),
15174 name == NULL ? (char_u *)_("Unknown") : name);
15175 return TRUE;
15176 }
15177 return FALSE;
15178}
15179
15180/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015182 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015183 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015186copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015187 typval_T *from;
15188 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015191 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015192 switch (from->v_type)
15193 {
15194 case VAR_NUMBER:
15195 to->vval.v_number = from->vval.v_number;
15196 break;
15197 case VAR_STRING:
15198 case VAR_FUNC:
15199 if (from->vval.v_string == NULL)
15200 to->vval.v_string = NULL;
15201 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015202 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015203 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015204 if (from->v_type == VAR_FUNC)
15205 func_ref(to->vval.v_string);
15206 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207 break;
15208 case VAR_LIST:
15209 if (from->vval.v_list == NULL)
15210 to->vval.v_list = NULL;
15211 else
15212 {
15213 to->vval.v_list = from->vval.v_list;
15214 ++to->vval.v_list->lv_refcount;
15215 }
15216 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015217 case VAR_DICT:
15218 if (from->vval.v_dict == NULL)
15219 to->vval.v_dict = NULL;
15220 else
15221 {
15222 to->vval.v_dict = from->vval.v_dict;
15223 ++to->vval.v_dict->dv_refcount;
15224 }
15225 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015226 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015227 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015228 break;
15229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230}
15231
15232/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015233 * Make a copy of an item.
15234 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015235 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15236 * reference to an already copied list/dict can be used.
15237 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015238 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015239 static int
15240item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015241 typval_T *from;
15242 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015243 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015244 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015245{
15246 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015247 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015248
Bram Moolenaar33570922005-01-25 22:26:29 +000015249 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015250 {
15251 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015252 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015253 }
15254 ++recurse;
15255
15256 switch (from->v_type)
15257 {
15258 case VAR_NUMBER:
15259 case VAR_STRING:
15260 case VAR_FUNC:
15261 copy_tv(from, to);
15262 break;
15263 case VAR_LIST:
15264 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015265 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015266 if (from->vval.v_list == NULL)
15267 to->vval.v_list = NULL;
15268 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15269 {
15270 /* use the copy made earlier */
15271 to->vval.v_list = from->vval.v_list->lv_copylist;
15272 ++to->vval.v_list->lv_refcount;
15273 }
15274 else
15275 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15276 if (to->vval.v_list == NULL)
15277 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015278 break;
15279 case VAR_DICT:
15280 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015281 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015282 if (from->vval.v_dict == NULL)
15283 to->vval.v_dict = NULL;
15284 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15285 {
15286 /* use the copy made earlier */
15287 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15288 ++to->vval.v_dict->dv_refcount;
15289 }
15290 else
15291 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15292 if (to->vval.v_dict == NULL)
15293 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015294 break;
15295 default:
15296 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015297 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015298 }
15299 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015300 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015301}
15302
15303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 * ":echo expr1 ..." print each argument separated with a space, add a
15305 * newline at the end.
15306 * ":echon expr1 ..." print each argument plain.
15307 */
15308 void
15309ex_echo(eap)
15310 exarg_T *eap;
15311{
15312 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015314 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 char_u *p;
15316 int needclr = TRUE;
15317 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015318 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319
15320 if (eap->skip)
15321 ++emsg_skip;
15322 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15323 {
15324 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 {
15327 /*
15328 * Report the invalid expression unless the expression evaluation
15329 * has been cancelled due to an aborting error, an interrupt, or an
15330 * exception.
15331 */
15332 if (!aborting())
15333 EMSG2(_(e_invexpr2), p);
15334 break;
15335 }
15336 if (!eap->skip)
15337 {
15338 if (atstart)
15339 {
15340 atstart = FALSE;
15341 /* Call msg_start() after eval1(), evaluating the expression
15342 * may cause a message to appear. */
15343 if (eap->cmdidx == CMD_echo)
15344 msg_start();
15345 }
15346 else if (eap->cmdidx == CMD_echo)
15347 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015348 p = echo_string(&rettv, &tofree, numbuf);
15349 if (p != NULL)
15350 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015352 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015354 if (*p != TAB && needclr)
15355 {
15356 /* remove any text still there from the command */
15357 msg_clr_eos();
15358 needclr = FALSE;
15359 }
15360 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 }
15362 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015363 {
15364#ifdef FEAT_MBYTE
15365 if (has_mbyte)
15366 {
15367 int i = (*mb_ptr2len_check)(p);
15368
15369 (void)msg_outtrans_len_attr(p, i, echo_attr);
15370 p += i - 1;
15371 }
15372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015374 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015377 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015379 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 arg = skipwhite(arg);
15381 }
15382 eap->nextcmd = check_nextcmd(arg);
15383
15384 if (eap->skip)
15385 --emsg_skip;
15386 else
15387 {
15388 /* remove text that may still be there from the command */
15389 if (needclr)
15390 msg_clr_eos();
15391 if (eap->cmdidx == CMD_echo)
15392 msg_end();
15393 }
15394}
15395
15396/*
15397 * ":echohl {name}".
15398 */
15399 void
15400ex_echohl(eap)
15401 exarg_T *eap;
15402{
15403 int id;
15404
15405 id = syn_name2id(eap->arg);
15406 if (id == 0)
15407 echo_attr = 0;
15408 else
15409 echo_attr = syn_id2attr(id);
15410}
15411
15412/*
15413 * ":execute expr1 ..." execute the result of an expression.
15414 * ":echomsg expr1 ..." Print a message
15415 * ":echoerr expr1 ..." Print an error
15416 * Each gets spaces around each argument and a newline at the end for
15417 * echo commands
15418 */
15419 void
15420ex_execute(eap)
15421 exarg_T *eap;
15422{
15423 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015424 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 int ret = OK;
15426 char_u *p;
15427 garray_T ga;
15428 int len;
15429 int save_did_emsg;
15430
15431 ga_init2(&ga, 1, 80);
15432
15433 if (eap->skip)
15434 ++emsg_skip;
15435 while (*arg != NUL && *arg != '|' && *arg != '\n')
15436 {
15437 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 {
15440 /*
15441 * Report the invalid expression unless the expression evaluation
15442 * has been cancelled due to an aborting error, an interrupt, or an
15443 * exception.
15444 */
15445 if (!aborting())
15446 EMSG2(_(e_invexpr2), p);
15447 ret = FAIL;
15448 break;
15449 }
15450
15451 if (!eap->skip)
15452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 len = (int)STRLEN(p);
15455 if (ga_grow(&ga, len + 2) == FAIL)
15456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 ret = FAIL;
15459 break;
15460 }
15461 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 ga.ga_len += len;
15465 }
15466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 arg = skipwhite(arg);
15469 }
15470
15471 if (ret != FAIL && ga.ga_data != NULL)
15472 {
15473 if (eap->cmdidx == CMD_echomsg)
15474 MSG_ATTR(ga.ga_data, echo_attr);
15475 else if (eap->cmdidx == CMD_echoerr)
15476 {
15477 /* We don't want to abort following commands, restore did_emsg. */
15478 save_did_emsg = did_emsg;
15479 EMSG((char_u *)ga.ga_data);
15480 if (!force_abort)
15481 did_emsg = save_did_emsg;
15482 }
15483 else if (eap->cmdidx == CMD_execute)
15484 do_cmdline((char_u *)ga.ga_data,
15485 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15486 }
15487
15488 ga_clear(&ga);
15489
15490 if (eap->skip)
15491 --emsg_skip;
15492
15493 eap->nextcmd = check_nextcmd(arg);
15494}
15495
15496/*
15497 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15498 * "arg" points to the "&" or '+' when called, to "option" when returning.
15499 * Returns NULL when no option name found. Otherwise pointer to the char
15500 * after the option name.
15501 */
15502 static char_u *
15503find_option_end(arg, opt_flags)
15504 char_u **arg;
15505 int *opt_flags;
15506{
15507 char_u *p = *arg;
15508
15509 ++p;
15510 if (*p == 'g' && p[1] == ':')
15511 {
15512 *opt_flags = OPT_GLOBAL;
15513 p += 2;
15514 }
15515 else if (*p == 'l' && p[1] == ':')
15516 {
15517 *opt_flags = OPT_LOCAL;
15518 p += 2;
15519 }
15520 else
15521 *opt_flags = 0;
15522
15523 if (!ASCII_ISALPHA(*p))
15524 return NULL;
15525 *arg = p;
15526
15527 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15528 p += 4; /* termcap option */
15529 else
15530 while (ASCII_ISALPHA(*p))
15531 ++p;
15532 return p;
15533}
15534
15535/*
15536 * ":function"
15537 */
15538 void
15539ex_function(eap)
15540 exarg_T *eap;
15541{
15542 char_u *theline;
15543 int j;
15544 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 char_u *name = NULL;
15547 char_u *p;
15548 char_u *arg;
15549 garray_T newargs;
15550 garray_T newlines;
15551 int varargs = FALSE;
15552 int mustend = FALSE;
15553 int flags = 0;
15554 ufunc_T *fp;
15555 int indent;
15556 int nesting;
15557 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015558 dictitem_T *v;
15559 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015560 static int func_nr = 0; /* number for nameless function */
15561 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015562 hashtab_T *ht;
15563 int todo;
15564 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565
15566 /*
15567 * ":function" without argument: list functions.
15568 */
15569 if (ends_excmd(*eap->arg))
15570 {
15571 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015572 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015573 todo = func_hashtab.ht_used;
15574 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015575 {
15576 if (!HASHITEM_EMPTY(hi))
15577 {
15578 --todo;
15579 fp = HI2UF(hi);
15580 if (!isdigit(*fp->uf_name))
15581 list_func_head(fp, FALSE);
15582 }
15583 }
15584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 eap->nextcmd = check_nextcmd(eap->arg);
15586 return;
15587 }
15588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015589 /*
15590 * Get the function name. There are these situations:
15591 * func normal function name
15592 * "name" == func, "fudi.fd_dict" == NULL
15593 * dict.func new dictionary entry
15594 * "name" == NULL, "fudi.fd_dict" set,
15595 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15596 * dict.func existing dict entry with a Funcref
15597 * "name" == fname, "fudi.fd_dict" set,
15598 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15599 * dict.func existing dict entry that's not a Funcref
15600 * "name" == NULL, "fudi.fd_dict" set,
15601 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015604 name = trans_function_name(&p, eap->skip, 0, &fudi);
15605 paren = (vim_strchr(p, '(') != NULL);
15606 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 {
15608 /*
15609 * Return on an invalid expression in braces, unless the expression
15610 * evaluation has been cancelled due to an aborting error, an
15611 * interrupt, or an exception.
15612 */
15613 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015615 if (!eap->skip && fudi.fd_newkey != NULL)
15616 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015617 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 else
15621 eap->skip = TRUE;
15622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 /* An error in a function call during evaluation of an expression in magic
15624 * braces should not cause the function not to be defined. */
15625 saved_did_emsg = did_emsg;
15626 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627
15628 /*
15629 * ":function func" with only function name: list function.
15630 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015631 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 {
15633 if (!ends_excmd(*skipwhite(p)))
15634 {
15635 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015636 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 }
15638 eap->nextcmd = check_nextcmd(p);
15639 if (eap->nextcmd != NULL)
15640 *p = NUL;
15641 if (!eap->skip && !got_int)
15642 {
15643 fp = find_func(name);
15644 if (fp != NULL)
15645 {
15646 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015647 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 {
15649 msg_putchar('\n');
15650 msg_outnum((long)(j + 1));
15651 if (j < 9)
15652 msg_putchar(' ');
15653 if (j < 99)
15654 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015655 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 out_flush(); /* show a line at a time */
15657 ui_breakcheck();
15658 }
15659 if (!got_int)
15660 {
15661 msg_putchar('\n');
15662 msg_puts((char_u *)" endfunction");
15663 }
15664 }
15665 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015666 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015668 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 }
15670
15671 /*
15672 * ":function name(arg1, arg2)" Define function.
15673 */
15674 p = skipwhite(p);
15675 if (*p != '(')
15676 {
15677 if (!eap->skip)
15678 {
15679 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015680 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 }
15682 /* attempt to continue by skipping some text */
15683 if (vim_strchr(p, '(') != NULL)
15684 p = vim_strchr(p, '(');
15685 }
15686 p = skipwhite(p + 1);
15687
15688 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15689 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15690
15691 /*
15692 * Isolate the arguments: "arg1, arg2, ...)"
15693 */
15694 while (*p != ')')
15695 {
15696 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15697 {
15698 varargs = TRUE;
15699 p += 3;
15700 mustend = TRUE;
15701 }
15702 else
15703 {
15704 arg = p;
15705 while (ASCII_ISALNUM(*p) || *p == '_')
15706 ++p;
15707 if (arg == p || isdigit(*arg)
15708 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15709 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15710 {
15711 if (!eap->skip)
15712 EMSG2(_("E125: Illegal argument: %s"), arg);
15713 break;
15714 }
15715 if (ga_grow(&newargs, 1) == FAIL)
15716 goto erret;
15717 c = *p;
15718 *p = NUL;
15719 arg = vim_strsave(arg);
15720 if (arg == NULL)
15721 goto erret;
15722 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15723 *p = c;
15724 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 if (*p == ',')
15726 ++p;
15727 else
15728 mustend = TRUE;
15729 }
15730 p = skipwhite(p);
15731 if (mustend && *p != ')')
15732 {
15733 if (!eap->skip)
15734 EMSG2(_(e_invarg2), eap->arg);
15735 break;
15736 }
15737 }
15738 ++p; /* skip the ')' */
15739
Bram Moolenaare9a41262005-01-15 22:18:47 +000015740 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 for (;;)
15742 {
15743 p = skipwhite(p);
15744 if (STRNCMP(p, "range", 5) == 0)
15745 {
15746 flags |= FC_RANGE;
15747 p += 5;
15748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015749 else if (STRNCMP(p, "dict", 4) == 0)
15750 {
15751 flags |= FC_DICT;
15752 p += 4;
15753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 else if (STRNCMP(p, "abort", 5) == 0)
15755 {
15756 flags |= FC_ABORT;
15757 p += 5;
15758 }
15759 else
15760 break;
15761 }
15762
15763 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15764 EMSG(_(e_trailing));
15765
15766 /*
15767 * Read the body of the function, until ":endfunction" is found.
15768 */
15769 if (KeyTyped)
15770 {
15771 /* Check if the function already exists, don't let the user type the
15772 * whole function before telling him it doesn't work! For a script we
15773 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015774 if (!eap->skip && !eap->forceit)
15775 {
15776 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15777 EMSG(_(e_funcdict));
15778 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015779 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781
15782 msg_putchar('\n'); /* don't overwrite the function name */
15783 cmdline_row = msg_row;
15784 }
15785
15786 indent = 2;
15787 nesting = 0;
15788 for (;;)
15789 {
15790 msg_scroll = TRUE;
15791 need_wait_return = FALSE;
15792 if (eap->getline == NULL)
15793 theline = getcmdline(':', 0L, indent);
15794 else
15795 theline = eap->getline(':', eap->cookie, indent);
15796 if (KeyTyped)
15797 lines_left = Rows - 1;
15798 if (theline == NULL)
15799 {
15800 EMSG(_("E126: Missing :endfunction"));
15801 goto erret;
15802 }
15803
15804 if (skip_until != NULL)
15805 {
15806 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15807 * don't check for ":endfunc". */
15808 if (STRCMP(theline, skip_until) == 0)
15809 {
15810 vim_free(skip_until);
15811 skip_until = NULL;
15812 }
15813 }
15814 else
15815 {
15816 /* skip ':' and blanks*/
15817 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15818 ;
15819
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015820 /* Check for "endfunction". */
15821 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 {
15823 vim_free(theline);
15824 break;
15825 }
15826
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 * at "end". */
15829 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15830 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015831 else if (STRNCMP(p, "if", 2) == 0
15832 || STRNCMP(p, "wh", 2) == 0
15833 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 || STRNCMP(p, "try", 3) == 0)
15835 indent += 2;
15836
15837 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015838 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 if (*p == '!')
15841 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 p += eval_fname_script(p);
15843 if (ASCII_ISALPHA(*p))
15844 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015845 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 if (*skipwhite(p) == '(')
15847 {
15848 ++nesting;
15849 indent += 2;
15850 }
15851 }
15852 }
15853
15854 /* Check for ":append" or ":insert". */
15855 p = skip_range(p, NULL);
15856 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15857 || (p[0] == 'i'
15858 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15859 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15860 skip_until = vim_strsave((char_u *)".");
15861
15862 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15863 arg = skipwhite(skiptowhite(p));
15864 if (arg[0] == '<' && arg[1] =='<'
15865 && ((p[0] == 'p' && p[1] == 'y'
15866 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15867 || (p[0] == 'p' && p[1] == 'e'
15868 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15869 || (p[0] == 't' && p[1] == 'c'
15870 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15871 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15872 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015873 || (p[0] == 'm' && p[1] == 'z'
15874 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 ))
15876 {
15877 /* ":python <<" continues until a dot, like ":append" */
15878 p = skipwhite(arg + 2);
15879 if (*p == NUL)
15880 skip_until = vim_strsave((char_u *)".");
15881 else
15882 skip_until = vim_strsave(p);
15883 }
15884 }
15885
15886 /* Add the line to the function. */
15887 if (ga_grow(&newlines, 1) == FAIL)
15888 goto erret;
15889 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15890 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 }
15892
15893 /* Don't define the function when skipping commands or when an error was
15894 * detected. */
15895 if (eap->skip || did_emsg)
15896 goto erret;
15897
15898 /*
15899 * If there are no errors, add the function
15900 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015901 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015902 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015903 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015905 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015906 emsg_funcname("E707: Function name conflicts with variable: %s",
15907 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015908 goto erret;
15909 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015910
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015911 fp = find_func(name);
15912 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015914 if (!eap->forceit)
15915 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015916 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015917 goto erret;
15918 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015919 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015921 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015922 name);
15923 goto erret;
15924 }
15925 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015926 ga_clear_strings(&(fp->uf_args));
15927 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015928 vim_free(name);
15929 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 }
15932 else
15933 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015934 char numbuf[20];
15935
15936 fp = NULL;
15937 if (fudi.fd_newkey == NULL && !eap->forceit)
15938 {
15939 EMSG(_(e_funcdict));
15940 goto erret;
15941 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015942 if (fudi.fd_di == NULL)
15943 {
15944 /* Can't add a function to a locked dictionary */
15945 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15946 goto erret;
15947 }
15948 /* Can't change an existing function if it is locked */
15949 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15950 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015951
15952 /* Give the function a sequential number. Can only be used with a
15953 * Funcref! */
15954 vim_free(name);
15955 sprintf(numbuf, "%d", ++func_nr);
15956 name = vim_strsave((char_u *)numbuf);
15957 if (name == NULL)
15958 goto erret;
15959 }
15960
15961 if (fp == NULL)
15962 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015963 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015964 {
15965 int slen, plen;
15966 char_u *scriptname;
15967
15968 /* Check that the autoload name matches the script name. */
15969 j = FAIL;
15970 if (sourcing_name != NULL)
15971 {
15972 scriptname = autoload_name(name);
15973 if (scriptname != NULL)
15974 {
15975 p = vim_strchr(scriptname, '/');
15976 plen = STRLEN(p);
15977 slen = STRLEN(sourcing_name);
15978 if (slen > plen && fnamecmp(p,
15979 sourcing_name + slen - plen) == 0)
15980 j = OK;
15981 vim_free(scriptname);
15982 }
15983 }
15984 if (j == FAIL)
15985 {
15986 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15987 goto erret;
15988 }
15989 }
15990
15991 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 if (fp == NULL)
15993 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015994
15995 if (fudi.fd_dict != NULL)
15996 {
15997 if (fudi.fd_di == NULL)
15998 {
15999 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016000 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016001 if (fudi.fd_di == NULL)
16002 {
16003 vim_free(fp);
16004 goto erret;
16005 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016006 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16007 {
16008 vim_free(fudi.fd_di);
16009 goto erret;
16010 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016011 }
16012 else
16013 /* overwrite existing dict entry */
16014 clear_tv(&fudi.fd_di->di_tv);
16015 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016016 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016017 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016018 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016019 }
16020
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016022 STRCPY(fp->uf_name, name);
16023 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016025 fp->uf_args = newargs;
16026 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016027#ifdef FEAT_PROFILE
16028 fp->uf_tml_count = NULL;
16029 fp->uf_tml_total = NULL;
16030 fp->uf_tml_self = NULL;
16031 fp->uf_profiling = FALSE;
16032 if (prof_def_func())
16033 func_do_profile(fp);
16034#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016035 fp->uf_varargs = varargs;
16036 fp->uf_flags = flags;
16037 fp->uf_calls = 0;
16038 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016039 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040
16041erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 ga_clear_strings(&newargs);
16043 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016044ret_free:
16045 vim_free(skip_until);
16046 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049}
16050
16051/*
16052 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016053 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016055 * flags:
16056 * TFN_INT: internal function name OK
16057 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 * Advances "pp" to just after the function name (if no error).
16059 */
16060 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016061trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 char_u **pp;
16063 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016064 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016065 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016067 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 char_u *start;
16069 char_u *end;
16070 int lead;
16071 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016073 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016074
16075 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016076 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016078
16079 /* Check for hard coded <SNR>: already translated function ID (from a user
16080 * command). */
16081 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16082 && (*pp)[2] == (int)KE_SNR)
16083 {
16084 *pp += 3;
16085 len = get_id_len(pp) + 3;
16086 return vim_strnsave(start, len);
16087 }
16088
16089 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16090 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016092 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016094
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016095 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16096 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016097 if (end == start)
16098 {
16099 if (!skip)
16100 EMSG(_("E129: Function name required"));
16101 goto theend;
16102 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016103 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016104 {
16105 /*
16106 * Report an invalid expression in braces, unless the expression
16107 * evaluation has been cancelled due to an aborting error, an
16108 * interrupt, or an exception.
16109 */
16110 if (!aborting())
16111 {
16112 if (end != NULL)
16113 EMSG2(_(e_invarg2), start);
16114 }
16115 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016116 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016117 goto theend;
16118 }
16119
16120 if (lv.ll_tv != NULL)
16121 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016122 if (fdp != NULL)
16123 {
16124 fdp->fd_dict = lv.ll_dict;
16125 fdp->fd_newkey = lv.ll_newkey;
16126 lv.ll_newkey = NULL;
16127 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016128 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016129 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16130 {
16131 name = vim_strsave(lv.ll_tv->vval.v_string);
16132 *pp = end;
16133 }
16134 else
16135 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016136 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16137 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016138 EMSG(_(e_funcref));
16139 else
16140 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016141 name = NULL;
16142 }
16143 goto theend;
16144 }
16145
16146 if (lv.ll_name == NULL)
16147 {
16148 /* Error found, but continue after the function name. */
16149 *pp = end;
16150 goto theend;
16151 }
16152
16153 if (lv.ll_exp_name != NULL)
16154 len = STRLEN(lv.ll_exp_name);
16155 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016156 {
16157 if (lead == 2) /* skip over "s:" */
16158 lv.ll_name += 2;
16159 len = (int)(end - lv.ll_name);
16160 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016161
16162 /*
16163 * Copy the function name to allocated memory.
16164 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16165 * Accept <SNR>123_name() outside a script.
16166 */
16167 if (skip)
16168 lead = 0; /* do nothing */
16169 else if (lead > 0)
16170 {
16171 lead = 3;
16172 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16173 {
16174 if (current_SID <= 0)
16175 {
16176 EMSG(_(e_usingsid));
16177 goto theend;
16178 }
16179 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16180 lead += (int)STRLEN(sid_buf);
16181 }
16182 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016183 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016184 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016185 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016186 goto theend;
16187 }
16188 name = alloc((unsigned)(len + lead + 1));
16189 if (name != NULL)
16190 {
16191 if (lead > 0)
16192 {
16193 name[0] = K_SPECIAL;
16194 name[1] = KS_EXTRA;
16195 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016196 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016197 STRCPY(name + 3, sid_buf);
16198 }
16199 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16200 name[len + lead] = NUL;
16201 }
16202 *pp = end;
16203
16204theend:
16205 clear_lval(&lv);
16206 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207}
16208
16209/*
16210 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16211 * Return 2 if "p" starts with "s:".
16212 * Return 0 otherwise.
16213 */
16214 static int
16215eval_fname_script(p)
16216 char_u *p;
16217{
16218 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16219 || STRNICMP(p + 1, "SNR>", 4) == 0))
16220 return 5;
16221 if (p[0] == 's' && p[1] == ':')
16222 return 2;
16223 return 0;
16224}
16225
16226/*
16227 * Return TRUE if "p" starts with "<SID>" or "s:".
16228 * Only works if eval_fname_script() returned non-zero for "p"!
16229 */
16230 static int
16231eval_fname_sid(p)
16232 char_u *p;
16233{
16234 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16235}
16236
16237/*
16238 * List the head of the function: "name(arg1, arg2)".
16239 */
16240 static void
16241list_func_head(fp, indent)
16242 ufunc_T *fp;
16243 int indent;
16244{
16245 int j;
16246
16247 msg_start();
16248 if (indent)
16249 MSG_PUTS(" ");
16250 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016251 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 {
16253 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016254 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 }
16256 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016257 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016259 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 {
16261 if (j)
16262 MSG_PUTS(", ");
16263 msg_puts(FUNCARG(fp, j));
16264 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016265 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 {
16267 if (j)
16268 MSG_PUTS(", ");
16269 MSG_PUTS("...");
16270 }
16271 msg_putchar(')');
16272}
16273
16274/*
16275 * Find a function by name, return pointer to it in ufuncs.
16276 * Return NULL for unknown function.
16277 */
16278 static ufunc_T *
16279find_func(name)
16280 char_u *name;
16281{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016282 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016284 hi = hash_find(&func_hashtab, name);
16285 if (!HASHITEM_EMPTY(hi))
16286 return HI2UF(hi);
16287 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288}
16289
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016290/*
16291 * Return TRUE if a function "name" exists.
16292 */
16293 static int
16294function_exists(name)
16295 char_u *name;
16296{
16297 char_u *p = name;
16298 int n = FALSE;
16299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016300 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016301 if (p != NULL)
16302 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016303 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016304 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016305 else
16306 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016307 vim_free(p);
16308 }
16309 return n;
16310}
16311
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016312/*
16313 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016314 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016315 */
16316 static int
16317builtin_function(name)
16318 char_u *name;
16319{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016320 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
16321 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016322}
16323
Bram Moolenaar05159a02005-02-26 23:04:13 +000016324#if defined(FEAT_PROFILE) || defined(PROTO)
16325/*
16326 * Start profiling function "fp".
16327 */
16328 static void
16329func_do_profile(fp)
16330 ufunc_T *fp;
16331{
16332 fp->uf_tm_count = 0;
16333 profile_zero(&fp->uf_tm_self);
16334 profile_zero(&fp->uf_tm_total);
16335 if (fp->uf_tml_count == NULL)
16336 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16337 (sizeof(int) * fp->uf_lines.ga_len));
16338 if (fp->uf_tml_total == NULL)
16339 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16340 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16341 if (fp->uf_tml_self == NULL)
16342 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16343 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16344 fp->uf_tml_idx = -1;
16345 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16346 || fp->uf_tml_self == NULL)
16347 return; /* out of memory */
16348
16349 fp->uf_profiling = TRUE;
16350}
16351
16352/*
16353 * Dump the profiling results for all functions in file "fd".
16354 */
16355 void
16356func_dump_profile(fd)
16357 FILE *fd;
16358{
16359 hashitem_T *hi;
16360 int todo;
16361 ufunc_T *fp;
16362 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016363 ufunc_T **sorttab;
16364 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016365
16366 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016367 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16368
Bram Moolenaar05159a02005-02-26 23:04:13 +000016369 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16370 {
16371 if (!HASHITEM_EMPTY(hi))
16372 {
16373 --todo;
16374 fp = HI2UF(hi);
16375 if (fp->uf_profiling)
16376 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016377 if (sorttab != NULL)
16378 sorttab[st_len++] = fp;
16379
Bram Moolenaar05159a02005-02-26 23:04:13 +000016380 if (fp->uf_name[0] == K_SPECIAL)
16381 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16382 else
16383 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16384 if (fp->uf_tm_count == 1)
16385 fprintf(fd, "Called 1 time\n");
16386 else
16387 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16388 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16389 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16390 fprintf(fd, "\n");
16391 fprintf(fd, "count total (s) self (s)\n");
16392
16393 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16394 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016395 prof_func_line(fd, fp->uf_tml_count[i],
16396 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016397 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16398 }
16399 fprintf(fd, "\n");
16400 }
16401 }
16402 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016403
16404 if (sorttab != NULL && st_len > 0)
16405 {
16406 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16407 prof_total_cmp);
16408 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16409 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16410 prof_self_cmp);
16411 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16412 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016413}
Bram Moolenaar73830342005-02-28 22:48:19 +000016414
16415 static void
16416prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16417 FILE *fd;
16418 ufunc_T **sorttab;
16419 int st_len;
16420 char *title;
16421 int prefer_self; /* when equal print only self time */
16422{
16423 int i;
16424 ufunc_T *fp;
16425
16426 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16427 fprintf(fd, "count total (s) self (s) function\n");
16428 for (i = 0; i < 20 && i < st_len; ++i)
16429 {
16430 fp = sorttab[i];
16431 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16432 prefer_self);
16433 if (fp->uf_name[0] == K_SPECIAL)
16434 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16435 else
16436 fprintf(fd, " %s()\n", fp->uf_name);
16437 }
16438 fprintf(fd, "\n");
16439}
16440
16441/*
16442 * Print the count and times for one function or function line.
16443 */
16444 static void
16445prof_func_line(fd, count, total, self, prefer_self)
16446 FILE *fd;
16447 int count;
16448 proftime_T *total;
16449 proftime_T *self;
16450 int prefer_self; /* when equal print only self time */
16451{
16452 if (count > 0)
16453 {
16454 fprintf(fd, "%5d ", count);
16455 if (prefer_self && profile_equal(total, self))
16456 fprintf(fd, " ");
16457 else
16458 fprintf(fd, "%s ", profile_msg(total));
16459 if (!prefer_self && profile_equal(total, self))
16460 fprintf(fd, " ");
16461 else
16462 fprintf(fd, "%s ", profile_msg(self));
16463 }
16464 else
16465 fprintf(fd, " ");
16466}
16467
16468/*
16469 * Compare function for total time sorting.
16470 */
16471 static int
16472#ifdef __BORLANDC__
16473_RTLENTRYF
16474#endif
16475prof_total_cmp(s1, s2)
16476 const void *s1;
16477 const void *s2;
16478{
16479 ufunc_T *p1, *p2;
16480
16481 p1 = *(ufunc_T **)s1;
16482 p2 = *(ufunc_T **)s2;
16483 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16484}
16485
16486/*
16487 * Compare function for self time sorting.
16488 */
16489 static int
16490#ifdef __BORLANDC__
16491_RTLENTRYF
16492#endif
16493prof_self_cmp(s1, s2)
16494 const void *s1;
16495 const void *s2;
16496{
16497 ufunc_T *p1, *p2;
16498
16499 p1 = *(ufunc_T **)s1;
16500 p2 = *(ufunc_T **)s2;
16501 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16502}
16503
Bram Moolenaar05159a02005-02-26 23:04:13 +000016504#endif
16505
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016506/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016507 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016508 * Return TRUE if a package was loaded.
16509 */
16510 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016511script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016512 char_u *name;
16513{
16514 char_u *p;
16515 char_u *scriptname;
16516 int ret = FALSE;
16517
16518 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016519 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016520 if (p == NULL || p <= name + 2)
16521 return FALSE;
16522
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016523 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16524 scriptname = autoload_name(name);
16525 if (cmd_runtime(scriptname, FALSE) == OK)
16526 ret = TRUE;
16527
16528 vim_free(scriptname);
16529 return ret;
16530}
16531
16532/*
16533 * Return the autoload script name for a function or variable name.
16534 * Returns NULL when out of memory.
16535 */
16536 static char_u *
16537autoload_name(name)
16538 char_u *name;
16539{
16540 char_u *p;
16541 char_u *scriptname;
16542
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016543 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016544 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16545 if (scriptname == NULL)
16546 return FALSE;
16547 STRCPY(scriptname, "autoload/");
16548 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016549 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016550 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016551 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016552 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016553 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016554}
16555
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16557
16558/*
16559 * Function given to ExpandGeneric() to obtain the list of user defined
16560 * function names.
16561 */
16562 char_u *
16563get_user_func_name(xp, idx)
16564 expand_T *xp;
16565 int idx;
16566{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016567 static long_u done;
16568 static hashitem_T *hi;
16569 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570
16571 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016573 done = 0;
16574 hi = func_hashtab.ht_array;
16575 }
16576 if (done < func_hashtab.ht_used)
16577 {
16578 if (done++ > 0)
16579 ++hi;
16580 while (HASHITEM_EMPTY(hi))
16581 ++hi;
16582 fp = HI2UF(hi);
16583
16584 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16585 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586
16587 cat_func_name(IObuff, fp);
16588 if (xp->xp_context != EXPAND_USER_FUNC)
16589 {
16590 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016591 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 STRCAT(IObuff, ")");
16593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 return IObuff;
16595 }
16596 return NULL;
16597}
16598
16599#endif /* FEAT_CMDL_COMPL */
16600
16601/*
16602 * Copy the function name of "fp" to buffer "buf".
16603 * "buf" must be able to hold the function name plus three bytes.
16604 * Takes care of script-local function names.
16605 */
16606 static void
16607cat_func_name(buf, fp)
16608 char_u *buf;
16609 ufunc_T *fp;
16610{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016611 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 {
16613 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016614 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 }
16616 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016617 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618}
16619
16620/*
16621 * ":delfunction {name}"
16622 */
16623 void
16624ex_delfunction(eap)
16625 exarg_T *eap;
16626{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016627 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 char_u *p;
16629 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016630 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631
16632 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016633 name = trans_function_name(&p, eap->skip, 0, &fudi);
16634 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016636 {
16637 if (fudi.fd_dict != NULL && !eap->skip)
16638 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 if (!ends_excmd(*skipwhite(p)))
16642 {
16643 vim_free(name);
16644 EMSG(_(e_trailing));
16645 return;
16646 }
16647 eap->nextcmd = check_nextcmd(p);
16648 if (eap->nextcmd != NULL)
16649 *p = NUL;
16650
16651 if (!eap->skip)
16652 fp = find_func(name);
16653 vim_free(name);
16654
16655 if (!eap->skip)
16656 {
16657 if (fp == NULL)
16658 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016659 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 return;
16661 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016662 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 {
16664 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16665 return;
16666 }
16667
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016668 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016670 /* Delete the dict item that refers to the function, it will
16671 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016672 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016674 else
16675 func_free(fp);
16676 }
16677}
16678
16679/*
16680 * Free a function and remove it from the list of functions.
16681 */
16682 static void
16683func_free(fp)
16684 ufunc_T *fp;
16685{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016686 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016687
16688 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016689 ga_clear_strings(&(fp->uf_args));
16690 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016691#ifdef FEAT_PROFILE
16692 vim_free(fp->uf_tml_count);
16693 vim_free(fp->uf_tml_total);
16694 vim_free(fp->uf_tml_self);
16695#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016696
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016697 /* remove the function from the function hashtable */
16698 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16699 if (HASHITEM_EMPTY(hi))
16700 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016701 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016702 hash_remove(&func_hashtab, hi);
16703
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016704 vim_free(fp);
16705}
16706
16707/*
16708 * Unreference a Function: decrement the reference count and free it when it
16709 * becomes zero. Only for numbered functions.
16710 */
16711 static void
16712func_unref(name)
16713 char_u *name;
16714{
16715 ufunc_T *fp;
16716
16717 if (name != NULL && isdigit(*name))
16718 {
16719 fp = find_func(name);
16720 if (fp == NULL)
16721 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016722 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016723 {
16724 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016725 * when "uf_calls" becomes zero. */
16726 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016727 func_free(fp);
16728 }
16729 }
16730}
16731
16732/*
16733 * Count a reference to a Function.
16734 */
16735 static void
16736func_ref(name)
16737 char_u *name;
16738{
16739 ufunc_T *fp;
16740
16741 if (name != NULL && isdigit(*name))
16742 {
16743 fp = find_func(name);
16744 if (fp == NULL)
16745 EMSG2(_(e_intern2), "func_ref()");
16746 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016747 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 }
16749}
16750
16751/*
16752 * Call a user function.
16753 */
16754 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016755call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 ufunc_T *fp; /* pointer to function */
16757 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 typval_T *argvars; /* arguments */
16759 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 linenr_T firstline; /* first line of range */
16761 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016762 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763{
Bram Moolenaar33570922005-01-25 22:26:29 +000016764 char_u *save_sourcing_name;
16765 linenr_T save_sourcing_lnum;
16766 scid_T save_current_SID;
16767 funccall_T fc;
16768 funccall_T *save_fcp = current_funccal;
16769 int save_did_emsg;
16770 static int depth = 0;
16771 dictitem_T *v;
16772 int fixvar_idx = 0; /* index in fixvar[] */
16773 int i;
16774 int ai;
16775 char_u numbuf[NUMBUFLEN];
16776 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016777#ifdef FEAT_PROFILE
16778 proftime_T wait_start;
16779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780
16781 /* If depth of calling is getting too high, don't execute the function */
16782 if (depth >= p_mfd)
16783 {
16784 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785 rettv->v_type = VAR_NUMBER;
16786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 return;
16788 }
16789 ++depth;
16790
16791 line_breakcheck(); /* check for CTRL-C hit */
16792
Bram Moolenaar33570922005-01-25 22:26:29 +000016793 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016795 fc.rettv = rettv;
16796 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 fc.linenr = 0;
16798 fc.returned = FALSE;
16799 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016801 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 fc.dbg_tick = debug_tick;
16803
Bram Moolenaar33570922005-01-25 22:26:29 +000016804 /*
16805 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16806 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16807 * each argument variable and saves a lot of time.
16808 */
16809 /*
16810 * Init l: variables.
16811 */
16812 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016813 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016814 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016815 /* Set l:self to "selfdict". */
16816 v = &fc.fixvar[fixvar_idx++].var;
16817 STRCPY(v->di_key, "self");
16818 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16819 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16820 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016821 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 v->di_tv.vval.v_dict = selfdict;
16823 ++selfdict->dv_refcount;
16824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016825
Bram Moolenaar33570922005-01-25 22:26:29 +000016826 /*
16827 * Init a: variables.
16828 * Set a:0 to "argcount".
16829 * Set a:000 to a list with room for the "..." arguments.
16830 */
16831 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16832 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016833 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016834 v = &fc.fixvar[fixvar_idx++].var;
16835 STRCPY(v->di_key, "000");
16836 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16837 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16838 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016839 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 v->di_tv.vval.v_list = &fc.l_varlist;
16841 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16842 fc.l_varlist.lv_refcount = 99999;
16843
16844 /*
16845 * Set a:firstline to "firstline" and a:lastline to "lastline".
16846 * Set a:name to named arguments.
16847 * Set a:N to the "..." arguments.
16848 */
16849 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16850 (varnumber_T)firstline);
16851 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16852 (varnumber_T)lastline);
16853 for (i = 0; i < argcount; ++i)
16854 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016855 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016856 if (ai < 0)
16857 /* named argument a:name */
16858 name = FUNCARG(fp, i);
16859 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 /* "..." argument a:1, a:2, etc. */
16862 sprintf((char *)numbuf, "%d", ai + 1);
16863 name = numbuf;
16864 }
16865 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16866 {
16867 v = &fc.fixvar[fixvar_idx++].var;
16868 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16869 }
16870 else
16871 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016872 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16873 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 if (v == NULL)
16875 break;
16876 v->di_flags = DI_FLAGS_RO;
16877 }
16878 STRCPY(v->di_key, name);
16879 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016881 /* Note: the values are copied directly to avoid alloc/free.
16882 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016884 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016885
16886 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16887 {
16888 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16889 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016890 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016891 }
16892 }
16893
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 /* Don't redraw while executing the function. */
16895 ++RedrawingDisabled;
16896 save_sourcing_name = sourcing_name;
16897 save_sourcing_lnum = sourcing_lnum;
16898 sourcing_lnum = 1;
16899 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016900 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 if (sourcing_name != NULL)
16902 {
16903 if (save_sourcing_name != NULL
16904 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16905 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16906 else
16907 STRCPY(sourcing_name, "function ");
16908 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16909
16910 if (p_verbose >= 12)
16911 {
16912 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000016913 verbose_enter_scroll();
16914
Bram Moolenaar555b2802005-05-19 21:08:39 +000016915 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 if (p_verbose >= 14)
16917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016919 char_u numbuf[NUMBUFLEN];
16920 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921
16922 msg_puts((char_u *)"(");
16923 for (i = 0; i < argcount; ++i)
16924 {
16925 if (i > 0)
16926 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016927 if (argvars[i].v_type == VAR_NUMBER)
16928 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 else
16930 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016931 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016932 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016934 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 }
16936 }
16937 msg_puts((char_u *)")");
16938 }
16939 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000016940
16941 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 --no_wait_return;
16943 }
16944 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016945#ifdef FEAT_PROFILE
16946 if (do_profiling)
16947 {
16948 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16949 func_do_profile(fp);
16950 if (fp->uf_profiling
16951 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16952 {
16953 ++fp->uf_tm_count;
16954 profile_start(&fp->uf_tm_start);
16955 profile_zero(&fp->uf_tm_children);
16956 }
16957 script_prof_save(&wait_start);
16958 }
16959#endif
16960
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016962 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 save_did_emsg = did_emsg;
16964 did_emsg = FALSE;
16965
16966 /* call do_cmdline() to execute the lines */
16967 do_cmdline(NULL, get_func_line, (void *)&fc,
16968 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16969
16970 --RedrawingDisabled;
16971
16972 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016973 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016975 clear_tv(rettv);
16976 rettv->v_type = VAR_NUMBER;
16977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 }
16979
Bram Moolenaar05159a02005-02-26 23:04:13 +000016980#ifdef FEAT_PROFILE
16981 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16982 {
16983 profile_end(&fp->uf_tm_start);
16984 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16985 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16986 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16987 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16988 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16989 {
16990 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16991 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16992 }
16993 }
16994#endif
16995
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 /* when being verbose, mention the return value */
16997 if (p_verbose >= 12)
16998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017000 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017003 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017005 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17006 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017009 char_u buf[MSG_BUF_LEN];
17010 char_u numbuf[NUMBUFLEN];
17011 char_u *tofree;
17012
Bram Moolenaar555b2802005-05-19 21:08:39 +000017013 /* The value may be very long. Skip the middle part, so that we
17014 * have some idea how it starts and ends. smsg() would always
17015 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017016 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017017 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017018 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017019 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 }
17021 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017022
17023 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 --no_wait_return;
17025 }
17026
17027 vim_free(sourcing_name);
17028 sourcing_name = save_sourcing_name;
17029 sourcing_lnum = save_sourcing_lnum;
17030 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017031#ifdef FEAT_PROFILE
17032 if (do_profiling)
17033 script_prof_restore(&wait_start);
17034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035
17036 if (p_verbose >= 12 && sourcing_name != NULL)
17037 {
17038 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017039 verbose_enter_scroll();
17040
Bram Moolenaar555b2802005-05-19 21:08:39 +000017041 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017043
17044 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 --no_wait_return;
17046 }
17047
17048 did_emsg |= save_did_emsg;
17049 current_funccal = save_fcp;
17050
Bram Moolenaar33570922005-01-25 22:26:29 +000017051 /* The a: variables typevals were not alloced, only free the allocated
17052 * variables. */
17053 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17054
17055 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 --depth;
17057}
17058
17059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017060 * Add a number variable "name" to dict "dp" with value "nr".
17061 */
17062 static void
17063add_nr_var(dp, v, name, nr)
17064 dict_T *dp;
17065 dictitem_T *v;
17066 char *name;
17067 varnumber_T nr;
17068{
17069 STRCPY(v->di_key, name);
17070 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17071 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17072 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017073 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017074 v->di_tv.vval.v_number = nr;
17075}
17076
17077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 * ":return [expr]"
17079 */
17080 void
17081ex_return(eap)
17082 exarg_T *eap;
17083{
17084 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 int returning = FALSE;
17087
17088 if (current_funccal == NULL)
17089 {
17090 EMSG(_("E133: :return not inside a function"));
17091 return;
17092 }
17093
17094 if (eap->skip)
17095 ++emsg_skip;
17096
17097 eap->nextcmd = NULL;
17098 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017099 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 {
17101 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 }
17106 /* It's safer to return also on error. */
17107 else if (!eap->skip)
17108 {
17109 /*
17110 * Return unless the expression evaluation has been cancelled due to an
17111 * aborting error, an interrupt, or an exception.
17112 */
17113 if (!aborting())
17114 returning = do_return(eap, FALSE, TRUE, NULL);
17115 }
17116
17117 /* When skipping or the return gets pending, advance to the next command
17118 * in this line (!returning). Otherwise, ignore the rest of the line.
17119 * Following lines will be ignored by get_func_line(). */
17120 if (returning)
17121 eap->nextcmd = NULL;
17122 else if (eap->nextcmd == NULL) /* no argument */
17123 eap->nextcmd = check_nextcmd(arg);
17124
17125 if (eap->skip)
17126 --emsg_skip;
17127}
17128
17129/*
17130 * Return from a function. Possibly makes the return pending. Also called
17131 * for a pending return at the ":endtry" or after returning from an extra
17132 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017134 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 * FALSE when the return gets pending.
17136 */
17137 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017138do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 exarg_T *eap;
17140 int reanimate;
17141 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143{
17144 int idx;
17145 struct condstack *cstack = eap->cstack;
17146
17147 if (reanimate)
17148 /* Undo the return. */
17149 current_funccal->returned = FALSE;
17150
17151 /*
17152 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17153 * not in its finally clause (which then is to be executed next) is found.
17154 * In this case, make the ":return" pending for execution at the ":endtry".
17155 * Otherwise, return normally.
17156 */
17157 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17158 if (idx >= 0)
17159 {
17160 cstack->cs_pending[idx] = CSTP_RETURN;
17161
17162 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017163 /* A pending return again gets pending. "rettv" points to an
17164 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017166 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 else
17168 {
17169 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017172 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 {
17176 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017178 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 else
17180 EMSG(_(e_outofmem));
17181 }
17182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184
17185 if (reanimate)
17186 {
17187 /* The pending return value could be overwritten by a ":return"
17188 * without argument in a finally clause; reset the default
17189 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017190 current_funccal->rettv->v_type = VAR_NUMBER;
17191 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 }
17193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 }
17196 else
17197 {
17198 current_funccal->returned = TRUE;
17199
17200 /* If the return is carried out now, store the return value. For
17201 * a return immediately after reanimation, the value is already
17202 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017203 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017205 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017206 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 }
17210 }
17211
17212 return idx < 0;
17213}
17214
17215/*
17216 * Free the variable with a pending return value.
17217 */
17218 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219discard_pending_return(rettv)
17220 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221{
Bram Moolenaar33570922005-01-25 22:26:29 +000017222 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223}
17224
17225/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 * is an allocated string. Used by report_pending() for verbose messages.
17228 */
17229 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230get_return_cmd(rettv)
17231 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017233 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017234 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017235 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017237 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017238 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017239 if (s == NULL)
17240 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241
17242 STRCPY(IObuff, ":return ");
17243 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17244 if (STRLEN(s) + 8 >= IOSIZE)
17245 STRCPY(IObuff + IOSIZE - 4, "...");
17246 vim_free(tofree);
17247 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248}
17249
17250/*
17251 * Get next function line.
17252 * Called by do_cmdline() to get the next line.
17253 * Returns allocated string, or NULL for end of function.
17254 */
17255/* ARGSUSED */
17256 char_u *
17257get_func_line(c, cookie, indent)
17258 int c; /* not used */
17259 void *cookie;
17260 int indent; /* not used */
17261{
Bram Moolenaar33570922005-01-25 22:26:29 +000017262 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017263 ufunc_T *fp = fcp->func;
17264 char_u *retval;
17265 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266
17267 /* If breakpoints have been added/deleted need to check for it. */
17268 if (fcp->dbg_tick != debug_tick)
17269 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017270 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 sourcing_lnum);
17272 fcp->dbg_tick = debug_tick;
17273 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017274#ifdef FEAT_PROFILE
17275 if (do_profiling)
17276 func_line_end(cookie);
17277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278
Bram Moolenaar05159a02005-02-26 23:04:13 +000017279 gap = &fp->uf_lines;
17280 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 retval = NULL;
17282 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17283 retval = NULL;
17284 else
17285 {
17286 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17287 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017288#ifdef FEAT_PROFILE
17289 if (do_profiling)
17290 func_line_start(cookie);
17291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 }
17293
17294 /* Did we encounter a breakpoint? */
17295 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17296 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017297 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017299 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 sourcing_lnum);
17301 fcp->dbg_tick = debug_tick;
17302 }
17303
17304 return retval;
17305}
17306
Bram Moolenaar05159a02005-02-26 23:04:13 +000017307#if defined(FEAT_PROFILE) || defined(PROTO)
17308/*
17309 * Called when starting to read a function line.
17310 * "sourcing_lnum" must be correct!
17311 * When skipping lines it may not actually be executed, but we won't find out
17312 * until later and we need to store the time now.
17313 */
17314 void
17315func_line_start(cookie)
17316 void *cookie;
17317{
17318 funccall_T *fcp = (funccall_T *)cookie;
17319 ufunc_T *fp = fcp->func;
17320
17321 if (fp->uf_profiling && sourcing_lnum >= 1
17322 && sourcing_lnum <= fp->uf_lines.ga_len)
17323 {
17324 fp->uf_tml_idx = sourcing_lnum - 1;
17325 fp->uf_tml_execed = FALSE;
17326 profile_start(&fp->uf_tml_start);
17327 profile_zero(&fp->uf_tml_children);
17328 profile_get_wait(&fp->uf_tml_wait);
17329 }
17330}
17331
17332/*
17333 * Called when actually executing a function line.
17334 */
17335 void
17336func_line_exec(cookie)
17337 void *cookie;
17338{
17339 funccall_T *fcp = (funccall_T *)cookie;
17340 ufunc_T *fp = fcp->func;
17341
17342 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17343 fp->uf_tml_execed = TRUE;
17344}
17345
17346/*
17347 * Called when done with a function line.
17348 */
17349 void
17350func_line_end(cookie)
17351 void *cookie;
17352{
17353 funccall_T *fcp = (funccall_T *)cookie;
17354 ufunc_T *fp = fcp->func;
17355
17356 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17357 {
17358 if (fp->uf_tml_execed)
17359 {
17360 ++fp->uf_tml_count[fp->uf_tml_idx];
17361 profile_end(&fp->uf_tml_start);
17362 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17363 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17364 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17365 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17366 }
17367 fp->uf_tml_idx = -1;
17368 }
17369}
17370#endif
17371
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372/*
17373 * Return TRUE if the currently active function should be ended, because a
17374 * return was encountered or an error occured. Used inside a ":while".
17375 */
17376 int
17377func_has_ended(cookie)
17378 void *cookie;
17379{
Bram Moolenaar33570922005-01-25 22:26:29 +000017380 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381
17382 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17383 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017384 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 || fcp->returned);
17386}
17387
17388/*
17389 * return TRUE if cookie indicates a function which "abort"s on errors.
17390 */
17391 int
17392func_has_abort(cookie)
17393 void *cookie;
17394{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017395 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396}
17397
17398#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17399typedef enum
17400{
17401 VAR_FLAVOUR_DEFAULT,
17402 VAR_FLAVOUR_SESSION,
17403 VAR_FLAVOUR_VIMINFO
17404} var_flavour_T;
17405
17406static var_flavour_T var_flavour __ARGS((char_u *varname));
17407
17408 static var_flavour_T
17409var_flavour(varname)
17410 char_u *varname;
17411{
17412 char_u *p = varname;
17413
17414 if (ASCII_ISUPPER(*p))
17415 {
17416 while (*(++p))
17417 if (ASCII_ISLOWER(*p))
17418 return VAR_FLAVOUR_SESSION;
17419 return VAR_FLAVOUR_VIMINFO;
17420 }
17421 else
17422 return VAR_FLAVOUR_DEFAULT;
17423}
17424#endif
17425
17426#if defined(FEAT_VIMINFO) || defined(PROTO)
17427/*
17428 * Restore global vars that start with a capital from the viminfo file
17429 */
17430 int
17431read_viminfo_varlist(virp, writing)
17432 vir_T *virp;
17433 int writing;
17434{
17435 char_u *tab;
17436 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438
17439 if (!writing && (find_viminfo_parameter('!') != NULL))
17440 {
17441 tab = vim_strchr(virp->vir_line + 1, '\t');
17442 if (tab != NULL)
17443 {
17444 *tab++ = '\0'; /* isolate the variable name */
17445 if (*tab == 'S') /* string var */
17446 is_string = TRUE;
17447
17448 tab = vim_strchr(tab, '\t');
17449 if (tab != NULL)
17450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 if (is_string)
17452 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017453 tv.v_type = VAR_STRING;
17454 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 }
17457 else
17458 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017459 tv.v_type = VAR_NUMBER;
17460 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017462 set_var(virp->vir_line + 1, &tv, FALSE);
17463 if (is_string)
17464 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 }
17466 }
17467 }
17468
17469 return viminfo_readline(virp);
17470}
17471
17472/*
17473 * Write global vars that start with a capital to the viminfo file
17474 */
17475 void
17476write_viminfo_varlist(fp)
17477 FILE *fp;
17478{
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 hashitem_T *hi;
17480 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017481 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017482 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017483 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486
17487 if (find_viminfo_parameter('!') == NULL)
17488 return;
17489
17490 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017491
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 todo = globvarht.ht_used;
17493 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017495 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017497 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 this_var = HI2DI(hi);
17499 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017502 {
17503 case VAR_STRING: s = "STR"; break;
17504 case VAR_NUMBER: s = "NUM"; break;
17505 default: continue;
17506 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017508 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17509 if (p != NULL)
17510 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017511 vim_free(tofree);
17512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 }
17514 }
17515}
17516#endif
17517
17518#if defined(FEAT_SESSION) || defined(PROTO)
17519 int
17520store_session_globals(fd)
17521 FILE *fd;
17522{
Bram Moolenaar33570922005-01-25 22:26:29 +000017523 hashitem_T *hi;
17524 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017525 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 char_u *p, *t;
17527
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 todo = globvarht.ht_used;
17529 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017531 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017533 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 this_var = HI2DI(hi);
17535 if ((this_var->di_tv.v_type == VAR_NUMBER
17536 || this_var->di_tv.v_type == VAR_STRING)
17537 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017538 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017539 /* Escape special characters with a backslash. Turn a LF and
17540 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017541 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017542 (char_u *)"\\\"\n\r");
17543 if (p == NULL) /* out of memory */
17544 break;
17545 for (t = p; *t != NUL; ++t)
17546 if (*t == '\n')
17547 *t = 'n';
17548 else if (*t == '\r')
17549 *t = 'r';
17550 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 this_var->di_key,
17552 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17553 : ' ',
17554 p,
17555 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17556 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017557 || put_eol(fd) == FAIL)
17558 {
17559 vim_free(p);
17560 return FAIL;
17561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 vim_free(p);
17563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 }
17565 }
17566 return OK;
17567}
17568#endif
17569
17570#endif /* FEAT_EVAL */
17571
17572#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17573
17574
17575#ifdef WIN3264
17576/*
17577 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17578 */
17579static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17580static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17581static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17582
17583/*
17584 * Get the short pathname of a file.
17585 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17586 */
17587 static int
17588get_short_pathname(fnamep, bufp, fnamelen)
17589 char_u **fnamep;
17590 char_u **bufp;
17591 int *fnamelen;
17592{
17593 int l,len;
17594 char_u *newbuf;
17595
17596 len = *fnamelen;
17597
17598 l = GetShortPathName(*fnamep, *fnamep, len);
17599 if (l > len - 1)
17600 {
17601 /* If that doesn't work (not enough space), then save the string
17602 * and try again with a new buffer big enough
17603 */
17604 newbuf = vim_strnsave(*fnamep, l);
17605 if (newbuf == NULL)
17606 return 0;
17607
17608 vim_free(*bufp);
17609 *fnamep = *bufp = newbuf;
17610
17611 l = GetShortPathName(*fnamep,*fnamep,l+1);
17612
17613 /* Really should always succeed, as the buffer is big enough */
17614 }
17615
17616 *fnamelen = l;
17617 return 1;
17618}
17619
17620/*
17621 * Create a short path name. Returns the length of the buffer it needs.
17622 * Doesn't copy over the end of the buffer passed in.
17623 */
17624 static int
17625shortpath_for_invalid_fname(fname, bufp, fnamelen)
17626 char_u **fname;
17627 char_u **bufp;
17628 int *fnamelen;
17629{
17630 char_u *s, *p, *pbuf2, *pbuf3;
17631 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017632 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633
17634 /* Make a copy */
17635 len2 = *fnamelen;
17636 pbuf2 = vim_strnsave(*fname, len2);
17637 pbuf3 = NULL;
17638
17639 s = pbuf2 + len2 - 1; /* Find the end */
17640 slen = 1;
17641 plen = len2;
17642
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017643 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 {
17645 --s;
17646 ++slen;
17647 --plen;
17648 }
17649
17650 do
17651 {
17652 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017653 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 {
17655 --s;
17656 ++slen;
17657 --plen;
17658 }
17659 if (s <= pbuf2)
17660 break;
17661
17662 /* Remeber the character that is about to be blatted */
17663 ch = *s;
17664 *s = 0; /* get_short_pathname requires a null-terminated string */
17665
17666 /* Try it in situ */
17667 p = pbuf2;
17668 if (!get_short_pathname(&p, &pbuf3, &plen))
17669 {
17670 vim_free(pbuf2);
17671 return -1;
17672 }
17673 *s = ch; /* Preserve the string */
17674 } while (plen == 0);
17675
17676 if (plen > 0)
17677 {
17678 /* Remeber the length of the new string. */
17679 *fnamelen = len = plen + slen;
17680 vim_free(*bufp);
17681 if (len > len2)
17682 {
17683 /* If there's not enough space in the currently allocated string,
17684 * then copy it to a buffer big enough.
17685 */
17686 *fname= *bufp = vim_strnsave(p, len);
17687 if (*fname == NULL)
17688 return -1;
17689 }
17690 else
17691 {
17692 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17693 *fname = *bufp = pbuf2;
17694 if (p != pbuf2)
17695 strncpy(*fname, p, plen);
17696 pbuf2 = NULL;
17697 }
17698 /* Concat the next bit */
17699 strncpy(*fname + plen, s, slen);
17700 (*fname)[len] = '\0';
17701 }
17702 vim_free(pbuf3);
17703 vim_free(pbuf2);
17704 return 0;
17705}
17706
17707/*
17708 * Get a pathname for a partial path.
17709 */
17710 static int
17711shortpath_for_partial(fnamep, bufp, fnamelen)
17712 char_u **fnamep;
17713 char_u **bufp;
17714 int *fnamelen;
17715{
17716 int sepcount, len, tflen;
17717 char_u *p;
17718 char_u *pbuf, *tfname;
17719 int hasTilde;
17720
17721 /* Count up the path seperators from the RHS.. so we know which part
17722 * of the path to return.
17723 */
17724 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017725 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 if (vim_ispathsep(*p))
17727 ++sepcount;
17728
17729 /* Need full path first (use expand_env() to remove a "~/") */
17730 hasTilde = (**fnamep == '~');
17731 if (hasTilde)
17732 pbuf = tfname = expand_env_save(*fnamep);
17733 else
17734 pbuf = tfname = FullName_save(*fnamep, FALSE);
17735
17736 len = tflen = STRLEN(tfname);
17737
17738 if (!get_short_pathname(&tfname, &pbuf, &len))
17739 return -1;
17740
17741 if (len == 0)
17742 {
17743 /* Don't have a valid filename, so shorten the rest of the
17744 * path if we can. This CAN give us invalid 8.3 filenames, but
17745 * there's not a lot of point in guessing what it might be.
17746 */
17747 len = tflen;
17748 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17749 return -1;
17750 }
17751
17752 /* Count the paths backward to find the beginning of the desired string. */
17753 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017754 {
17755#ifdef FEAT_MBYTE
17756 if (has_mbyte)
17757 p -= mb_head_off(tfname, p);
17758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 if (vim_ispathsep(*p))
17760 {
17761 if (sepcount == 0 || (hasTilde && sepcount == 1))
17762 break;
17763 else
17764 sepcount --;
17765 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 if (hasTilde)
17768 {
17769 --p;
17770 if (p >= tfname)
17771 *p = '~';
17772 else
17773 return -1;
17774 }
17775 else
17776 ++p;
17777
17778 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17779 vim_free(*bufp);
17780 *fnamelen = (int)STRLEN(p);
17781 *bufp = pbuf;
17782 *fnamep = p;
17783
17784 return 0;
17785}
17786#endif /* WIN3264 */
17787
17788/*
17789 * Adjust a filename, according to a string of modifiers.
17790 * *fnamep must be NUL terminated when called. When returning, the length is
17791 * determined by *fnamelen.
17792 * Returns valid flags.
17793 * When there is an error, *fnamep is set to NULL.
17794 */
17795 int
17796modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17797 char_u *src; /* string with modifiers */
17798 int *usedlen; /* characters after src that are used */
17799 char_u **fnamep; /* file name so far */
17800 char_u **bufp; /* buffer for allocated file name or NULL */
17801 int *fnamelen; /* length of fnamep */
17802{
17803 int valid = 0;
17804 char_u *tail;
17805 char_u *s, *p, *pbuf;
17806 char_u dirname[MAXPATHL];
17807 int c;
17808 int has_fullname = 0;
17809#ifdef WIN3264
17810 int has_shortname = 0;
17811#endif
17812
17813repeat:
17814 /* ":p" - full path/file_name */
17815 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17816 {
17817 has_fullname = 1;
17818
17819 valid |= VALID_PATH;
17820 *usedlen += 2;
17821
17822 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17823 if ((*fnamep)[0] == '~'
17824#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17825 && ((*fnamep)[1] == '/'
17826# ifdef BACKSLASH_IN_FILENAME
17827 || (*fnamep)[1] == '\\'
17828# endif
17829 || (*fnamep)[1] == NUL)
17830
17831#endif
17832 )
17833 {
17834 *fnamep = expand_env_save(*fnamep);
17835 vim_free(*bufp); /* free any allocated file name */
17836 *bufp = *fnamep;
17837 if (*fnamep == NULL)
17838 return -1;
17839 }
17840
17841 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017842 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 {
17844 if (vim_ispathsep(*p)
17845 && p[1] == '.'
17846 && (p[2] == NUL
17847 || vim_ispathsep(p[2])
17848 || (p[2] == '.'
17849 && (p[3] == NUL || vim_ispathsep(p[3])))))
17850 break;
17851 }
17852
17853 /* FullName_save() is slow, don't use it when not needed. */
17854 if (*p != NUL || !vim_isAbsName(*fnamep))
17855 {
17856 *fnamep = FullName_save(*fnamep, *p != NUL);
17857 vim_free(*bufp); /* free any allocated file name */
17858 *bufp = *fnamep;
17859 if (*fnamep == NULL)
17860 return -1;
17861 }
17862
17863 /* Append a path separator to a directory. */
17864 if (mch_isdir(*fnamep))
17865 {
17866 /* Make room for one or two extra characters. */
17867 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17868 vim_free(*bufp); /* free any allocated file name */
17869 *bufp = *fnamep;
17870 if (*fnamep == NULL)
17871 return -1;
17872 add_pathsep(*fnamep);
17873 }
17874 }
17875
17876 /* ":." - path relative to the current directory */
17877 /* ":~" - path relative to the home directory */
17878 /* ":8" - shortname path - postponed till after */
17879 while (src[*usedlen] == ':'
17880 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17881 {
17882 *usedlen += 2;
17883 if (c == '8')
17884 {
17885#ifdef WIN3264
17886 has_shortname = 1; /* Postpone this. */
17887#endif
17888 continue;
17889 }
17890 pbuf = NULL;
17891 /* Need full path first (use expand_env() to remove a "~/") */
17892 if (!has_fullname)
17893 {
17894 if (c == '.' && **fnamep == '~')
17895 p = pbuf = expand_env_save(*fnamep);
17896 else
17897 p = pbuf = FullName_save(*fnamep, FALSE);
17898 }
17899 else
17900 p = *fnamep;
17901
17902 has_fullname = 0;
17903
17904 if (p != NULL)
17905 {
17906 if (c == '.')
17907 {
17908 mch_dirname(dirname, MAXPATHL);
17909 s = shorten_fname(p, dirname);
17910 if (s != NULL)
17911 {
17912 *fnamep = s;
17913 if (pbuf != NULL)
17914 {
17915 vim_free(*bufp); /* free any allocated file name */
17916 *bufp = pbuf;
17917 pbuf = NULL;
17918 }
17919 }
17920 }
17921 else
17922 {
17923 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17924 /* Only replace it when it starts with '~' */
17925 if (*dirname == '~')
17926 {
17927 s = vim_strsave(dirname);
17928 if (s != NULL)
17929 {
17930 *fnamep = s;
17931 vim_free(*bufp);
17932 *bufp = s;
17933 }
17934 }
17935 }
17936 vim_free(pbuf);
17937 }
17938 }
17939
17940 tail = gettail(*fnamep);
17941 *fnamelen = (int)STRLEN(*fnamep);
17942
17943 /* ":h" - head, remove "/file_name", can be repeated */
17944 /* Don't remove the first "/" or "c:\" */
17945 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17946 {
17947 valid |= VALID_HEAD;
17948 *usedlen += 2;
17949 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017950 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 --tail;
17952 *fnamelen = (int)(tail - *fnamep);
17953#ifdef VMS
17954 if (*fnamelen > 0)
17955 *fnamelen += 1; /* the path separator is part of the path */
17956#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017957 while (tail > s && !after_pathsep(s, tail))
17958 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 }
17960
17961 /* ":8" - shortname */
17962 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17963 {
17964 *usedlen += 2;
17965#ifdef WIN3264
17966 has_shortname = 1;
17967#endif
17968 }
17969
17970#ifdef WIN3264
17971 /* Check shortname after we have done 'heads' and before we do 'tails'
17972 */
17973 if (has_shortname)
17974 {
17975 pbuf = NULL;
17976 /* Copy the string if it is shortened by :h */
17977 if (*fnamelen < (int)STRLEN(*fnamep))
17978 {
17979 p = vim_strnsave(*fnamep, *fnamelen);
17980 if (p == 0)
17981 return -1;
17982 vim_free(*bufp);
17983 *bufp = *fnamep = p;
17984 }
17985
17986 /* Split into two implementations - makes it easier. First is where
17987 * there isn't a full name already, second is where there is.
17988 */
17989 if (!has_fullname && !vim_isAbsName(*fnamep))
17990 {
17991 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17992 return -1;
17993 }
17994 else
17995 {
17996 int l;
17997
17998 /* Simple case, already have the full-name
17999 * Nearly always shorter, so try first time. */
18000 l = *fnamelen;
18001 if (!get_short_pathname(fnamep, bufp, &l))
18002 return -1;
18003
18004 if (l == 0)
18005 {
18006 /* Couldn't find the filename.. search the paths.
18007 */
18008 l = *fnamelen;
18009 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18010 return -1;
18011 }
18012 *fnamelen = l;
18013 }
18014 }
18015#endif /* WIN3264 */
18016
18017 /* ":t" - tail, just the basename */
18018 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18019 {
18020 *usedlen += 2;
18021 *fnamelen -= (int)(tail - *fnamep);
18022 *fnamep = tail;
18023 }
18024
18025 /* ":e" - extension, can be repeated */
18026 /* ":r" - root, without extension, can be repeated */
18027 while (src[*usedlen] == ':'
18028 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18029 {
18030 /* find a '.' in the tail:
18031 * - for second :e: before the current fname
18032 * - otherwise: The last '.'
18033 */
18034 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18035 s = *fnamep - 2;
18036 else
18037 s = *fnamep + *fnamelen - 1;
18038 for ( ; s > tail; --s)
18039 if (s[0] == '.')
18040 break;
18041 if (src[*usedlen + 1] == 'e') /* :e */
18042 {
18043 if (s > tail)
18044 {
18045 *fnamelen += (int)(*fnamep - (s + 1));
18046 *fnamep = s + 1;
18047#ifdef VMS
18048 /* cut version from the extension */
18049 s = *fnamep + *fnamelen - 1;
18050 for ( ; s > *fnamep; --s)
18051 if (s[0] == ';')
18052 break;
18053 if (s > *fnamep)
18054 *fnamelen = s - *fnamep;
18055#endif
18056 }
18057 else if (*fnamep <= tail)
18058 *fnamelen = 0;
18059 }
18060 else /* :r */
18061 {
18062 if (s > tail) /* remove one extension */
18063 *fnamelen = (int)(s - *fnamep);
18064 }
18065 *usedlen += 2;
18066 }
18067
18068 /* ":s?pat?foo?" - substitute */
18069 /* ":gs?pat?foo?" - global substitute */
18070 if (src[*usedlen] == ':'
18071 && (src[*usedlen + 1] == 's'
18072 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18073 {
18074 char_u *str;
18075 char_u *pat;
18076 char_u *sub;
18077 int sep;
18078 char_u *flags;
18079 int didit = FALSE;
18080
18081 flags = (char_u *)"";
18082 s = src + *usedlen + 2;
18083 if (src[*usedlen + 1] == 'g')
18084 {
18085 flags = (char_u *)"g";
18086 ++s;
18087 }
18088
18089 sep = *s++;
18090 if (sep)
18091 {
18092 /* find end of pattern */
18093 p = vim_strchr(s, sep);
18094 if (p != NULL)
18095 {
18096 pat = vim_strnsave(s, (int)(p - s));
18097 if (pat != NULL)
18098 {
18099 s = p + 1;
18100 /* find end of substitution */
18101 p = vim_strchr(s, sep);
18102 if (p != NULL)
18103 {
18104 sub = vim_strnsave(s, (int)(p - s));
18105 str = vim_strnsave(*fnamep, *fnamelen);
18106 if (sub != NULL && str != NULL)
18107 {
18108 *usedlen = (int)(p + 1 - src);
18109 s = do_string_sub(str, pat, sub, flags);
18110 if (s != NULL)
18111 {
18112 *fnamep = s;
18113 *fnamelen = (int)STRLEN(s);
18114 vim_free(*bufp);
18115 *bufp = s;
18116 didit = TRUE;
18117 }
18118 }
18119 vim_free(sub);
18120 vim_free(str);
18121 }
18122 vim_free(pat);
18123 }
18124 }
18125 /* after using ":s", repeat all the modifiers */
18126 if (didit)
18127 goto repeat;
18128 }
18129 }
18130
18131 return valid;
18132}
18133
18134/*
18135 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18136 * "flags" can be "g" to do a global substitute.
18137 * Returns an allocated string, NULL for error.
18138 */
18139 char_u *
18140do_string_sub(str, pat, sub, flags)
18141 char_u *str;
18142 char_u *pat;
18143 char_u *sub;
18144 char_u *flags;
18145{
18146 int sublen;
18147 regmatch_T regmatch;
18148 int i;
18149 int do_all;
18150 char_u *tail;
18151 garray_T ga;
18152 char_u *ret;
18153 char_u *save_cpo;
18154
18155 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18156 save_cpo = p_cpo;
18157 p_cpo = (char_u *)"";
18158
18159 ga_init2(&ga, 1, 200);
18160
18161 do_all = (flags[0] == 'g');
18162
18163 regmatch.rm_ic = p_ic;
18164 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18165 if (regmatch.regprog != NULL)
18166 {
18167 tail = str;
18168 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18169 {
18170 /*
18171 * Get some space for a temporary buffer to do the substitution
18172 * into. It will contain:
18173 * - The text up to where the match is.
18174 * - The substituted text.
18175 * - The text after the match.
18176 */
18177 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18178 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18179 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18180 {
18181 ga_clear(&ga);
18182 break;
18183 }
18184
18185 /* copy the text up to where the match is */
18186 i = (int)(regmatch.startp[0] - tail);
18187 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18188 /* add the substituted text */
18189 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18190 + ga.ga_len + i, TRUE, TRUE, FALSE);
18191 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 /* avoid getting stuck on a match with an empty string */
18193 if (tail == regmatch.endp[0])
18194 {
18195 if (*tail == NUL)
18196 break;
18197 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18198 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 }
18200 else
18201 {
18202 tail = regmatch.endp[0];
18203 if (*tail == NUL)
18204 break;
18205 }
18206 if (!do_all)
18207 break;
18208 }
18209
18210 if (ga.ga_data != NULL)
18211 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18212
18213 vim_free(regmatch.regprog);
18214 }
18215
18216 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18217 ga_clear(&ga);
18218 p_cpo = save_cpo;
18219
18220 return ret;
18221}
18222
18223#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */