blob: 6b16cf9b6488b4904abd2e1c736a64d3235f6630 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000110
111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
123hashtab_T compat_hashtab;
124
125/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000126 * Array to hold the hashtab with variables local to each sourced script.
127 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000129typedef struct
130{
131 dictitem_T sv_var;
132 dict_T sv_dict;
133} scriptvar_T;
134
135static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
136#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
137#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139static int echo_attr = 0; /* attributes used for ":echo" */
140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000141/* Values for trans_function_name() argument: */
142#define TFN_INT 1 /* internal function name OK */
143#define TFN_QUIET 2 /* no error messages */
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
146 * Structure to hold info for a user function.
147 */
148typedef struct ufunc ufunc_T;
149
150struct ufunc
151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000152 int uf_varargs; /* variable nr of arguments */
153 int uf_flags;
154 int uf_calls; /* nr of active calls */
155 garray_T uf_args; /* arguments */
156 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000157#ifdef FEAT_PROFILE
158 int uf_profiling; /* TRUE when func is being profiled */
159 /* profiling the function as a whole */
160 int uf_tm_count; /* nr of calls */
161 proftime_T uf_tm_total; /* time spend in function + children */
162 proftime_T uf_tm_self; /* time spend in function itself */
163 proftime_T uf_tm_start; /* time at function call */
164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
167 proftime_T *uf_tml_total; /* time spend in a line + children */
168 proftime_T *uf_tml_self; /* time spend in a line itself */
169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
267#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
272 int vv_len; /* length of name */
273 dictitem_T vv_di; /* value and name for key */
274 char vv_filler[16]; /* space for LONGEST name below!!! */
275 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
276} vimvars[VV_LEN] =
277{
278 /*
279 * The order here must match the VV_ defines in vim.h!
280 * Initializing a union does not work, leave tv.vval empty to get zero's.
281 */
282 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
283 {VV_NAME("count1", VAR_NUMBER), VV_RO},
284 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
285 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
286 {VV_NAME("warningmsg", VAR_STRING), 0},
287 {VV_NAME("statusmsg", VAR_STRING), 0},
288 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
289 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
290 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
291 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
292 {VV_NAME("termresponse", VAR_STRING), VV_RO},
293 {VV_NAME("fname", VAR_STRING), VV_RO},
294 {VV_NAME("lang", VAR_STRING), VV_RO},
295 {VV_NAME("lc_time", VAR_STRING), VV_RO},
296 {VV_NAME("ctype", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
298 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
299 {VV_NAME("fname_in", VAR_STRING), VV_RO},
300 {VV_NAME("fname_out", VAR_STRING), VV_RO},
301 {VV_NAME("fname_new", VAR_STRING), VV_RO},
302 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
303 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
304 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
307 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
308 {VV_NAME("progname", VAR_STRING), VV_RO},
309 {VV_NAME("servername", VAR_STRING), VV_RO},
310 {VV_NAME("dying", VAR_NUMBER), VV_RO},
311 {VV_NAME("exception", VAR_STRING), VV_RO},
312 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
313 {VV_NAME("register", VAR_STRING), VV_RO},
314 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
315 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000316 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
317 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000318 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000319 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
320 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000321 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
322 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
323 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
324 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
325 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000326};
327
328/* shorthand */
329#define vv_type vv_di.di_tv.v_type
330#define vv_nr vv_di.di_tv.vval.v_number
331#define vv_str vv_di.di_tv.vval.v_string
332#define vv_tv vv_di.di_tv
333
334/*
335 * The v: variables are stored in dictionary "vimvardict".
336 * "vimvars_var" is the variable that is used for the "l:" scope.
337 */
338static dict_T vimvardict;
339static dictitem_T vimvars_var;
340#define vimvarht vimvardict.dv_hashtab
341
342static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
343static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
349static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000350static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000351static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355static list_T *list_alloc __ARGS((void));
356static void list_unref __ARGS((list_T *l));
357static void list_free __ARGS((list_T *l));
358static listitem_T *listitem_alloc __ARGS((void));
359static void listitem_free __ARGS((listitem_T *item));
360static void listitem_remove __ARGS((list_T *l, listitem_T *item));
361static long list_len __ARGS((list_T *l));
362static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
363static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
364static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
365static int string_isa_number __ARGS((char_u *s));
366static listitem_T *list_find __ARGS((list_T *l, long n));
367static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000368static void list_append __ARGS((list_T *l, listitem_T *item));
369static int list_append_tv __ARGS((list_T *l, typval_T *tv));
370static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
371static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
372static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000373static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000374static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
375static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000376static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar33570922005-01-25 22:26:29 +0000378static void dict_unref __ARGS((dict_T *d));
379static void dict_free __ARGS((dict_T *d));
380static dictitem_T *dictitem_alloc __ARGS((char_u *key));
381static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
382static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
383static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000384static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int dict_add __ARGS((dict_T *d, dictitem_T *item));
386static long dict_len __ARGS((dict_T *d));
387static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
388static char_u *dict2string __ARGS((typval_T *tv));
389static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390
391static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
392static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
393static char_u *string_quote __ARGS((char_u *str, int function));
394static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int find_internal_func __ARGS((char_u *name));
396static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
397static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
398static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000399static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400
401static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
425static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000464static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000492static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000509static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513#ifdef vim_mkdir
514static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000521static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000539static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
545#ifdef HAVE_STRFTIME
546static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
547#endif
548static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000560static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577
578static win_T *find_win_by_nr __ARGS((typval_T *vp));
579static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
580static int get_env_len __ARGS((char_u **arg));
581static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram 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 {
1881 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1882 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;
4095 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 if (**arg != NUL)
4098 ++*arg;
4099 break;
4100
4101 /*
4102 * nested expression: (expression).
4103 */
4104 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (**arg == ')')
4107 ++*arg;
4108 else if (ret == OK)
4109 {
4110 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 ret = FAIL;
4113 }
4114 break;
4115
Bram Moolenaar8c711452005-01-14 21:53:12 +00004116 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004119
4120 if (ret == NOTDONE)
4121 {
4122 /*
4123 * Must be a variable or function name.
4124 * Can also be a curly-braces kind of name: {expr}.
4125 */
4126 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004127 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004128 if (alias != NULL)
4129 s = alias;
4130
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004131 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004132 ret = FAIL;
4133 else
4134 {
4135 if (**arg == '(') /* recursive! */
4136 {
4137 /* If "s" is the name of a variable of type VAR_FUNC
4138 * use its contents. */
4139 s = deref_func_name(s, &len);
4140
4141 /* Invoke the function. */
4142 ret = get_func_tv(s, len, rettv, arg,
4143 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004144 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004145 /* Stop the expression evaluation when immediately
4146 * aborting on error, or when an interrupt occurred or
4147 * an exception was thrown but not caught. */
4148 if (aborting())
4149 {
4150 if (ret == OK)
4151 clear_tv(rettv);
4152 ret = FAIL;
4153 }
4154 }
4155 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004156 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004157 else
4158 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004159 }
4160
4161 if (alias != NULL)
4162 vim_free(alias);
4163 }
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 *arg = skipwhite(*arg);
4166
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004167 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4168 * expr(expr). */
4169 if (ret == OK)
4170 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 /*
4173 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4174 */
4175 if (ret == OK && evaluate && end_leader > start_leader)
4176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 while (end_leader > start_leader)
4179 {
4180 --end_leader;
4181 if (*end_leader == '!')
4182 val = !val;
4183 else if (*end_leader == '-')
4184 val = -val;
4185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
4187 rettv->v_type = VAR_NUMBER;
4188 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190
4191 return ret;
4192}
4193
4194/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195 * Evaluate an "[expr]" or "[expr:expr]" index.
4196 * "*arg" points to the '['.
4197 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4198 */
4199 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004200eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004201 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004202 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004203 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004204 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004205{
4206 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004207 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004209 long len = -1;
4210 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004212 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004216 if (verbose)
4217 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004218 return FAIL;
4219 }
4220
Bram Moolenaar8c711452005-01-14 21:53:12 +00004221 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004222 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004223 /*
4224 * dict.name
4225 */
4226 key = *arg + 1;
4227 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4228 ;
4229 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004230 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232 }
4233 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004235 /*
4236 * something[idx]
4237 *
4238 * Get the (first) variable from inside the [].
4239 */
4240 *arg = skipwhite(*arg + 1);
4241 if (**arg == ':')
4242 empty1 = TRUE;
4243 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4244 return FAIL;
4245
4246 /*
4247 * Get the second variable from inside the [:].
4248 */
4249 if (**arg == ':')
4250 {
4251 range = TRUE;
4252 *arg = skipwhite(*arg + 1);
4253 if (**arg == ']')
4254 empty2 = TRUE;
4255 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4256 {
4257 clear_tv(&var1);
4258 return FAIL;
4259 }
4260 }
4261
4262 /* Check for the ']'. */
4263 if (**arg != ']')
4264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004265 if (verbose)
4266 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 clear_tv(&var1);
4268 if (range)
4269 clear_tv(&var2);
4270 return FAIL;
4271 }
4272 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004273 }
4274
4275 if (evaluate)
4276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004277 n1 = 0;
4278 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 n1 = get_tv_number(&var1);
4281 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004282 }
4283 if (range)
4284 {
4285 if (empty2)
4286 n2 = -1;
4287 else
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 n2 = get_tv_number(&var2);
4290 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004291 }
4292 }
4293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 {
4296 case VAR_NUMBER:
4297 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004299 len = (long)STRLEN(s);
4300 if (range)
4301 {
4302 /* The resulting variable is a substring. If the indexes
4303 * are out of range the result is empty. */
4304 if (n1 < 0)
4305 {
4306 n1 = len + n1;
4307 if (n1 < 0)
4308 n1 = 0;
4309 }
4310 if (n2 < 0)
4311 n2 = len + n2;
4312 else if (n2 >= len)
4313 n2 = len;
4314 if (n1 >= len || n2 < 0 || n1 > n2)
4315 s = NULL;
4316 else
4317 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4318 }
4319 else
4320 {
4321 /* The resulting variable is a string of a single
4322 * character. If the index is too big or negative the
4323 * result is empty. */
4324 if (n1 >= len || n1 < 0)
4325 s = NULL;
4326 else
4327 s = vim_strnsave(s + n1, 1);
4328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(rettv);
4330 rettv->v_type = VAR_STRING;
4331 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004332 break;
4333
4334 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004336 if (n1 < 0)
4337 n1 = len + n1;
4338 if (!empty1 && (n1 < 0 || n1 >= len))
4339 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004340 if (verbose)
4341 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004342 return FAIL;
4343 }
4344 if (range)
4345 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004346 list_T *l;
4347 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004348
4349 if (n2 < 0)
4350 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004351 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004352 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004353 if (verbose)
4354 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004355 return FAIL;
4356 }
4357 l = list_alloc();
4358 if (l == NULL)
4359 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004361 n1 <= n2; ++n1)
4362 {
4363 if (list_append_tv(l, &item->li_tv) == FAIL)
4364 {
4365 list_free(l);
4366 return FAIL;
4367 }
4368 item = item->li_next;
4369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(rettv);
4371 rettv->v_type = VAR_LIST;
4372 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004373 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004374 }
4375 else
4376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004378 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 clear_tv(rettv);
4380 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004381 }
4382 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004383
4384 case VAR_DICT:
4385 if (range)
4386 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004387 if (verbose)
4388 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 if (len == -1)
4390 clear_tv(&var1);
4391 return FAIL;
4392 }
4393 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004395
4396 if (len == -1)
4397 {
4398 key = get_tv_string(&var1);
4399 if (*key == NUL)
4400 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004401 if (verbose)
4402 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004403 clear_tv(&var1);
4404 return FAIL;
4405 }
4406 }
4407
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004408 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004409
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004410 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004411 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 if (len == -1)
4413 clear_tv(&var1);
4414 if (item == NULL)
4415 return FAIL;
4416
4417 copy_tv(&item->di_tv, &var1);
4418 clear_tv(rettv);
4419 *rettv = var1;
4420 }
4421 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004422 }
4423 }
4424
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004425 return OK;
4426}
4427
4428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * Get an option value.
4430 * "arg" points to the '&' or '+' before the option name.
4431 * "arg" is advanced to character after the option name.
4432 * Return OK or FAIL.
4433 */
4434 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004437 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 int evaluate;
4439{
4440 char_u *option_end;
4441 long numval;
4442 char_u *stringval;
4443 int opt_type;
4444 int c;
4445 int working = (**arg == '+'); /* has("+option") */
4446 int ret = OK;
4447 int opt_flags;
4448
4449 /*
4450 * Isolate the option name and find its value.
4451 */
4452 option_end = find_option_end(arg, &opt_flags);
4453 if (option_end == NULL)
4454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 EMSG2(_("E112: Option name missing: %s"), *arg);
4457 return FAIL;
4458 }
4459
4460 if (!evaluate)
4461 {
4462 *arg = option_end;
4463 return OK;
4464 }
4465
4466 c = *option_end;
4467 *option_end = NUL;
4468 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 if (opt_type == -3) /* invalid name */
4472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 EMSG2(_("E113: Unknown option: %s"), *arg);
4475 ret = FAIL;
4476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 if (opt_type == -2) /* hidden string option */
4480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 rettv->v_type = VAR_STRING;
4482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484 else if (opt_type == -1) /* hidden number option */
4485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 rettv->v_type = VAR_NUMBER;
4487 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 else if (opt_type == 1) /* number option */
4490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004491 rettv->v_type = VAR_NUMBER;
4492 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 else /* string option */
4495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 rettv->v_type = VAR_STRING;
4497 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 }
4500 else if (working && (opt_type == -2 || opt_type == -1))
4501 ret = FAIL;
4502
4503 *option_end = c; /* put back for error messages */
4504 *arg = option_end;
4505
4506 return ret;
4507}
4508
4509/*
4510 * Allocate a variable for a string constant.
4511 * Return OK or FAIL.
4512 */
4513 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004514get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 int evaluate;
4518{
4519 char_u *p;
4520 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 int extra = 0;
4522
4523 /*
4524 * Find the end of the string, skipping backslashed characters.
4525 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004526 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
4528 if (*p == '\\' && p[1] != NUL)
4529 {
4530 ++p;
4531 /* A "\<x>" form occupies at least 4 characters, and produces up
4532 * to 6 characters: reserve space for 2 extra */
4533 if (*p == '<')
4534 extra += 2;
4535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537
4538 if (*p != '"')
4539 {
4540 EMSG2(_("E114: Missing quote: %s"), *arg);
4541 return FAIL;
4542 }
4543
4544 /* If only parsing, set *arg and return here */
4545 if (!evaluate)
4546 {
4547 *arg = p + 1;
4548 return OK;
4549 }
4550
4551 /*
4552 * Copy the string into allocated memory, handling backslashed
4553 * characters.
4554 */
4555 name = alloc((unsigned)(p - *arg + extra));
4556 if (name == NULL)
4557 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 rettv->v_type = VAR_STRING;
4559 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 if (*p == '\\')
4564 {
4565 switch (*++p)
4566 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004567 case 'b': *name++ = BS; ++p; break;
4568 case 'e': *name++ = ESC; ++p; break;
4569 case 'f': *name++ = FF; ++p; break;
4570 case 'n': *name++ = NL; ++p; break;
4571 case 'r': *name++ = CAR; ++p; break;
4572 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 case 'X': /* hex: "\x1", "\x12" */
4575 case 'x':
4576 case 'u': /* Unicode: "\u0023" */
4577 case 'U':
4578 if (vim_isxdigit(p[1]))
4579 {
4580 int n, nr;
4581 int c = toupper(*p);
4582
4583 if (c == 'X')
4584 n = 2;
4585 else
4586 n = 4;
4587 nr = 0;
4588 while (--n >= 0 && vim_isxdigit(p[1]))
4589 {
4590 ++p;
4591 nr = (nr << 4) + hex2nr(*p);
4592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004593 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#ifdef FEAT_MBYTE
4595 /* For "\u" store the number according to
4596 * 'encoding'. */
4597 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 else
4600#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 break;
4604
4605 /* octal: "\1", "\12", "\123" */
4606 case '0':
4607 case '1':
4608 case '2':
4609 case '3':
4610 case '4':
4611 case '5':
4612 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 case '7': *name = *p++ - '0';
4614 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 *name = (*name << 3) + *p++ - '0';
4617 if (*p >= '0' && *p <= '7')
4618 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 break;
4622
4623 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (extra != 0)
4626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 break;
4629 }
4630 /* FALLTHROUGH */
4631
Bram Moolenaar8c711452005-01-14 21:53:12 +00004632 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 break;
4634 }
4635 }
4636 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 *arg = p + 1;
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return OK;
4644}
4645
4646/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 * Return OK or FAIL.
4649 */
4650 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int evaluate;
4655{
4656 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004657 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658 int reduce = 0;
4659
4660 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004662 */
4663 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004668 break;
4669 ++reduce;
4670 ++p;
4671 }
4672 }
4673
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004677 return FAIL;
4678 }
4679
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004681 if (!evaluate)
4682 {
4683 *arg = p + 1;
4684 return OK;
4685 }
4686
4687 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004689 */
4690 str = alloc((unsigned)((p - *arg) - reduce));
4691 if (str == NULL)
4692 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 rettv->v_type = VAR_STRING;
4694 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004695
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004701 break;
4702 ++p;
4703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004707 *arg = p + 1;
4708
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004709 return OK;
4710}
4711
4712/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713 * Allocate a variable for a List and fill it from "*arg".
4714 * Return OK or FAIL.
4715 */
4716 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004717get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 int evaluate;
4721{
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 list_T *l = NULL;
4723 typval_T tv;
4724 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725
4726 if (evaluate)
4727 {
4728 l = list_alloc();
4729 if (l == NULL)
4730 return FAIL;
4731 }
4732
4733 *arg = skipwhite(*arg + 1);
4734 while (**arg != ']' && **arg != NUL)
4735 {
4736 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4737 goto failret;
4738 if (evaluate)
4739 {
4740 item = listitem_alloc();
4741 if (item != NULL)
4742 {
4743 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004744 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 list_append(l, item);
4746 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004747 else
4748 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 }
4750
4751 if (**arg == ']')
4752 break;
4753 if (**arg != ',')
4754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 goto failret;
4757 }
4758 *arg = skipwhite(*arg + 1);
4759 }
4760
4761 if (**arg != ']')
4762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764failret:
4765 if (evaluate)
4766 list_free(l);
4767 return FAIL;
4768 }
4769
4770 *arg = skipwhite(*arg + 1);
4771 if (evaluate)
4772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 rettv->v_type = VAR_LIST;
4774 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 ++l->lv_refcount;
4776 }
4777
4778 return OK;
4779}
4780
4781/*
4782 * Allocate an empty header for a list.
4783 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004784 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785list_alloc()
4786{
Bram Moolenaar33570922005-01-25 22:26:29 +00004787 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788}
4789
4790/*
4791 * Unreference a list: decrement the reference count and free it when it
4792 * becomes zero.
4793 */
4794 static void
4795list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797{
4798 if (l != NULL && --l->lv_refcount <= 0)
4799 list_free(l);
4800}
4801
4802/*
4803 * Free a list, including all items it points to.
4804 * Ignores the reference count.
4805 */
4806 static void
4807list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004808 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809{
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 listitem_T *item;
4811 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812
4813 for (item = l->lv_first; item != NULL; item = next)
4814 {
4815 next = item->li_next;
4816 listitem_free(item);
4817 }
4818 vim_free(l);
4819}
4820
4821/*
4822 * Allocate a list item.
4823 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004824 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825listitem_alloc()
4826{
Bram Moolenaar33570922005-01-25 22:26:29 +00004827 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828}
4829
4830/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004831 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004832 */
4833 static void
4834listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004835 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 vim_free(item);
4839}
4840
4841/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004842 * Remove a list item from a List and free it. Also clears the value.
4843 */
4844 static void
4845listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004846 list_T *l;
4847 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004848{
4849 list_remove(l, item, item);
4850 listitem_free(item);
4851}
4852
4853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 * Get the number of items in a list.
4855 */
4856 static long
4857list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004858 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 if (l == NULL)
4861 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004862 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863}
4864
4865/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004866 * Return TRUE when two lists have exactly the same values.
4867 */
4868 static int
4869list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004870 list_T *l1;
4871 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004872 int ic; /* ignore case for strings */
4873{
Bram Moolenaar33570922005-01-25 22:26:29 +00004874 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004875
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004876 if (list_len(l1) != list_len(l2))
4877 return FALSE;
4878
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004879 for (item1 = l1->lv_first, item2 = l2->lv_first;
4880 item1 != NULL && item2 != NULL;
4881 item1 = item1->li_next, item2 = item2->li_next)
4882 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4883 return FALSE;
4884 return item1 == NULL && item2 == NULL;
4885}
4886
4887/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004888 * Return TRUE when two dictionaries have exactly the same key/values.
4889 */
4890 static int
4891dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004892 dict_T *d1;
4893 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004894 int ic; /* ignore case for strings */
4895{
Bram Moolenaar33570922005-01-25 22:26:29 +00004896 hashitem_T *hi;
4897 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004898 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004899
4900 if (dict_len(d1) != dict_len(d2))
4901 return FALSE;
4902
Bram Moolenaar33570922005-01-25 22:26:29 +00004903 todo = d1->dv_hashtab.ht_used;
4904 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004906 if (!HASHITEM_EMPTY(hi))
4907 {
4908 item2 = dict_find(d2, hi->hi_key, -1);
4909 if (item2 == NULL)
4910 return FALSE;
4911 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4912 return FALSE;
4913 --todo;
4914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004915 }
4916 return TRUE;
4917}
4918
4919/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004920 * Return TRUE if "tv1" and "tv2" have the same value.
4921 * Compares the items just like "==" would compare them.
4922 */
4923 static int
4924tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004925 typval_T *tv1;
4926 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004927 int ic; /* ignore case */
4928{
4929 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4930
4931 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4932 {
4933 /* recursive! */
4934 if (tv1->v_type != tv2->v_type
4935 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4936 return FALSE;
4937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004938 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4939 {
4940 /* recursive! */
4941 if (tv1->v_type != tv2->v_type
4942 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4943 return FALSE;
4944 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004945 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4946 {
4947 if (tv1->v_type != tv2->v_type
4948 || tv1->vval.v_string == NULL
4949 || tv2->vval.v_string == NULL
4950 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4951 return FALSE;
4952 }
4953 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4954 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004955 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4956 * Don't consider "4x" to be equal to 4. */
4957 if ((tv1->v_type == VAR_STRING
4958 && !string_isa_number(tv1->vval.v_string))
4959 || (tv2->v_type == VAR_STRING
4960 && !string_isa_number(tv2->vval.v_string)))
4961 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004962 if (get_tv_number(tv1) != get_tv_number(tv2))
4963 return FALSE;
4964 }
4965 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4966 get_tv_string_buf(tv2, buf2)) != 0)
4967 return FALSE;
4968 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4969 get_tv_string_buf(tv2, buf2)) != 0)
4970 return FALSE;
4971 return TRUE;
4972}
4973
4974/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004975 * Return TRUE if "tv" is a number without other non-white characters.
4976 */
4977 static int
4978string_isa_number(s)
4979 char_u *s;
4980{
4981 int len;
4982
4983 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4984 return len > 0 && *skipwhite(s + len) == NUL;
4985}
4986
4987/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 * Locate item with index "n" in list "l" and return it.
4989 * A negative index is counted from the end; -1 is the last item.
4990 * Returns NULL when "n" is out of range.
4991 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004992 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004994 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 long n;
4996{
Bram Moolenaar33570922005-01-25 22:26:29 +00004997 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004998 long idx;
4999
5000 if (l == NULL)
5001 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005002
5003 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005004 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005005 n = l->lv_len + n;
5006
5007 /* Check for index out of range. */
5008 if (n < 0 || n >= l->lv_len)
5009 return NULL;
5010
5011 /* When there is a cached index may start search from there. */
5012 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005014 if (n < l->lv_idx / 2)
5015 {
5016 /* closest to the start of the list */
5017 item = l->lv_first;
5018 idx = 0;
5019 }
5020 else if (n > (l->lv_idx + l->lv_len) / 2)
5021 {
5022 /* closest to the end of the list */
5023 item = l->lv_last;
5024 idx = l->lv_len - 1;
5025 }
5026 else
5027 {
5028 /* closest to the cached index */
5029 item = l->lv_idx_item;
5030 idx = l->lv_idx;
5031 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005032 }
5033 else
5034 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005035 if (n < l->lv_len / 2)
5036 {
5037 /* closest to the start of the list */
5038 item = l->lv_first;
5039 idx = 0;
5040 }
5041 else
5042 {
5043 /* closest to the end of the list */
5044 item = l->lv_last;
5045 idx = l->lv_len - 1;
5046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005048
5049 while (n > idx)
5050 {
5051 /* search forward */
5052 item = item->li_next;
5053 ++idx;
5054 }
5055 while (n < idx)
5056 {
5057 /* search backward */
5058 item = item->li_prev;
5059 --idx;
5060 }
5061
5062 /* cache the used index */
5063 l->lv_idx = idx;
5064 l->lv_idx_item = item;
5065
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 return item;
5067}
5068
5069/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005070 * Locate "item" list "l" and return its index.
5071 * Returns -1 when "item" is not in the list.
5072 */
5073 static long
5074list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 list_T *l;
5076 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005077{
5078 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005079 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005080
5081 if (l == NULL)
5082 return -1;
5083 idx = 0;
5084 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5085 ++idx;
5086 if (li == NULL)
5087 return -1;
5088 return idx;;
5089}
5090
5091/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 * Append item "item" to the end of list "l".
5093 */
5094 static void
5095list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005096 list_T *l;
5097 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098{
5099 if (l->lv_last == NULL)
5100 {
5101 /* empty list */
5102 l->lv_first = item;
5103 l->lv_last = item;
5104 item->li_prev = NULL;
5105 }
5106 else
5107 {
5108 l->lv_last->li_next = item;
5109 item->li_prev = l->lv_last;
5110 l->lv_last = item;
5111 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005112 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 item->li_next = NULL;
5114}
5115
5116/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005117 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005118 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 */
5120 static int
5121list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005122 list_T *l;
5123 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005125 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126
Bram Moolenaar05159a02005-02-26 23:04:13 +00005127 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005129 copy_tv(tv, &li->li_tv);
5130 list_append(l, li);
5131 return OK;
5132}
5133
5134/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005135 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005136 * Return FAIL when out of memory.
5137 */
5138 int
5139list_append_dict(list, dict)
5140 list_T *list;
5141 dict_T *dict;
5142{
5143 listitem_T *li = listitem_alloc();
5144
5145 if (li == NULL)
5146 return FAIL;
5147 li->li_tv.v_type = VAR_DICT;
5148 li->li_tv.v_lock = 0;
5149 li->li_tv.vval.v_dict = dict;
5150 list_append(list, li);
5151 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152 return OK;
5153}
5154
5155/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005156 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005157 * If "item" is NULL append at the end.
5158 * Return FAIL when out of memory.
5159 */
5160 static int
5161list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005162 list_T *l;
5163 typval_T *tv;
5164 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005165{
Bram Moolenaar33570922005-01-25 22:26:29 +00005166 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005167
5168 if (ni == NULL)
5169 return FAIL;
5170 copy_tv(tv, &ni->li_tv);
5171 if (item == NULL)
5172 /* Append new item at end of list. */
5173 list_append(l, ni);
5174 else
5175 {
5176 /* Insert new item before existing item. */
5177 ni->li_prev = item->li_prev;
5178 ni->li_next = item;
5179 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005180 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005181 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005182 ++l->lv_idx;
5183 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005184 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005185 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005186 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005187 l->lv_idx_item = NULL;
5188 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005189 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005190 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005191 }
5192 return OK;
5193}
5194
5195/*
5196 * Extend "l1" with "l2".
5197 * If "bef" is NULL append at the end, otherwise insert before this item.
5198 * Returns FAIL when out of memory.
5199 */
5200 static int
5201list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005202 list_T *l1;
5203 list_T *l2;
5204 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005205{
Bram Moolenaar33570922005-01-25 22:26:29 +00005206 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005207
5208 for (item = l2->lv_first; item != NULL; item = item->li_next)
5209 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5210 return FAIL;
5211 return OK;
5212}
5213
5214/*
5215 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5216 * Return FAIL when out of memory.
5217 */
5218 static int
5219list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005220 list_T *l1;
5221 list_T *l2;
5222 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005223{
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005225
5226 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005227 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005228 if (l == NULL)
5229 return FAIL;
5230 tv->v_type = VAR_LIST;
5231 tv->vval.v_list = l;
5232
5233 /* append all items from the second list */
5234 return list_extend(l, l2, NULL);
5235}
5236
5237/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005240 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 * Returns NULL when out of memory.
5242 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005243 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005244list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005245 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005247 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248{
Bram Moolenaar33570922005-01-25 22:26:29 +00005249 list_T *copy;
5250 listitem_T *item;
5251 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252
5253 if (orig == NULL)
5254 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255
5256 copy = list_alloc();
5257 if (copy != NULL)
5258 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005259 if (copyID != 0)
5260 {
5261 /* Do this before adding the items, because one of the items may
5262 * refer back to this list. */
5263 orig->lv_copyID = copyID;
5264 orig->lv_copylist = copy;
5265 }
5266 for (item = orig->lv_first; item != NULL && !got_int;
5267 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 {
5269 ni = listitem_alloc();
5270 if (ni == NULL)
5271 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005273 {
5274 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5275 {
5276 vim_free(ni);
5277 break;
5278 }
5279 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005281 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 list_append(copy, ni);
5283 }
5284 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005285 if (item != NULL)
5286 {
5287 list_unref(copy);
5288 copy = NULL;
5289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 }
5291
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 return copy;
5293}
5294
5295/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005296 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005297 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005299 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005300list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005301 list_T *l;
5302 listitem_T *item;
5303 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005304{
Bram Moolenaar33570922005-01-25 22:26:29 +00005305 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005307 /* notify watchers */
5308 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005310 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005311 list_fix_watch(l, ip);
5312 if (ip == item2)
5313 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005315
5316 if (item2->li_next == NULL)
5317 l->lv_last = item->li_prev;
5318 else
5319 item2->li_next->li_prev = item->li_prev;
5320 if (item->li_prev == NULL)
5321 l->lv_first = item2->li_next;
5322 else
5323 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005324 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325}
5326
5327/*
5328 * Return an allocated string with the string representation of a list.
5329 * May return NULL.
5330 */
5331 static char_u *
5332list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005333 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334{
5335 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336
5337 if (tv->vval.v_list == NULL)
5338 return NULL;
5339 ga_init2(&ga, (int)sizeof(char), 80);
5340 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005341 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5342 {
5343 vim_free(ga.ga_data);
5344 return NULL;
5345 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 ga_append(&ga, ']');
5347 ga_append(&ga, NUL);
5348 return (char_u *)ga.ga_data;
5349}
5350
5351/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005352 * Join list "l" into a string in "*gap", using separator "sep".
5353 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005354 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005355 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005356 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005357list_join(gap, l, sep, echo)
5358 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005360 char_u *sep;
5361 int echo;
5362{
5363 int first = TRUE;
5364 char_u *tofree;
5365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005366 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005367 char_u *s;
5368
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005369 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005370 {
5371 if (first)
5372 first = FALSE;
5373 else
5374 ga_concat(gap, sep);
5375
5376 if (echo)
5377 s = echo_string(&item->li_tv, &tofree, numbuf);
5378 else
5379 s = tv2string(&item->li_tv, &tofree, numbuf);
5380 if (s != NULL)
5381 ga_concat(gap, s);
5382 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005383 if (s == NULL)
5384 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005385 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005386 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005387}
5388
5389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 * Allocate an empty header for a dictionary.
5391 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005392 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393dict_alloc()
5394{
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005396
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005398 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005399 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005400 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005401 d->dv_lock = 0;
5402 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005403 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005404 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005405 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406}
5407
5408/*
5409 * Unreference a Dictionary: decrement the reference count and free it when it
5410 * becomes zero.
5411 */
5412 static void
5413dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005414 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415{
5416 if (d != NULL && --d->dv_refcount <= 0)
5417 dict_free(d);
5418}
5419
5420/*
5421 * Free a Dictionary, including all items it contains.
5422 * Ignores the reference count.
5423 */
5424 static void
5425dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005428 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005431 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 * hashtab. Must not try to resize the hashtab! */
5433 todo = d->dv_hashtab.ht_used;
5434 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005436 if (!HASHITEM_EMPTY(hi))
5437 {
5438 dictitem_free(HI2DI(hi));
5439 --todo;
5440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 vim_free(d);
5444}
5445
5446/*
5447 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005448 * The "key" is copied to the new item.
5449 * Note that the value of the item "di_tv" still needs to be initialized!
5450 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005451 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005453dictitem_alloc(key)
5454 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455{
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005457
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005459 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005461 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005462 di->di_flags = 0;
5463 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005464 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465}
5466
5467/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005468 * Make a copy of a Dictionary item.
5469 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005470 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005471dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005472 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005473{
Bram Moolenaar33570922005-01-25 22:26:29 +00005474 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005475
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005477 if (di != NULL)
5478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005479 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005480 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005481 copy_tv(&org->di_tv, &di->di_tv);
5482 }
5483 return di;
5484}
5485
5486/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 * Remove item "item" from Dictionary "dict" and free it.
5488 */
5489 static void
5490dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 dict_T *dict;
5492 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005493{
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005495
Bram Moolenaar33570922005-01-25 22:26:29 +00005496 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005497 if (HASHITEM_EMPTY(hi))
5498 EMSG2(_(e_intern2), "dictitem_remove()");
5499 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005501 dictitem_free(item);
5502}
5503
5504/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005505 * Free a dict item. Also clears the value.
5506 */
5507 static void
5508dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 clear_tv(&item->di_tv);
5512 vim_free(item);
5513}
5514
5515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005516 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5517 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005518 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005519 * Returns NULL when out of memory.
5520 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005521 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005522dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005524 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005525 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005526{
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 dict_T *copy;
5528 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005529 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005531
5532 if (orig == NULL)
5533 return NULL;
5534
5535 copy = dict_alloc();
5536 if (copy != NULL)
5537 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005538 if (copyID != 0)
5539 {
5540 orig->dv_copyID = copyID;
5541 orig->dv_copydict = copy;
5542 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005544 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005545 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005546 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005547 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005548 --todo;
5549
5550 di = dictitem_alloc(hi->hi_key);
5551 if (di == NULL)
5552 break;
5553 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005554 {
5555 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5556 copyID) == FAIL)
5557 {
5558 vim_free(di);
5559 break;
5560 }
5561 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005562 else
5563 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5564 if (dict_add(copy, di) == FAIL)
5565 {
5566 dictitem_free(di);
5567 break;
5568 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005569 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005570 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005571
Bram Moolenaare9a41262005-01-15 22:18:47 +00005572 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005573 if (todo > 0)
5574 {
5575 dict_unref(copy);
5576 copy = NULL;
5577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005578 }
5579
5580 return copy;
5581}
5582
5583/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005585 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005587 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 dict_T *d;
5590 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591{
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593}
5594
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005596 * Add a number or string entry to dictionary "d".
5597 * When "str" is NULL use number "nr", otherwise use "str".
5598 * Returns FAIL when out of memory and when key already exists.
5599 */
5600 int
5601dict_add_nr_str(d, key, nr, str)
5602 dict_T *d;
5603 char *key;
5604 long nr;
5605 char_u *str;
5606{
5607 dictitem_T *item;
5608
5609 item = dictitem_alloc((char_u *)key);
5610 if (item == NULL)
5611 return FAIL;
5612 item->di_tv.v_lock = 0;
5613 if (str == NULL)
5614 {
5615 item->di_tv.v_type = VAR_NUMBER;
5616 item->di_tv.vval.v_number = nr;
5617 }
5618 else
5619 {
5620 item->di_tv.v_type = VAR_STRING;
5621 item->di_tv.vval.v_string = vim_strsave(str);
5622 }
5623 if (dict_add(d, item) == FAIL)
5624 {
5625 dictitem_free(item);
5626 return FAIL;
5627 }
5628 return OK;
5629}
5630
5631/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005632 * Get the number of items in a Dictionary.
5633 */
5634 static long
5635dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005637{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638 if (d == NULL)
5639 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005640 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005641}
5642
5643/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 * Find item "key[len]" in Dictionary "d".
5645 * If "len" is negative use strlen(key).
5646 * Returns NULL when not found.
5647 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005649dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005650 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 char_u *key;
5652 int len;
5653{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005654#define AKEYLEN 200
5655 char_u buf[AKEYLEN];
5656 char_u *akey;
5657 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005658 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005660 if (len < 0)
5661 akey = key;
5662 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005664 tofree = akey = vim_strnsave(key, len);
5665 if (akey == NULL)
5666 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005667 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005668 else
5669 {
5670 /* Avoid a malloc/free by using buf[]. */
5671 STRNCPY(buf, key, len);
5672 buf[len] = NUL;
5673 akey = buf;
5674 }
5675
Bram Moolenaar33570922005-01-25 22:26:29 +00005676 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005677 vim_free(tofree);
5678 if (HASHITEM_EMPTY(hi))
5679 return NULL;
5680 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681}
5682
5683/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005684 * Get a string item from a dictionary in allocated memory.
5685 * Returns NULL if the entry doesn't exist or out of memory.
5686 */
5687 char_u *
5688get_dict_string(d, key)
5689 dict_T *d;
5690 char_u *key;
5691{
5692 dictitem_T *di;
5693
5694 di = dict_find(d, key, -1);
5695 if (di == NULL)
5696 return NULL;
5697 return vim_strsave(get_tv_string(&di->di_tv));
5698}
5699
5700/*
5701 * Get a number item from a dictionary.
5702 * Returns 0 if the entry doesn't exist or out of memory.
5703 */
5704 long
5705get_dict_number(d, key)
5706 dict_T *d;
5707 char_u *key;
5708{
5709 dictitem_T *di;
5710
5711 di = dict_find(d, key, -1);
5712 if (di == NULL)
5713 return 0;
5714 return get_tv_number(&di->di_tv);
5715}
5716
5717/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 * Return an allocated string with the string representation of a Dictionary.
5719 * May return NULL.
5720 */
5721 static char_u *
5722dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724{
5725 garray_T ga;
5726 int first = TRUE;
5727 char_u *tofree;
5728 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005732 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005734 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 return NULL;
5736 ga_init2(&ga, (int)sizeof(char), 80);
5737 ga_append(&ga, '{');
5738
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005740 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005742 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005744 --todo;
5745
5746 if (first)
5747 first = FALSE;
5748 else
5749 ga_concat(&ga, (char_u *)", ");
5750
5751 tofree = string_quote(hi->hi_key, FALSE);
5752 if (tofree != NULL)
5753 {
5754 ga_concat(&ga, tofree);
5755 vim_free(tofree);
5756 }
5757 ga_concat(&ga, (char_u *)": ");
5758 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5759 if (s != NULL)
5760 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005762 if (s == NULL)
5763 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005766 if (todo > 0)
5767 {
5768 vim_free(ga.ga_data);
5769 return NULL;
5770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771
5772 ga_append(&ga, '}');
5773 ga_append(&ga, NUL);
5774 return (char_u *)ga.ga_data;
5775}
5776
5777/*
5778 * Allocate a variable for a Dictionary and fill it from "*arg".
5779 * Return OK or FAIL. Returns NOTDONE for {expr}.
5780 */
5781 static int
5782get_dict_tv(arg, rettv, evaluate)
5783 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005784 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 int evaluate;
5786{
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 dict_T *d = NULL;
5788 typval_T tvkey;
5789 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005793 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794
5795 /*
5796 * First check if it's not a curly-braces thing: {expr}.
5797 * Must do this without evaluating, otherwise a function may be called
5798 * twice. Unfortunately this means we need to call eval1() twice for the
5799 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005800 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005802 if (*start != '}')
5803 {
5804 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5805 return FAIL;
5806 if (*start == '}')
5807 return NOTDONE;
5808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809
5810 if (evaluate)
5811 {
5812 d = dict_alloc();
5813 if (d == NULL)
5814 return FAIL;
5815 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005816 tvkey.v_type = VAR_UNKNOWN;
5817 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818
5819 *arg = skipwhite(*arg + 1);
5820 while (**arg != '}' && **arg != NUL)
5821 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005822 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 goto failret;
5824 if (**arg != ':')
5825 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005826 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005827 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 goto failret;
5829 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005830 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 if (*key == NUL)
5832 {
5833 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005834 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 goto failret;
5836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837
5838 *arg = skipwhite(*arg + 1);
5839 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5840 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005841 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 goto failret;
5843 }
5844 if (evaluate)
5845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005846 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 if (item != NULL)
5848 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00005849 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005850 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 clear_tv(&tv);
5852 goto failret;
5853 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005854 item = dictitem_alloc(key);
5855 clear_tv(&tvkey);
5856 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005859 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005860 if (dict_add(d, item) == FAIL)
5861 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 }
5863 }
5864
5865 if (**arg == '}')
5866 break;
5867 if (**arg != ',')
5868 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005869 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 goto failret;
5871 }
5872 *arg = skipwhite(*arg + 1);
5873 }
5874
5875 if (**arg != '}')
5876 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005877 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878failret:
5879 if (evaluate)
5880 dict_free(d);
5881 return FAIL;
5882 }
5883
5884 *arg = skipwhite(*arg + 1);
5885 if (evaluate)
5886 {
5887 rettv->v_type = VAR_DICT;
5888 rettv->vval.v_dict = d;
5889 ++d->dv_refcount;
5890 }
5891
5892 return OK;
5893}
5894
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 * Return a string with the string representation of a variable.
5897 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 * May return NULL;
5901 */
5902 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005908 static int recurse = 0;
5909 char_u *r = NULL;
5910
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005913 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005914 *tofree = NULL;
5915 return NULL;
5916 }
5917 ++recurse;
5918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 switch (tv->v_type)
5920 {
5921 case VAR_FUNC:
5922 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005923 r = tv->vval.v_string;
5924 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 case VAR_LIST:
5926 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005927 r = *tofree;
5928 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 case VAR_DICT:
5930 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931 r = *tofree;
5932 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005933 case VAR_STRING:
5934 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935 *tofree = NULL;
5936 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005938 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005939 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005940 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005942
5943 --recurse;
5944 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945}
5946
5947/*
5948 * Return a string with the string representation of a variable.
5949 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5950 * "numbuf" is used for a number.
5951 * Puts quotes around strings, so that they can be parsed back by eval().
5952 * May return NULL;
5953 */
5954 static char_u *
5955tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005957 char_u **tofree;
5958 char_u *numbuf;
5959{
5960 switch (tv->v_type)
5961 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962 case VAR_FUNC:
5963 *tofree = string_quote(tv->vval.v_string, TRUE);
5964 return *tofree;
5965 case VAR_STRING:
5966 *tofree = string_quote(tv->vval.v_string, FALSE);
5967 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005970 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005973 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005975 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976}
5977
5978/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 * Return string "str" in ' quotes, doubling ' characters.
5980 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982 */
5983 static char_u *
5984string_quote(str, function)
5985 char_u *str;
5986 int function;
5987{
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 char_u *p, *r, *s;
5990
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 len = (function ? 13 : 3);
5992 if (str != NULL)
5993 {
5994 len += STRLEN(str);
5995 for (p = str; *p != NUL; mb_ptr_adv(p))
5996 if (*p == '\'')
5997 ++len;
5998 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005999 s = r = alloc(len);
6000 if (r != NULL)
6001 {
6002 if (function)
6003 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005 r += 10;
6006 }
6007 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 if (str != NULL)
6010 for (p = str; *p != NUL; )
6011 {
6012 if (*p == '\'')
6013 *r++ = '\'';
6014 MB_COPY_CHAR(p, r);
6015 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006016 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 if (function)
6018 *r++ = ')';
6019 *r++ = NUL;
6020 }
6021 return s;
6022}
6023
6024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 * Get the value of an environment variable.
6026 * "arg" is pointing to the '$'. It is advanced to after the name.
6027 * If the environment variable was not set, silently assume it is empty.
6028 * Always return OK.
6029 */
6030 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006031get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 int evaluate;
6035{
6036 char_u *string = NULL;
6037 int len;
6038 int cc;
6039 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006040 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
6042 ++*arg;
6043 name = *arg;
6044 len = get_env_len(arg);
6045 if (evaluate)
6046 {
6047 if (len != 0)
6048 {
6049 cc = name[len];
6050 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006051 /* first try vim_getenv(), fast for normal environment vars */
6052 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006054 {
6055 if (!mustfree)
6056 string = vim_strsave(string);
6057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 else
6059 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006060 if (mustfree)
6061 vim_free(string);
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 /* next try expanding things like $VIM and ${HOME} */
6064 string = expand_env_save(name - 1);
6065 if (string != NULL && *string == '$')
6066 {
6067 vim_free(string);
6068 string = NULL;
6069 }
6070 }
6071 name[len] = cc;
6072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006073 rettv->v_type = VAR_STRING;
6074 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076
6077 return OK;
6078}
6079
6080/*
6081 * Array with names and number of arguments of all internal functions
6082 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6083 */
6084static struct fst
6085{
6086 char *f_name; /* function name */
6087 char f_min_argc; /* minimal number of arguments */
6088 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091} functions[] =
6092{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006093 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {"append", 2, 2, f_append},
6095 {"argc", 0, 0, f_argc},
6096 {"argidx", 0, 0, f_argidx},
6097 {"argv", 1, 1, f_argv},
6098 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006099 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {"bufexists", 1, 1, f_bufexists},
6101 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6102 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6103 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6104 {"buflisted", 1, 1, f_buflisted},
6105 {"bufloaded", 1, 1, f_bufloaded},
6106 {"bufname", 1, 1, f_bufname},
6107 {"bufnr", 1, 1, f_bufnr},
6108 {"bufwinnr", 1, 1, f_bufwinnr},
6109 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006110 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006111 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {"char2nr", 1, 1, f_char2nr},
6113 {"cindent", 1, 1, f_cindent},
6114 {"col", 1, 1, f_col},
6115 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006117 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {"cscope_connection",0,3, f_cscope_connection},
6119 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006120 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {"delete", 1, 1, f_delete},
6122 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006123 {"diff_filler", 1, 1, f_diff_filler},
6124 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006125 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006127 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {"eventhandler", 0, 0, f_eventhandler},
6129 {"executable", 1, 1, f_executable},
6130 {"exists", 1, 1, f_exists},
6131 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6134 {"filereadable", 1, 1, f_filereadable},
6135 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006136 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006137 {"finddir", 1, 3, f_finddir},
6138 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {"fnamemodify", 2, 2, f_fnamemodify},
6140 {"foldclosed", 1, 1, f_foldclosed},
6141 {"foldclosedend", 1, 1, f_foldclosedend},
6142 {"foldlevel", 1, 1, f_foldlevel},
6143 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006144 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006147 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {"getbufvar", 2, 2, f_getbufvar},
6149 {"getchar", 0, 1, f_getchar},
6150 {"getcharmod", 0, 0, f_getcharmod},
6151 {"getcmdline", 0, 0, f_getcmdline},
6152 {"getcmdpos", 0, 0, f_getcmdpos},
6153 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006154 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006155 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {"getfsize", 1, 1, f_getfsize},
6157 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006158 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006159 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006160 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {"getreg", 0, 1, f_getreg},
6162 {"getregtype", 0, 1, f_getregtype},
6163 {"getwinposx", 0, 0, f_getwinposx},
6164 {"getwinposy", 0, 0, f_getwinposy},
6165 {"getwinvar", 2, 2, f_getwinvar},
6166 {"glob", 1, 1, f_glob},
6167 {"globpath", 2, 2, f_globpath},
6168 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006169 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {"hasmapto", 1, 2, f_hasmapto},
6171 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6172 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6173 {"histadd", 2, 2, f_histadd},
6174 {"histdel", 1, 2, f_histdel},
6175 {"histget", 1, 2, f_histget},
6176 {"histnr", 1, 1, f_histnr},
6177 {"hlID", 1, 1, f_hlID},
6178 {"hlexists", 1, 1, f_hlexists},
6179 {"hostname", 0, 0, f_hostname},
6180 {"iconv", 3, 3, f_iconv},
6181 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006182 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {"input", 1, 2, f_input},
6184 {"inputdialog", 1, 3, f_inputdialog},
6185 {"inputrestore", 0, 0, f_inputrestore},
6186 {"inputsave", 0, 0, f_inputsave},
6187 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006190 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006191 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006192 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006193 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {"libcall", 3, 3, f_libcall},
6197 {"libcallnr", 3, 3, f_libcallnr},
6198 {"line", 1, 1, f_line},
6199 {"line2byte", 1, 1, f_line2byte},
6200 {"lispindent", 1, 1, f_lispindent},
6201 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006202 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {"maparg", 1, 2, f_maparg},
6204 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006205 {"match", 2, 4, f_match},
6206 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006207 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006208 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006209 {"max", 1, 1, f_max},
6210 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006211#ifdef vim_mkdir
6212 {"mkdir", 1, 3, f_mkdir},
6213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {"mode", 0, 0, f_mode},
6215 {"nextnonblank", 1, 1, f_nextnonblank},
6216 {"nr2char", 1, 1, f_nr2char},
6217 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006218 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006219 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 {"remote_expr", 2, 3, f_remote_expr},
6221 {"remote_foreground", 1, 1, f_remote_foreground},
6222 {"remote_peek", 1, 2, f_remote_peek},
6223 {"remote_read", 1, 1, f_remote_read},
6224 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006227 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006229 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {"search", 1, 2, f_search},
6231 {"searchpair", 3, 5, f_searchpair},
6232 {"server2client", 2, 2, f_server2client},
6233 {"serverlist", 0, 0, f_serverlist},
6234 {"setbufvar", 3, 3, f_setbufvar},
6235 {"setcmdpos", 1, 1, f_setcmdpos},
6236 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006237 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {"setreg", 2, 3, f_setreg},
6239 {"setwinvar", 3, 3, f_setwinvar},
6240 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006241 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006242 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243#ifdef HAVE_STRFTIME
6244 {"strftime", 1, 2, f_strftime},
6245#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006246 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {"strlen", 1, 1, f_strlen},
6249 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006250 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {"strtrans", 1, 1, f_strtrans},
6252 {"submatch", 1, 1, f_submatch},
6253 {"substitute", 4, 4, f_substitute},
6254 {"synID", 3, 3, f_synID},
6255 {"synIDattr", 2, 3, f_synIDattr},
6256 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006257 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006258 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {"tempname", 0, 0, f_tempname},
6260 {"tolower", 1, 1, f_tolower},
6261 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006262 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {"virtcol", 1, 1, f_virtcol},
6266 {"visualmode", 0, 1, f_visualmode},
6267 {"winbufnr", 1, 1, f_winbufnr},
6268 {"wincol", 0, 0, f_wincol},
6269 {"winheight", 1, 1, f_winheight},
6270 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006271 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {"winrestcmd", 0, 0, f_winrestcmd},
6273 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006274 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275};
6276
6277#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6278
6279/*
6280 * Function given to ExpandGeneric() to obtain the list of internal
6281 * or user defined function names.
6282 */
6283 char_u *
6284get_function_name(xp, idx)
6285 expand_T *xp;
6286 int idx;
6287{
6288 static int intidx = -1;
6289 char_u *name;
6290
6291 if (idx == 0)
6292 intidx = -1;
6293 if (intidx < 0)
6294 {
6295 name = get_user_func_name(xp, idx);
6296 if (name != NULL)
6297 return name;
6298 }
6299 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6300 {
6301 STRCPY(IObuff, functions[intidx].f_name);
6302 STRCAT(IObuff, "(");
6303 if (functions[intidx].f_max_argc == 0)
6304 STRCAT(IObuff, ")");
6305 return IObuff;
6306 }
6307
6308 return NULL;
6309}
6310
6311/*
6312 * Function given to ExpandGeneric() to obtain the list of internal or
6313 * user defined variable or function names.
6314 */
6315/*ARGSUSED*/
6316 char_u *
6317get_expr_name(xp, idx)
6318 expand_T *xp;
6319 int idx;
6320{
6321 static int intidx = -1;
6322 char_u *name;
6323
6324 if (idx == 0)
6325 intidx = -1;
6326 if (intidx < 0)
6327 {
6328 name = get_function_name(xp, idx);
6329 if (name != NULL)
6330 return name;
6331 }
6332 return get_user_var_name(xp, ++intidx);
6333}
6334
6335#endif /* FEAT_CMDL_COMPL */
6336
6337/*
6338 * Find internal function in table above.
6339 * Return index, or -1 if not found
6340 */
6341 static int
6342find_internal_func(name)
6343 char_u *name; /* name of the function */
6344{
6345 int first = 0;
6346 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6347 int cmp;
6348 int x;
6349
6350 /*
6351 * Find the function name in the table. Binary search.
6352 */
6353 while (first <= last)
6354 {
6355 x = first + ((unsigned)(last - first) >> 1);
6356 cmp = STRCMP(name, functions[x].f_name);
6357 if (cmp < 0)
6358 last = x - 1;
6359 else if (cmp > 0)
6360 first = x + 1;
6361 else
6362 return x;
6363 }
6364 return -1;
6365}
6366
6367/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6369 * name it contains, otherwise return "name".
6370 */
6371 static char_u *
6372deref_func_name(name, lenp)
6373 char_u *name;
6374 int *lenp;
6375{
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 int cc;
6378
6379 cc = name[*lenp];
6380 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006381 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 {
6387 *lenp = 0;
6388 return (char_u *)""; /* just in case */
6389 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 *lenp = STRLEN(v->di_tv.vval.v_string);
6391 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 }
6393
6394 return name;
6395}
6396
6397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * Allocate a variable for the result of a function.
6399 * Return OK or FAIL.
6400 */
6401 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006402get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6403 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 char_u *name; /* name of the function */
6405 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 char_u **arg; /* argument, pointing to the '(' */
6408 linenr_T firstline; /* first line of range */
6409 linenr_T lastline; /* last line of range */
6410 int *doesrange; /* return: function handled range */
6411 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 char_u *argp;
6415 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 int argcount = 0; /* number of arguments found */
6418
6419 /*
6420 * Get the arguments.
6421 */
6422 argp = *arg;
6423 while (argcount < MAX_FUNC_ARGS)
6424 {
6425 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6426 if (*argp == ')' || *argp == ',' || *argp == NUL)
6427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6429 {
6430 ret = FAIL;
6431 break;
6432 }
6433 ++argcount;
6434 if (*argp != ',')
6435 break;
6436 }
6437 if (*argp == ')')
6438 ++argp;
6439 else
6440 ret = FAIL;
6441
6442 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006443 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006444 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 {
6447 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006448 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006450 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006454 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456 *arg = skipwhite(argp);
6457 return ret;
6458}
6459
6460
6461/*
6462 * Call a function with its resolved parameters
6463 * Return OK or FAIL.
6464 */
6465 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006466call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006467 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 char_u *name; /* name of the function */
6469 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 linenr_T firstline; /* first line of range */
6474 linenr_T lastline; /* last line of range */
6475 int *doesrange; /* return: function handled range */
6476 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480#define ERROR_UNKNOWN 0
6481#define ERROR_TOOMANY 1
6482#define ERROR_TOOFEW 2
6483#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484#define ERROR_DICT 4
6485#define ERROR_NONE 5
6486#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 int error = ERROR_NONE;
6488 int i;
6489 int llen;
6490 ufunc_T *fp;
6491 int cc;
6492#define FLEN_FIXED 40
6493 char_u fname_buf[FLEN_FIXED + 1];
6494 char_u *fname;
6495
6496 /*
6497 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6498 * Change <SNR>123_name() to K_SNR 123_name().
6499 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6500 */
6501 cc = name[len];
6502 name[len] = NUL;
6503 llen = eval_fname_script(name);
6504 if (llen > 0)
6505 {
6506 fname_buf[0] = K_SPECIAL;
6507 fname_buf[1] = KS_EXTRA;
6508 fname_buf[2] = (int)KE_SNR;
6509 i = 3;
6510 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6511 {
6512 if (current_SID <= 0)
6513 error = ERROR_SCRIPT;
6514 else
6515 {
6516 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6517 i = (int)STRLEN(fname_buf);
6518 }
6519 }
6520 if (i + STRLEN(name + llen) < FLEN_FIXED)
6521 {
6522 STRCPY(fname_buf + i, name + llen);
6523 fname = fname_buf;
6524 }
6525 else
6526 {
6527 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6528 if (fname == NULL)
6529 error = ERROR_OTHER;
6530 else
6531 {
6532 mch_memmove(fname, fname_buf, (size_t)i);
6533 STRCPY(fname + i, name + llen);
6534 }
6535 }
6536 }
6537 else
6538 fname = name;
6539
6540 *doesrange = FALSE;
6541
6542
6543 /* execute the function if no errors detected and executing */
6544 if (evaluate && error == ERROR_NONE)
6545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 error = ERROR_UNKNOWN;
6548
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006549 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551 /*
6552 * User defined function.
6553 */
6554 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006557 /* Trigger FuncUndefined event, may load the function. */
6558 if (fp == NULL
6559 && apply_autocmds(EVENT_FUNCUNDEFINED,
6560 fname, fname, TRUE, NULL)
6561 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006563 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 fp = find_func(fname);
6565 }
6566#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006567 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006568 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006569 {
6570 /* loaded a package, search for the function again */
6571 fp = find_func(fname);
6572 }
6573
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (fp != NULL)
6575 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006576 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006578 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006580 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006582 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006583 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 else
6585 {
6586 /*
6587 * Call the user function.
6588 * Save and restore search patterns, script variables and
6589 * redo buffer.
6590 */
6591 save_search_patterns();
6592 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006593 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006594 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006596 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6597 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6598 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006599 /* Function was unreferenced while being used, free it
6600 * now. */
6601 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 restoreRedobuff();
6603 restore_search_patterns();
6604 error = ERROR_NONE;
6605 }
6606 }
6607 }
6608 else
6609 {
6610 /*
6611 * Find the function name in the table, call its implementation.
6612 */
6613 i = find_internal_func(fname);
6614 if (i >= 0)
6615 {
6616 if (argcount < functions[i].f_min_argc)
6617 error = ERROR_TOOFEW;
6618 else if (argcount > functions[i].f_max_argc)
6619 error = ERROR_TOOMANY;
6620 else
6621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006623 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 error = ERROR_NONE;
6625 }
6626 }
6627 }
6628 /*
6629 * The function call (or "FuncUndefined" autocommand sequence) might
6630 * have been aborted by an error, an interrupt, or an explicitly thrown
6631 * exception that has not been caught so far. This situation can be
6632 * tested for by calling aborting(). For an error in an internal
6633 * function or for the "E132" error in call_user_func(), however, the
6634 * throw point at which the "force_abort" flag (temporarily reset by
6635 * emsg()) is normally updated has not been reached yet. We need to
6636 * update that flag first to make aborting() reliable.
6637 */
6638 update_force_abort();
6639 }
6640 if (error == ERROR_NONE)
6641 ret = OK;
6642
6643 /*
6644 * Report an error unless the argument evaluation or function call has been
6645 * cancelled due to an aborting error, an interrupt, or an exception.
6646 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647 if (!aborting())
6648 {
6649 switch (error)
6650 {
6651 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 break;
6654 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 break;
6657 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006658 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006659 name);
6660 break;
6661 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006663 name);
6664 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006665 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006666 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006667 name);
6668 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 }
6670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 name[len] = cc;
6673 if (fname != name && fname != fname_buf)
6674 vim_free(fname);
6675
6676 return ret;
6677}
6678
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679/*
6680 * Give an error message with a function name. Handle <SNR> things.
6681 */
6682 static void
6683emsg_funcname(msg, name)
6684 char *msg;
6685 char_u *name;
6686{
6687 char_u *p;
6688
6689 if (*name == K_SPECIAL)
6690 p = concat_str((char_u *)"<SNR>", name + 3);
6691 else
6692 p = name;
6693 EMSG2(_(msg), p);
6694 if (p != name)
6695 vim_free(p);
6696}
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698/*********************************************
6699 * Implementation of the built-in functions
6700 */
6701
6702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006703 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 */
6705 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006706f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006707 typval_T *argvars;
6708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
Bram Moolenaar33570922005-01-25 22:26:29 +00006710 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006712 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006713 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006715 if ((l = argvars[0].vval.v_list) != NULL
6716 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6717 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006718 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 }
6720 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006721 EMSG(_(e_listreq));
6722}
6723
6724/*
6725 * "append(lnum, string/list)" function
6726 */
6727 static void
6728f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 typval_T *argvars;
6730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006731{
6732 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006733 list_T *l = NULL;
6734 listitem_T *li = NULL;
6735 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006736 long added = 0;
6737
6738 rettv->vval.v_number = 1; /* Default: Failed */
6739 lnum = get_tv_lnum(argvars);
6740 if (lnum >= 0
6741 && lnum <= curbuf->b_ml.ml_line_count
6742 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006743 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006744 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006745 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006746 l = argvars[1].vval.v_list;
6747 if (l == NULL)
6748 return;
6749 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006751 for (;;)
6752 {
6753 if (l == NULL)
6754 tv = &argvars[1]; /* append a string */
6755 else if (li == NULL)
6756 break; /* end of list */
6757 else
6758 tv = &li->li_tv; /* append item from list */
6759 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6760 ++added;
6761 if (l == NULL)
6762 break;
6763 li = li->li_next;
6764 }
6765
6766 appended_lines_mark(lnum, added);
6767 if (curwin->w_cursor.lnum > lnum)
6768 curwin->w_cursor.lnum += added;
6769 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771}
6772
6773/*
6774 * "argc()" function
6775 */
6776/* ARGSUSED */
6777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006778f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 typval_T *argvars;
6780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006782 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783}
6784
6785/*
6786 * "argidx()" function
6787 */
6788/* ARGSUSED */
6789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 typval_T *argvars;
6792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795}
6796
6797/*
6798 * "argv(nr)" function
6799 */
6800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006802 typval_T *argvars;
6803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 int idx;
6806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006809 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006811 rettv->vval.v_string = NULL;
6812 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813}
6814
6815/*
6816 * "browse(save, title, initdir, default)" function
6817 */
6818/* ARGSUSED */
6819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006820f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 typval_T *argvars;
6822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824#ifdef FEAT_BROWSE
6825 int save;
6826 char_u *title;
6827 char_u *initdir;
6828 char_u *defname;
6829 char_u buf[NUMBUFLEN];
6830 char_u buf2[NUMBUFLEN];
6831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832 save = get_tv_number(&argvars[0]);
6833 title = get_tv_string(&argvars[1]);
6834 initdir = get_tv_string_buf(&argvars[2], buf);
6835 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006838 do_browse(save ? BROWSE_SAVE : 0,
6839 title, defname, NULL, initdir, NULL, curbuf);
6840#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006842#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006844}
6845
6846/*
6847 * "browsedir(title, initdir)" function
6848 */
6849/* ARGSUSED */
6850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006851f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 typval_T *argvars;
6853 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006854{
6855#ifdef FEAT_BROWSE
6856 char_u *title;
6857 char_u *initdir;
6858 char_u buf[NUMBUFLEN];
6859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006860 title = get_tv_string(&argvars[0]);
6861 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006863 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006864 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006868 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869}
6870
Bram Moolenaar33570922005-01-25 22:26:29 +00006871static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873/*
6874 * Find a buffer by number or exact name.
6875 */
6876 static buf_T *
6877find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006878 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 if (avar->v_type == VAR_NUMBER)
6883 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006884 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006886 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006887 if (buf == NULL)
6888 {
6889 /* No full path name match, try a match with a URL or a "nofile"
6890 * buffer, these don't use the full path. */
6891 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6892 if (buf->b_fname != NULL
6893 && (path_with_url(buf->b_fname)
6894#ifdef FEAT_QUICKFIX
6895 || bt_nofile(buf)
6896#endif
6897 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006898 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006899 break;
6900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902 return buf;
6903}
6904
6905/*
6906 * "bufexists(expr)" function
6907 */
6908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006909f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 typval_T *argvars;
6911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006913 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914}
6915
6916/*
6917 * "buflisted(expr)" function
6918 */
6919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 typval_T *argvars;
6922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 buf_T *buf;
6925
6926 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006927 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929
6930/*
6931 * "bufloaded(expr)" function
6932 */
6933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006934f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 typval_T *argvars;
6936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 buf_T *buf;
6939
6940 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942}
6943
Bram Moolenaar33570922005-01-25 22:26:29 +00006944static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946/*
6947 * Get buffer by number or pattern.
6948 */
6949 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006950get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006953 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 int save_magic;
6955 char_u *save_cpo;
6956 buf_T *buf;
6957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006958 if (tv->v_type == VAR_NUMBER)
6959 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006960 if (tv->v_type != VAR_STRING)
6961 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (name == NULL || *name == NUL)
6963 return curbuf;
6964 if (name[0] == '$' && name[1] == NUL)
6965 return lastbuf;
6966
6967 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6968 save_magic = p_magic;
6969 p_magic = TRUE;
6970 save_cpo = p_cpo;
6971 p_cpo = (char_u *)"";
6972
6973 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6974 TRUE, FALSE));
6975
6976 p_magic = save_magic;
6977 p_cpo = save_cpo;
6978
6979 /* If not found, try expanding the name, like done for bufexists(). */
6980 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006981 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983 return buf;
6984}
6985
6986/*
6987 * "bufname(expr)" function
6988 */
6989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 typval_T *argvars;
6992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
6994 buf_T *buf;
6995
6996 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 buf = get_buf_tv(&argvars[0]);
6998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007000 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 --emsg_off;
7004}
7005
7006/*
7007 * "bufnr(expr)" function
7008 */
7009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007010f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 typval_T *argvars;
7012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
7014 buf_T *buf;
7015
7016 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007017 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007019 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 --emsg_off;
7023}
7024
7025/*
7026 * "bufwinnr(nr)" function
7027 */
7028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007029f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 typval_T *argvars;
7031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033#ifdef FEAT_WINDOWS
7034 win_T *wp;
7035 int winnr = 0;
7036#endif
7037 buf_T *buf;
7038
7039 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007040 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041#ifdef FEAT_WINDOWS
7042 for (wp = firstwin; wp; wp = wp->w_next)
7043 {
7044 ++winnr;
7045 if (wp->w_buffer == buf)
7046 break;
7047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007048 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007050 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051#endif
7052 --emsg_off;
7053}
7054
7055/*
7056 * "byte2line(byte)" function
7057 */
7058/*ARGSUSED*/
7059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007060f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 typval_T *argvars;
7062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066#else
7067 long boff = 0;
7068
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007069 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007071 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 (linenr_T)0, &boff);
7075#endif
7076}
7077
7078/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007079 * "byteidx()" function
7080 */
7081/*ARGSUSED*/
7082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 typval_T *argvars;
7085 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007086{
7087#ifdef FEAT_MBYTE
7088 char_u *t;
7089#endif
7090 char_u *str;
7091 long idx;
7092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007093 str = get_tv_string(&argvars[0]);
7094 idx = get_tv_number(&argvars[1]);
7095 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007096 if (idx < 0)
7097 return;
7098
7099#ifdef FEAT_MBYTE
7100 t = str;
7101 for ( ; idx > 0; idx--)
7102 {
7103 if (*t == NUL) /* EOL reached */
7104 return;
7105 t += mb_ptr2len_check(t);
7106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007107 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007108#else
7109 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007110 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007111#endif
7112}
7113
7114/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007115 * "call(func, arglist)" function
7116 */
7117 static void
7118f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 typval_T *argvars;
7120 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007121{
7122 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007124 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007126 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007128
7129 rettv->vval.v_number = 0;
7130 if (argvars[1].v_type != VAR_LIST)
7131 {
7132 EMSG(_(e_listreq));
7133 return;
7134 }
7135 if (argvars[1].vval.v_list == NULL)
7136 return;
7137
7138 if (argvars[0].v_type == VAR_FUNC)
7139 func = argvars[0].vval.v_string;
7140 else
7141 func = get_tv_string(&argvars[0]);
7142
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 if (argvars[2].v_type != VAR_UNKNOWN)
7144 {
7145 if (argvars[2].v_type != VAR_DICT)
7146 {
7147 EMSG(_(e_dictreq));
7148 return;
7149 }
7150 selfdict = argvars[2].vval.v_dict;
7151 }
7152
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007153 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7154 item = item->li_next)
7155 {
7156 if (argc == MAX_FUNC_ARGS)
7157 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007158 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007159 break;
7160 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007161 /* Make a copy of each argument. This is needed to be able to set
7162 * v_lock to VAR_FIXED in the copy without changing the original list.
7163 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007164 copy_tv(&item->li_tv, &argv[argc++]);
7165 }
7166
7167 if (item == NULL)
7168 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7170 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007171
7172 /* Free the arguments. */
7173 while (argc > 0)
7174 clear_tv(&argv[--argc]);
7175}
7176
7177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 * "char2nr(string)" function
7179 */
7180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007181f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 typval_T *argvars;
7183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185#ifdef FEAT_MBYTE
7186 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007187 rettv->vval.v_number =
7188 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 else
7190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007191 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192}
7193
7194/*
7195 * "cindent(lnum)" function
7196 */
7197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007198f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 typval_T *argvars;
7200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202#ifdef FEAT_CINDENT
7203 pos_T pos;
7204 linenr_T lnum;
7205
7206 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007207 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7209 {
7210 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 curwin->w_cursor = pos;
7213 }
7214 else
7215#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217}
7218
7219/*
7220 * "col(string)" function
7221 */
7222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 typval_T *argvars;
7225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
7227 colnr_T col = 0;
7228 pos_T *fp;
7229
7230 fp = var2fpos(&argvars[0], FALSE);
7231 if (fp != NULL)
7232 {
7233 if (fp->col == MAXCOL)
7234 {
7235 /* '> can be MAXCOL, get the length of the line then */
7236 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7237 col = STRLEN(ml_get(fp->lnum)) + 1;
7238 else
7239 col = MAXCOL;
7240 }
7241 else
7242 {
7243 col = fp->col + 1;
7244#ifdef FEAT_VIRTUALEDIT
7245 /* col(".") when the cursor is on the NUL at the end of the line
7246 * because of "coladd" can be seen as an extra column. */
7247 if (virtual_active() && fp == &curwin->w_cursor)
7248 {
7249 char_u *p = ml_get_cursor();
7250
7251 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7252 curwin->w_virtcol - curwin->w_cursor.coladd))
7253 {
7254# ifdef FEAT_MBYTE
7255 int l;
7256
7257 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7258 col += l;
7259# else
7260 if (*p != NUL && p[1] == NUL)
7261 ++col;
7262# endif
7263 }
7264 }
7265#endif
7266 }
7267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007268 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269}
7270
7271/*
7272 * "confirm(message, buttons[, default [, type]])" function
7273 */
7274/*ARGSUSED*/
7275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 typval_T *argvars;
7278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7281 char_u *message;
7282 char_u *buttons = NULL;
7283 char_u buf[NUMBUFLEN];
7284 char_u buf2[NUMBUFLEN];
7285 int def = 1;
7286 int type = VIM_GENERIC;
7287 int c;
7288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007289 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007292 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007293 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007295 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007296 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007298 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007299 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 switch (TOUPPER_ASC(c))
7301 {
7302 case 'E': type = VIM_ERROR; break;
7303 case 'Q': type = VIM_QUESTION; break;
7304 case 'I': type = VIM_INFO; break;
7305 case 'W': type = VIM_WARNING; break;
7306 case 'G': type = VIM_GENERIC; break;
7307 }
7308 }
7309 }
7310 }
7311
7312 if (buttons == NULL || *buttons == NUL)
7313 buttons = (char_u *)_("&Ok");
7314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007315 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 def, NULL);
7317#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007318 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#endif
7320}
7321
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007322/*
7323 * "copy()" function
7324 */
7325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007326f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 typval_T *argvars;
7328 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007329{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007330 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
7333/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007334 * "count()" function
7335 */
7336 static void
7337f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 typval_T *argvars;
7339 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007340{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007341 long n = 0;
7342 int ic = FALSE;
7343
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007345 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 listitem_T *li;
7347 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007349
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 if ((l = argvars[0].vval.v_list) != NULL)
7351 {
7352 li = l->lv_first;
7353 if (argvars[2].v_type != VAR_UNKNOWN)
7354 {
7355 ic = get_tv_number(&argvars[2]);
7356 if (argvars[3].v_type != VAR_UNKNOWN)
7357 {
7358 idx = get_tv_number(&argvars[3]);
7359 li = list_find(l, idx);
7360 if (li == NULL)
7361 EMSGN(_(e_listidx), idx);
7362 }
7363 }
7364
7365 for ( ; li != NULL; li = li->li_next)
7366 if (tv_equal(&li->li_tv, &argvars[1], ic))
7367 ++n;
7368 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 else if (argvars[0].v_type == VAR_DICT)
7371 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 int todo;
7373 dict_T *d;
7374 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 if ((d = argvars[0].vval.v_dict) != NULL)
7377 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 if (argvars[2].v_type != VAR_UNKNOWN)
7379 {
7380 ic = get_tv_number(&argvars[2]);
7381 if (argvars[3].v_type != VAR_UNKNOWN)
7382 EMSG(_(e_invarg));
7383 }
7384
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 todo = d->dv_hashtab.ht_used;
7386 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 {
7388 if (!HASHITEM_EMPTY(hi))
7389 {
7390 --todo;
7391 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7392 ++n;
7393 }
7394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 }
7396 }
7397 else
7398 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007399 rettv->vval.v_number = n;
7400}
7401
7402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7404 *
7405 * Checks the existence of a cscope connection.
7406 */
7407/*ARGSUSED*/
7408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007409f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 typval_T *argvars;
7411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413#ifdef FEAT_CSCOPE
7414 int num = 0;
7415 char_u *dbpath = NULL;
7416 char_u *prepend = NULL;
7417 char_u buf[NUMBUFLEN];
7418
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007419 if (argvars[0].v_type != VAR_UNKNOWN
7420 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007422 num = (int)get_tv_number(&argvars[0]);
7423 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007424 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007425 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007428 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007430 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431#endif
7432}
7433
7434/*
7435 * "cursor(lnum, col)" function
7436 *
7437 * Moves the cursor to the specified line and column
7438 */
7439/*ARGSUSED*/
7440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T *argvars;
7443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
7445 long line, col;
7446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (line > 0)
7449 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 if (col > 0)
7452 curwin->w_cursor.col = col - 1;
7453#ifdef FEAT_VIRTUALEDIT
7454 curwin->w_cursor.coladd = 0;
7455#endif
7456
7457 /* Make sure the cursor is in a valid position. */
7458 check_cursor();
7459#ifdef FEAT_MBYTE
7460 /* Correct cursor for multi-byte character. */
7461 if (has_mbyte)
7462 mb_adjust_cursor();
7463#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007464
7465 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466}
7467
7468/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007469 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 */
7471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007473 typval_T *argvars;
7474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007476 static int copyID = 0;
7477 int noref = 0;
7478
7479 if (argvars[1].v_type != VAR_UNKNOWN)
7480 noref = get_tv_number(&argvars[1]);
7481 if (noref < 0 || noref > 1)
7482 EMSG(_(e_invarg));
7483 else
7484 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485}
7486
7487/*
7488 * "delete()" function
7489 */
7490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 typval_T *argvars;
7493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007498 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499}
7500
7501/*
7502 * "did_filetype()" function
7503 */
7504/*ARGSUSED*/
7505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 typval_T *argvars;
7508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007513 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514#endif
7515}
7516
7517/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007518 * "diff_filler()" function
7519 */
7520/*ARGSUSED*/
7521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007522f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 typval_T *argvars;
7524 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007525{
7526#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007528#endif
7529}
7530
7531/*
7532 * "diff_hlID()" function
7533 */
7534/*ARGSUSED*/
7535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 typval_T *argvars;
7538 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007539{
7540#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007542 static linenr_T prev_lnum = 0;
7543 static int changedtick = 0;
7544 static int fnum = 0;
7545 static int change_start = 0;
7546 static int change_end = 0;
7547 static enum hlf_value hlID = 0;
7548 int filler_lines;
7549 int col;
7550
7551 if (lnum != prev_lnum
7552 || changedtick != curbuf->b_changedtick
7553 || fnum != curbuf->b_fnum)
7554 {
7555 /* New line, buffer, change: need to get the values. */
7556 filler_lines = diff_check(curwin, lnum);
7557 if (filler_lines < 0)
7558 {
7559 if (filler_lines == -1)
7560 {
7561 change_start = MAXCOL;
7562 change_end = -1;
7563 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7564 hlID = HLF_ADD; /* added line */
7565 else
7566 hlID = HLF_CHD; /* changed line */
7567 }
7568 else
7569 hlID = HLF_ADD; /* added line */
7570 }
7571 else
7572 hlID = (enum hlf_value)0;
7573 prev_lnum = lnum;
7574 changedtick = curbuf->b_changedtick;
7575 fnum = curbuf->b_fnum;
7576 }
7577
7578 if (hlID == HLF_CHD || hlID == HLF_TXD)
7579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007581 if (col >= change_start && col <= change_end)
7582 hlID = HLF_TXD; /* changed text */
7583 else
7584 hlID = HLF_CHD; /* changed line */
7585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007587#endif
7588}
7589
7590/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007591 * "empty({expr})" function
7592 */
7593 static void
7594f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 typval_T *argvars;
7596 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007597{
7598 int n;
7599
7600 switch (argvars[0].v_type)
7601 {
7602 case VAR_STRING:
7603 case VAR_FUNC:
7604 n = argvars[0].vval.v_string == NULL
7605 || *argvars[0].vval.v_string == NUL;
7606 break;
7607 case VAR_NUMBER:
7608 n = argvars[0].vval.v_number == 0;
7609 break;
7610 case VAR_LIST:
7611 n = argvars[0].vval.v_list == NULL
7612 || argvars[0].vval.v_list->lv_first == NULL;
7613 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 case VAR_DICT:
7615 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007618 default:
7619 EMSG2(_(e_intern2), "f_empty()");
7620 n = 0;
7621 }
7622
7623 rettv->vval.v_number = n;
7624}
7625
7626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 * "escape({string}, {chars})" function
7628 */
7629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 char_u buf[NUMBUFLEN];
7635
Bram Moolenaar758711c2005-02-02 23:11:38 +00007636 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7637 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639}
7640
7641/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 * "eval()" function
7643 */
7644/*ARGSUSED*/
7645 static void
7646f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 typval_T *argvars;
7648 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007649{
7650 char_u *s;
7651
7652 s = get_tv_string(&argvars[0]);
7653 s = skipwhite(s);
7654
7655 if (eval1(&s, rettv, TRUE) == FAIL)
7656 rettv->vval.v_number = 0;
7657 else if (*s != NUL)
7658 EMSG(_(e_trailing));
7659}
7660
7661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 * "eventhandler()" function
7663 */
7664/*ARGSUSED*/
7665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 typval_T *argvars;
7668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671}
7672
7673/*
7674 * "executable()" function
7675 */
7676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007677f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 typval_T *argvars;
7679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682}
7683
7684/*
7685 * "exists()" function
7686 */
7687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 typval_T *argvars;
7690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
7692 char_u *p;
7693 char_u *name;
7694 int n = FALSE;
7695 int len = 0;
7696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 if (*p == '$') /* environment variable */
7699 {
7700 /* first try "normal" environment variables (fast) */
7701 if (mch_getenv(p + 1) != NULL)
7702 n = TRUE;
7703 else
7704 {
7705 /* try expanding things like $VIM and ${HOME} */
7706 p = expand_env_save(p);
7707 if (p != NULL && *p != '$')
7708 n = TRUE;
7709 vim_free(p);
7710 }
7711 }
7712 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 else if (*p == '*') /* internal or user defined function */
7715 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718 else if (*p == ':')
7719 {
7720 n = cmd_exists(p + 1);
7721 }
7722 else if (*p == '#')
7723 {
7724#ifdef FEAT_AUTOCMD
7725 name = p + 1;
7726 p = vim_strchr(name, '#');
7727 if (p != NULL)
7728 n = au_exists(name, p, p + 1);
7729 else
7730 n = au_exists(name, name + STRLEN(name), NULL);
7731#endif
7732 }
7733 else /* internal variable */
7734 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007735 char_u *tofree;
7736 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007738 /* get_name_len() takes care of expanding curly braces */
7739 name = p;
7740 len = get_name_len(&p, &tofree, TRUE, FALSE);
7741 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007743 if (tofree != NULL)
7744 name = tofree;
7745 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7746 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007748 /* handle d.key, l[idx], f(expr) */
7749 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7750 if (n)
7751 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759}
7760
7761/*
7762 * "expand()" function
7763 */
7764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *argvars;
7767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 char_u *s;
7770 int len;
7771 char_u *errormsg;
7772 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7773 expand_T xpc;
7774
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->v_type = VAR_STRING;
7776 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 if (*s == '%' || *s == '#' || *s == '<')
7778 {
7779 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 --emsg_off;
7782 }
7783 else
7784 {
7785 /* When the optional second argument is non-zero, don't remove matches
7786 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 flags |= WILD_KEEP_ALL;
7789 ExpandInit(&xpc);
7790 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007791 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 ExpandCleanup(&xpc);
7793 }
7794}
7795
7796/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007799 */
7800 static void
7801f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *argvars;
7803 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007804{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007805 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007807 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 list_T *l1, *l2;
7809 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007810 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007811
Bram Moolenaare9a41262005-01-15 22:18:47 +00007812 l1 = argvars[0].vval.v_list;
7813 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007814 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7815 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 {
7817 if (argvars[2].v_type != VAR_UNKNOWN)
7818 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007819 before = get_tv_number(&argvars[2]);
7820 if (before == l1->lv_len)
7821 item = NULL;
7822 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007824 item = list_find(l1, before);
7825 if (item == NULL)
7826 {
7827 EMSGN(_(e_listidx), before);
7828 return;
7829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 }
7831 }
7832 else
7833 item = NULL;
7834 list_extend(l1, l2, item);
7835
7836 ++l1->lv_refcount;
7837 copy_tv(&argvars[0], rettv);
7838 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007840 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7841 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 dict_T *d1, *d2;
7843 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 char_u *action;
7845 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007846 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007847 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848
7849 d1 = argvars[0].vval.v_dict;
7850 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007851 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7852 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853 {
7854 /* Check the third argument. */
7855 if (argvars[2].v_type != VAR_UNKNOWN)
7856 {
7857 static char *(av[]) = {"keep", "force", "error"};
7858
7859 action = get_tv_string(&argvars[2]);
7860 for (i = 0; i < 3; ++i)
7861 if (STRCMP(action, av[i]) == 0)
7862 break;
7863 if (i == 3)
7864 {
7865 EMSGN(_(e_invarg2), action);
7866 return;
7867 }
7868 }
7869 else
7870 action = (char_u *)"force";
7871
7872 /* Go over all entries in the second dict and add them to the
7873 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 todo = d2->dv_hashtab.ht_used;
7875 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007877 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 --todo;
7880 di1 = dict_find(d1, hi2->hi_key, -1);
7881 if (di1 == NULL)
7882 {
7883 di1 = dictitem_copy(HI2DI(hi2));
7884 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7885 dictitem_free(di1);
7886 }
7887 else if (*action == 'e')
7888 {
7889 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7890 break;
7891 }
7892 else if (*action == 'f')
7893 {
7894 clear_tv(&di1->di_tv);
7895 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7896 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897 }
7898 }
7899
7900 ++d1->dv_refcount;
7901 copy_tv(&argvars[0], rettv);
7902 }
7903 }
7904 else
7905 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007906}
7907
7908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 * "filereadable()" function
7910 */
7911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007912f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 typval_T *argvars;
7914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 FILE *fd;
7917 char_u *p;
7918 int n;
7919
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7922 {
7923 n = TRUE;
7924 fclose(fd);
7925 }
7926 else
7927 n = FALSE;
7928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930}
7931
7932/*
7933 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7934 * rights to write into.
7935 */
7936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 typval_T *argvars;
7939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941 char_u *p;
7942 int retval = 0;
7943#if defined(UNIX) || defined(VMS)
7944 int perm = 0;
7945#endif
7946
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948#if defined(UNIX) || defined(VMS)
7949 perm = mch_getperm(p);
7950#endif
7951#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7952 if (
7953# ifdef WIN3264
7954 mch_writable(p) &&
7955# else
7956# if defined(UNIX) || defined(VMS)
7957 (perm & 0222) &&
7958# endif
7959# endif
7960 mch_access((char *)p, W_OK) == 0
7961 )
7962#endif
7963 {
7964 ++retval;
7965 if (mch_isdir(p))
7966 ++retval;
7967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
Bram Moolenaar33570922005-01-25 22:26:29 +00007971static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007972
7973 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007974findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 typval_T *argvars;
7976 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007977 int dir;
7978{
7979#ifdef FEAT_SEARCHPATH
7980 char_u *fname;
7981 char_u *fresult = NULL;
7982 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7983 char_u *p;
7984 char_u pathbuf[NUMBUFLEN];
7985 int count = 1;
7986 int first = TRUE;
7987
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007990 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 if (*p != NUL)
7994 path = p;
7995
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007998 }
7999
8000 do
8001 {
8002 vim_free(fresult);
8003 fresult = find_file_in_path_option(first ? fname : NULL,
8004 first ? (int)STRLEN(fname) : 0,
8005 0, first, path, dir, NULL);
8006 first = FALSE;
8007 } while (--count > 0 && fresult != NULL);
8008
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008010#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008012#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008014}
8015
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008016static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8017static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008018static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8019static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008020
8021/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008022 * Prepare v: variable "idx" to be used.
8023 * Save the current typeval in "save_tv".
8024 * When not used yet add the variable to the v: hashtable.
8025 */
8026 static void
8027prepare_vimvar(idx, save_tv)
8028 int idx;
8029 typval_T *save_tv;
8030{
8031 *save_tv = vimvars[idx].vv_tv;
8032 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8033 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8034}
8035
8036/*
8037 * Restore v: variable "idx" to typeval "save_tv".
8038 * When no longer defined, remove the variable from the v: hashtable.
8039 */
8040 static void
8041restore_vimvar(idx, save_tv)
8042 int idx;
8043 typval_T *save_tv;
8044{
8045 hashitem_T *hi;
8046
8047 clear_tv(&vimvars[idx].vv_tv);
8048 vimvars[idx].vv_tv = *save_tv;
8049 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8050 {
8051 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8052 if (HASHITEM_EMPTY(hi))
8053 EMSG2(_(e_intern2), "restore_vimvar()");
8054 else
8055 hash_remove(&vimvarht, hi);
8056 }
8057}
8058
8059/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008060 * Implementation of map() and filter().
8061 */
8062 static void
8063filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 typval_T *argvars;
8065 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008066 int map;
8067{
8068 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008070 listitem_T *li, *nli;
8071 list_T *l = NULL;
8072 dictitem_T *di;
8073 hashtab_T *ht;
8074 hashitem_T *hi;
8075 dict_T *d = NULL;
8076 typval_T save_val;
8077 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008079 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008080 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8081
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008082
8083 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008084 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008085 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008086 if ((l = argvars[0].vval.v_list) == NULL
8087 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088 return;
8089 }
8090 else if (argvars[0].v_type == VAR_DICT)
8091 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008092 if ((d = argvars[0].vval.v_dict) == NULL
8093 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008094 return;
8095 }
8096 else
8097 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008098 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008099 return;
8100 }
8101
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008102 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104
8105 if (argvars[0].v_type == VAR_DICT)
8106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008107 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008109
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008111 hash_lock(ht);
8112 todo = ht->ht_used;
8113 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008115 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008117 --todo;
8118 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008119 if (tv_check_lock(di->di_tv.v_lock, msg))
8120 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008122 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8123 break;
8124 if (!map && rem)
8125 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008127 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008128 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008129 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008130
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008131 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008132 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 else
8134 {
8135 for (li = l->lv_first; li != NULL; li = nli)
8136 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008137 if (tv_check_lock(li->li_tv.v_lock, msg))
8138 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 nli = li->li_next;
8140 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8141 break;
8142 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008143 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008144 }
8145 }
8146
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008147 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148
8149 copy_tv(&argvars[0], rettv);
8150}
8151
8152 static int
8153filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155 char_u *expr;
8156 int map;
8157 int *remp;
8158{
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 char_u *s;
8161
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163 s = expr;
8164 if (eval1(&s, &rettv, TRUE) == FAIL)
8165 return FAIL;
8166 if (*s != NUL) /* check for trailing chars after expr */
8167 {
8168 EMSG2(_(e_invexpr2), s);
8169 return FAIL;
8170 }
8171 if (map)
8172 {
8173 /* map(): replace the list item value */
8174 clear_tv(tv);
8175 *tv = rettv;
8176 }
8177 else
8178 {
8179 /* filter(): when expr is zero remove the item */
8180 *remp = (get_tv_number(&rettv) == 0);
8181 clear_tv(&rettv);
8182 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185}
8186
8187/*
8188 * "filter()" function
8189 */
8190 static void
8191f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008192 typval_T *argvars;
8193 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008194{
8195 filter_map(argvars, rettv, FALSE);
8196}
8197
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008198/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008199 * "finddir({fname}[, {path}[, {count}]])" function
8200 */
8201 static void
8202f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 typval_T *argvars;
8204 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008205{
8206 findfilendir(argvars, rettv, TRUE);
8207}
8208
8209/*
8210 * "findfile({fname}[, {path}[, {count}]])" function
8211 */
8212 static void
8213f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008214 typval_T *argvars;
8215 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008216{
8217 findfilendir(argvars, rettv, FALSE);
8218}
8219
8220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 * "fnamemodify({fname}, {mods})" function
8222 */
8223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 typval_T *argvars;
8226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 char_u *fname;
8229 char_u *mods;
8230 int usedlen = 0;
8231 int len;
8232 char_u *fbuf = NULL;
8233 char_u buf[NUMBUFLEN];
8234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008235 fname = get_tv_string(&argvars[0]);
8236 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 len = (int)STRLEN(fname);
8238
8239 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8240
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 vim_free(fbuf);
8247}
8248
Bram Moolenaar33570922005-01-25 22:26:29 +00008249static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251/*
8252 * "foldclosed()" function
8253 */
8254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 typval_T *argvars;
8257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 int end;
8259{
8260#ifdef FEAT_FOLDING
8261 linenr_T lnum;
8262 linenr_T first, last;
8263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8266 {
8267 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8268 {
8269 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008270 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 return;
8274 }
8275 }
8276#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279
8280/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008281 * "foldclosed()" function
8282 */
8283 static void
8284f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 typval_T *argvars;
8286 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287{
8288 foldclosed_both(argvars, rettv, FALSE);
8289}
8290
8291/*
8292 * "foldclosedend()" function
8293 */
8294 static void
8295f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008298{
8299 foldclosed_both(argvars, rettv, TRUE);
8300}
8301
8302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 * "foldlevel()" function
8304 */
8305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *argvars;
8308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309{
8310#ifdef FEAT_FOLDING
8311 linenr_T lnum;
8312
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008313 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008315 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 else
8317#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319}
8320
8321/*
8322 * "foldtext()" function
8323 */
8324/*ARGSUSED*/
8325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 typval_T *argvars;
8328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330#ifdef FEAT_FOLDING
8331 linenr_T lnum;
8332 char_u *s;
8333 char_u *r;
8334 int len;
8335 char *txt;
8336#endif
8337
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008338 rettv->v_type = VAR_STRING;
8339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8342 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8343 <= curbuf->b_ml.ml_line_count
8344 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {
8346 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8348 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
8350 if (!linewhite(lnum))
8351 break;
8352 ++lnum;
8353 }
8354
8355 /* Find interesting text in this line. */
8356 s = skipwhite(ml_get(lnum));
8357 /* skip C comment-start */
8358 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008361 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008362 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008363 {
8364 s = skipwhite(ml_get(lnum + 1));
8365 if (*s == '*')
8366 s = skipwhite(s + 1);
8367 }
8368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 txt = _("+-%s%3ld lines: ");
8370 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008371 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 + 20 /* for %3ld */
8373 + STRLEN(s))); /* concatenated */
8374 if (r != NULL)
8375 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008376 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8377 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8378 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 len = (int)STRLEN(r);
8380 STRCAT(r, s);
8381 /* remove 'foldmarker' and 'commentstring' */
8382 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385 }
8386#endif
8387}
8388
8389/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008390 * "foldtextresult(lnum)" function
8391 */
8392/*ARGSUSED*/
8393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008394f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008395 typval_T *argvars;
8396 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008397{
8398#ifdef FEAT_FOLDING
8399 linenr_T lnum;
8400 char_u *text;
8401 char_u buf[51];
8402 foldinfo_T foldinfo;
8403 int fold_count;
8404#endif
8405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406 rettv->v_type = VAR_STRING;
8407 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008408#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008410 fold_count = foldedCount(curwin, lnum, &foldinfo);
8411 if (fold_count > 0)
8412 {
8413 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8414 &foldinfo, buf);
8415 if (text == buf)
8416 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008418 }
8419#endif
8420}
8421
8422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 * "foreground()" function
8424 */
8425/*ARGSUSED*/
8426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008427f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 typval_T *argvars;
8429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008431 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432#ifdef FEAT_GUI
8433 if (gui.in_use)
8434 gui_mch_set_foreground();
8435#else
8436# ifdef WIN32
8437 win32_set_foreground();
8438# endif
8439#endif
8440}
8441
8442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 * "function()" function
8444 */
8445/*ARGSUSED*/
8446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008447f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 typval_T *argvars;
8449 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008450{
8451 char_u *s;
8452
Bram Moolenaara7043832005-01-21 11:56:39 +00008453 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008455 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008456 EMSG2(_(e_invarg2), s);
8457 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008458 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008459 else
8460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008461 rettv->vval.v_string = vim_strsave(s);
8462 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008463 }
8464}
8465
8466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008467 * "get()" function
8468 */
8469 static void
8470f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 typval_T *argvars;
8472 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008473{
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 listitem_T *li;
8475 list_T *l;
8476 dictitem_T *di;
8477 dict_T *d;
8478 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008479
Bram Moolenaare9a41262005-01-15 22:18:47 +00008480 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008481 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008482 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008483 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008484 li = list_find(l, get_tv_number(&argvars[1]));
8485 if (li != NULL)
8486 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008487 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 else if (argvars[0].v_type == VAR_DICT)
8490 {
8491 if ((d = argvars[0].vval.v_dict) != NULL)
8492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008493 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 if (di != NULL)
8495 tv = &di->di_tv;
8496 }
8497 }
8498 else
8499 EMSG2(_(e_listdictarg), "get()");
8500
8501 if (tv == NULL)
8502 {
8503 if (argvars[2].v_type == VAR_UNKNOWN)
8504 rettv->vval.v_number = 0;
8505 else
8506 copy_tv(&argvars[2], rettv);
8507 }
8508 else
8509 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008510}
8511
8512/*
8513 * "getbufvar()" function
8514 */
8515 static void
8516f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 typval_T *argvars;
8518 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008519{
8520 buf_T *buf;
8521 buf_T *save_curbuf;
8522 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008524
8525 ++emsg_off;
8526 buf = get_buf_tv(&argvars[0]);
8527 varname = get_tv_string(&argvars[1]);
8528
8529 rettv->v_type = VAR_STRING;
8530 rettv->vval.v_string = NULL;
8531
8532 if (buf != NULL && varname != NULL)
8533 {
8534 if (*varname == '&') /* buffer-local-option */
8535 {
8536 /* set curbuf to be our buf, temporarily */
8537 save_curbuf = curbuf;
8538 curbuf = buf;
8539
8540 get_option_tv(&varname, rettv, TRUE);
8541
8542 /* restore previous notion of curbuf */
8543 curbuf = save_curbuf;
8544 }
8545 else
8546 {
8547 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008548 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008549 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008551 }
8552 }
8553
8554 --emsg_off;
8555}
8556
8557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 * "getchar()" function
8559 */
8560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 typval_T *argvars;
8563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 varnumber_T n;
8566
8567 ++no_mapping;
8568 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008569 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 /* getchar(): blocking wait. */
8571 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 /* getchar(1): only check if char avail */
8574 n = vpeekc();
8575 else if (vpeekc() == NUL)
8576 /* getchar(0) and no char avail: return zero */
8577 n = 0;
8578 else
8579 /* getchar(0) and char avail: return char */
8580 n = safe_vgetc();
8581 --no_mapping;
8582 --allow_keys;
8583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (IS_SPECIAL(n) || mod_mask != 0)
8586 {
8587 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8588 int i = 0;
8589
8590 /* Turn a special key into three bytes, plus modifier. */
8591 if (mod_mask != 0)
8592 {
8593 temp[i++] = K_SPECIAL;
8594 temp[i++] = KS_MODIFIER;
8595 temp[i++] = mod_mask;
8596 }
8597 if (IS_SPECIAL(n))
8598 {
8599 temp[i++] = K_SPECIAL;
8600 temp[i++] = K_SECOND(n);
8601 temp[i++] = K_THIRD(n);
8602 }
8603#ifdef FEAT_MBYTE
8604 else if (has_mbyte)
8605 i += (*mb_char2bytes)(n, temp + i);
8606#endif
8607 else
8608 temp[i++] = n;
8609 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 rettv->v_type = VAR_STRING;
8611 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613}
8614
8615/*
8616 * "getcharmod()" function
8617 */
8618/*ARGSUSED*/
8619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 typval_T *argvars;
8622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625}
8626
8627/*
8628 * "getcmdline()" function
8629 */
8630/*ARGSUSED*/
8631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 typval_T *argvars;
8634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636 rettv->v_type = VAR_STRING;
8637 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638}
8639
8640/*
8641 * "getcmdpos()" function
8642 */
8643/*ARGSUSED*/
8644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 typval_T *argvars;
8647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650}
8651
8652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 * "getcwd()" function
8654 */
8655/*ARGSUSED*/
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 char_u cwd[MAXPATHL];
8662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 else
8667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008670 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671#endif
8672 }
8673}
8674
8675/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008676 * "getfontname()" function
8677 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008678/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 rettv->v_type = VAR_STRING;
8685 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008686#ifdef FEAT_GUI
8687 if (gui.in_use)
8688 {
8689 GuiFont font;
8690 char_u *name = NULL;
8691
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008692 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008693 {
8694 /* Get the "Normal" font. Either the name saved by
8695 * hl_set_font_name() or from the font ID. */
8696 font = gui.norm_font;
8697 name = hl_get_font_name();
8698 }
8699 else
8700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008702 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8703 return;
8704 font = gui_mch_get_font(name, FALSE);
8705 if (font == NOFONT)
8706 return; /* Invalid font name, return empty string. */
8707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008709 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008710 gui_mch_free_font(font);
8711 }
8712#endif
8713}
8714
8715/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008716 * "getfperm({fname})" function
8717 */
8718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T *argvars;
8721 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008722{
8723 char_u *fname;
8724 struct stat st;
8725 char_u *perm = NULL;
8726 char_u flags[] = "rwx";
8727 int i;
8728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008731 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008732 if (mch_stat((char *)fname, &st) >= 0)
8733 {
8734 perm = vim_strsave((char_u *)"---------");
8735 if (perm != NULL)
8736 {
8737 for (i = 0; i < 9; i++)
8738 {
8739 if (st.st_mode & (1 << (8 - i)))
8740 perm[i] = flags[i % 3];
8741 }
8742 }
8743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008745}
8746
8747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 * "getfsize({fname})" function
8749 */
8750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751f_getfsize(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
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
8762 if (mch_stat((char *)fname, &st) >= 0)
8763 {
8764 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
8769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771}
8772
8773/*
8774 * "getftime({fname})" function
8775 */
8776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008778 typval_T *argvars;
8779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 char_u *fname;
8782 struct stat st;
8783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790}
8791
8792/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008793 * "getftype({fname})" function
8794 */
8795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 typval_T *argvars;
8798 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008799{
8800 char_u *fname;
8801 struct stat st;
8802 char_u *type = NULL;
8803 char *t;
8804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008808 if (mch_lstat((char *)fname, &st) >= 0)
8809 {
8810#ifdef S_ISREG
8811 if (S_ISREG(st.st_mode))
8812 t = "file";
8813 else if (S_ISDIR(st.st_mode))
8814 t = "dir";
8815# ifdef S_ISLNK
8816 else if (S_ISLNK(st.st_mode))
8817 t = "link";
8818# endif
8819# ifdef S_ISBLK
8820 else if (S_ISBLK(st.st_mode))
8821 t = "bdev";
8822# endif
8823# ifdef S_ISCHR
8824 else if (S_ISCHR(st.st_mode))
8825 t = "cdev";
8826# endif
8827# ifdef S_ISFIFO
8828 else if (S_ISFIFO(st.st_mode))
8829 t = "fifo";
8830# endif
8831# ifdef S_ISSOCK
8832 else if (S_ISSOCK(st.st_mode))
8833 t = "fifo";
8834# endif
8835 else
8836 t = "other";
8837#else
8838# ifdef S_IFMT
8839 switch (st.st_mode & S_IFMT)
8840 {
8841 case S_IFREG: t = "file"; break;
8842 case S_IFDIR: t = "dir"; break;
8843# ifdef S_IFLNK
8844 case S_IFLNK: t = "link"; break;
8845# endif
8846# ifdef S_IFBLK
8847 case S_IFBLK: t = "bdev"; break;
8848# endif
8849# ifdef S_IFCHR
8850 case S_IFCHR: t = "cdev"; break;
8851# endif
8852# ifdef S_IFIFO
8853 case S_IFIFO: t = "fifo"; break;
8854# endif
8855# ifdef S_IFSOCK
8856 case S_IFSOCK: t = "socket"; break;
8857# endif
8858 default: t = "other";
8859 }
8860# else
8861 if (mch_isdir(fname))
8862 t = "dir";
8863 else
8864 t = "file";
8865# endif
8866#endif
8867 type = vim_strsave((char_u *)t);
8868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008870}
8871
8872/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008873 * "getline(lnum)" function
8874 */
8875 static void
8876f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *argvars;
8878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008879{
8880 linenr_T lnum;
8881 linenr_T end;
8882 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 list_T *l;
8884 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008885
8886 lnum = get_tv_lnum(argvars);
8887
8888 if (argvars[1].v_type == VAR_UNKNOWN)
8889 {
8890 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8891 p = ml_get(lnum);
8892 else
8893 p = (char_u *)"";
8894
8895 rettv->v_type = VAR_STRING;
8896 rettv->vval.v_string = vim_strsave(p);
8897 }
8898 else
8899 {
8900 end = get_tv_lnum(&argvars[1]);
8901 if (end < lnum)
8902 {
8903 EMSG(_(e_invrange));
8904 rettv->vval.v_number = 0;
8905 }
8906 else
8907 {
8908 l = list_alloc();
8909 if (l != NULL)
8910 {
8911 if (lnum < 1)
8912 lnum = 1;
8913 if (end > curbuf->b_ml.ml_line_count)
8914 end = curbuf->b_ml.ml_line_count;
8915 while (lnum <= end)
8916 {
8917 li = listitem_alloc();
8918 if (li == NULL)
8919 break;
8920 list_append(l, li);
8921 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008922 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008923 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8924 }
8925 rettv->vval.v_list = l;
8926 rettv->v_type = VAR_LIST;
8927 ++l->lv_refcount;
8928 }
8929 }
8930 }
8931}
8932
8933/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00008934 * "getqflist()" function
8935 */
8936/*ARGSUSED*/
8937 static void
8938f_getqflist(argvars, rettv)
8939 typval_T *argvars;
8940 typval_T *rettv;
8941{
8942#ifdef FEAT_QUICKFIX
8943 list_T *l;
8944#endif
8945
8946 rettv->vval.v_number = FALSE;
8947#ifdef FEAT_QUICKFIX
8948 l = list_alloc();
8949 if (l != NULL)
8950 {
8951 if (get_errorlist(l) != FAIL)
8952 {
8953 rettv->vval.v_list = l;
8954 rettv->v_type = VAR_LIST;
8955 ++l->lv_refcount;
8956 }
8957 else
8958 list_free(l);
8959 }
8960#endif
8961}
8962
8963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 * "getreg()" function
8965 */
8966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
8971 char_u *strregname;
8972 int regname;
8973
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008974 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008977 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 regname = (strregname == NULL ? '"' : *strregname);
8979 if (regname == 0)
8980 regname = '"';
8981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->v_type = VAR_STRING;
8983 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
8987 * "getregtype()" function
8988 */
8989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 char_u *strregname;
8995 int regname;
8996 char_u buf[NUMBUFLEN + 2];
8997 long reglen = 0;
8998
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 else
9002 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004
9005 regname = (strregname == NULL ? '"' : *strregname);
9006 if (regname == 0)
9007 regname = '"';
9008
9009 buf[0] = NUL;
9010 buf[1] = NUL;
9011 switch (get_reg_type(regname, &reglen))
9012 {
9013 case MLINE: buf[0] = 'V'; break;
9014 case MCHAR: buf[0] = 'v'; break;
9015#ifdef FEAT_VISUAL
9016 case MBLOCK:
9017 buf[0] = Ctrl_V;
9018 sprintf((char *)buf + 1, "%ld", reglen + 1);
9019 break;
9020#endif
9021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 rettv->v_type = VAR_STRING;
9023 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024}
9025
9026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 * "getwinposx()" function
9028 */
9029/*ARGSUSED*/
9030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031f_getwinposx(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 = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044#endif
9045}
9046
9047/*
9048 * "getwinposy()" function
9049 */
9050/*ARGSUSED*/
9051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009053 typval_T *argvars;
9054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057#ifdef FEAT_GUI
9058 if (gui.in_use)
9059 {
9060 int x, y;
9061
9062 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
9065#endif
9066}
9067
9068/*
9069 * "getwinvar()" function
9070 */
9071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009073 typval_T *argvars;
9074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
9076 win_T *win, *oldcurwin;
9077 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 ++emsg_off;
9081 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->v_type = VAR_STRING;
9085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
9087 if (win != NULL && varname != NULL)
9088 {
9089 if (*varname == '&') /* window-local-option */
9090 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009091 /* Set curwin to be our win, temporarily. Also set curbuf, so
9092 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 oldcurwin = curwin;
9094 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009095 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098
9099 /* restore previous notion of curwin */
9100 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009101 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 }
9103 else
9104 {
9105 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009106 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110 }
9111
9112 --emsg_off;
9113}
9114
9115/*
9116 * "glob()" function
9117 */
9118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 typval_T *argvars;
9121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 expand_T xpc;
9124
9125 ExpandInit(&xpc);
9126 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->v_type = VAR_STRING;
9128 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9130 ExpandCleanup(&xpc);
9131}
9132
9133/*
9134 * "globpath()" function
9135 */
9136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009138 typval_T *argvars;
9139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141 char_u buf1[NUMBUFLEN];
9142
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 rettv->v_type = VAR_STRING;
9144 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9145 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146}
9147
9148/*
9149 * "has()" function
9150 */
9151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 int i;
9157 char_u *name;
9158 int n = FALSE;
9159 static char *(has_list[]) =
9160 {
9161#ifdef AMIGA
9162 "amiga",
9163# ifdef FEAT_ARP
9164 "arp",
9165# endif
9166#endif
9167#ifdef __BEOS__
9168 "beos",
9169#endif
9170#ifdef MSDOS
9171# ifdef DJGPP
9172 "dos32",
9173# else
9174 "dos16",
9175# endif
9176#endif
9177#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9178 "mac",
9179#endif
9180#if defined(MACOS_X_UNIX)
9181 "macunix",
9182#endif
9183#ifdef OS2
9184 "os2",
9185#endif
9186#ifdef __QNX__
9187 "qnx",
9188#endif
9189#ifdef RISCOS
9190 "riscos",
9191#endif
9192#ifdef UNIX
9193 "unix",
9194#endif
9195#ifdef VMS
9196 "vms",
9197#endif
9198#ifdef WIN16
9199 "win16",
9200#endif
9201#ifdef WIN32
9202 "win32",
9203#endif
9204#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9205 "win32unix",
9206#endif
9207#ifdef WIN64
9208 "win64",
9209#endif
9210#ifdef EBCDIC
9211 "ebcdic",
9212#endif
9213#ifndef CASE_INSENSITIVE_FILENAME
9214 "fname_case",
9215#endif
9216#ifdef FEAT_ARABIC
9217 "arabic",
9218#endif
9219#ifdef FEAT_AUTOCMD
9220 "autocmd",
9221#endif
9222#ifdef FEAT_BEVAL
9223 "balloon_eval",
9224#endif
9225#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9226 "builtin_terms",
9227# ifdef ALL_BUILTIN_TCAPS
9228 "all_builtin_terms",
9229# endif
9230#endif
9231#ifdef FEAT_BYTEOFF
9232 "byte_offset",
9233#endif
9234#ifdef FEAT_CINDENT
9235 "cindent",
9236#endif
9237#ifdef FEAT_CLIENTSERVER
9238 "clientserver",
9239#endif
9240#ifdef FEAT_CLIPBOARD
9241 "clipboard",
9242#endif
9243#ifdef FEAT_CMDL_COMPL
9244 "cmdline_compl",
9245#endif
9246#ifdef FEAT_CMDHIST
9247 "cmdline_hist",
9248#endif
9249#ifdef FEAT_COMMENTS
9250 "comments",
9251#endif
9252#ifdef FEAT_CRYPT
9253 "cryptv",
9254#endif
9255#ifdef FEAT_CSCOPE
9256 "cscope",
9257#endif
9258#ifdef DEBUG
9259 "debug",
9260#endif
9261#ifdef FEAT_CON_DIALOG
9262 "dialog_con",
9263#endif
9264#ifdef FEAT_GUI_DIALOG
9265 "dialog_gui",
9266#endif
9267#ifdef FEAT_DIFF
9268 "diff",
9269#endif
9270#ifdef FEAT_DIGRAPHS
9271 "digraphs",
9272#endif
9273#ifdef FEAT_DND
9274 "dnd",
9275#endif
9276#ifdef FEAT_EMACS_TAGS
9277 "emacs_tags",
9278#endif
9279 "eval", /* always present, of course! */
9280#ifdef FEAT_EX_EXTRA
9281 "ex_extra",
9282#endif
9283#ifdef FEAT_SEARCH_EXTRA
9284 "extra_search",
9285#endif
9286#ifdef FEAT_FKMAP
9287 "farsi",
9288#endif
9289#ifdef FEAT_SEARCHPATH
9290 "file_in_path",
9291#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009292#if defined(UNIX) && !defined(USE_SYSTEM)
9293 "filterpipe",
9294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295#ifdef FEAT_FIND_ID
9296 "find_in_path",
9297#endif
9298#ifdef FEAT_FOLDING
9299 "folding",
9300#endif
9301#ifdef FEAT_FOOTER
9302 "footer",
9303#endif
9304#if !defined(USE_SYSTEM) && defined(UNIX)
9305 "fork",
9306#endif
9307#ifdef FEAT_GETTEXT
9308 "gettext",
9309#endif
9310#ifdef FEAT_GUI
9311 "gui",
9312#endif
9313#ifdef FEAT_GUI_ATHENA
9314# ifdef FEAT_GUI_NEXTAW
9315 "gui_neXtaw",
9316# else
9317 "gui_athena",
9318# endif
9319#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009320#ifdef FEAT_GUI_KDE
9321 "gui_kde",
9322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#ifdef FEAT_GUI_GTK
9324 "gui_gtk",
9325# ifdef HAVE_GTK2
9326 "gui_gtk2",
9327# endif
9328#endif
9329#ifdef FEAT_GUI_MAC
9330 "gui_mac",
9331#endif
9332#ifdef FEAT_GUI_MOTIF
9333 "gui_motif",
9334#endif
9335#ifdef FEAT_GUI_PHOTON
9336 "gui_photon",
9337#endif
9338#ifdef FEAT_GUI_W16
9339 "gui_win16",
9340#endif
9341#ifdef FEAT_GUI_W32
9342 "gui_win32",
9343#endif
9344#ifdef FEAT_HANGULIN
9345 "hangul_input",
9346#endif
9347#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9348 "iconv",
9349#endif
9350#ifdef FEAT_INS_EXPAND
9351 "insert_expand",
9352#endif
9353#ifdef FEAT_JUMPLIST
9354 "jumplist",
9355#endif
9356#ifdef FEAT_KEYMAP
9357 "keymap",
9358#endif
9359#ifdef FEAT_LANGMAP
9360 "langmap",
9361#endif
9362#ifdef FEAT_LIBCALL
9363 "libcall",
9364#endif
9365#ifdef FEAT_LINEBREAK
9366 "linebreak",
9367#endif
9368#ifdef FEAT_LISP
9369 "lispindent",
9370#endif
9371#ifdef FEAT_LISTCMDS
9372 "listcmds",
9373#endif
9374#ifdef FEAT_LOCALMAP
9375 "localmap",
9376#endif
9377#ifdef FEAT_MENU
9378 "menu",
9379#endif
9380#ifdef FEAT_SESSION
9381 "mksession",
9382#endif
9383#ifdef FEAT_MODIFY_FNAME
9384 "modify_fname",
9385#endif
9386#ifdef FEAT_MOUSE
9387 "mouse",
9388#endif
9389#ifdef FEAT_MOUSESHAPE
9390 "mouseshape",
9391#endif
9392#if defined(UNIX) || defined(VMS)
9393# ifdef FEAT_MOUSE_DEC
9394 "mouse_dec",
9395# endif
9396# ifdef FEAT_MOUSE_GPM
9397 "mouse_gpm",
9398# endif
9399# ifdef FEAT_MOUSE_JSB
9400 "mouse_jsbterm",
9401# endif
9402# ifdef FEAT_MOUSE_NET
9403 "mouse_netterm",
9404# endif
9405# ifdef FEAT_MOUSE_PTERM
9406 "mouse_pterm",
9407# endif
9408# ifdef FEAT_MOUSE_XTERM
9409 "mouse_xterm",
9410# endif
9411#endif
9412#ifdef FEAT_MBYTE
9413 "multi_byte",
9414#endif
9415#ifdef FEAT_MBYTE_IME
9416 "multi_byte_ime",
9417#endif
9418#ifdef FEAT_MULTI_LANG
9419 "multi_lang",
9420#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009421#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009422#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009423 "mzscheme",
9424#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#ifdef FEAT_OLE
9427 "ole",
9428#endif
9429#ifdef FEAT_OSFILETYPE
9430 "osfiletype",
9431#endif
9432#ifdef FEAT_PATH_EXTRA
9433 "path_extra",
9434#endif
9435#ifdef FEAT_PERL
9436#ifndef DYNAMIC_PERL
9437 "perl",
9438#endif
9439#endif
9440#ifdef FEAT_PYTHON
9441#ifndef DYNAMIC_PYTHON
9442 "python",
9443#endif
9444#endif
9445#ifdef FEAT_POSTSCRIPT
9446 "postscript",
9447#endif
9448#ifdef FEAT_PRINTER
9449 "printer",
9450#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009451#ifdef FEAT_PROFILE
9452 "profile",
9453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454#ifdef FEAT_QUICKFIX
9455 "quickfix",
9456#endif
9457#ifdef FEAT_RIGHTLEFT
9458 "rightleft",
9459#endif
9460#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9461 "ruby",
9462#endif
9463#ifdef FEAT_SCROLLBIND
9464 "scrollbind",
9465#endif
9466#ifdef FEAT_CMDL_INFO
9467 "showcmd",
9468 "cmdline_info",
9469#endif
9470#ifdef FEAT_SIGNS
9471 "signs",
9472#endif
9473#ifdef FEAT_SMARTINDENT
9474 "smartindent",
9475#endif
9476#ifdef FEAT_SNIFF
9477 "sniff",
9478#endif
9479#ifdef FEAT_STL_OPT
9480 "statusline",
9481#endif
9482#ifdef FEAT_SUN_WORKSHOP
9483 "sun_workshop",
9484#endif
9485#ifdef FEAT_NETBEANS_INTG
9486 "netbeans_intg",
9487#endif
9488#ifdef FEAT_SYN_HL
9489 "syntax",
9490#endif
9491#if defined(USE_SYSTEM) || !defined(UNIX)
9492 "system",
9493#endif
9494#ifdef FEAT_TAG_BINS
9495 "tag_binary",
9496#endif
9497#ifdef FEAT_TAG_OLDSTATIC
9498 "tag_old_static",
9499#endif
9500#ifdef FEAT_TAG_ANYWHITE
9501 "tag_any_white",
9502#endif
9503#ifdef FEAT_TCL
9504# ifndef DYNAMIC_TCL
9505 "tcl",
9506# endif
9507#endif
9508#ifdef TERMINFO
9509 "terminfo",
9510#endif
9511#ifdef FEAT_TERMRESPONSE
9512 "termresponse",
9513#endif
9514#ifdef FEAT_TEXTOBJ
9515 "textobjects",
9516#endif
9517#ifdef HAVE_TGETENT
9518 "tgetent",
9519#endif
9520#ifdef FEAT_TITLE
9521 "title",
9522#endif
9523#ifdef FEAT_TOOLBAR
9524 "toolbar",
9525#endif
9526#ifdef FEAT_USR_CMDS
9527 "user-commands", /* was accidentally included in 5.4 */
9528 "user_commands",
9529#endif
9530#ifdef FEAT_VIMINFO
9531 "viminfo",
9532#endif
9533#ifdef FEAT_VERTSPLIT
9534 "vertsplit",
9535#endif
9536#ifdef FEAT_VIRTUALEDIT
9537 "virtualedit",
9538#endif
9539#ifdef FEAT_VISUAL
9540 "visual",
9541#endif
9542#ifdef FEAT_VISUALEXTRA
9543 "visualextra",
9544#endif
9545#ifdef FEAT_VREPLACE
9546 "vreplace",
9547#endif
9548#ifdef FEAT_WILDIGN
9549 "wildignore",
9550#endif
9551#ifdef FEAT_WILDMENU
9552 "wildmenu",
9553#endif
9554#ifdef FEAT_WINDOWS
9555 "windows",
9556#endif
9557#ifdef FEAT_WAK
9558 "winaltkeys",
9559#endif
9560#ifdef FEAT_WRITEBACKUP
9561 "writebackup",
9562#endif
9563#ifdef FEAT_XIM
9564 "xim",
9565#endif
9566#ifdef FEAT_XFONTSET
9567 "xfontset",
9568#endif
9569#ifdef USE_XSMP
9570 "xsmp",
9571#endif
9572#ifdef USE_XSMP_INTERACT
9573 "xsmp_interact",
9574#endif
9575#ifdef FEAT_XCLIPBOARD
9576 "xterm_clipboard",
9577#endif
9578#ifdef FEAT_XTERM_SAVE
9579 "xterm_save",
9580#endif
9581#if defined(UNIX) && defined(FEAT_X11)
9582 "X11",
9583#endif
9584 NULL
9585 };
9586
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 for (i = 0; has_list[i] != NULL; ++i)
9589 if (STRICMP(name, has_list[i]) == 0)
9590 {
9591 n = TRUE;
9592 break;
9593 }
9594
9595 if (n == FALSE)
9596 {
9597 if (STRNICMP(name, "patch", 5) == 0)
9598 n = has_patch(atoi((char *)name + 5));
9599 else if (STRICMP(name, "vim_starting") == 0)
9600 n = (starting != 0);
9601#ifdef DYNAMIC_TCL
9602 else if (STRICMP(name, "tcl") == 0)
9603 n = tcl_enabled(FALSE);
9604#endif
9605#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9606 else if (STRICMP(name, "iconv") == 0)
9607 n = iconv_enabled(FALSE);
9608#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009609#ifdef DYNAMIC_MZSCHEME
9610 else if (STRICMP(name, "mzscheme") == 0)
9611 n = mzscheme_enabled(FALSE);
9612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613#ifdef DYNAMIC_RUBY
9614 else if (STRICMP(name, "ruby") == 0)
9615 n = ruby_enabled(FALSE);
9616#endif
9617#ifdef DYNAMIC_PYTHON
9618 else if (STRICMP(name, "python") == 0)
9619 n = python_enabled(FALSE);
9620#endif
9621#ifdef DYNAMIC_PERL
9622 else if (STRICMP(name, "perl") == 0)
9623 n = perl_enabled(FALSE);
9624#endif
9625#ifdef FEAT_GUI
9626 else if (STRICMP(name, "gui_running") == 0)
9627 n = (gui.in_use || gui.starting);
9628# ifdef FEAT_GUI_W32
9629 else if (STRICMP(name, "gui_win32s") == 0)
9630 n = gui_is_win32s();
9631# endif
9632# ifdef FEAT_BROWSE
9633 else if (STRICMP(name, "browse") == 0)
9634 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9635# endif
9636#endif
9637#ifdef FEAT_SYN_HL
9638 else if (STRICMP(name, "syntax_items") == 0)
9639 n = syntax_present(curbuf);
9640#endif
9641#if defined(WIN3264)
9642 else if (STRICMP(name, "win95") == 0)
9643 n = mch_windows95();
9644#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009645#ifdef FEAT_NETBEANS_INTG
9646 else if (STRICMP(name, "netbeans_enabled") == 0)
9647 n = usingNetbeans;
9648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 }
9650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652}
9653
9654/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009655 * "has_key()" function
9656 */
9657 static void
9658f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 typval_T *argvars;
9660 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661{
9662 rettv->vval.v_number = 0;
9663 if (argvars[0].v_type != VAR_DICT)
9664 {
9665 EMSG(_(e_dictreq));
9666 return;
9667 }
9668 if (argvars[0].vval.v_dict == NULL)
9669 return;
9670
9671 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009672 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009673}
9674
9675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 * "hasmapto()" function
9677 */
9678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 char_u *name;
9684 char_u *mode;
9685 char_u buf[NUMBUFLEN];
9686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009688 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 mode = (char_u *)"nvo";
9690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697}
9698
9699/*
9700 * "histadd()" function
9701 */
9702/*ARGSUSED*/
9703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 typval_T *argvars;
9706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708#ifdef FEAT_CMDHIST
9709 int histype;
9710 char_u *str;
9711 char_u buf[NUMBUFLEN];
9712#endif
9713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 if (check_restricted() || check_secure())
9716 return;
9717#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 if (histype >= 0)
9720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (*str != NUL)
9723 {
9724 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 return;
9727 }
9728 }
9729#endif
9730}
9731
9732/*
9733 * "histdel()" function
9734 */
9735/*ARGSUSED*/
9736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741#ifdef FEAT_CMDHIST
9742 int n;
9743 char_u buf[NUMBUFLEN];
9744
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009745 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009748 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9751 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 else
9753 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9755 get_tv_string_buf(&argvars[1], buf));
9756 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#endif
9760}
9761
9762/*
9763 * "histget()" function
9764 */
9765/*ARGSUSED*/
9766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771#ifdef FEAT_CMDHIST
9772 int type;
9773 int idx;
9774
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009776 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 idx = get_history_idx(type);
9778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 idx = (int)get_tv_number(&argvars[1]);
9780 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785}
9786
9787/*
9788 * "histnr()" function
9789 */
9790/*ARGSUSED*/
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796 int i;
9797
9798#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 if (i >= HIST_CMD && i < HIST_COUNT)
9801 i = get_history_idx(i);
9802 else
9803#endif
9804 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
9808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 * "highlightID(name)" function
9810 */
9811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009813 typval_T *argvars;
9814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817}
9818
9819/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009820 * "highlight_exists()" function
9821 */
9822 static void
9823f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009824 typval_T *argvars;
9825 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009826{
9827 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9828}
9829
9830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 * "hostname()" function
9832 */
9833/*ARGSUSED*/
9834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009836 typval_T *argvars;
9837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 char_u hostname[256];
9840
9841 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->v_type = VAR_STRING;
9843 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844}
9845
9846/*
9847 * iconv() function
9848 */
9849/*ARGSUSED*/
9850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009852 typval_T *argvars;
9853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
9855#ifdef FEAT_MBYTE
9856 char_u buf1[NUMBUFLEN];
9857 char_u buf2[NUMBUFLEN];
9858 char_u *from, *to, *str;
9859 vimconv_T vimconv;
9860#endif
9861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->v_type = VAR_STRING;
9863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
9865#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 str = get_tv_string(&argvars[0]);
9867 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9868 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 vimconv.vc_type = CONV_NONE;
9870 convert_setup(&vimconv, from, to);
9871
9872 /* If the encodings are equal, no conversion needed. */
9873 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
9878 convert_setup(&vimconv, NULL, NULL);
9879 vim_free(from);
9880 vim_free(to);
9881#endif
9882}
9883
9884/*
9885 * "indent()" function
9886 */
9887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 typval_T *argvars;
9890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
9892 linenr_T lnum;
9893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899}
9900
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901/*
9902 * "index()" function
9903 */
9904 static void
9905f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009906 typval_T *argvars;
9907 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009908{
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 list_T *l;
9910 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009911 long idx = 0;
9912 int ic = FALSE;
9913
9914 rettv->vval.v_number = -1;
9915 if (argvars[0].v_type != VAR_LIST)
9916 {
9917 EMSG(_(e_listreq));
9918 return;
9919 }
9920 l = argvars[0].vval.v_list;
9921 if (l != NULL)
9922 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009923 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009925 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009926 /* Start at specified item. Use the cached index that list_find()
9927 * sets, so that a negative number also works. */
9928 item = list_find(l, get_tv_number(&argvars[2]));
9929 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009930 if (argvars[3].v_type != VAR_UNKNOWN)
9931 ic = get_tv_number(&argvars[3]);
9932 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009933
Bram Moolenaar758711c2005-02-02 23:11:38 +00009934 for ( ; item != NULL; item = item->li_next, ++idx)
9935 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009936 {
9937 rettv->vval.v_number = idx;
9938 break;
9939 }
9940 }
9941}
9942
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943static int inputsecret_flag = 0;
9944
9945/*
9946 * "input()" function
9947 * Also handles inputsecret() when inputsecret is set.
9948 */
9949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009951 typval_T *argvars;
9952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009954 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 char_u *p = NULL;
9956 int c;
9957 char_u buf[NUMBUFLEN];
9958 int cmd_silent_save = cmd_silent;
9959
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
9962#ifdef NO_CONSOLE_INPUT
9963 /* While starting up, there is no place to enter text. */
9964 if (no_console_input())
9965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 return;
9968 }
9969#endif
9970
9971 cmd_silent = FALSE; /* Want to see the prompt. */
9972 if (prompt != NULL)
9973 {
9974 /* Only the part of the message after the last NL is considered as
9975 * prompt for the command line */
9976 p = vim_strrchr(prompt, '\n');
9977 if (p == NULL)
9978 p = prompt;
9979 else
9980 {
9981 ++p;
9982 c = *p;
9983 *p = NUL;
9984 msg_start();
9985 msg_clr_eos();
9986 msg_puts_attr(prompt, echo_attr);
9987 msg_didout = FALSE;
9988 msg_starthere();
9989 *p = c;
9990 }
9991 cmdline_row = msg_row;
9992 }
9993
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009994 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9999
10000 /* since the user typed this, no need to wait for return */
10001 need_wait_return = FALSE;
10002 msg_didout = FALSE;
10003 cmd_silent = cmd_silent_save;
10004}
10005
10006/*
10007 * "inputdialog()" function
10008 */
10009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010011 typval_T *argvars;
10012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014#if defined(FEAT_GUI_TEXTDIALOG)
10015 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10016 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10017 {
10018 char_u *message;
10019 char_u buf[NUMBUFLEN];
10020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010022 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 IObuff[IOSIZE - 1] = NUL;
10026 }
10027 else
10028 IObuff[0] = NUL;
10029 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
10030 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else
10033 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010034 if (argvars[1].v_type != VAR_UNKNOWN
10035 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036 rettv->vval.v_string = vim_strsave(
10037 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
10043 else
10044#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10049
10050/*
10051 * "inputrestore()" function
10052 */
10053/*ARGSUSED*/
10054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010056 typval_T *argvars;
10057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 if (ga_userinput.ga_len > 0)
10060 {
10061 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10063 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 }
10066 else if (p_verbose > 1)
10067 {
10068 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 }
10071}
10072
10073/*
10074 * "inputsave()" function
10075 */
10076/*ARGSUSED*/
10077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010079 typval_T *argvars;
10080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 /* Add an entry to the stack of typehead storage. */
10083 if (ga_grow(&ga_userinput, 1) == OK)
10084 {
10085 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10086 + ga_userinput.ga_len);
10087 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 }
10090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092}
10093
10094/*
10095 * "inputsecret()" function
10096 */
10097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 ++cmdline_star;
10103 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 --cmdline_star;
10106 --inputsecret_flag;
10107}
10108
10109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010110 * "insert()" function
10111 */
10112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010114 typval_T *argvars;
10115 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010116{
10117 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010118 listitem_T *item;
10119 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010120
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010121 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010122 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010123 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010124 else if ((l = argvars[0].vval.v_list) != NULL
10125 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010126 {
10127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010128 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010129
Bram Moolenaar758711c2005-02-02 23:11:38 +000010130 if (before == l->lv_len)
10131 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010132 else
10133 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010134 item = list_find(l, before);
10135 if (item == NULL)
10136 {
10137 EMSGN(_(e_listidx), before);
10138 l = NULL;
10139 }
10140 }
10141 if (l != NULL)
10142 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010143 list_insert_tv(l, &argvars[1], item);
10144 ++l->lv_refcount;
10145 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010146 }
10147 }
10148}
10149
10150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 * "isdirectory()" function
10152 */
10153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010155 typval_T *argvars;
10156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159}
10160
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010161/*
10162 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10163 * it refers to a List or Dictionary that is locked.
10164 */
10165 static int
10166tv_islocked(tv)
10167 typval_T *tv;
10168{
10169 return (tv->v_lock & VAR_LOCKED)
10170 || (tv->v_type == VAR_LIST
10171 && tv->vval.v_list != NULL
10172 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10173 || (tv->v_type == VAR_DICT
10174 && tv->vval.v_dict != NULL
10175 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10176}
10177
10178/*
10179 * "islocked()" function
10180 */
10181 static void
10182f_islocked(argvars, rettv)
10183 typval_T *argvars;
10184 typval_T *rettv;
10185{
10186 lval_T lv;
10187 char_u *end;
10188 dictitem_T *di;
10189
10190 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010191 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10192 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010193 if (end != NULL && lv.ll_name != NULL)
10194 {
10195 if (*end != NUL)
10196 EMSG(_(e_trailing));
10197 else
10198 {
10199 if (lv.ll_tv == NULL)
10200 {
10201 if (check_changedtick(lv.ll_name))
10202 rettv->vval.v_number = 1; /* always locked */
10203 else
10204 {
10205 di = find_var(lv.ll_name, NULL);
10206 if (di != NULL)
10207 {
10208 /* Consider a variable locked when:
10209 * 1. the variable itself is locked
10210 * 2. the value of the variable is locked.
10211 * 3. the List or Dict value is locked.
10212 */
10213 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10214 || tv_islocked(&di->di_tv));
10215 }
10216 }
10217 }
10218 else if (lv.ll_range)
10219 EMSG(_("E745: Range not allowed"));
10220 else if (lv.ll_newkey != NULL)
10221 EMSG2(_(e_dictkey), lv.ll_newkey);
10222 else if (lv.ll_list != NULL)
10223 /* List item. */
10224 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10225 else
10226 /* Dictionary item. */
10227 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10228 }
10229 }
10230
10231 clear_lval(&lv);
10232}
10233
Bram Moolenaar33570922005-01-25 22:26:29 +000010234static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010235
10236/*
10237 * Turn a dict into a list:
10238 * "what" == 0: list of keys
10239 * "what" == 1: list of values
10240 * "what" == 2: list of items
10241 */
10242 static void
10243dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010244 typval_T *argvars;
10245 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010246 int what;
10247{
Bram Moolenaar33570922005-01-25 22:26:29 +000010248 list_T *l;
10249 list_T *l2;
10250 dictitem_T *di;
10251 hashitem_T *hi;
10252 listitem_T *li;
10253 listitem_T *li2;
10254 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010255 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010256
10257 rettv->vval.v_number = 0;
10258 if (argvars[0].v_type != VAR_DICT)
10259 {
10260 EMSG(_(e_dictreq));
10261 return;
10262 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010263 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010264 return;
10265
10266 l = list_alloc();
10267 if (l == NULL)
10268 return;
10269 rettv->v_type = VAR_LIST;
10270 rettv->vval.v_list = l;
10271 ++l->lv_refcount;
10272
Bram Moolenaar33570922005-01-25 22:26:29 +000010273 todo = d->dv_hashtab.ht_used;
10274 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010276 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010277 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010278 --todo;
10279 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010280
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010281 li = listitem_alloc();
10282 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010283 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010284 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010285
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010286 if (what == 0)
10287 {
10288 /* keys() */
10289 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010290 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010291 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10292 }
10293 else if (what == 1)
10294 {
10295 /* values() */
10296 copy_tv(&di->di_tv, &li->li_tv);
10297 }
10298 else
10299 {
10300 /* items() */
10301 l2 = list_alloc();
10302 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010303 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010304 li->li_tv.vval.v_list = l2;
10305 if (l2 == NULL)
10306 break;
10307 ++l2->lv_refcount;
10308
10309 li2 = listitem_alloc();
10310 if (li2 == NULL)
10311 break;
10312 list_append(l2, li2);
10313 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010314 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010315 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10316
10317 li2 = listitem_alloc();
10318 if (li2 == NULL)
10319 break;
10320 list_append(l2, li2);
10321 copy_tv(&di->di_tv, &li2->li_tv);
10322 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010323 }
10324 }
10325}
10326
10327/*
10328 * "items(dict)" function
10329 */
10330 static void
10331f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010334{
10335 dict_list(argvars, rettv, 2);
10336}
10337
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010339 * "join()" function
10340 */
10341 static void
10342f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010343 typval_T *argvars;
10344 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010345{
10346 garray_T ga;
10347 char_u *sep;
10348
10349 rettv->vval.v_number = 0;
10350 if (argvars[0].v_type != VAR_LIST)
10351 {
10352 EMSG(_(e_listreq));
10353 return;
10354 }
10355 if (argvars[0].vval.v_list == NULL)
10356 return;
10357 if (argvars[1].v_type == VAR_UNKNOWN)
10358 sep = (char_u *)" ";
10359 else
10360 sep = get_tv_string(&argvars[1]);
10361
10362 ga_init2(&ga, (int)sizeof(char), 80);
10363 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10364 ga_append(&ga, NUL);
10365
10366 rettv->v_type = VAR_STRING;
10367 rettv->vval.v_string = (char_u *)ga.ga_data;
10368}
10369
10370/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010371 * "keys()" function
10372 */
10373 static void
10374f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 typval_T *argvars;
10376 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010377{
10378 dict_list(argvars, rettv, 0);
10379}
10380
10381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 * "last_buffer_nr()" function.
10383 */
10384/*ARGSUSED*/
10385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 typval_T *argvars;
10388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390 int n = 0;
10391 buf_T *buf;
10392
10393 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10394 if (n < buf->b_fnum)
10395 n = buf->b_fnum;
10396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010398}
10399
10400/*
10401 * "len()" function
10402 */
10403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 typval_T *argvars;
10406 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010407{
10408 switch (argvars[0].v_type)
10409 {
10410 case VAR_STRING:
10411 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 rettv->vval.v_number = (varnumber_T)STRLEN(
10413 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414 break;
10415 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010417 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010418 case VAR_DICT:
10419 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10420 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010421 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010422 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010423 break;
10424 }
10425}
10426
Bram Moolenaar33570922005-01-25 22:26:29 +000010427static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010428
10429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010431 typval_T *argvars;
10432 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010433 int type;
10434{
10435#ifdef FEAT_LIBCALL
10436 char_u *string_in;
10437 char_u **string_result;
10438 int nr_result;
10439#endif
10440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010442 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010446
10447 if (check_restricted() || check_secure())
10448 return;
10449
10450#ifdef FEAT_LIBCALL
10451 /* The first two args must be strings, otherwise its meaningless */
10452 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10453 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010454 string_in = NULL;
10455 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010456 string_in = argvars[2].vval.v_string;
10457 if (type == VAR_NUMBER)
10458 string_result = NULL;
10459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010461 if (mch_libcall(argvars[0].vval.v_string,
10462 argvars[1].vval.v_string,
10463 string_in,
10464 argvars[2].vval.v_number,
10465 string_result,
10466 &nr_result) == OK
10467 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010469 }
10470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
10472
10473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010474 * "libcall()" function
10475 */
10476 static void
10477f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *argvars;
10479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010480{
10481 libcall_common(argvars, rettv, VAR_STRING);
10482}
10483
10484/*
10485 * "libcallnr()" function
10486 */
10487 static void
10488f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010491{
10492 libcall_common(argvars, rettv, VAR_NUMBER);
10493}
10494
10495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "line(string)" function
10497 */
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_line(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 linenr_T lnum = 0;
10504 pos_T *fp;
10505
10506 fp = var2fpos(&argvars[0], TRUE);
10507 if (fp != NULL)
10508 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
10513 * "line2byte(lnum)" function
10514 */
10515/*ARGSUSED*/
10516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523#else
10524 linenr_T lnum;
10525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10531 if (rettv->vval.v_number >= 0)
10532 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533#endif
10534}
10535
10536/*
10537 * "lispindent(lnum)" function
10538 */
10539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010541 typval_T *argvars;
10542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543{
10544#ifdef FEAT_LISP
10545 pos_T pos;
10546 linenr_T lnum;
10547
10548 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10551 {
10552 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 curwin->w_cursor = pos;
10555 }
10556 else
10557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
10562 * "localtime()" function
10563 */
10564/*ARGSUSED*/
10565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010567 typval_T *argvars;
10568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571}
10572
Bram Moolenaar33570922005-01-25 22:26:29 +000010573static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
10575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *argvars;
10578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 int exact;
10580{
10581 char_u *keys;
10582 char_u *which;
10583 char_u buf[NUMBUFLEN];
10584 char_u *keys_buf = NULL;
10585 char_u *rhs;
10586 int mode;
10587 garray_T ga;
10588
10589 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 rettv->v_type = VAR_STRING;
10591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 if (*keys == NUL)
10595 return;
10596
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010597 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 else
10600 which = (char_u *)"";
10601 mode = get_map_mode(&which, 0);
10602
10603 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000010604 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 vim_free(keys_buf);
10606 if (rhs != NULL)
10607 {
10608 ga_init(&ga);
10609 ga.ga_itemsize = 1;
10610 ga.ga_growsize = 40;
10611
10612 while (*rhs != NUL)
10613 ga_concat(&ga, str2special(&rhs, FALSE));
10614
10615 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 }
10618}
10619
10620/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621 * "map()" function
10622 */
10623 static void
10624f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 typval_T *argvars;
10626 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627{
10628 filter_map(argvars, rettv, TRUE);
10629}
10630
10631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 */
10634 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010636 typval_T *argvars;
10637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010639 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640}
10641
10642/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010643 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 */
10645 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010646f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010647 typval_T *argvars;
10648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010650 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651}
10652
Bram Moolenaar33570922005-01-25 22:26:29 +000010653static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
10655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010657 typval_T *argvars;
10658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 int type;
10660{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010661 char_u *str = NULL;
10662 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 char_u *pat;
10664 regmatch_T regmatch;
10665 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 char_u *save_cpo;
10668 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010669 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010670 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 list_T *l = NULL;
10672 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010673 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010674 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675
10676 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10677 save_cpo = p_cpo;
10678 p_cpo = (char_u *)"";
10679
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010680 rettv->vval.v_number = -1;
10681 if (type == 3)
10682 {
10683 /* return empty list when there are no matches */
10684 if ((rettv->vval.v_list = list_alloc()) == NULL)
10685 goto theend;
10686 rettv->v_type = VAR_LIST;
10687 ++rettv->vval.v_list->lv_refcount;
10688 }
10689 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->v_type = VAR_STRING;
10692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010695 if (argvars[0].v_type == VAR_LIST)
10696 {
10697 if ((l = argvars[0].vval.v_list) == NULL)
10698 goto theend;
10699 li = l->lv_first;
10700 }
10701 else
10702 expr = str = get_tv_string(&argvars[0]);
10703
10704 pat = get_tv_string_buf(&argvars[1], patbuf);
10705
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010709 if (l != NULL)
10710 {
10711 li = list_find(l, start);
10712 if (li == NULL)
10713 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010714 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010715 }
10716 else
10717 {
10718 if (start < 0)
10719 start = 0;
10720 if (start > (long)STRLEN(str))
10721 goto theend;
10722 str += start;
10723 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010724
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010725 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 }
10728
10729 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10730 if (regmatch.regprog != NULL)
10731 {
10732 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010733
10734 while (1)
10735 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010736 if (l != NULL)
10737 {
10738 if (li == NULL)
10739 {
10740 match = FALSE;
10741 break;
10742 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010743 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010744 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010745 if (str == NULL)
10746 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010747 }
10748
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010749 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010750
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010751 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010752 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010753 if (l == NULL && !match)
10754 break;
10755
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010756 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010757 if (l != NULL)
10758 {
10759 li = li->li_next;
10760 ++idx;
10761 }
10762 else
10763 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010764#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010765 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010766#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010767 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010768#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010769 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010770 }
10771
10772 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010774 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010775 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010776 int i;
10777
10778 /* return list with matched string and submatches */
10779 for (i = 0; i < NSUBEXP; ++i)
10780 {
10781 if (regmatch.endp[i] == NULL)
10782 break;
10783 li = listitem_alloc();
10784 if (li == NULL)
10785 break;
10786 li->li_tv.v_type = VAR_STRING;
10787 li->li_tv.v_lock = 0;
10788 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10789 (int)(regmatch.endp[i] - regmatch.startp[i]));
10790 list_append(rettv->vval.v_list, li);
10791 }
10792 }
10793 else if (type == 2)
10794 {
10795 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010796 if (l != NULL)
10797 copy_tv(&li->li_tv, rettv);
10798 else
10799 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010801 }
10802 else if (l != NULL)
10803 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 else
10805 {
10806 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 (varnumber_T)(regmatch.startp[0] - str);
10809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 }
10814 }
10815 vim_free(regmatch.regprog);
10816 }
10817
10818theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010819 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 p_cpo = save_cpo;
10821}
10822
10823/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010824 * "match()" function
10825 */
10826 static void
10827f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 typval_T *argvars;
10829 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830{
10831 find_some_match(argvars, rettv, 1);
10832}
10833
10834/*
10835 * "matchend()" function
10836 */
10837 static void
10838f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010839 typval_T *argvars;
10840 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010841{
10842 find_some_match(argvars, rettv, 0);
10843}
10844
10845/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010846 * "matchlist()" function
10847 */
10848 static void
10849f_matchlist(argvars, rettv)
10850 typval_T *argvars;
10851 typval_T *rettv;
10852{
10853 find_some_match(argvars, rettv, 3);
10854}
10855
10856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010857 * "matchstr()" function
10858 */
10859 static void
10860f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010861 typval_T *argvars;
10862 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010863{
10864 find_some_match(argvars, rettv, 2);
10865}
10866
Bram Moolenaar33570922005-01-25 22:26:29 +000010867static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010868
10869 static void
10870max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 typval_T *argvars;
10872 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010873 int domax;
10874{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010875 long n = 0;
10876 long i;
10877
10878 if (argvars[0].v_type == VAR_LIST)
10879 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010880 list_T *l;
10881 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010882
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010883 l = argvars[0].vval.v_list;
10884 if (l != NULL)
10885 {
10886 li = l->lv_first;
10887 if (li != NULL)
10888 {
10889 n = get_tv_number(&li->li_tv);
10890 while (1)
10891 {
10892 li = li->li_next;
10893 if (li == NULL)
10894 break;
10895 i = get_tv_number(&li->li_tv);
10896 if (domax ? i > n : i < n)
10897 n = i;
10898 }
10899 }
10900 }
10901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010902 else if (argvars[0].v_type == VAR_DICT)
10903 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010905 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010907 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010908
10909 d = argvars[0].vval.v_dict;
10910 if (d != NULL)
10911 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010912 todo = d->dv_hashtab.ht_used;
10913 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010915 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010917 --todo;
10918 i = get_tv_number(&HI2DI(hi)->di_tv);
10919 if (first)
10920 {
10921 n = i;
10922 first = FALSE;
10923 }
10924 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010925 n = i;
10926 }
10927 }
10928 }
10929 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010930 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010931 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010932 rettv->vval.v_number = n;
10933}
10934
10935/*
10936 * "max()" function
10937 */
10938 static void
10939f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010940 typval_T *argvars;
10941 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010942{
10943 max_min(argvars, rettv, TRUE);
10944}
10945
10946/*
10947 * "min()" function
10948 */
10949 static void
10950f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010951 typval_T *argvars;
10952 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010953{
10954 max_min(argvars, rettv, FALSE);
10955}
10956
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010957static int mkdir_recurse __ARGS((char_u *dir, int prot));
10958
10959/*
10960 * Create the directory in which "dir" is located, and higher levels when
10961 * needed.
10962 */
10963 static int
10964mkdir_recurse(dir, prot)
10965 char_u *dir;
10966 int prot;
10967{
10968 char_u *p;
10969 char_u *updir;
10970 int r = FAIL;
10971
10972 /* Get end of directory name in "dir".
10973 * We're done when it's "/" or "c:/". */
10974 p = gettail_sep(dir);
10975 if (p <= get_past_head(dir))
10976 return OK;
10977
10978 /* If the directory exists we're done. Otherwise: create it.*/
10979 updir = vim_strnsave(dir, (int)(p - dir));
10980 if (updir == NULL)
10981 return FAIL;
10982 if (mch_isdir(updir))
10983 r = OK;
10984 else if (mkdir_recurse(updir, prot) == OK)
10985 r = vim_mkdir_emsg(updir, prot);
10986 vim_free(updir);
10987 return r;
10988}
10989
10990#ifdef vim_mkdir
10991/*
10992 * "mkdir()" function
10993 */
10994 static void
10995f_mkdir(argvars, rettv)
10996 typval_T *argvars;
10997 typval_T *rettv;
10998{
10999 char_u *dir;
11000 char_u buf[NUMBUFLEN];
11001 int prot = 0755;
11002
11003 rettv->vval.v_number = FAIL;
11004 if (check_restricted() || check_secure())
11005 return;
11006
11007 dir = get_tv_string_buf(&argvars[0], buf);
11008 if (argvars[1].v_type != VAR_UNKNOWN)
11009 {
11010 if (argvars[2].v_type != VAR_UNKNOWN)
11011 prot = get_tv_number(&argvars[2]);
11012 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
11013 mkdir_recurse(dir, prot);
11014 }
11015 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
11016}
11017#endif
11018
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 * "mode()" function
11021 */
11022/*ARGSUSED*/
11023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011025 typval_T *argvars;
11026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027{
11028 char_u buf[2];
11029
11030#ifdef FEAT_VISUAL
11031 if (VIsual_active)
11032 {
11033 if (VIsual_select)
11034 buf[0] = VIsual_mode + 's' - 'v';
11035 else
11036 buf[0] = VIsual_mode;
11037 }
11038 else
11039#endif
11040 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11041 buf[0] = 'r';
11042 else if (State & INSERT)
11043 {
11044 if (State & REPLACE_FLAG)
11045 buf[0] = 'R';
11046 else
11047 buf[0] = 'i';
11048 }
11049 else if (State & CMDLINE)
11050 buf[0] = 'c';
11051 else
11052 buf[0] = 'n';
11053
11054 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->vval.v_string = vim_strsave(buf);
11056 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057}
11058
11059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011060 * "nextnonblank()" function
11061 */
11062 static void
11063f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *argvars;
11065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011066{
11067 linenr_T lnum;
11068
11069 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11070 {
11071 if (lnum > curbuf->b_ml.ml_line_count)
11072 {
11073 lnum = 0;
11074 break;
11075 }
11076 if (*skipwhite(ml_get(lnum)) != NUL)
11077 break;
11078 }
11079 rettv->vval.v_number = lnum;
11080}
11081
11082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 * "nr2char()" function
11084 */
11085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 typval_T *argvars;
11088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
11090 char_u buf[NUMBUFLEN];
11091
11092#ifdef FEAT_MBYTE
11093 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 else
11096#endif
11097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 buf[1] = NUL;
11100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->v_type = VAR_STRING;
11102 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011103}
11104
11105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011106 * "prevnonblank()" function
11107 */
11108 static void
11109f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 typval_T *argvars;
11111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112{
11113 linenr_T lnum;
11114
11115 lnum = get_tv_lnum(argvars);
11116 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11117 lnum = 0;
11118 else
11119 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11120 --lnum;
11121 rettv->vval.v_number = lnum;
11122}
11123
Bram Moolenaar8c711452005-01-14 21:53:12 +000011124/*
11125 * "range()" function
11126 */
11127 static void
11128f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011131{
11132 long start;
11133 long end;
11134 long stride = 1;
11135 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 list_T *l;
11137 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011138
11139 start = get_tv_number(&argvars[0]);
11140 if (argvars[1].v_type == VAR_UNKNOWN)
11141 {
11142 end = start - 1;
11143 start = 0;
11144 }
11145 else
11146 {
11147 end = get_tv_number(&argvars[1]);
11148 if (argvars[2].v_type != VAR_UNKNOWN)
11149 stride = get_tv_number(&argvars[2]);
11150 }
11151
11152 rettv->vval.v_number = 0;
11153 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011154 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011155 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011156 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011157 else
11158 {
11159 l = list_alloc();
11160 if (l != NULL)
11161 {
11162 rettv->v_type = VAR_LIST;
11163 rettv->vval.v_list = l;
11164 ++l->lv_refcount;
11165
11166 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11167 {
11168 li = listitem_alloc();
11169 if (li == NULL)
11170 break;
11171 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011172 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011173 li->li_tv.vval.v_number = i;
11174 list_append(l, li);
11175 }
11176 }
11177 }
11178}
11179
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011180/*
11181 * "readfile()" function
11182 */
11183 static void
11184f_readfile(argvars, rettv)
11185 typval_T *argvars;
11186 typval_T *rettv;
11187{
11188 int binary = FALSE;
11189 char_u *fname;
11190 FILE *fd;
11191 list_T *l;
11192 listitem_T *li;
11193#define FREAD_SIZE 200 /* optimized for text lines */
11194 char_u buf[FREAD_SIZE];
11195 int readlen; /* size of last fread() */
11196 int buflen; /* nr of valid chars in buf[] */
11197 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11198 int tolist; /* first byte in buf[] still to be put in list */
11199 int chop; /* how many CR to chop off */
11200 char_u *prev = NULL; /* previously read bytes, if any */
11201 int prevlen = 0; /* length of "prev" if not NULL */
11202 char_u *s;
11203 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011204 long maxline = MAXLNUM;
11205 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011207 if (argvars[1].v_type != VAR_UNKNOWN)
11208 {
11209 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11210 binary = TRUE;
11211 if (argvars[2].v_type != VAR_UNKNOWN)
11212 maxline = get_tv_number(&argvars[2]);
11213 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011214
11215 l = list_alloc();
11216 if (l == NULL)
11217 return;
11218 rettv->v_type = VAR_LIST;
11219 rettv->vval.v_list = l;
11220 l->lv_refcount = 1;
11221
11222 /* Always open the file in binary mode, library functions have a mind of
11223 * their own about CR-LF conversion. */
11224 fname = get_tv_string(&argvars[0]);
11225 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11226 {
11227 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11228 return;
11229 }
11230
11231 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011232 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011233 {
11234 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11235 buflen = filtd + readlen;
11236 tolist = 0;
11237 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11238 {
11239 if (buf[filtd] == '\n' || readlen <= 0)
11240 {
11241 /* Only when in binary mode add an empty list item when the
11242 * last line ends in a '\n'. */
11243 if (!binary && readlen == 0 && filtd == 0)
11244 break;
11245
11246 /* Found end-of-line or end-of-file: add a text line to the
11247 * list. */
11248 chop = 0;
11249 if (!binary)
11250 while (filtd - chop - 1 >= tolist
11251 && buf[filtd - chop - 1] == '\r')
11252 ++chop;
11253 len = filtd - tolist - chop;
11254 if (prev == NULL)
11255 s = vim_strnsave(buf + tolist, len);
11256 else
11257 {
11258 s = alloc((unsigned)(prevlen + len + 1));
11259 if (s != NULL)
11260 {
11261 mch_memmove(s, prev, prevlen);
11262 vim_free(prev);
11263 prev = NULL;
11264 mch_memmove(s + prevlen, buf + tolist, len);
11265 s[prevlen + len] = NUL;
11266 }
11267 }
11268 tolist = filtd + 1;
11269
11270 li = listitem_alloc();
11271 if (li == NULL)
11272 {
11273 vim_free(s);
11274 break;
11275 }
11276 li->li_tv.v_type = VAR_STRING;
11277 li->li_tv.v_lock = 0;
11278 li->li_tv.vval.v_string = s;
11279 list_append(l, li);
11280
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011281 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011282 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011283 if (readlen <= 0)
11284 break;
11285 }
11286 else if (buf[filtd] == NUL)
11287 buf[filtd] = '\n';
11288 }
11289 if (readlen <= 0)
11290 break;
11291
11292 if (tolist == 0)
11293 {
11294 /* "buf" is full, need to move text to an allocated buffer */
11295 if (prev == NULL)
11296 {
11297 prev = vim_strnsave(buf, buflen);
11298 prevlen = buflen;
11299 }
11300 else
11301 {
11302 s = alloc((unsigned)(prevlen + buflen));
11303 if (s != NULL)
11304 {
11305 mch_memmove(s, prev, prevlen);
11306 mch_memmove(s + prevlen, buf, buflen);
11307 vim_free(prev);
11308 prev = s;
11309 prevlen += buflen;
11310 }
11311 }
11312 filtd = 0;
11313 }
11314 else
11315 {
11316 mch_memmove(buf, buf + tolist, buflen - tolist);
11317 filtd -= tolist;
11318 }
11319 }
11320
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011321 /*
11322 * For a negative line count use only the lines at the end of the file,
11323 * free the rest.
11324 */
11325 if (maxline < 0)
11326 while (cnt > -maxline)
11327 {
11328 listitem_remove(l, l->lv_first);
11329 --cnt;
11330 }
11331
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011332 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011333 fclose(fd);
11334}
11335
11336
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11338static void make_connection __ARGS((void));
11339static int check_connection __ARGS((void));
11340
11341 static void
11342make_connection()
11343{
11344 if (X_DISPLAY == NULL
11345# ifdef FEAT_GUI
11346 && !gui.in_use
11347# endif
11348 )
11349 {
11350 x_force_connect = TRUE;
11351 setup_term_clip();
11352 x_force_connect = FALSE;
11353 }
11354}
11355
11356 static int
11357check_connection()
11358{
11359 make_connection();
11360 if (X_DISPLAY == NULL)
11361 {
11362 EMSG(_("E240: No connection to Vim server"));
11363 return FAIL;
11364 }
11365 return OK;
11366}
11367#endif
11368
11369#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011370static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011371
11372 static void
11373remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *argvars;
11375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376 int expr;
11377{
11378 char_u *server_name;
11379 char_u *keys;
11380 char_u *r = NULL;
11381 char_u buf[NUMBUFLEN];
11382# ifdef WIN32
11383 HWND w;
11384# else
11385 Window w;
11386# endif
11387
11388 if (check_restricted() || check_secure())
11389 return;
11390
11391# ifdef FEAT_X11
11392 if (check_connection() == FAIL)
11393 return;
11394# endif
11395
11396 server_name = get_tv_string(&argvars[0]);
11397 keys = get_tv_string_buf(&argvars[1], buf);
11398# ifdef WIN32
11399 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11400# else
11401 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11402 < 0)
11403# endif
11404 {
11405 if (r != NULL)
11406 EMSG(r); /* sending worked but evaluation failed */
11407 else
11408 EMSG2(_("E241: Unable to send to %s"), server_name);
11409 return;
11410 }
11411
11412 rettv->vval.v_string = r;
11413
11414 if (argvars[2].v_type != VAR_UNKNOWN)
11415 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000011417 char_u str[30];
Bram Moolenaar0d660222005-01-07 21:51:51 +000011418
11419 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 v.di_tv.v_type = VAR_STRING;
11421 v.di_tv.vval.v_string = vim_strsave(str);
11422 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11423 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424 }
11425}
11426#endif
11427
11428/*
11429 * "remote_expr()" function
11430 */
11431/*ARGSUSED*/
11432 static void
11433f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 typval_T *argvars;
11435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011436{
11437 rettv->v_type = VAR_STRING;
11438 rettv->vval.v_string = NULL;
11439#ifdef FEAT_CLIENTSERVER
11440 remote_common(argvars, rettv, TRUE);
11441#endif
11442}
11443
11444/*
11445 * "remote_foreground()" function
11446 */
11447/*ARGSUSED*/
11448 static void
11449f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011450 typval_T *argvars;
11451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011452{
11453 rettv->vval.v_number = 0;
11454#ifdef FEAT_CLIENTSERVER
11455# ifdef WIN32
11456 /* On Win32 it's done in this application. */
11457 serverForeground(get_tv_string(&argvars[0]));
11458# else
11459 /* Send a foreground() expression to the server. */
11460 argvars[1].v_type = VAR_STRING;
11461 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11462 argvars[2].v_type = VAR_UNKNOWN;
11463 remote_common(argvars, rettv, TRUE);
11464 vim_free(argvars[1].vval.v_string);
11465# endif
11466#endif
11467}
11468
11469/*ARGSUSED*/
11470 static void
11471f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011474{
11475#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011477 char_u *s = NULL;
11478# ifdef WIN32
11479 int n = 0;
11480# endif
11481
11482 if (check_restricted() || check_secure())
11483 {
11484 rettv->vval.v_number = -1;
11485 return;
11486 }
11487# ifdef WIN32
11488 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11489 if (n == 0)
11490 rettv->vval.v_number = -1;
11491 else
11492 {
11493 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11494 rettv->vval.v_number = (s != NULL);
11495 }
11496# else
11497 rettv->vval.v_number = 0;
11498 if (check_connection() == FAIL)
11499 return;
11500
11501 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11502 serverStrToWin(get_tv_string(&argvars[0])), &s);
11503# endif
11504
11505 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11506 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011507 v.di_tv.v_type = VAR_STRING;
11508 v.di_tv.vval.v_string = vim_strsave(s);
11509 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11510 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511 }
11512#else
11513 rettv->vval.v_number = -1;
11514#endif
11515}
11516
11517/*ARGSUSED*/
11518 static void
11519f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011520 typval_T *argvars;
11521 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011522{
11523 char_u *r = NULL;
11524
11525#ifdef FEAT_CLIENTSERVER
11526 if (!check_restricted() && !check_secure())
11527 {
11528# ifdef WIN32
11529 /* The server's HWND is encoded in the 'id' parameter */
11530 int n = 0;
11531
11532 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11533 if (n != 0)
11534 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11535 if (r == NULL)
11536# else
11537 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11538 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11539# endif
11540 EMSG(_("E277: Unable to read a server reply"));
11541 }
11542#endif
11543 rettv->v_type = VAR_STRING;
11544 rettv->vval.v_string = r;
11545}
11546
11547/*
11548 * "remote_send()" function
11549 */
11550/*ARGSUSED*/
11551 static void
11552f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 typval_T *argvars;
11554 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555{
11556 rettv->v_type = VAR_STRING;
11557 rettv->vval.v_string = NULL;
11558#ifdef FEAT_CLIENTSERVER
11559 remote_common(argvars, rettv, FALSE);
11560#endif
11561}
11562
11563/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011564 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011565 */
11566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *argvars;
11569 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011570{
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 list_T *l;
11572 listitem_T *item, *item2;
11573 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011574 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011575 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011576 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 dict_T *d;
11578 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011579
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011580 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011581 if (argvars[0].v_type == VAR_DICT)
11582 {
11583 if (argvars[2].v_type != VAR_UNKNOWN)
11584 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011585 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011586 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011587 {
11588 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011589 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011590 if (di == NULL)
11591 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011592 else
11593 {
11594 *rettv = di->di_tv;
11595 init_tv(&di->di_tv);
11596 dictitem_remove(d, di);
11597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011598 }
11599 }
11600 else if (argvars[0].v_type != VAR_LIST)
11601 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011602 else if ((l = argvars[0].vval.v_list) != NULL
11603 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011606 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011607 if (item == NULL)
11608 EMSGN(_(e_listidx), idx);
11609 else
11610 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011611 if (argvars[2].v_type == VAR_UNKNOWN)
11612 {
11613 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011614 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011615 *rettv = item->li_tv;
11616 vim_free(item);
11617 }
11618 else
11619 {
11620 /* Remove range of items, return list with values. */
11621 end = get_tv_number(&argvars[2]);
11622 item2 = list_find(l, end);
11623 if (item2 == NULL)
11624 EMSGN(_(e_listidx), end);
11625 else
11626 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011627 int cnt = 0;
11628
11629 for (li = item; li != NULL; li = li->li_next)
11630 {
11631 ++cnt;
11632 if (li == item2)
11633 break;
11634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011635 if (li == NULL) /* didn't find "item2" after "item" */
11636 EMSG(_(e_invrange));
11637 else
11638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011639 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011640 l = list_alloc();
11641 if (l != NULL)
11642 {
11643 rettv->v_type = VAR_LIST;
11644 rettv->vval.v_list = l;
11645 l->lv_first = item;
11646 l->lv_last = item2;
11647 l->lv_refcount = 1;
11648 item->li_prev = NULL;
11649 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011650 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011651 }
11652 }
11653 }
11654 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 }
11656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657}
11658
11659/*
11660 * "rename({from}, {to})" function
11661 */
11662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *argvars;
11665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
11667 char_u buf[NUMBUFLEN];
11668
11669 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11673 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674}
11675
11676/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011677 * "repeat()" function
11678 */
11679/*ARGSUSED*/
11680 static void
11681f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 typval_T *argvars;
11683 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011684{
11685 char_u *p;
11686 int n;
11687 int slen;
11688 int len;
11689 char_u *r;
11690 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011692
11693 n = get_tv_number(&argvars[1]);
11694 if (argvars[0].v_type == VAR_LIST)
11695 {
11696 l = list_alloc();
11697 if (l != NULL && argvars[0].vval.v_list != NULL)
11698 {
11699 l->lv_refcount = 1;
11700 while (n-- > 0)
11701 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11702 break;
11703 }
11704 rettv->v_type = VAR_LIST;
11705 rettv->vval.v_list = l;
11706 }
11707 else
11708 {
11709 p = get_tv_string(&argvars[0]);
11710 rettv->v_type = VAR_STRING;
11711 rettv->vval.v_string = NULL;
11712
11713 slen = (int)STRLEN(p);
11714 len = slen * n;
11715 if (len <= 0)
11716 return;
11717
11718 r = alloc(len + 1);
11719 if (r != NULL)
11720 {
11721 for (i = 0; i < n; i++)
11722 mch_memmove(r + i * slen, p, (size_t)slen);
11723 r[len] = NUL;
11724 }
11725
11726 rettv->vval.v_string = r;
11727 }
11728}
11729
11730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 * "resolve()" function
11732 */
11733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011735 typval_T *argvars;
11736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737{
11738 char_u *p;
11739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741#ifdef FEAT_SHORTCUT
11742 {
11743 char_u *v = NULL;
11744
11745 v = mch_resolve_shortcut(p);
11746 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 }
11751#else
11752# ifdef HAVE_READLINK
11753 {
11754 char_u buf[MAXPATHL + 1];
11755 char_u *cpy;
11756 int len;
11757 char_u *remain = NULL;
11758 char_u *q;
11759 int is_relative_to_current = FALSE;
11760 int has_trailing_pathsep = FALSE;
11761 int limit = 100;
11762
11763 p = vim_strsave(p);
11764
11765 if (p[0] == '.' && (vim_ispathsep(p[1])
11766 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11767 is_relative_to_current = TRUE;
11768
11769 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011770 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 has_trailing_pathsep = TRUE;
11772
11773 q = getnextcomp(p);
11774 if (*q != NUL)
11775 {
11776 /* Separate the first path component in "p", and keep the
11777 * remainder (beginning with the path separator). */
11778 remain = vim_strsave(q - 1);
11779 q[-1] = NUL;
11780 }
11781
11782 for (;;)
11783 {
11784 for (;;)
11785 {
11786 len = readlink((char *)p, (char *)buf, MAXPATHL);
11787 if (len <= 0)
11788 break;
11789 buf[len] = NUL;
11790
11791 if (limit-- == 0)
11792 {
11793 vim_free(p);
11794 vim_free(remain);
11795 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 goto fail;
11798 }
11799
11800 /* Ensure that the result will have a trailing path separator
11801 * if the argument has one. */
11802 if (remain == NULL && has_trailing_pathsep)
11803 add_pathsep(buf);
11804
11805 /* Separate the first path component in the link value and
11806 * concatenate the remainders. */
11807 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11808 if (*q != NUL)
11809 {
11810 if (remain == NULL)
11811 remain = vim_strsave(q - 1);
11812 else
11813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011814 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 if (cpy != NULL)
11816 {
11817 STRCAT(cpy, remain);
11818 vim_free(remain);
11819 remain = cpy;
11820 }
11821 }
11822 q[-1] = NUL;
11823 }
11824
11825 q = gettail(p);
11826 if (q > p && *q == NUL)
11827 {
11828 /* Ignore trailing path separator. */
11829 q[-1] = NUL;
11830 q = gettail(p);
11831 }
11832 if (q > p && !mch_isFullName(buf))
11833 {
11834 /* symlink is relative to directory of argument */
11835 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11836 if (cpy != NULL)
11837 {
11838 STRCPY(cpy, p);
11839 STRCPY(gettail(cpy), buf);
11840 vim_free(p);
11841 p = cpy;
11842 }
11843 }
11844 else
11845 {
11846 vim_free(p);
11847 p = vim_strsave(buf);
11848 }
11849 }
11850
11851 if (remain == NULL)
11852 break;
11853
11854 /* Append the first path component of "remain" to "p". */
11855 q = getnextcomp(remain + 1);
11856 len = q - remain - (*q != NUL);
11857 cpy = vim_strnsave(p, STRLEN(p) + len);
11858 if (cpy != NULL)
11859 {
11860 STRNCAT(cpy, remain, len);
11861 vim_free(p);
11862 p = cpy;
11863 }
11864 /* Shorten "remain". */
11865 if (*q != NUL)
11866 STRCPY(remain, q - 1);
11867 else
11868 {
11869 vim_free(remain);
11870 remain = NULL;
11871 }
11872 }
11873
11874 /* If the result is a relative path name, make it explicitly relative to
11875 * the current directory if and only if the argument had this form. */
11876 if (!vim_ispathsep(*p))
11877 {
11878 if (is_relative_to_current
11879 && *p != NUL
11880 && !(p[0] == '.'
11881 && (p[1] == NUL
11882 || vim_ispathsep(p[1])
11883 || (p[1] == '.'
11884 && (p[2] == NUL
11885 || vim_ispathsep(p[2]))))))
11886 {
11887 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011888 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 if (cpy != NULL)
11890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 vim_free(p);
11892 p = cpy;
11893 }
11894 }
11895 else if (!is_relative_to_current)
11896 {
11897 /* Strip leading "./". */
11898 q = p;
11899 while (q[0] == '.' && vim_ispathsep(q[1]))
11900 q += 2;
11901 if (q > p)
11902 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11903 }
11904 }
11905
11906 /* Ensure that the result will have no trailing path separator
11907 * if the argument had none. But keep "/" or "//". */
11908 if (!has_trailing_pathsep)
11909 {
11910 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011911 if (after_pathsep(p, q))
11912 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 }
11914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 }
11917# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919# endif
11920#endif
11921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011922 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
11924#ifdef HAVE_READLINK
11925fail:
11926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011931 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 */
11933 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011934f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *argvars;
11936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937{
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 list_T *l;
11939 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940
Bram Moolenaar0d660222005-01-07 21:51:51 +000011941 rettv->vval.v_number = 0;
11942 if (argvars[0].v_type != VAR_LIST)
11943 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011944 else if ((l = argvars[0].vval.v_list) != NULL
11945 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011946 {
11947 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011948 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011949 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011950 while (li != NULL)
11951 {
11952 ni = li->li_prev;
11953 list_append(l, li);
11954 li = ni;
11955 }
11956 rettv->vval.v_list = l;
11957 rettv->v_type = VAR_LIST;
11958 ++l->lv_refcount;
11959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960}
11961
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011962#define SP_NOMOVE 1 /* don't move cursor */
11963#define SP_REPEAT 2 /* repeat to find outer pair */
11964#define SP_RETCOUNT 4 /* return matchcount */
11965
Bram Moolenaar33570922005-01-25 22:26:29 +000011966static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967
11968/*
11969 * Get flags for a search function.
11970 * Possibly sets "p_ws".
11971 * Returns BACKWARD, FORWARD or zero (for an error).
11972 */
11973 static int
11974get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011975 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011976 int *flagsp;
11977{
11978 int dir = FORWARD;
11979 char_u *flags;
11980 char_u nbuf[NUMBUFLEN];
11981 int mask;
11982
11983 if (varp->v_type != VAR_UNKNOWN)
11984 {
11985 flags = get_tv_string_buf(varp, nbuf);
11986 while (*flags != NUL)
11987 {
11988 switch (*flags)
11989 {
11990 case 'b': dir = BACKWARD; break;
11991 case 'w': p_ws = TRUE; break;
11992 case 'W': p_ws = FALSE; break;
11993 default: mask = 0;
11994 if (flagsp != NULL)
11995 switch (*flags)
11996 {
11997 case 'n': mask = SP_NOMOVE; break;
11998 case 'r': mask = SP_REPEAT; break;
11999 case 'm': mask = SP_RETCOUNT; break;
12000 }
12001 if (mask == 0)
12002 {
12003 EMSG2(_(e_invarg2), flags);
12004 dir = 0;
12005 }
12006 else
12007 *flagsp |= mask;
12008 }
12009 if (dir == 0)
12010 break;
12011 ++flags;
12012 }
12013 }
12014 return dir;
12015}
12016
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017/*
12018 * "search()" function
12019 */
12020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024{
12025 char_u *pat;
12026 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012027 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 int save_p_ws = p_ws;
12029 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012030 int flags = 0;
12031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012035 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12036 if (dir == 0)
12037 goto theend;
12038 if ((flags & ~SP_NOMOVE) != 0)
12039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012041 goto theend;
12042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012044 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12046 SEARCH_KEEP, RE_SEARCH) != FAIL)
12047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 curwin->w_cursor = pos;
12050 /* "/$" will put the cursor after the end of the line, may need to
12051 * correct that here */
12052 check_cursor();
12053 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012054
12055 /* If 'n' flag is used: restore cursor position. */
12056 if (flags & SP_NOMOVE)
12057 curwin->w_cursor = save_cursor;
12058theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 p_ws = save_p_ws;
12060}
12061
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062/*
12063 * "searchpair()" function
12064 */
12065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012067 typval_T *argvars;
12068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069{
12070 char_u *spat, *mpat, *epat;
12071 char_u *skip;
12072 char_u *pat, *pat2, *pat3;
12073 pos_T pos;
12074 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012075 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 pos_T save_cursor;
12077 pos_T save_pos;
12078 int save_p_ws = p_ws;
12079 char_u *save_cpo;
12080 int dir;
12081 int flags = 0;
12082 char_u nbuf1[NUMBUFLEN];
12083 char_u nbuf2[NUMBUFLEN];
12084 char_u nbuf3[NUMBUFLEN];
12085 int n;
12086 int r;
12087 int nest = 1;
12088 int err;
12089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091
12092 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12093 save_cpo = p_cpo;
12094 p_cpo = (char_u *)"";
12095
12096 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 spat = get_tv_string(&argvars[0]);
12098 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12099 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100
12101 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12102 * start/middle/end (pat3, for the top pair). */
12103 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12104 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12105 if (pat2 == NULL || pat3 == NULL)
12106 goto theend;
12107 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12108 if (*mpat == NUL)
12109 STRCPY(pat3, pat2);
12110 else
12111 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12112 spat, epat, mpat);
12113
12114 /* Handle the optional fourth argument: flags */
12115 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012116 if (dir == 0)
12117 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118
12119 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012120 if (argvars[3].v_type == VAR_UNKNOWN
12121 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 skip = (char_u *)"";
12123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012124 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125
12126 save_cursor = curwin->w_cursor;
12127 pos = curwin->w_cursor;
12128 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012129 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 pat = pat3;
12131 for (;;)
12132 {
12133 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12134 SEARCH_KEEP, RE_SEARCH);
12135 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12136 /* didn't find it or found the first match again: FAIL */
12137 break;
12138
12139 if (firstpos.lnum == 0)
12140 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012141 if (equalpos(pos, foundpos))
12142 {
12143 /* Found the same position again. Can happen with a pattern that
12144 * has "\zs" at the end and searching backwards. Advance one
12145 * character and try again. */
12146 if (dir == BACKWARD)
12147 decl(&pos);
12148 else
12149 incl(&pos);
12150 }
12151 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152
12153 /* If the skip pattern matches, ignore this match. */
12154 if (*skip != NUL)
12155 {
12156 save_pos = curwin->w_cursor;
12157 curwin->w_cursor = pos;
12158 r = eval_to_bool(skip, &err, NULL, FALSE);
12159 curwin->w_cursor = save_pos;
12160 if (err)
12161 {
12162 /* Evaluating {skip} caused an error, break here. */
12163 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 break;
12166 }
12167 if (r)
12168 continue;
12169 }
12170
12171 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12172 {
12173 /* Found end when searching backwards or start when searching
12174 * forward: nested pair. */
12175 ++nest;
12176 pat = pat2; /* nested, don't search for middle */
12177 }
12178 else
12179 {
12180 /* Found end when searching forward or start when searching
12181 * backward: end of (nested) pair; or found middle in outer pair. */
12182 if (--nest == 1)
12183 pat = pat3; /* outer level, search for middle */
12184 }
12185
12186 if (nest == 0)
12187 {
12188 /* Found the match: return matchcount or line number. */
12189 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 curwin->w_cursor = pos;
12194 if (!(flags & SP_REPEAT))
12195 break;
12196 nest = 1; /* search for next unmatched */
12197 }
12198 }
12199
12200 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 curwin->w_cursor = save_cursor;
12203
12204theend:
12205 vim_free(pat2);
12206 vim_free(pat3);
12207 p_ws = save_p_ws;
12208 p_cpo = save_cpo;
12209}
12210
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211/*ARGSUSED*/
12212 static void
12213f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012214 typval_T *argvars;
12215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217#ifdef FEAT_CLIENTSERVER
12218 char_u buf[NUMBUFLEN];
12219 char_u *server = get_tv_string(&argvars[0]);
12220 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221
Bram Moolenaar0d660222005-01-07 21:51:51 +000012222 rettv->vval.v_number = -1;
12223 if (check_restricted() || check_secure())
12224 return;
12225# ifdef FEAT_X11
12226 if (check_connection() == FAIL)
12227 return;
12228# endif
12229
12230 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012232 EMSG(_("E258: Unable to send to client"));
12233 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012235 rettv->vval.v_number = 0;
12236#else
12237 rettv->vval.v_number = -1;
12238#endif
12239}
12240
12241/*ARGSUSED*/
12242 static void
12243f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 typval_T *argvars;
12245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012246{
12247 char_u *r = NULL;
12248
12249#ifdef FEAT_CLIENTSERVER
12250# ifdef WIN32
12251 r = serverGetVimNames();
12252# else
12253 make_connection();
12254 if (X_DISPLAY != NULL)
12255 r = serverGetVimNames(X_DISPLAY);
12256# endif
12257#endif
12258 rettv->v_type = VAR_STRING;
12259 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260}
12261
12262/*
12263 * "setbufvar()" function
12264 */
12265/*ARGSUSED*/
12266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012268 typval_T *argvars;
12269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270{
12271 buf_T *buf;
12272#ifdef FEAT_AUTOCMD
12273 aco_save_T aco;
12274#else
12275 buf_T *save_curbuf;
12276#endif
12277 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012278 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 char_u nbuf[NUMBUFLEN];
12280
12281 if (check_restricted() || check_secure())
12282 return;
12283 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 buf = get_buf_tv(&argvars[0]);
12285 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 varp = &argvars[2];
12287
12288 if (buf != NULL && varname != NULL && varp != NULL)
12289 {
12290 /* set curbuf to be our buf, temporarily */
12291#ifdef FEAT_AUTOCMD
12292 aucmd_prepbuf(&aco, buf);
12293#else
12294 save_curbuf = curbuf;
12295 curbuf = buf;
12296#endif
12297
12298 if (*varname == '&')
12299 {
12300 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 set_option_value(varname, get_tv_number(varp),
12302 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 }
12304 else
12305 {
12306 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12307 if (bufvarname != NULL)
12308 {
12309 STRCPY(bufvarname, "b:");
12310 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012311 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 vim_free(bufvarname);
12313 }
12314 }
12315
12316 /* reset notion of buffer */
12317#ifdef FEAT_AUTOCMD
12318 aucmd_restbuf(&aco);
12319#else
12320 curbuf = save_curbuf;
12321#endif
12322 }
12323 --emsg_off;
12324}
12325
12326/*
12327 * "setcmdpos()" function
12328 */
12329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 typval_T *argvars;
12332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 rettv->vval.v_number = set_cmdline_pos(
12335 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336}
12337
12338/*
12339 * "setline()" function
12340 */
12341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012343 typval_T *argvars;
12344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345{
12346 linenr_T lnum;
12347 char_u *line;
12348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 lnum = get_tv_lnum(argvars);
12350 line = get_tv_string(&argvars[1]);
12351 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
12353 if (lnum >= 1
12354 && lnum <= curbuf->b_ml.ml_line_count
12355 && u_savesub(lnum) == OK
12356 && ml_replace(lnum, line, TRUE) == OK)
12357 {
12358 changed_bytes(lnum, 0);
12359 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 }
12362}
12363
12364/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012365 * "setqflist()" function
12366 */
12367/*ARGSUSED*/
12368 static void
12369f_setqflist(argvars, rettv)
12370 typval_T *argvars;
12371 typval_T *rettv;
12372{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012373 char_u *act;
12374 int action = ' ';
12375
Bram Moolenaar2641f772005-03-25 21:58:17 +000012376 rettv->vval.v_number = -1;
12377
12378#ifdef FEAT_QUICKFIX
12379 if (argvars[0].v_type != VAR_LIST)
12380 EMSG(_(e_listreq));
12381 else
12382 {
12383 list_T *l = argvars[0].vval.v_list;
12384
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012385 if (argvars[1].v_type == VAR_STRING)
12386 {
12387 act = get_tv_string(&argvars[1]);
12388 if (*act == 'a' || *act == 'r')
12389 action = *act;
12390 }
12391
12392 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012393 rettv->vval.v_number = 0;
12394 }
12395#endif
12396}
12397
12398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 * "setreg()" function
12400 */
12401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012403 typval_T *argvars;
12404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405{
12406 int regname;
12407 char_u *strregname;
12408 char_u *stropt;
12409 int append;
12410 char_u yank_type;
12411 long block_len;
12412
12413 block_len = -1;
12414 yank_type = MAUTO;
12415 append = FALSE;
12416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 strregname = get_tv_string(argvars);
12418 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419
12420 regname = (strregname == NULL ? '"' : *strregname);
12421 if (regname == 0 || regname == '@')
12422 regname = '"';
12423 else if (regname == '=')
12424 return;
12425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 switch (*stropt)
12430 {
12431 case 'a': case 'A': /* append */
12432 append = TRUE;
12433 break;
12434 case 'v': case 'c': /* character-wise selection */
12435 yank_type = MCHAR;
12436 break;
12437 case 'V': case 'l': /* line-wise selection */
12438 yank_type = MLINE;
12439 break;
12440#ifdef FEAT_VISUAL
12441 case 'b': case Ctrl_V: /* block-wise selection */
12442 yank_type = MBLOCK;
12443 if (VIM_ISDIGIT(stropt[1]))
12444 {
12445 ++stropt;
12446 block_len = getdigits(&stropt) - 1;
12447 --stropt;
12448 }
12449 break;
12450#endif
12451 }
12452 }
12453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457}
12458
12459
12460/*
12461 * "setwinvar(expr)" function
12462 */
12463/*ARGSUSED*/
12464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012466 typval_T *argvars;
12467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469 win_T *win;
12470#ifdef FEAT_WINDOWS
12471 win_T *save_curwin;
12472#endif
12473 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 char_u nbuf[NUMBUFLEN];
12476
12477 if (check_restricted() || check_secure())
12478 return;
12479 ++emsg_off;
12480 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 varp = &argvars[2];
12483
12484 if (win != NULL && varname != NULL && varp != NULL)
12485 {
12486#ifdef FEAT_WINDOWS
12487 /* set curwin to be our win, temporarily */
12488 save_curwin = curwin;
12489 curwin = win;
12490 curbuf = curwin->w_buffer;
12491#endif
12492
12493 if (*varname == '&')
12494 {
12495 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 set_option_value(varname, get_tv_number(varp),
12497 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 }
12499 else
12500 {
12501 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12502 if (winvarname != NULL)
12503 {
12504 STRCPY(winvarname, "w:");
12505 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012506 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 vim_free(winvarname);
12508 }
12509 }
12510
12511#ifdef FEAT_WINDOWS
12512 /* Restore current window, if it's still valid (autocomands can make
12513 * it invalid). */
12514 if (win_valid(save_curwin))
12515 {
12516 curwin = save_curwin;
12517 curbuf = curwin->w_buffer;
12518 }
12519#endif
12520 }
12521 --emsg_off;
12522}
12523
12524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 */
12527 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012529 typval_T *argvars;
12530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533
Bram Moolenaar0d660222005-01-07 21:51:51 +000012534 p = get_tv_string(&argvars[0]);
12535 rettv->vval.v_string = vim_strsave(p);
12536 simplify_filename(rettv->vval.v_string); /* simplify in place */
12537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538}
12539
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540static int
12541#ifdef __BORLANDC__
12542 _RTLENTRYF
12543#endif
12544 item_compare __ARGS((const void *s1, const void *s2));
12545static int
12546#ifdef __BORLANDC__
12547 _RTLENTRYF
12548#endif
12549 item_compare2 __ARGS((const void *s1, const void *s2));
12550
12551static int item_compare_ic;
12552static char_u *item_compare_func;
12553#define ITEM_COMPARE_FAIL 999
12554
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558 static int
12559#ifdef __BORLANDC__
12560_RTLENTRYF
12561#endif
12562item_compare(s1, s2)
12563 const void *s1;
12564 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566 char_u *p1, *p2;
12567 char_u *tofree1, *tofree2;
12568 int res;
12569 char_u numbuf1[NUMBUFLEN];
12570 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571
Bram Moolenaar33570922005-01-25 22:26:29 +000012572 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12573 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 if (item_compare_ic)
12575 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577 res = STRCMP(p1, p2);
12578 vim_free(tofree1);
12579 vim_free(tofree2);
12580 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581}
12582
12583 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584#ifdef __BORLANDC__
12585_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587item_compare2(s1, s2)
12588 const void *s1;
12589 const void *s2;
12590{
12591 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012592 typval_T rettv;
12593 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012596 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12597 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012598 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12599 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600
12601 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12602 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012603 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604 clear_tv(&argv[0]);
12605 clear_tv(&argv[1]);
12606
12607 if (res == FAIL)
12608 res = ITEM_COMPARE_FAIL;
12609 else
12610 res = get_tv_number(&rettv);
12611 clear_tv(&rettv);
12612 return res;
12613}
12614
12615/*
12616 * "sort({list})" function
12617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 typval_T *argvars;
12621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622{
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 list_T *l;
12624 listitem_T *li;
12625 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012626 long len;
12627 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 rettv->vval.v_number = 0;
12630 if (argvars[0].v_type != VAR_LIST)
12631 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 else
12633 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012635 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012636 return;
12637 rettv->vval.v_list = l;
12638 rettv->v_type = VAR_LIST;
12639 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641 len = list_len(l);
12642 if (len <= 1)
12643 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 item_compare_ic = FALSE;
12646 item_compare_func = NULL;
12647 if (argvars[1].v_type != VAR_UNKNOWN)
12648 {
12649 if (argvars[1].v_type == VAR_FUNC)
12650 item_compare_func = argvars[0].vval.v_string;
12651 else
12652 {
12653 i = get_tv_number(&argvars[1]);
12654 if (i == 1)
12655 item_compare_ic = TRUE;
12656 else
12657 item_compare_func = get_tv_string(&argvars[1]);
12658 }
12659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663 if (ptrs == NULL)
12664 return;
12665 i = 0;
12666 for (li = l->lv_first; li != NULL; li = li->li_next)
12667 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668
Bram Moolenaar0d660222005-01-07 21:51:51 +000012669 /* test the compare function */
12670 if (item_compare_func != NULL
12671 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12672 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012673 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675 {
12676 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012677 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012678 item_compare_func == NULL ? item_compare : item_compare2);
12679
12680 /* Clear the List and append the items in the sorted order. */
12681 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012682 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012683 for (i = 0; i < len; ++i)
12684 list_append(l, ptrs[i]);
12685 }
12686
12687 vim_free(ptrs);
12688 }
12689}
12690
12691 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012692f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012695{
12696 char_u *str;
12697 char_u *end;
12698 char_u *pat;
12699 regmatch_T regmatch;
12700 char_u patbuf[NUMBUFLEN];
12701 char_u *save_cpo;
12702 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012703 listitem_T *ni;
12704 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012705 colnr_T col = 0;
12706
12707 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12708 save_cpo = p_cpo;
12709 p_cpo = (char_u *)"";
12710
12711 str = get_tv_string(&argvars[0]);
12712 if (argvars[1].v_type == VAR_UNKNOWN)
12713 pat = (char_u *)"[\\x01- ]\\+";
12714 else
12715 pat = get_tv_string_buf(&argvars[1], patbuf);
12716
12717 l = list_alloc();
12718 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012720 rettv->v_type = VAR_LIST;
12721 rettv->vval.v_list = l;
12722 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723
Bram Moolenaar0d660222005-01-07 21:51:51 +000012724 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12725 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012727 regmatch.rm_ic = FALSE;
12728 while (*str != NUL)
12729 {
12730 match = vim_regexec_nl(&regmatch, str, col);
12731 if (match)
12732 end = regmatch.startp[0];
12733 else
12734 end = str + STRLEN(str);
12735 if (end > str)
12736 {
12737 ni = listitem_alloc();
12738 if (ni == NULL)
12739 break;
12740 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012741 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012742 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12743 list_append(l, ni);
12744 }
12745 if (!match)
12746 break;
12747 /* Advance to just after the match. */
12748 if (regmatch.endp[0] > str)
12749 col = 0;
12750 else
12751 {
12752 /* Don't get stuck at the same match. */
12753#ifdef FEAT_MBYTE
12754 col = mb_ptr2len_check(regmatch.endp[0]);
12755#else
12756 col = 1;
12757#endif
12758 }
12759 str = regmatch.endp[0];
12760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761
Bram Moolenaar0d660222005-01-07 21:51:51 +000012762 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764
Bram Moolenaar0d660222005-01-07 21:51:51 +000012765 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766}
12767
12768#ifdef HAVE_STRFTIME
12769/*
12770 * "strftime({format}[, {time}])" function
12771 */
12772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012774 typval_T *argvars;
12775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776{
12777 char_u result_buf[256];
12778 struct tm *curtime;
12779 time_t seconds;
12780 char_u *p;
12781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012785 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 seconds = time(NULL);
12787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 curtime = localtime(&seconds);
12790 /* MSVC returns NULL for an invalid value of seconds. */
12791 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 else
12794 {
12795# ifdef FEAT_MBYTE
12796 vimconv_T conv;
12797 char_u *enc;
12798
12799 conv.vc_type = CONV_NONE;
12800 enc = enc_locale();
12801 convert_setup(&conv, p_enc, enc);
12802 if (conv.vc_type != CONV_NONE)
12803 p = string_convert(&conv, p, NULL);
12804# endif
12805 if (p != NULL)
12806 (void)strftime((char *)result_buf, sizeof(result_buf),
12807 (char *)p, curtime);
12808 else
12809 result_buf[0] = NUL;
12810
12811# ifdef FEAT_MBYTE
12812 if (conv.vc_type != CONV_NONE)
12813 vim_free(p);
12814 convert_setup(&conv, enc, p_enc);
12815 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 else
12818# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820
12821# ifdef FEAT_MBYTE
12822 /* Release conversion descriptors */
12823 convert_setup(&conv, NULL, NULL);
12824 vim_free(enc);
12825# endif
12826 }
12827}
12828#endif
12829
12830/*
12831 * "stridx()" function
12832 */
12833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 typval_T *argvars;
12836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
12838 char_u buf[NUMBUFLEN];
12839 char_u *needle;
12840 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 if (argvars[2].v_type != VAR_UNKNOWN)
12850 {
12851 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012852 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012853 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012854 if (start_idx >= 0)
12855 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 }
12857
12858 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12859 if (pos != NULL)
12860 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861}
12862
12863/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012864 * "string()" function
12865 */
12866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 typval_T *argvars;
12869 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012870{
12871 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012872 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012875 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012876 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
12880/*
12881 * "strlen()" function
12882 */
12883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *argvars;
12886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->vval.v_number = (varnumber_T)(STRLEN(
12889 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890}
12891
12892/*
12893 * "strpart()" function
12894 */
12895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *argvars;
12898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899{
12900 char_u *p;
12901 int n;
12902 int len;
12903 int slen;
12904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 slen = (int)STRLEN(p);
12907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012909 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 else
12912 len = slen - n; /* default len: all bytes that are available. */
12913
12914 /*
12915 * Only return the overlap between the specified part and the actual
12916 * string.
12917 */
12918 if (n < 0)
12919 {
12920 len += n;
12921 n = 0;
12922 }
12923 else if (n > slen)
12924 n = slen;
12925 if (len < 0)
12926 len = 0;
12927 else if (n + len > slen)
12928 len = slen - n;
12929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->v_type = VAR_STRING;
12931 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012935 * "strridx()" function
12936 */
12937 static void
12938f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012939 typval_T *argvars;
12940 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012941{
12942 char_u buf[NUMBUFLEN];
12943 char_u *needle;
12944 char_u *haystack;
12945 char_u *rest;
12946 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012947 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012948
12949 needle = get_tv_string(&argvars[1]);
12950 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012951 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012952 if (argvars[2].v_type != VAR_UNKNOWN)
12953 {
12954 /* Third argument: upper limit for index */
12955 end_idx = get_tv_number(&argvars[2]);
12956 if (end_idx < 0)
12957 {
12958 /* can never find a match */
12959 rettv->vval.v_number = -1;
12960 return;
12961 }
12962 }
12963 else
12964 end_idx = haystack_len;
12965
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012967 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012968 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012969 lastmatch = haystack + end_idx;
12970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012972 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012973 for (rest = haystack; *rest != '\0'; ++rest)
12974 {
12975 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012976 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012977 break;
12978 lastmatch = rest;
12979 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012980 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012981
12982 if (lastmatch == NULL)
12983 rettv->vval.v_number = -1;
12984 else
12985 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12986}
12987
12988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 * "strtrans()" function
12990 */
12991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *argvars;
12994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->v_type = VAR_STRING;
12997 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998}
12999
13000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001 * "submatch()" function
13002 */
13003 static void
13004f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013005 typval_T *argvars;
13006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013007{
13008 rettv->v_type = VAR_STRING;
13009 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
13010}
13011
13012/*
13013 * "substitute()" function
13014 */
13015 static void
13016f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013017 typval_T *argvars;
13018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013019{
13020 char_u patbuf[NUMBUFLEN];
13021 char_u subbuf[NUMBUFLEN];
13022 char_u flagsbuf[NUMBUFLEN];
13023
13024 rettv->v_type = VAR_STRING;
13025 rettv->vval.v_string = do_string_sub(
13026 get_tv_string(&argvars[0]),
13027 get_tv_string_buf(&argvars[1], patbuf),
13028 get_tv_string_buf(&argvars[2], subbuf),
13029 get_tv_string_buf(&argvars[3], flagsbuf));
13030}
13031
13032/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013033 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 */
13035/*ARGSUSED*/
13036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013037f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 typval_T *argvars;
13039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040{
13041 int id = 0;
13042#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013043 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 long col;
13045 int trans;
13046
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013047 lnum = get_tv_lnum(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 col = get_tv_number(&argvars[1]) - 1;
13049 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013051 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13052 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13053 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054#endif
13055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057}
13058
13059/*
13060 * "synIDattr(id, what [, mode])" function
13061 */
13062/*ARGSUSED*/
13063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 typval_T *argvars;
13066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067{
13068 char_u *p = NULL;
13069#ifdef FEAT_SYN_HL
13070 int id;
13071 char_u *what;
13072 char_u *mode;
13073 char_u modebuf[NUMBUFLEN];
13074 int modec;
13075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 id = get_tv_number(&argvars[0]);
13077 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013078 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 modec = TOLOWER_ASC(mode[0]);
13082 if (modec != 't' && modec != 'c'
13083#ifdef FEAT_GUI
13084 && modec != 'g'
13085#endif
13086 )
13087 modec = 0; /* replace invalid with current */
13088 }
13089 else
13090 {
13091#ifdef FEAT_GUI
13092 if (gui.in_use)
13093 modec = 'g';
13094 else
13095#endif
13096 if (t_colors > 1)
13097 modec = 'c';
13098 else
13099 modec = 't';
13100 }
13101
13102
13103 switch (TOLOWER_ASC(what[0]))
13104 {
13105 case 'b':
13106 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13107 p = highlight_color(id, what, modec);
13108 else /* bold */
13109 p = highlight_has_attr(id, HL_BOLD, modec);
13110 break;
13111
13112 case 'f': /* fg[#] */
13113 p = highlight_color(id, what, modec);
13114 break;
13115
13116 case 'i':
13117 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13118 p = highlight_has_attr(id, HL_INVERSE, modec);
13119 else /* italic */
13120 p = highlight_has_attr(id, HL_ITALIC, modec);
13121 break;
13122
13123 case 'n': /* name */
13124 p = get_highlight_name(NULL, id - 1);
13125 break;
13126
13127 case 'r': /* reverse */
13128 p = highlight_has_attr(id, HL_INVERSE, modec);
13129 break;
13130
13131 case 's': /* standout */
13132 p = highlight_has_attr(id, HL_STANDOUT, modec);
13133 break;
13134
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013135 case 'u':
13136 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13137 /* underline */
13138 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13139 else
13140 /* undercurl */
13141 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 break;
13143 }
13144
13145 if (p != NULL)
13146 p = vim_strsave(p);
13147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013148 rettv->v_type = VAR_STRING;
13149 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150}
13151
13152/*
13153 * "synIDtrans(id)" function
13154 */
13155/*ARGSUSED*/
13156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013157f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013158 typval_T *argvars;
13159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160{
13161 int id;
13162
13163#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165
13166 if (id > 0)
13167 id = syn_get_final_id(id);
13168 else
13169#endif
13170 id = 0;
13171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173}
13174
13175/*
13176 * "system()" function
13177 */
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013183 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013185 char_u *infile = NULL;
13186 char_u buf[NUMBUFLEN];
13187 int err = FALSE;
13188 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013191 {
13192 /*
13193 * Write the string to a temp file, to be used for input of the shell
13194 * command.
13195 */
13196 if ((infile = vim_tempname('i')) == NULL)
13197 {
13198 EMSG(_(e_notmp));
13199 return;
13200 }
13201
13202 fd = mch_fopen((char *)infile, WRITEBIN);
13203 if (fd == NULL)
13204 {
13205 EMSG2(_(e_notopen), infile);
13206 goto done;
13207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013209 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13210 err = TRUE;
13211 if (fclose(fd) != 0)
13212 err = TRUE;
13213 if (err)
13214 {
13215 EMSG(_("E677: Error writing temp file"));
13216 goto done;
13217 }
13218 }
13219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013221
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222#ifdef USE_CR
13223 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013224 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 {
13226 char_u *s;
13227
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013228 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 {
13230 if (*s == CAR)
13231 *s = NL;
13232 }
13233 }
13234#else
13235# ifdef USE_CRNL
13236 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013237 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 {
13239 char_u *s, *d;
13240
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013241 d = res;
13242 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 {
13244 if (s[0] == CAR && s[1] == NL)
13245 ++s;
13246 *d++ = *s;
13247 }
13248 *d = NUL;
13249 }
13250# endif
13251#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013252
13253done:
13254 if (infile != NULL)
13255 {
13256 mch_remove(infile);
13257 vim_free(infile);
13258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259 rettv->v_type = VAR_STRING;
13260 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261}
13262
13263/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013264 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013265 */
13266 static void
13267f_taglist(argvars, rettv)
13268 typval_T *argvars;
13269 typval_T *rettv;
13270{
13271 char_u *tag_pattern;
13272 list_T *l;
13273
13274 tag_pattern = get_tv_string(&argvars[0]);
13275
13276 rettv->vval.v_number = FALSE;
13277
13278 l = list_alloc();
13279 if (l != NULL)
13280 {
13281 if (get_tags(l, tag_pattern) != FAIL)
13282 {
13283 rettv->vval.v_list = l;
13284 rettv->v_type = VAR_LIST;
13285 ++l->lv_refcount;
13286 }
13287 else
13288 list_free(l);
13289 }
13290}
13291
13292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 * "tempname()" function
13294 */
13295/*ARGSUSED*/
13296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 typval_T *argvars;
13299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300{
13301 static int x = 'A';
13302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->v_type = VAR_STRING;
13304 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305
13306 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13307 * names. Skip 'I' and 'O', they are used for shell redirection. */
13308 do
13309 {
13310 if (x == 'Z')
13311 x = '0';
13312 else if (x == '9')
13313 x = 'A';
13314 else
13315 {
13316#ifdef EBCDIC
13317 if (x == 'I')
13318 x = 'J';
13319 else if (x == 'R')
13320 x = 'S';
13321 else
13322#endif
13323 ++x;
13324 }
13325 } while (x == 'I' || x == 'O');
13326}
13327
13328/*
13329 * "tolower(string)" function
13330 */
13331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013333 typval_T *argvars;
13334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335{
13336 char_u *p;
13337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 p = vim_strsave(get_tv_string(&argvars[0]));
13339 rettv->v_type = VAR_STRING;
13340 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341
13342 if (p != NULL)
13343 while (*p != NUL)
13344 {
13345#ifdef FEAT_MBYTE
13346 int l;
13347
13348 if (enc_utf8)
13349 {
13350 int c, lc;
13351
13352 c = utf_ptr2char(p);
13353 lc = utf_tolower(c);
13354 l = utf_ptr2len_check(p);
13355 /* TODO: reallocate string when byte count changes. */
13356 if (utf_char2len(lc) == l)
13357 utf_char2bytes(lc, p);
13358 p += l;
13359 }
13360 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13361 p += l; /* skip multi-byte character */
13362 else
13363#endif
13364 {
13365 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13366 ++p;
13367 }
13368 }
13369}
13370
13371/*
13372 * "toupper(string)" function
13373 */
13374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013375f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *argvars;
13377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378{
13379 char_u *p;
13380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381 p = vim_strsave(get_tv_string(&argvars[0]));
13382 rettv->v_type = VAR_STRING;
13383 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384
13385 if (p != NULL)
13386 while (*p != NUL)
13387 {
13388#ifdef FEAT_MBYTE
13389 int l;
13390
13391 if (enc_utf8)
13392 {
13393 int c, uc;
13394
13395 c = utf_ptr2char(p);
13396 uc = utf_toupper(c);
13397 l = utf_ptr2len_check(p);
13398 /* TODO: reallocate string when byte count changes. */
13399 if (utf_char2len(uc) == l)
13400 utf_char2bytes(uc, p);
13401 p += l;
13402 }
13403 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13404 p += l; /* skip multi-byte character */
13405 else
13406#endif
13407 {
13408 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13409 p++;
13410 }
13411 }
13412}
13413
13414/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013415 * "tr(string, fromstr, tostr)" function
13416 */
13417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 typval_T *argvars;
13420 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013421{
13422 char_u *instr;
13423 char_u *fromstr;
13424 char_u *tostr;
13425 char_u *p;
13426#ifdef FEAT_MBYTE
13427 int inlen;
13428 int fromlen;
13429 int tolen;
13430 int idx;
13431 char_u *cpstr;
13432 int cplen;
13433 int first = TRUE;
13434#endif
13435 char_u buf[NUMBUFLEN];
13436 char_u buf2[NUMBUFLEN];
13437 garray_T ga;
13438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 instr = get_tv_string(&argvars[0]);
13440 fromstr = get_tv_string_buf(&argvars[1], buf);
13441 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013442
13443 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->v_type = VAR_STRING;
13445 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013446 ga_init2(&ga, (int)sizeof(char), 80);
13447
13448#ifdef FEAT_MBYTE
13449 if (!has_mbyte)
13450#endif
13451 /* not multi-byte: fromstr and tostr must be the same length */
13452 if (STRLEN(fromstr) != STRLEN(tostr))
13453 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013454#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013455error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013456#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013457 EMSG2(_(e_invarg2), fromstr);
13458 ga_clear(&ga);
13459 return;
13460 }
13461
13462 /* fromstr and tostr have to contain the same number of chars */
13463 while (*instr != NUL)
13464 {
13465#ifdef FEAT_MBYTE
13466 if (has_mbyte)
13467 {
13468 inlen = mb_ptr2len_check(instr);
13469 cpstr = instr;
13470 cplen = inlen;
13471 idx = 0;
13472 for (p = fromstr; *p != NUL; p += fromlen)
13473 {
13474 fromlen = mb_ptr2len_check(p);
13475 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13476 {
13477 for (p = tostr; *p != NUL; p += tolen)
13478 {
13479 tolen = mb_ptr2len_check(p);
13480 if (idx-- == 0)
13481 {
13482 cplen = tolen;
13483 cpstr = p;
13484 break;
13485 }
13486 }
13487 if (*p == NUL) /* tostr is shorter than fromstr */
13488 goto error;
13489 break;
13490 }
13491 ++idx;
13492 }
13493
13494 if (first && cpstr == instr)
13495 {
13496 /* Check that fromstr and tostr have the same number of
13497 * (multi-byte) characters. Done only once when a character
13498 * of instr doesn't appear in fromstr. */
13499 first = FALSE;
13500 for (p = tostr; *p != NUL; p += tolen)
13501 {
13502 tolen = mb_ptr2len_check(p);
13503 --idx;
13504 }
13505 if (idx != 0)
13506 goto error;
13507 }
13508
13509 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013510 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013511 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013512
13513 instr += inlen;
13514 }
13515 else
13516#endif
13517 {
13518 /* When not using multi-byte chars we can do it faster. */
13519 p = vim_strchr(fromstr, *instr);
13520 if (p != NULL)
13521 ga_append(&ga, tostr[p - fromstr]);
13522 else
13523 ga_append(&ga, *instr);
13524 ++instr;
13525 }
13526 }
13527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013529}
13530
13531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 * "type(expr)" function
13533 */
13534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013536 typval_T *argvars;
13537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013539 int n;
13540
13541 switch (argvars[0].v_type)
13542 {
13543 case VAR_NUMBER: n = 0; break;
13544 case VAR_STRING: n = 1; break;
13545 case VAR_FUNC: n = 2; break;
13546 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013547 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013548 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13549 }
13550 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551}
13552
13553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013554 * "values(dict)" function
13555 */
13556 static void
13557f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 typval_T *argvars;
13559 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013560{
13561 dict_list(argvars, rettv, 1);
13562}
13563
13564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 * "virtcol(string)" function
13566 */
13567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571{
13572 colnr_T vcol = 0;
13573 pos_T *fp;
13574
13575 fp = var2fpos(&argvars[0], FALSE);
13576 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13577 {
13578 getvvcol(curwin, fp, NULL, NULL, &vcol);
13579 ++vcol;
13580 }
13581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583}
13584
13585/*
13586 * "visualmode()" function
13587 */
13588/*ARGSUSED*/
13589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013591 typval_T *argvars;
13592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
13594#ifdef FEAT_VISUAL
13595 char_u str[2];
13596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 str[0] = curbuf->b_visual_mode_eval;
13599 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601
13602 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013603 if ((argvars[0].v_type == VAR_NUMBER
13604 && argvars[0].vval.v_number != 0)
13605 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 curbuf->b_visual_mode_eval = NUL;
13608#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610#endif
13611}
13612
13613/*
13614 * "winbufnr(nr)" function
13615 */
13616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *argvars;
13619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620{
13621 win_T *wp;
13622
13623 wp = find_win_by_nr(&argvars[0]);
13624 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628}
13629
13630/*
13631 * "wincol()" function
13632 */
13633/*ARGSUSED*/
13634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *argvars;
13637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638{
13639 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641}
13642
13643/*
13644 * "winheight(nr)" function
13645 */
13646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013648 typval_T *argvars;
13649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650{
13651 win_T *wp;
13652
13653 wp = find_win_by_nr(&argvars[0]);
13654 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658}
13659
13660/*
13661 * "winline()" function
13662 */
13663/*ARGSUSED*/
13664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668{
13669 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673/*
13674 * "winnr()" function
13675 */
13676/* ARGSUSED */
13677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678f_winnr(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 int nr = 1;
13683#ifdef FEAT_WINDOWS
13684 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013685 win_T *twin = curwin;
13686 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013688 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013691 if (STRCMP(arg, "$") == 0)
13692 twin = lastwin;
13693 else if (STRCMP(arg, "#") == 0)
13694 {
13695 twin = prevwin;
13696 if (prevwin == NULL)
13697 nr = 0;
13698 }
13699 else
13700 {
13701 EMSG2(_(e_invexpr2), arg);
13702 nr = 0;
13703 }
13704 }
13705
13706 if (nr > 0)
13707 for (wp = firstwin; wp != twin; wp = wp->w_next)
13708 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711}
13712
13713/*
13714 * "winrestcmd()" function
13715 */
13716/* ARGSUSED */
13717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013719 typval_T *argvars;
13720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
13722#ifdef FEAT_WINDOWS
13723 win_T *wp;
13724 int winnr = 1;
13725 garray_T ga;
13726 char_u buf[50];
13727
13728 ga_init2(&ga, (int)sizeof(char), 70);
13729 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13730 {
13731 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13732 ga_concat(&ga, buf);
13733# ifdef FEAT_VERTSPLIT
13734 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13735 ga_concat(&ga, buf);
13736# endif
13737 ++winnr;
13738 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013739 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746}
13747
13748/*
13749 * "winwidth(nr)" function
13750 */
13751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *argvars;
13754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
13756 win_T *wp;
13757
13758 wp = find_win_by_nr(&argvars[0]);
13759 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 else
13762#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013763 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766#endif
13767}
13768
13769 static win_T *
13770find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772{
13773#ifdef FEAT_WINDOWS
13774 win_T *wp;
13775#endif
13776 int nr;
13777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779
13780#ifdef FEAT_WINDOWS
13781 if (nr == 0)
13782 return curwin;
13783
13784 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13785 if (--nr <= 0)
13786 break;
13787 return wp;
13788#else
13789 if (nr == 0 || nr == 1)
13790 return curwin;
13791 return NULL;
13792#endif
13793}
13794
13795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013796 * "writefile()" function
13797 */
13798 static void
13799f_writefile(argvars, rettv)
13800 typval_T *argvars;
13801 typval_T *rettv;
13802{
13803 int binary = FALSE;
13804 char_u *fname;
13805 FILE *fd;
13806 listitem_T *li;
13807 char_u *s;
13808 int ret = 0;
13809 int c;
13810
13811 if (argvars[0].v_type != VAR_LIST)
13812 {
13813 EMSG2(_(e_listarg), "writefile()");
13814 return;
13815 }
13816 if (argvars[0].vval.v_list == NULL)
13817 return;
13818
13819 if (argvars[2].v_type != VAR_UNKNOWN
13820 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13821 binary = TRUE;
13822
13823 /* Always open the file in binary mode, library functions have a mind of
13824 * their own about CR-LF conversion. */
13825 fname = get_tv_string(&argvars[1]);
13826 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13827 {
13828 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13829 ret = -1;
13830 }
13831 else
13832 {
13833 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13834 li = li->li_next)
13835 {
13836 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13837 {
13838 if (*s == '\n')
13839 c = putc(NUL, fd);
13840 else
13841 c = putc(*s, fd);
13842 if (c == EOF)
13843 {
13844 ret = -1;
13845 break;
13846 }
13847 }
13848 if (!binary || li->li_next != NULL)
13849 if (putc('\n', fd) == EOF)
13850 {
13851 ret = -1;
13852 break;
13853 }
13854 if (ret < 0)
13855 {
13856 EMSG(_(e_write));
13857 break;
13858 }
13859 }
13860 fclose(fd);
13861 }
13862
13863 rettv->vval.v_number = ret;
13864}
13865
13866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 * Translate a String variable into a position.
13868 */
13869 static pos_T *
13870var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 int lnum; /* TRUE when $ is last line */
13873{
13874 char_u *name;
13875 static pos_T pos;
13876 pos_T *pp;
13877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 if (name[0] == '.') /* cursor */
13880 return &curwin->w_cursor;
13881 if (name[0] == '\'') /* mark */
13882 {
13883 pp = getmark(name[1], FALSE);
13884 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13885 return NULL;
13886 return pp;
13887 }
13888 if (name[0] == '$') /* last column or line */
13889 {
13890 if (lnum)
13891 {
13892 pos.lnum = curbuf->b_ml.ml_line_count;
13893 pos.col = 0;
13894 }
13895 else
13896 {
13897 pos.lnum = curwin->w_cursor.lnum;
13898 pos.col = (colnr_T)STRLEN(ml_get_curline());
13899 }
13900 return &pos;
13901 }
13902 return NULL;
13903}
13904
13905/*
13906 * Get the length of an environment variable name.
13907 * Advance "arg" to the first character after the name.
13908 * Return 0 for error.
13909 */
13910 static int
13911get_env_len(arg)
13912 char_u **arg;
13913{
13914 char_u *p;
13915 int len;
13916
13917 for (p = *arg; vim_isIDc(*p); ++p)
13918 ;
13919 if (p == *arg) /* no name found */
13920 return 0;
13921
13922 len = (int)(p - *arg);
13923 *arg = p;
13924 return len;
13925}
13926
13927/*
13928 * Get the length of the name of a function or internal variable.
13929 * "arg" is advanced to the first non-white character after the name.
13930 * Return 0 if something is wrong.
13931 */
13932 static int
13933get_id_len(arg)
13934 char_u **arg;
13935{
13936 char_u *p;
13937 int len;
13938
13939 /* Find the end of the name. */
13940 for (p = *arg; eval_isnamec(*p); ++p)
13941 ;
13942 if (p == *arg) /* no name found */
13943 return 0;
13944
13945 len = (int)(p - *arg);
13946 *arg = skipwhite(p);
13947
13948 return len;
13949}
13950
13951/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013952 * Get the length of the name of a variable or function.
13953 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013955 * Return -1 if curly braces expansion failed.
13956 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 * If the name contains 'magic' {}'s, expand them and return the
13958 * expanded name in an allocated string via 'alias' - caller must free.
13959 */
13960 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013961get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 char_u **arg;
13963 char_u **alias;
13964 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013965 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966{
13967 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 char_u *p;
13969 char_u *expr_start;
13970 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971
13972 *alias = NULL; /* default to no alias */
13973
13974 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13975 && (*arg)[2] == (int)KE_SNR)
13976 {
13977 /* hard coded <SNR>, already translated */
13978 *arg += 3;
13979 return get_id_len(arg) + 3;
13980 }
13981 len = eval_fname_script(*arg);
13982 if (len > 0)
13983 {
13984 /* literal "<SID>", "s:" or "<SNR>" */
13985 *arg += len;
13986 }
13987
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013991 p = find_name_end(*arg, &expr_start, &expr_end,
13992 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 if (expr_start != NULL)
13994 {
13995 char_u *temp_string;
13996
13997 if (!evaluate)
13998 {
13999 len += (int)(p - *arg);
14000 *arg = skipwhite(p);
14001 return len;
14002 }
14003
14004 /*
14005 * Include any <SID> etc in the expanded string:
14006 * Thus the -len here.
14007 */
14008 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14009 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014010 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 *alias = temp_string;
14012 *arg = skipwhite(p);
14013 return (int)STRLEN(temp_string);
14014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015
14016 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014017 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 EMSG2(_(e_invexpr2), *arg);
14019
14020 return len;
14021}
14022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023/*
14024 * Find the end of a variable or function name, taking care of magic braces.
14025 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14026 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014027 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 * Return a pointer to just after the name. Equal to "arg" if there is no
14029 * valid name.
14030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014032find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 char_u *arg;
14034 char_u **expr_start;
14035 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014036 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 int mb_nest = 0;
14039 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 char_u *p;
14041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 *expr_start = NULL;
14045 *expr_end = NULL;
14046 }
14047
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014048 /* Quick check for valid starting character. */
14049 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14050 return arg;
14051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014052 for (p = arg; *p != NUL
14053 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014054 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014055 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056 || mb_nest != 0
14057 || br_nest != 0); ++p)
14058 {
14059 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 if (*p == '[')
14062 ++br_nest;
14063 else if (*p == ']')
14064 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014066 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 if (*p == '{')
14069 {
14070 mb_nest++;
14071 if (expr_start != NULL && *expr_start == NULL)
14072 *expr_start = p;
14073 }
14074 else if (*p == '}')
14075 {
14076 mb_nest--;
14077 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14078 *expr_end = p;
14079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 }
14082
14083 return p;
14084}
14085
14086/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014087 * Expands out the 'magic' {}'s in a variable/function name.
14088 * Note that this can call itself recursively, to deal with
14089 * constructs like foo{bar}{baz}{bam}
14090 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14091 * "in_start" ^
14092 * "expr_start" ^
14093 * "expr_end" ^
14094 * "in_end" ^
14095 *
14096 * Returns a new allocated string, which the caller must free.
14097 * Returns NULL for failure.
14098 */
14099 static char_u *
14100make_expanded_name(in_start, expr_start, expr_end, in_end)
14101 char_u *in_start;
14102 char_u *expr_start;
14103 char_u *expr_end;
14104 char_u *in_end;
14105{
14106 char_u c1;
14107 char_u *retval = NULL;
14108 char_u *temp_result;
14109 char_u *nextcmd = NULL;
14110
14111 if (expr_end == NULL || in_end == NULL)
14112 return NULL;
14113 *expr_start = NUL;
14114 *expr_end = NUL;
14115 c1 = *in_end;
14116 *in_end = NUL;
14117
14118 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14119 if (temp_result != NULL && nextcmd == NULL)
14120 {
14121 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14122 + (in_end - expr_end) + 1));
14123 if (retval != NULL)
14124 {
14125 STRCPY(retval, in_start);
14126 STRCAT(retval, temp_result);
14127 STRCAT(retval, expr_end + 1);
14128 }
14129 }
14130 vim_free(temp_result);
14131
14132 *in_end = c1; /* put char back for error messages */
14133 *expr_start = '{';
14134 *expr_end = '}';
14135
14136 if (retval != NULL)
14137 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014138 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014139 if (expr_start != NULL)
14140 {
14141 /* Further expansion! */
14142 temp_result = make_expanded_name(retval, expr_start,
14143 expr_end, temp_result);
14144 vim_free(retval);
14145 retval = temp_result;
14146 }
14147 }
14148
14149 return retval;
14150}
14151
14152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014154 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 */
14156 static int
14157eval_isnamec(c)
14158 int c;
14159{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014160 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14161}
14162
14163/*
14164 * Return TRUE if character "c" can be used as the first character in a
14165 * variable or function name (excluding '{' and '}').
14166 */
14167 static int
14168eval_isnamec1(c)
14169 int c;
14170{
14171 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172}
14173
14174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 * Set number v: variable to "val".
14176 */
14177 void
14178set_vim_var_nr(idx, val)
14179 int idx;
14180 long val;
14181{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014182 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
14185/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014186 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 */
14188 long
14189get_vim_var_nr(idx)
14190 int idx;
14191{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014192 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193}
14194
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014195#if defined(FEAT_AUTOCMD) || defined(PROTO)
14196/*
14197 * Get string v: variable value. Uses a static buffer, can only be used once.
14198 */
14199 char_u *
14200get_vim_var_str(idx)
14201 int idx;
14202{
14203 return get_tv_string(&vimvars[idx].vv_tv);
14204}
14205#endif
14206
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207/*
14208 * Set v:count, v:count1 and v:prevcount.
14209 */
14210 void
14211set_vcount(count, count1)
14212 long count;
14213 long count1;
14214{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014215 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14216 vimvars[VV_COUNT].vv_nr = count;
14217 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218}
14219
14220/*
14221 * Set string v: variable to a copy of "val".
14222 */
14223 void
14224set_vim_var_string(idx, val, len)
14225 int idx;
14226 char_u *val;
14227 int len; /* length of "val" to use or -1 (whole string) */
14228{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014229 /* Need to do this (at least) once, since we can't initialize a union.
14230 * Will always be invoked when "v:progname" is set. */
14231 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14232
Bram Moolenaare9a41262005-01-15 22:18:47 +000014233 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014235 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014237 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014239 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240}
14241
14242/*
14243 * Set v:register if needed.
14244 */
14245 void
14246set_reg_var(c)
14247 int c;
14248{
14249 char_u regname;
14250
14251 if (c == 0 || c == ' ')
14252 regname = '"';
14253 else
14254 regname = c;
14255 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014256 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 set_vim_var_string(VV_REG, &regname, 1);
14258}
14259
14260/*
14261 * Get or set v:exception. If "oldval" == NULL, return the current value.
14262 * Otherwise, restore the value to "oldval" and return NULL.
14263 * Must always be called in pairs to save and restore v:exception! Does not
14264 * take care of memory allocations.
14265 */
14266 char_u *
14267v_exception(oldval)
14268 char_u *oldval;
14269{
14270 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014271 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272
Bram Moolenaare9a41262005-01-15 22:18:47 +000014273 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 return NULL;
14275}
14276
14277/*
14278 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14279 * Otherwise, restore the value to "oldval" and return NULL.
14280 * Must always be called in pairs to save and restore v:throwpoint! Does not
14281 * take care of memory allocations.
14282 */
14283 char_u *
14284v_throwpoint(oldval)
14285 char_u *oldval;
14286{
14287 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014288 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289
Bram Moolenaare9a41262005-01-15 22:18:47 +000014290 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 return NULL;
14292}
14293
14294#if defined(FEAT_AUTOCMD) || defined(PROTO)
14295/*
14296 * Set v:cmdarg.
14297 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14298 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14299 * Must always be called in pairs!
14300 */
14301 char_u *
14302set_cmdarg(eap, oldarg)
14303 exarg_T *eap;
14304 char_u *oldarg;
14305{
14306 char_u *oldval;
14307 char_u *newval;
14308 unsigned len;
14309
Bram Moolenaare9a41262005-01-15 22:18:47 +000014310 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014311 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014313 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014314 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014315 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 }
14317
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014318 if (eap->force_bin == FORCE_BIN)
14319 len = 6;
14320 else if (eap->force_bin == FORCE_NOBIN)
14321 len = 8;
14322 else
14323 len = 0;
14324 if (eap->force_ff != 0)
14325 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14326# ifdef FEAT_MBYTE
14327 if (eap->force_enc != 0)
14328 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14329# endif
14330
14331 newval = alloc(len + 1);
14332 if (newval == NULL)
14333 return NULL;
14334
14335 if (eap->force_bin == FORCE_BIN)
14336 sprintf((char *)newval, " ++bin");
14337 else if (eap->force_bin == FORCE_NOBIN)
14338 sprintf((char *)newval, " ++nobin");
14339 else
14340 *newval = NUL;
14341 if (eap->force_ff != 0)
14342 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14343 eap->cmd + eap->force_ff);
14344# ifdef FEAT_MBYTE
14345 if (eap->force_enc != 0)
14346 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14347 eap->cmd + eap->force_enc);
14348# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014349 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014350 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351}
14352#endif
14353
14354/*
14355 * Get the value of internal variable "name".
14356 * Return OK or FAIL.
14357 */
14358 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014359get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 char_u *name;
14361 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014362 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014363 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364{
14365 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *tv = NULL;
14367 typval_T atv;
14368 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370
14371 /* truncate the name, so that we can use strcmp() */
14372 cc = name[len];
14373 name[len] = NUL;
14374
14375 /*
14376 * Check for "b:changedtick".
14377 */
14378 if (STRCMP(name, "b:changedtick") == 0)
14379 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014380 atv.v_type = VAR_NUMBER;
14381 atv.vval.v_number = curbuf->b_changedtick;
14382 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 }
14384
14385 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 * Check for user-defined variables.
14387 */
14388 else
14389 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014390 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 }
14394
Bram Moolenaare9a41262005-01-15 22:18:47 +000014395 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 ret = FAIL;
14400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014402 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403
14404 name[len] = cc;
14405
14406 return ret;
14407}
14408
14409/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014410 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14411 * Also handle function call with Funcref variable: func(expr)
14412 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14413 */
14414 static int
14415handle_subscript(arg, rettv, evaluate, verbose)
14416 char_u **arg;
14417 typval_T *rettv;
14418 int evaluate; /* do more than finding the end */
14419 int verbose; /* give error messages */
14420{
14421 int ret = OK;
14422 dict_T *selfdict = NULL;
14423 char_u *s;
14424 int len;
14425
14426 while (ret == OK
14427 && (**arg == '['
14428 || (**arg == '.' && rettv->v_type == VAR_DICT)
14429 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14430 && !vim_iswhite(*(*arg - 1)))
14431 {
14432 if (**arg == '(')
14433 {
14434 s = rettv->vval.v_string;
14435
14436 /* Invoke the function. Recursive! */
14437 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14438 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14439 &len, evaluate, selfdict);
14440
14441 /* Stop the expression evaluation when immediately aborting on
14442 * error, or when an interrupt occurred or an exception was thrown
14443 * but not caught. */
14444 if (aborting())
14445 {
14446 if (ret == OK)
14447 clear_tv(rettv);
14448 ret = FAIL;
14449 }
14450 dict_unref(selfdict);
14451 selfdict = NULL;
14452 }
14453 else /* **arg == '[' || **arg == '.' */
14454 {
14455 dict_unref(selfdict);
14456 if (rettv->v_type == VAR_DICT)
14457 {
14458 selfdict = rettv->vval.v_dict;
14459 if (selfdict != NULL)
14460 ++selfdict->dv_refcount;
14461 }
14462 else
14463 selfdict = NULL;
14464 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14465 {
14466 clear_tv(rettv);
14467 ret = FAIL;
14468 }
14469 }
14470 }
14471 dict_unref(selfdict);
14472 return ret;
14473}
14474
14475/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014476 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14477 * value).
14478 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014479 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014481{
Bram Moolenaar33570922005-01-25 22:26:29 +000014482 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014483}
14484
14485/*
14486 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 * The string "s" must have been allocated, it is consumed.
14488 * Return NULL for out of memory, the variable otherwise.
14489 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014490 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 char_u *s;
14493{
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv = alloc_tv();
14497 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv->v_type = VAR_STRING;
14500 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 }
14502 else
14503 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505}
14506
14507/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014508 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 */
14510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014512 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513{
14514 if (varp != NULL)
14515 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014516 switch (varp->v_type)
14517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014518 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014519 func_unref(varp->vval.v_string);
14520 /*FALLTHROUGH*/
14521 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014522 vim_free(varp->vval.v_string);
14523 break;
14524 case VAR_LIST:
14525 list_unref(varp->vval.v_list);
14526 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014527 case VAR_DICT:
14528 dict_unref(varp->vval.v_dict);
14529 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014530 case VAR_NUMBER:
14531 case VAR_UNKNOWN:
14532 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014533 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014534 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014535 break;
14536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 vim_free(varp);
14538 }
14539}
14540
14541/*
14542 * Free the memory for a variable value and set the value to NULL or 0.
14543 */
14544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014545clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547{
14548 if (varp != NULL)
14549 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014550 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014552 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014553 func_unref(varp->vval.v_string);
14554 /*FALLTHROUGH*/
14555 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014556 vim_free(varp->vval.v_string);
14557 varp->vval.v_string = NULL;
14558 break;
14559 case VAR_LIST:
14560 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014561 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014562 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014563 case VAR_DICT:
14564 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014565 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014566 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014567 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014568 varp->vval.v_number = 0;
14569 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570 case VAR_UNKNOWN:
14571 break;
14572 default:
14573 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014575 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 }
14577}
14578
14579/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580 * Set the value of a variable to NULL without freeing items.
14581 */
14582 static void
14583init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014584 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585{
14586 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014587 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588}
14589
14590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 * Get the number value of a variable.
14592 * If it is a String variable, uses vim_str2nr().
14593 */
14594 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014595get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014598 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014600 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014602 case VAR_NUMBER:
14603 n = (long)(varp->vval.v_number);
14604 break;
14605 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014606 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607 break;
14608 case VAR_STRING:
14609 if (varp->vval.v_string != NULL)
14610 vim_str2nr(varp->vval.v_string, NULL, NULL,
14611 TRUE, TRUE, &n, NULL);
14612 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014613 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014614 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014616 case VAR_DICT:
14617 EMSG(_("E728: Using a Dictionary as a number"));
14618 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014619 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014620 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014621 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014623 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624}
14625
14626/*
14627 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14628 */
14629 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632{
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634 linenr_T lnum;
14635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 if (lnum == 0) /* no valid number, try using line() */
14638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 rettv.v_type = VAR_NUMBER;
14640 f_line(argvars, &rettv);
14641 lnum = rettv.vval.v_number;
14642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643 }
14644 return lnum;
14645}
14646
14647/*
14648 * Get the string value of a variable.
14649 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014650 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14651 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 * If the String variable has never been set, return an empty string.
14653 * Never returns NULL;
14654 */
14655 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014656get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014657 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
14659 static char_u mybuf[NUMBUFLEN];
14660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014661 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662}
14663
14664 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014667 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014669 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014671 case VAR_NUMBER:
14672 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14673 return buf;
14674 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014675 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014676 break;
14677 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014678 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014679 break;
14680 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014681 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014682 break;
14683 case VAR_STRING:
14684 if (varp->vval.v_string != NULL)
14685 return varp->vval.v_string;
14686 break;
14687 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014688 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014691 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692}
14693
14694/*
14695 * Find variable "name" in the list of variables.
14696 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014697 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014698 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014699 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014701 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014702find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaara7043832005-01-21 11:56:39 +000014709 ht = find_var_ht(name, &varname);
14710 if (htp != NULL)
14711 *htp = ht;
14712 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014714 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715}
14716
14717/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014718 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014719 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014722find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014723 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014724 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014725 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014726{
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 hashitem_T *hi;
14728
14729 if (*varname == NUL)
14730 {
14731 /* Must be something like "s:", otherwise "ht" would be NULL. */
14732 switch (varname[-2])
14733 {
14734 case 's': return &SCRIPT_SV(current_SID).sv_var;
14735 case 'g': return &globvars_var;
14736 case 'v': return &vimvars_var;
14737 case 'b': return &curbuf->b_bufvar;
14738 case 'w': return &curwin->w_winvar;
14739 case 'l': return &current_funccal->l_vars_var;
14740 case 'a': return &current_funccal->l_avars_var;
14741 }
14742 return NULL;
14743 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014744
14745 hi = hash_find(ht, varname);
14746 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014747 {
14748 /* For global variables we may try auto-loading the script. If it
14749 * worked find the variable again. */
14750 if (ht == &globvarht && !writing
14751 && script_autoload(varname) && !aborting())
14752 hi = hash_find(ht, varname);
14753 if (HASHITEM_EMPTY(hi))
14754 return NULL;
14755 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014756 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014757}
14758
14759/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014760 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014761 * Set "varname" to the start of name without ':'.
14762 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014763 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014764find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 char_u *name;
14766 char_u **varname;
14767{
14768 if (name[1] != ':')
14769 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014770 /* The name must not start with a colon or #. */
14771 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 return NULL;
14773 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014774
14775 /* "version" is "v:version" in all scopes */
14776 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14777 return &compat_hashtab;
14778
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 return &globvarht; /* global variable */
14781 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 }
14783 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014784 if (*name == 'g') /* global variable */
14785 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014786 /* There must be no ':' or '#' in the rest of the name, unless g: is used
14787 */
14788 if (vim_strchr(name + 2, ':') != NULL
14789 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014790 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014794 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 if (*name == 'v') /* v: variable */
14796 return &vimvarht;
14797 if (*name == 'a' && current_funccal != NULL) /* function argument */
14798 return &current_funccal->l_avars.dv_hashtab;
14799 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14800 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 if (*name == 's' /* script variable */
14802 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14803 return &SCRIPT_VARS(current_SID);
14804 return NULL;
14805}
14806
14807/*
14808 * Get the string value of a (global/local) variable.
14809 * Returns NULL when it doesn't exist.
14810 */
14811 char_u *
14812get_var_value(name)
14813 char_u *name;
14814{
Bram Moolenaar33570922005-01-25 22:26:29 +000014815 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816
Bram Moolenaara7043832005-01-21 11:56:39 +000014817 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 if (v == NULL)
14819 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014820 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821}
14822
14823/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014824 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 * sourcing this script and when executing functions defined in the script.
14826 */
14827 void
14828new_script_vars(id)
14829 scid_T id;
14830{
Bram Moolenaara7043832005-01-21 11:56:39 +000014831 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014832 hashtab_T *ht;
14833 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014834
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14836 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014837 /* Re-allocating ga_data means that an ht_array pointing to
14838 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014839 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014840 for (i = 1; i <= ga_scripts.ga_len; ++i)
14841 {
14842 ht = &SCRIPT_VARS(i);
14843 if (ht->ht_mask == HT_INIT_SIZE - 1)
14844 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014845 sv = &SCRIPT_SV(i);
14846 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014847 }
14848
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 while (ga_scripts.ga_len < id)
14850 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014851 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14852 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 }
14855 }
14856}
14857
14858/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014859 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14860 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 */
14862 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014863init_var_dict(dict, dict_var)
14864 dict_T *dict;
14865 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866{
Bram Moolenaar33570922005-01-25 22:26:29 +000014867 hash_init(&dict->dv_hashtab);
14868 dict->dv_refcount = 99999;
14869 dict_var->di_tv.vval.v_dict = dict;
14870 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014871 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014872 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14873 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874}
14875
14876/*
14877 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014878 * Frees all allocated variables and the value they contain.
14879 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 */
14881 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014882vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014883 hashtab_T *ht;
14884{
14885 vars_clear_ext(ht, TRUE);
14886}
14887
14888/*
14889 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14890 */
14891 static void
14892vars_clear_ext(ht, free_val)
14893 hashtab_T *ht;
14894 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895{
Bram Moolenaara7043832005-01-21 11:56:39 +000014896 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014897 hashitem_T *hi;
14898 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899
Bram Moolenaar33570922005-01-25 22:26:29 +000014900 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014901 todo = ht->ht_used;
14902 for (hi = ht->ht_array; todo > 0; ++hi)
14903 {
14904 if (!HASHITEM_EMPTY(hi))
14905 {
14906 --todo;
14907
Bram Moolenaar33570922005-01-25 22:26:29 +000014908 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014909 * ht_array might change then. hash_clear() takes care of it
14910 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014911 v = HI2DI(hi);
14912 if (free_val)
14913 clear_tv(&v->di_tv);
14914 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14915 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014916 }
14917 }
14918 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919}
14920
Bram Moolenaara7043832005-01-21 11:56:39 +000014921/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 * Delete a variable from hashtab "ht" at item "hi".
14923 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014926delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014927 hashtab_T *ht;
14928 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929{
Bram Moolenaar33570922005-01-25 22:26:29 +000014930 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014931
14932 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014933 clear_tv(&di->di_tv);
14934 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935}
14936
14937/*
14938 * List the value of one internal variable.
14939 */
14940 static void
14941list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 char_u *prefix;
14944{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014945 char_u *tofree;
14946 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014947 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014948
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 s = echo_string(&v->di_tv, &tofree, numbuf);
14950 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014951 s == NULL ? (char_u *)"" : s);
14952 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953}
14954
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 static void
14956list_one_var_a(prefix, name, type, string)
14957 char_u *prefix;
14958 char_u *name;
14959 int type;
14960 char_u *string;
14961{
14962 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14963 if (name != NULL) /* "a:" vars don't have a name stored */
14964 msg_puts(name);
14965 msg_putchar(' ');
14966 msg_advance(22);
14967 if (type == VAR_NUMBER)
14968 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014969 else if (type == VAR_FUNC)
14970 msg_putchar('*');
14971 else if (type == VAR_LIST)
14972 {
14973 msg_putchar('[');
14974 if (*string == '[')
14975 ++string;
14976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014977 else if (type == VAR_DICT)
14978 {
14979 msg_putchar('{');
14980 if (*string == '{')
14981 ++string;
14982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 else
14984 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014985
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014987
14988 if (type == VAR_FUNC)
14989 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990}
14991
14992/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 * If the variable already exists, the value is updated.
14995 * Otherwise the variable is created.
14996 */
14997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015000 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002{
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015007 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015008 {
15009 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15010 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15011 ? name[2] : name[0]))
15012 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015013 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015014 return;
15015 }
15016 if (function_exists(name))
15017 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015018 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15019 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015020 return;
15021 }
15022 }
15023
Bram Moolenaara7043832005-01-21 11:56:39 +000015024 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015025 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015026 {
15027 EMSG2(_("E461: Illegal variable name: %s"), name);
15028 return;
15029 }
15030
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015031 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015035 if (var_check_ro(v->di_flags, name)
15036 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 return;
15038 if (v->di_tv.v_type != tv->v_type
15039 && !((v->di_tv.v_type == VAR_STRING
15040 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 && (tv->v_type == VAR_STRING
15042 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015043 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015044 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015045 return;
15046 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015047
15048 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015049 * Handle setting internal v: variables separately: we don't change
15050 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 */
15052 if (ht == &vimvarht)
15053 {
15054 if (v->di_tv.v_type == VAR_STRING)
15055 {
15056 vim_free(v->di_tv.vval.v_string);
15057 if (copy || tv->v_type != VAR_STRING)
15058 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15059 else
15060 {
15061 /* Take over the string to avoid an extra alloc/free. */
15062 v->di_tv.vval.v_string = tv->vval.v_string;
15063 tv->vval.v_string = NULL;
15064 }
15065 }
15066 else if (v->di_tv.v_type != VAR_NUMBER)
15067 EMSG2(_(e_intern2), "set_var()");
15068 else
15069 v->di_tv.vval.v_number = get_tv_number(tv);
15070 return;
15071 }
15072
15073 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 }
15075 else /* add a new variable */
15076 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015077 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15078 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015079 if (v == NULL)
15080 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015084 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 return;
15086 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015087 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015092 else
15093 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015094 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015095 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098}
15099
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015100/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15102 * Also give an error message.
15103 */
15104 static int
15105var_check_ro(flags, name)
15106 int flags;
15107 char_u *name;
15108{
15109 if (flags & DI_FLAGS_RO)
15110 {
15111 EMSG2(_(e_readonlyvar), name);
15112 return TRUE;
15113 }
15114 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15115 {
15116 EMSG2(_(e_readonlysbx), name);
15117 return TRUE;
15118 }
15119 return FALSE;
15120}
15121
15122/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015123 * Return TRUE if typeval "tv" is set to be locked (immutable).
15124 * Also give an error message, using "name".
15125 */
15126 static int
15127tv_check_lock(lock, name)
15128 int lock;
15129 char_u *name;
15130{
15131 if (lock & VAR_LOCKED)
15132 {
15133 EMSG2(_("E741: Value is locked: %s"),
15134 name == NULL ? (char_u *)_("Unknown") : name);
15135 return TRUE;
15136 }
15137 if (lock & VAR_FIXED)
15138 {
15139 EMSG2(_("E742: Cannot change value of %s"),
15140 name == NULL ? (char_u *)_("Unknown") : name);
15141 return TRUE;
15142 }
15143 return FALSE;
15144}
15145
15146/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015148 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015149 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *from;
15154 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015156 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015157 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015158 switch (from->v_type)
15159 {
15160 case VAR_NUMBER:
15161 to->vval.v_number = from->vval.v_number;
15162 break;
15163 case VAR_STRING:
15164 case VAR_FUNC:
15165 if (from->vval.v_string == NULL)
15166 to->vval.v_string = NULL;
15167 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015169 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015170 if (from->v_type == VAR_FUNC)
15171 func_ref(to->vval.v_string);
15172 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015173 break;
15174 case VAR_LIST:
15175 if (from->vval.v_list == NULL)
15176 to->vval.v_list = NULL;
15177 else
15178 {
15179 to->vval.v_list = from->vval.v_list;
15180 ++to->vval.v_list->lv_refcount;
15181 }
15182 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015183 case VAR_DICT:
15184 if (from->vval.v_dict == NULL)
15185 to->vval.v_dict = NULL;
15186 else
15187 {
15188 to->vval.v_dict = from->vval.v_dict;
15189 ++to->vval.v_dict->dv_refcount;
15190 }
15191 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015192 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015194 break;
15195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196}
15197
15198/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015199 * Make a copy of an item.
15200 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015201 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15202 * reference to an already copied list/dict can be used.
15203 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015204 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015205 static int
15206item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *from;
15208 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015209 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015210 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015211{
15212 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015213 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015214
Bram Moolenaar33570922005-01-25 22:26:29 +000015215 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015216 {
15217 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015218 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015219 }
15220 ++recurse;
15221
15222 switch (from->v_type)
15223 {
15224 case VAR_NUMBER:
15225 case VAR_STRING:
15226 case VAR_FUNC:
15227 copy_tv(from, to);
15228 break;
15229 case VAR_LIST:
15230 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015231 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015232 if (from->vval.v_list == NULL)
15233 to->vval.v_list = NULL;
15234 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15235 {
15236 /* use the copy made earlier */
15237 to->vval.v_list = from->vval.v_list->lv_copylist;
15238 ++to->vval.v_list->lv_refcount;
15239 }
15240 else
15241 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15242 if (to->vval.v_list == NULL)
15243 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015244 break;
15245 case VAR_DICT:
15246 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015247 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015248 if (from->vval.v_dict == NULL)
15249 to->vval.v_dict = NULL;
15250 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15251 {
15252 /* use the copy made earlier */
15253 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15254 ++to->vval.v_dict->dv_refcount;
15255 }
15256 else
15257 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15258 if (to->vval.v_dict == NULL)
15259 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015260 break;
15261 default:
15262 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015263 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015264 }
15265 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015266 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015267}
15268
15269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 * ":echo expr1 ..." print each argument separated with a space, add a
15271 * newline at the end.
15272 * ":echon expr1 ..." print each argument plain.
15273 */
15274 void
15275ex_echo(eap)
15276 exarg_T *eap;
15277{
15278 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015279 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 char_u *p;
15282 int needclr = TRUE;
15283 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015284 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285
15286 if (eap->skip)
15287 ++emsg_skip;
15288 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15289 {
15290 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 {
15293 /*
15294 * Report the invalid expression unless the expression evaluation
15295 * has been cancelled due to an aborting error, an interrupt, or an
15296 * exception.
15297 */
15298 if (!aborting())
15299 EMSG2(_(e_invexpr2), p);
15300 break;
15301 }
15302 if (!eap->skip)
15303 {
15304 if (atstart)
15305 {
15306 atstart = FALSE;
15307 /* Call msg_start() after eval1(), evaluating the expression
15308 * may cause a message to appear. */
15309 if (eap->cmdidx == CMD_echo)
15310 msg_start();
15311 }
15312 else if (eap->cmdidx == CMD_echo)
15313 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015314 p = echo_string(&rettv, &tofree, numbuf);
15315 if (p != NULL)
15316 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015318 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015320 if (*p != TAB && needclr)
15321 {
15322 /* remove any text still there from the command */
15323 msg_clr_eos();
15324 needclr = FALSE;
15325 }
15326 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 }
15328 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015329 {
15330#ifdef FEAT_MBYTE
15331 if (has_mbyte)
15332 {
15333 int i = (*mb_ptr2len_check)(p);
15334
15335 (void)msg_outtrans_len_attr(p, i, echo_attr);
15336 p += i - 1;
15337 }
15338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015340 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015343 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 arg = skipwhite(arg);
15347 }
15348 eap->nextcmd = check_nextcmd(arg);
15349
15350 if (eap->skip)
15351 --emsg_skip;
15352 else
15353 {
15354 /* remove text that may still be there from the command */
15355 if (needclr)
15356 msg_clr_eos();
15357 if (eap->cmdidx == CMD_echo)
15358 msg_end();
15359 }
15360}
15361
15362/*
15363 * ":echohl {name}".
15364 */
15365 void
15366ex_echohl(eap)
15367 exarg_T *eap;
15368{
15369 int id;
15370
15371 id = syn_name2id(eap->arg);
15372 if (id == 0)
15373 echo_attr = 0;
15374 else
15375 echo_attr = syn_id2attr(id);
15376}
15377
15378/*
15379 * ":execute expr1 ..." execute the result of an expression.
15380 * ":echomsg expr1 ..." Print a message
15381 * ":echoerr expr1 ..." Print an error
15382 * Each gets spaces around each argument and a newline at the end for
15383 * echo commands
15384 */
15385 void
15386ex_execute(eap)
15387 exarg_T *eap;
15388{
15389 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015390 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 int ret = OK;
15392 char_u *p;
15393 garray_T ga;
15394 int len;
15395 int save_did_emsg;
15396
15397 ga_init2(&ga, 1, 80);
15398
15399 if (eap->skip)
15400 ++emsg_skip;
15401 while (*arg != NUL && *arg != '|' && *arg != '\n')
15402 {
15403 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 {
15406 /*
15407 * Report the invalid expression unless the expression evaluation
15408 * has been cancelled due to an aborting error, an interrupt, or an
15409 * exception.
15410 */
15411 if (!aborting())
15412 EMSG2(_(e_invexpr2), p);
15413 ret = FAIL;
15414 break;
15415 }
15416
15417 if (!eap->skip)
15418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 len = (int)STRLEN(p);
15421 if (ga_grow(&ga, len + 2) == FAIL)
15422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 ret = FAIL;
15425 break;
15426 }
15427 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 ga.ga_len += len;
15431 }
15432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 arg = skipwhite(arg);
15435 }
15436
15437 if (ret != FAIL && ga.ga_data != NULL)
15438 {
15439 if (eap->cmdidx == CMD_echomsg)
15440 MSG_ATTR(ga.ga_data, echo_attr);
15441 else if (eap->cmdidx == CMD_echoerr)
15442 {
15443 /* We don't want to abort following commands, restore did_emsg. */
15444 save_did_emsg = did_emsg;
15445 EMSG((char_u *)ga.ga_data);
15446 if (!force_abort)
15447 did_emsg = save_did_emsg;
15448 }
15449 else if (eap->cmdidx == CMD_execute)
15450 do_cmdline((char_u *)ga.ga_data,
15451 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15452 }
15453
15454 ga_clear(&ga);
15455
15456 if (eap->skip)
15457 --emsg_skip;
15458
15459 eap->nextcmd = check_nextcmd(arg);
15460}
15461
15462/*
15463 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15464 * "arg" points to the "&" or '+' when called, to "option" when returning.
15465 * Returns NULL when no option name found. Otherwise pointer to the char
15466 * after the option name.
15467 */
15468 static char_u *
15469find_option_end(arg, opt_flags)
15470 char_u **arg;
15471 int *opt_flags;
15472{
15473 char_u *p = *arg;
15474
15475 ++p;
15476 if (*p == 'g' && p[1] == ':')
15477 {
15478 *opt_flags = OPT_GLOBAL;
15479 p += 2;
15480 }
15481 else if (*p == 'l' && p[1] == ':')
15482 {
15483 *opt_flags = OPT_LOCAL;
15484 p += 2;
15485 }
15486 else
15487 *opt_flags = 0;
15488
15489 if (!ASCII_ISALPHA(*p))
15490 return NULL;
15491 *arg = p;
15492
15493 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15494 p += 4; /* termcap option */
15495 else
15496 while (ASCII_ISALPHA(*p))
15497 ++p;
15498 return p;
15499}
15500
15501/*
15502 * ":function"
15503 */
15504 void
15505ex_function(eap)
15506 exarg_T *eap;
15507{
15508 char_u *theline;
15509 int j;
15510 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 char_u *name = NULL;
15513 char_u *p;
15514 char_u *arg;
15515 garray_T newargs;
15516 garray_T newlines;
15517 int varargs = FALSE;
15518 int mustend = FALSE;
15519 int flags = 0;
15520 ufunc_T *fp;
15521 int indent;
15522 int nesting;
15523 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015524 dictitem_T *v;
15525 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015526 static int func_nr = 0; /* number for nameless function */
15527 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015528 hashtab_T *ht;
15529 int todo;
15530 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531
15532 /*
15533 * ":function" without argument: list functions.
15534 */
15535 if (ends_excmd(*eap->arg))
15536 {
15537 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015538 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015539 todo = func_hashtab.ht_used;
15540 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015541 {
15542 if (!HASHITEM_EMPTY(hi))
15543 {
15544 --todo;
15545 fp = HI2UF(hi);
15546 if (!isdigit(*fp->uf_name))
15547 list_func_head(fp, FALSE);
15548 }
15549 }
15550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 eap->nextcmd = check_nextcmd(eap->arg);
15552 return;
15553 }
15554
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015555 /*
15556 * Get the function name. There are these situations:
15557 * func normal function name
15558 * "name" == func, "fudi.fd_dict" == NULL
15559 * dict.func new dictionary entry
15560 * "name" == NULL, "fudi.fd_dict" set,
15561 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15562 * dict.func existing dict entry with a Funcref
15563 * "name" == fname, "fudi.fd_dict" set,
15564 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15565 * dict.func existing dict entry that's not a Funcref
15566 * "name" == NULL, "fudi.fd_dict" set,
15567 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015570 name = trans_function_name(&p, eap->skip, 0, &fudi);
15571 paren = (vim_strchr(p, '(') != NULL);
15572 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 {
15574 /*
15575 * Return on an invalid expression in braces, unless the expression
15576 * evaluation has been cancelled due to an aborting error, an
15577 * interrupt, or an exception.
15578 */
15579 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015581 if (!eap->skip && fudi.fd_newkey != NULL)
15582 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015583 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 else
15587 eap->skip = TRUE;
15588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 /* An error in a function call during evaluation of an expression in magic
15590 * braces should not cause the function not to be defined. */
15591 saved_did_emsg = did_emsg;
15592 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593
15594 /*
15595 * ":function func" with only function name: list function.
15596 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015597 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 {
15599 if (!ends_excmd(*skipwhite(p)))
15600 {
15601 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015602 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 }
15604 eap->nextcmd = check_nextcmd(p);
15605 if (eap->nextcmd != NULL)
15606 *p = NUL;
15607 if (!eap->skip && !got_int)
15608 {
15609 fp = find_func(name);
15610 if (fp != NULL)
15611 {
15612 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015613 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 {
15615 msg_putchar('\n');
15616 msg_outnum((long)(j + 1));
15617 if (j < 9)
15618 msg_putchar(' ');
15619 if (j < 99)
15620 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015621 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 out_flush(); /* show a line at a time */
15623 ui_breakcheck();
15624 }
15625 if (!got_int)
15626 {
15627 msg_putchar('\n');
15628 msg_puts((char_u *)" endfunction");
15629 }
15630 }
15631 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015632 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015634 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 }
15636
15637 /*
15638 * ":function name(arg1, arg2)" Define function.
15639 */
15640 p = skipwhite(p);
15641 if (*p != '(')
15642 {
15643 if (!eap->skip)
15644 {
15645 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015646 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 }
15648 /* attempt to continue by skipping some text */
15649 if (vim_strchr(p, '(') != NULL)
15650 p = vim_strchr(p, '(');
15651 }
15652 p = skipwhite(p + 1);
15653
15654 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15655 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15656
15657 /*
15658 * Isolate the arguments: "arg1, arg2, ...)"
15659 */
15660 while (*p != ')')
15661 {
15662 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15663 {
15664 varargs = TRUE;
15665 p += 3;
15666 mustend = TRUE;
15667 }
15668 else
15669 {
15670 arg = p;
15671 while (ASCII_ISALNUM(*p) || *p == '_')
15672 ++p;
15673 if (arg == p || isdigit(*arg)
15674 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15675 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15676 {
15677 if (!eap->skip)
15678 EMSG2(_("E125: Illegal argument: %s"), arg);
15679 break;
15680 }
15681 if (ga_grow(&newargs, 1) == FAIL)
15682 goto erret;
15683 c = *p;
15684 *p = NUL;
15685 arg = vim_strsave(arg);
15686 if (arg == NULL)
15687 goto erret;
15688 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15689 *p = c;
15690 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 if (*p == ',')
15692 ++p;
15693 else
15694 mustend = TRUE;
15695 }
15696 p = skipwhite(p);
15697 if (mustend && *p != ')')
15698 {
15699 if (!eap->skip)
15700 EMSG2(_(e_invarg2), eap->arg);
15701 break;
15702 }
15703 }
15704 ++p; /* skip the ')' */
15705
Bram Moolenaare9a41262005-01-15 22:18:47 +000015706 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 for (;;)
15708 {
15709 p = skipwhite(p);
15710 if (STRNCMP(p, "range", 5) == 0)
15711 {
15712 flags |= FC_RANGE;
15713 p += 5;
15714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015715 else if (STRNCMP(p, "dict", 4) == 0)
15716 {
15717 flags |= FC_DICT;
15718 p += 4;
15719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 else if (STRNCMP(p, "abort", 5) == 0)
15721 {
15722 flags |= FC_ABORT;
15723 p += 5;
15724 }
15725 else
15726 break;
15727 }
15728
15729 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15730 EMSG(_(e_trailing));
15731
15732 /*
15733 * Read the body of the function, until ":endfunction" is found.
15734 */
15735 if (KeyTyped)
15736 {
15737 /* Check if the function already exists, don't let the user type the
15738 * whole function before telling him it doesn't work! For a script we
15739 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015740 if (!eap->skip && !eap->forceit)
15741 {
15742 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15743 EMSG(_(e_funcdict));
15744 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015745 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747
15748 msg_putchar('\n'); /* don't overwrite the function name */
15749 cmdline_row = msg_row;
15750 }
15751
15752 indent = 2;
15753 nesting = 0;
15754 for (;;)
15755 {
15756 msg_scroll = TRUE;
15757 need_wait_return = FALSE;
15758 if (eap->getline == NULL)
15759 theline = getcmdline(':', 0L, indent);
15760 else
15761 theline = eap->getline(':', eap->cookie, indent);
15762 if (KeyTyped)
15763 lines_left = Rows - 1;
15764 if (theline == NULL)
15765 {
15766 EMSG(_("E126: Missing :endfunction"));
15767 goto erret;
15768 }
15769
15770 if (skip_until != NULL)
15771 {
15772 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15773 * don't check for ":endfunc". */
15774 if (STRCMP(theline, skip_until) == 0)
15775 {
15776 vim_free(skip_until);
15777 skip_until = NULL;
15778 }
15779 }
15780 else
15781 {
15782 /* skip ':' and blanks*/
15783 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15784 ;
15785
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015786 /* Check for "endfunction". */
15787 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 {
15789 vim_free(theline);
15790 break;
15791 }
15792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015793 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 * at "end". */
15795 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15796 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015797 else if (STRNCMP(p, "if", 2) == 0
15798 || STRNCMP(p, "wh", 2) == 0
15799 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 || STRNCMP(p, "try", 3) == 0)
15801 indent += 2;
15802
15803 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015804 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015806 if (*p == '!')
15807 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 p += eval_fname_script(p);
15809 if (ASCII_ISALPHA(*p))
15810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015811 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 if (*skipwhite(p) == '(')
15813 {
15814 ++nesting;
15815 indent += 2;
15816 }
15817 }
15818 }
15819
15820 /* Check for ":append" or ":insert". */
15821 p = skip_range(p, NULL);
15822 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15823 || (p[0] == 'i'
15824 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15825 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15826 skip_until = vim_strsave((char_u *)".");
15827
15828 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15829 arg = skipwhite(skiptowhite(p));
15830 if (arg[0] == '<' && arg[1] =='<'
15831 && ((p[0] == 'p' && p[1] == 'y'
15832 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15833 || (p[0] == 'p' && p[1] == 'e'
15834 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15835 || (p[0] == 't' && p[1] == 'c'
15836 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15837 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15838 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015839 || (p[0] == 'm' && p[1] == 'z'
15840 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 ))
15842 {
15843 /* ":python <<" continues until a dot, like ":append" */
15844 p = skipwhite(arg + 2);
15845 if (*p == NUL)
15846 skip_until = vim_strsave((char_u *)".");
15847 else
15848 skip_until = vim_strsave(p);
15849 }
15850 }
15851
15852 /* Add the line to the function. */
15853 if (ga_grow(&newlines, 1) == FAIL)
15854 goto erret;
15855 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15856 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 }
15858
15859 /* Don't define the function when skipping commands or when an error was
15860 * detected. */
15861 if (eap->skip || did_emsg)
15862 goto erret;
15863
15864 /*
15865 * If there are no errors, add the function
15866 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015867 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015868 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015869 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015870 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015871 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015872 emsg_funcname("E707: Function name conflicts with variable: %s",
15873 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015874 goto erret;
15875 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015876
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015877 fp = find_func(name);
15878 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015880 if (!eap->forceit)
15881 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015882 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015883 goto erret;
15884 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015885 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015886 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015887 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015888 name);
15889 goto erret;
15890 }
15891 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015892 ga_clear_strings(&(fp->uf_args));
15893 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015894 vim_free(name);
15895 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 }
15898 else
15899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015900 char numbuf[20];
15901
15902 fp = NULL;
15903 if (fudi.fd_newkey == NULL && !eap->forceit)
15904 {
15905 EMSG(_(e_funcdict));
15906 goto erret;
15907 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015908 if (fudi.fd_di == NULL)
15909 {
15910 /* Can't add a function to a locked dictionary */
15911 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15912 goto erret;
15913 }
15914 /* Can't change an existing function if it is locked */
15915 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15916 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015917
15918 /* Give the function a sequential number. Can only be used with a
15919 * Funcref! */
15920 vim_free(name);
15921 sprintf(numbuf, "%d", ++func_nr);
15922 name = vim_strsave((char_u *)numbuf);
15923 if (name == NULL)
15924 goto erret;
15925 }
15926
15927 if (fp == NULL)
15928 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015929 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015930 {
15931 int slen, plen;
15932 char_u *scriptname;
15933
15934 /* Check that the autoload name matches the script name. */
15935 j = FAIL;
15936 if (sourcing_name != NULL)
15937 {
15938 scriptname = autoload_name(name);
15939 if (scriptname != NULL)
15940 {
15941 p = vim_strchr(scriptname, '/');
15942 plen = STRLEN(p);
15943 slen = STRLEN(sourcing_name);
15944 if (slen > plen && fnamecmp(p,
15945 sourcing_name + slen - plen) == 0)
15946 j = OK;
15947 vim_free(scriptname);
15948 }
15949 }
15950 if (j == FAIL)
15951 {
15952 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15953 goto erret;
15954 }
15955 }
15956
15957 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 if (fp == NULL)
15959 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015960
15961 if (fudi.fd_dict != NULL)
15962 {
15963 if (fudi.fd_di == NULL)
15964 {
15965 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015966 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015967 if (fudi.fd_di == NULL)
15968 {
15969 vim_free(fp);
15970 goto erret;
15971 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015972 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15973 {
15974 vim_free(fudi.fd_di);
15975 goto erret;
15976 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015977 }
15978 else
15979 /* overwrite existing dict entry */
15980 clear_tv(&fudi.fd_di->di_tv);
15981 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015982 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015983 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015984 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015985 }
15986
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015988 STRCPY(fp->uf_name, name);
15989 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015991 fp->uf_args = newargs;
15992 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015993#ifdef FEAT_PROFILE
15994 fp->uf_tml_count = NULL;
15995 fp->uf_tml_total = NULL;
15996 fp->uf_tml_self = NULL;
15997 fp->uf_profiling = FALSE;
15998 if (prof_def_func())
15999 func_do_profile(fp);
16000#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016001 fp->uf_varargs = varargs;
16002 fp->uf_flags = flags;
16003 fp->uf_calls = 0;
16004 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016005 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006
16007erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 ga_clear_strings(&newargs);
16009 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016010ret_free:
16011 vim_free(skip_until);
16012 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015}
16016
16017/*
16018 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016019 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016021 * flags:
16022 * TFN_INT: internal function name OK
16023 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 * Advances "pp" to just after the function name (if no error).
16025 */
16026 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016027trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 char_u **pp;
16029 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016030 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016031 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016033 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 char_u *start;
16035 char_u *end;
16036 int lead;
16037 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016039 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016040
16041 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016042 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016044
16045 /* Check for hard coded <SNR>: already translated function ID (from a user
16046 * command). */
16047 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16048 && (*pp)[2] == (int)KE_SNR)
16049 {
16050 *pp += 3;
16051 len = get_id_len(pp) + 3;
16052 return vim_strnsave(start, len);
16053 }
16054
16055 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16056 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016058 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016060
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016061 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16062 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016063 if (end == start)
16064 {
16065 if (!skip)
16066 EMSG(_("E129: Function name required"));
16067 goto theend;
16068 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016069 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016070 {
16071 /*
16072 * Report an invalid expression in braces, unless the expression
16073 * evaluation has been cancelled due to an aborting error, an
16074 * interrupt, or an exception.
16075 */
16076 if (!aborting())
16077 {
16078 if (end != NULL)
16079 EMSG2(_(e_invarg2), start);
16080 }
16081 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016082 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016083 goto theend;
16084 }
16085
16086 if (lv.ll_tv != NULL)
16087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016088 if (fdp != NULL)
16089 {
16090 fdp->fd_dict = lv.ll_dict;
16091 fdp->fd_newkey = lv.ll_newkey;
16092 lv.ll_newkey = NULL;
16093 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016094 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016095 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16096 {
16097 name = vim_strsave(lv.ll_tv->vval.v_string);
16098 *pp = end;
16099 }
16100 else
16101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016102 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16103 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016104 EMSG(_(e_funcref));
16105 else
16106 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016107 name = NULL;
16108 }
16109 goto theend;
16110 }
16111
16112 if (lv.ll_name == NULL)
16113 {
16114 /* Error found, but continue after the function name. */
16115 *pp = end;
16116 goto theend;
16117 }
16118
16119 if (lv.ll_exp_name != NULL)
16120 len = STRLEN(lv.ll_exp_name);
16121 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016122 {
16123 if (lead == 2) /* skip over "s:" */
16124 lv.ll_name += 2;
16125 len = (int)(end - lv.ll_name);
16126 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016127
16128 /*
16129 * Copy the function name to allocated memory.
16130 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16131 * Accept <SNR>123_name() outside a script.
16132 */
16133 if (skip)
16134 lead = 0; /* do nothing */
16135 else if (lead > 0)
16136 {
16137 lead = 3;
16138 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16139 {
16140 if (current_SID <= 0)
16141 {
16142 EMSG(_(e_usingsid));
16143 goto theend;
16144 }
16145 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16146 lead += (int)STRLEN(sid_buf);
16147 }
16148 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016149 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016150 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016151 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016152 goto theend;
16153 }
16154 name = alloc((unsigned)(len + lead + 1));
16155 if (name != NULL)
16156 {
16157 if (lead > 0)
16158 {
16159 name[0] = K_SPECIAL;
16160 name[1] = KS_EXTRA;
16161 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016162 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016163 STRCPY(name + 3, sid_buf);
16164 }
16165 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16166 name[len + lead] = NUL;
16167 }
16168 *pp = end;
16169
16170theend:
16171 clear_lval(&lv);
16172 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173}
16174
16175/*
16176 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16177 * Return 2 if "p" starts with "s:".
16178 * Return 0 otherwise.
16179 */
16180 static int
16181eval_fname_script(p)
16182 char_u *p;
16183{
16184 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16185 || STRNICMP(p + 1, "SNR>", 4) == 0))
16186 return 5;
16187 if (p[0] == 's' && p[1] == ':')
16188 return 2;
16189 return 0;
16190}
16191
16192/*
16193 * Return TRUE if "p" starts with "<SID>" or "s:".
16194 * Only works if eval_fname_script() returned non-zero for "p"!
16195 */
16196 static int
16197eval_fname_sid(p)
16198 char_u *p;
16199{
16200 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16201}
16202
16203/*
16204 * List the head of the function: "name(arg1, arg2)".
16205 */
16206 static void
16207list_func_head(fp, indent)
16208 ufunc_T *fp;
16209 int indent;
16210{
16211 int j;
16212
16213 msg_start();
16214 if (indent)
16215 MSG_PUTS(" ");
16216 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016217 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 {
16219 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016220 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 }
16222 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016223 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016225 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 {
16227 if (j)
16228 MSG_PUTS(", ");
16229 msg_puts(FUNCARG(fp, j));
16230 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016231 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 {
16233 if (j)
16234 MSG_PUTS(", ");
16235 MSG_PUTS("...");
16236 }
16237 msg_putchar(')');
16238}
16239
16240/*
16241 * Find a function by name, return pointer to it in ufuncs.
16242 * Return NULL for unknown function.
16243 */
16244 static ufunc_T *
16245find_func(name)
16246 char_u *name;
16247{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016248 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016250 hi = hash_find(&func_hashtab, name);
16251 if (!HASHITEM_EMPTY(hi))
16252 return HI2UF(hi);
16253 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016256/*
16257 * Return TRUE if a function "name" exists.
16258 */
16259 static int
16260function_exists(name)
16261 char_u *name;
16262{
16263 char_u *p = name;
16264 int n = FALSE;
16265
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016266 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016267 if (p != NULL)
16268 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016269 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016270 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016271 else
16272 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016273 vim_free(p);
16274 }
16275 return n;
16276}
16277
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016278/*
16279 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016280 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016281 */
16282 static int
16283builtin_function(name)
16284 char_u *name;
16285{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016286 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
16287 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016288}
16289
Bram Moolenaar05159a02005-02-26 23:04:13 +000016290#if defined(FEAT_PROFILE) || defined(PROTO)
16291/*
16292 * Start profiling function "fp".
16293 */
16294 static void
16295func_do_profile(fp)
16296 ufunc_T *fp;
16297{
16298 fp->uf_tm_count = 0;
16299 profile_zero(&fp->uf_tm_self);
16300 profile_zero(&fp->uf_tm_total);
16301 if (fp->uf_tml_count == NULL)
16302 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16303 (sizeof(int) * fp->uf_lines.ga_len));
16304 if (fp->uf_tml_total == NULL)
16305 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16306 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16307 if (fp->uf_tml_self == NULL)
16308 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16309 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16310 fp->uf_tml_idx = -1;
16311 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16312 || fp->uf_tml_self == NULL)
16313 return; /* out of memory */
16314
16315 fp->uf_profiling = TRUE;
16316}
16317
16318/*
16319 * Dump the profiling results for all functions in file "fd".
16320 */
16321 void
16322func_dump_profile(fd)
16323 FILE *fd;
16324{
16325 hashitem_T *hi;
16326 int todo;
16327 ufunc_T *fp;
16328 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016329 ufunc_T **sorttab;
16330 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016331
16332 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016333 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16334
Bram Moolenaar05159a02005-02-26 23:04:13 +000016335 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16336 {
16337 if (!HASHITEM_EMPTY(hi))
16338 {
16339 --todo;
16340 fp = HI2UF(hi);
16341 if (fp->uf_profiling)
16342 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016343 if (sorttab != NULL)
16344 sorttab[st_len++] = fp;
16345
Bram Moolenaar05159a02005-02-26 23:04:13 +000016346 if (fp->uf_name[0] == K_SPECIAL)
16347 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16348 else
16349 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16350 if (fp->uf_tm_count == 1)
16351 fprintf(fd, "Called 1 time\n");
16352 else
16353 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16354 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16355 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16356 fprintf(fd, "\n");
16357 fprintf(fd, "count total (s) self (s)\n");
16358
16359 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16360 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016361 prof_func_line(fd, fp->uf_tml_count[i],
16362 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016363 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16364 }
16365 fprintf(fd, "\n");
16366 }
16367 }
16368 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016369
16370 if (sorttab != NULL && st_len > 0)
16371 {
16372 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16373 prof_total_cmp);
16374 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16375 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16376 prof_self_cmp);
16377 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16378 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016379}
Bram Moolenaar73830342005-02-28 22:48:19 +000016380
16381 static void
16382prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16383 FILE *fd;
16384 ufunc_T **sorttab;
16385 int st_len;
16386 char *title;
16387 int prefer_self; /* when equal print only self time */
16388{
16389 int i;
16390 ufunc_T *fp;
16391
16392 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16393 fprintf(fd, "count total (s) self (s) function\n");
16394 for (i = 0; i < 20 && i < st_len; ++i)
16395 {
16396 fp = sorttab[i];
16397 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16398 prefer_self);
16399 if (fp->uf_name[0] == K_SPECIAL)
16400 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16401 else
16402 fprintf(fd, " %s()\n", fp->uf_name);
16403 }
16404 fprintf(fd, "\n");
16405}
16406
16407/*
16408 * Print the count and times for one function or function line.
16409 */
16410 static void
16411prof_func_line(fd, count, total, self, prefer_self)
16412 FILE *fd;
16413 int count;
16414 proftime_T *total;
16415 proftime_T *self;
16416 int prefer_self; /* when equal print only self time */
16417{
16418 if (count > 0)
16419 {
16420 fprintf(fd, "%5d ", count);
16421 if (prefer_self && profile_equal(total, self))
16422 fprintf(fd, " ");
16423 else
16424 fprintf(fd, "%s ", profile_msg(total));
16425 if (!prefer_self && profile_equal(total, self))
16426 fprintf(fd, " ");
16427 else
16428 fprintf(fd, "%s ", profile_msg(self));
16429 }
16430 else
16431 fprintf(fd, " ");
16432}
16433
16434/*
16435 * Compare function for total time sorting.
16436 */
16437 static int
16438#ifdef __BORLANDC__
16439_RTLENTRYF
16440#endif
16441prof_total_cmp(s1, s2)
16442 const void *s1;
16443 const void *s2;
16444{
16445 ufunc_T *p1, *p2;
16446
16447 p1 = *(ufunc_T **)s1;
16448 p2 = *(ufunc_T **)s2;
16449 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16450}
16451
16452/*
16453 * Compare function for self time sorting.
16454 */
16455 static int
16456#ifdef __BORLANDC__
16457_RTLENTRYF
16458#endif
16459prof_self_cmp(s1, s2)
16460 const void *s1;
16461 const void *s2;
16462{
16463 ufunc_T *p1, *p2;
16464
16465 p1 = *(ufunc_T **)s1;
16466 p2 = *(ufunc_T **)s2;
16467 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16468}
16469
Bram Moolenaar05159a02005-02-26 23:04:13 +000016470#endif
16471
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016472/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016473 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016474 * Return TRUE if a package was loaded.
16475 */
16476 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016477script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016478 char_u *name;
16479{
16480 char_u *p;
16481 char_u *scriptname;
16482 int ret = FALSE;
16483
16484 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016485 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016486 if (p == NULL || p <= name + 2)
16487 return FALSE;
16488
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016489 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16490 scriptname = autoload_name(name);
16491 if (cmd_runtime(scriptname, FALSE) == OK)
16492 ret = TRUE;
16493
16494 vim_free(scriptname);
16495 return ret;
16496}
16497
16498/*
16499 * Return the autoload script name for a function or variable name.
16500 * Returns NULL when out of memory.
16501 */
16502 static char_u *
16503autoload_name(name)
16504 char_u *name;
16505{
16506 char_u *p;
16507 char_u *scriptname;
16508
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016509 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016510 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16511 if (scriptname == NULL)
16512 return FALSE;
16513 STRCPY(scriptname, "autoload/");
16514 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016515 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016516 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016517 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016518 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016519 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016520}
16521
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16523
16524/*
16525 * Function given to ExpandGeneric() to obtain the list of user defined
16526 * function names.
16527 */
16528 char_u *
16529get_user_func_name(xp, idx)
16530 expand_T *xp;
16531 int idx;
16532{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016533 static long_u done;
16534 static hashitem_T *hi;
16535 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536
16537 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016539 done = 0;
16540 hi = func_hashtab.ht_array;
16541 }
16542 if (done < func_hashtab.ht_used)
16543 {
16544 if (done++ > 0)
16545 ++hi;
16546 while (HASHITEM_EMPTY(hi))
16547 ++hi;
16548 fp = HI2UF(hi);
16549
16550 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16551 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552
16553 cat_func_name(IObuff, fp);
16554 if (xp->xp_context != EXPAND_USER_FUNC)
16555 {
16556 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016557 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 STRCAT(IObuff, ")");
16559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 return IObuff;
16561 }
16562 return NULL;
16563}
16564
16565#endif /* FEAT_CMDL_COMPL */
16566
16567/*
16568 * Copy the function name of "fp" to buffer "buf".
16569 * "buf" must be able to hold the function name plus three bytes.
16570 * Takes care of script-local function names.
16571 */
16572 static void
16573cat_func_name(buf, fp)
16574 char_u *buf;
16575 ufunc_T *fp;
16576{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016577 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 {
16579 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016580 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 }
16582 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016583 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584}
16585
16586/*
16587 * ":delfunction {name}"
16588 */
16589 void
16590ex_delfunction(eap)
16591 exarg_T *eap;
16592{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016593 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 char_u *p;
16595 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597
16598 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016599 name = trans_function_name(&p, eap->skip, 0, &fudi);
16600 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016602 {
16603 if (fudi.fd_dict != NULL && !eap->skip)
16604 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 if (!ends_excmd(*skipwhite(p)))
16608 {
16609 vim_free(name);
16610 EMSG(_(e_trailing));
16611 return;
16612 }
16613 eap->nextcmd = check_nextcmd(p);
16614 if (eap->nextcmd != NULL)
16615 *p = NUL;
16616
16617 if (!eap->skip)
16618 fp = find_func(name);
16619 vim_free(name);
16620
16621 if (!eap->skip)
16622 {
16623 if (fp == NULL)
16624 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016625 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 return;
16627 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016628 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 {
16630 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16631 return;
16632 }
16633
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016634 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016636 /* Delete the dict item that refers to the function, it will
16637 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016638 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016640 else
16641 func_free(fp);
16642 }
16643}
16644
16645/*
16646 * Free a function and remove it from the list of functions.
16647 */
16648 static void
16649func_free(fp)
16650 ufunc_T *fp;
16651{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016652 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016653
16654 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016655 ga_clear_strings(&(fp->uf_args));
16656 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016657#ifdef FEAT_PROFILE
16658 vim_free(fp->uf_tml_count);
16659 vim_free(fp->uf_tml_total);
16660 vim_free(fp->uf_tml_self);
16661#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016662
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016663 /* remove the function from the function hashtable */
16664 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16665 if (HASHITEM_EMPTY(hi))
16666 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016667 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016668 hash_remove(&func_hashtab, hi);
16669
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016670 vim_free(fp);
16671}
16672
16673/*
16674 * Unreference a Function: decrement the reference count and free it when it
16675 * becomes zero. Only for numbered functions.
16676 */
16677 static void
16678func_unref(name)
16679 char_u *name;
16680{
16681 ufunc_T *fp;
16682
16683 if (name != NULL && isdigit(*name))
16684 {
16685 fp = find_func(name);
16686 if (fp == NULL)
16687 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016688 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016689 {
16690 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016691 * when "uf_calls" becomes zero. */
16692 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016693 func_free(fp);
16694 }
16695 }
16696}
16697
16698/*
16699 * Count a reference to a Function.
16700 */
16701 static void
16702func_ref(name)
16703 char_u *name;
16704{
16705 ufunc_T *fp;
16706
16707 if (name != NULL && isdigit(*name))
16708 {
16709 fp = find_func(name);
16710 if (fp == NULL)
16711 EMSG2(_(e_intern2), "func_ref()");
16712 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016713 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 }
16715}
16716
16717/*
16718 * Call a user function.
16719 */
16720 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016721call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 ufunc_T *fp; /* pointer to function */
16723 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 typval_T *argvars; /* arguments */
16725 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 linenr_T firstline; /* first line of range */
16727 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016728 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729{
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 char_u *save_sourcing_name;
16731 linenr_T save_sourcing_lnum;
16732 scid_T save_current_SID;
16733 funccall_T fc;
16734 funccall_T *save_fcp = current_funccal;
16735 int save_did_emsg;
16736 static int depth = 0;
16737 dictitem_T *v;
16738 int fixvar_idx = 0; /* index in fixvar[] */
16739 int i;
16740 int ai;
16741 char_u numbuf[NUMBUFLEN];
16742 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016743#ifdef FEAT_PROFILE
16744 proftime_T wait_start;
16745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746
16747 /* If depth of calling is getting too high, don't execute the function */
16748 if (depth >= p_mfd)
16749 {
16750 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751 rettv->v_type = VAR_NUMBER;
16752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 return;
16754 }
16755 ++depth;
16756
16757 line_breakcheck(); /* check for CTRL-C hit */
16758
Bram Moolenaar33570922005-01-25 22:26:29 +000016759 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016761 fc.rettv = rettv;
16762 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 fc.linenr = 0;
16764 fc.returned = FALSE;
16765 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016767 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 fc.dbg_tick = debug_tick;
16769
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 /*
16771 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16772 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16773 * each argument variable and saves a lot of time.
16774 */
16775 /*
16776 * Init l: variables.
16777 */
16778 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016779 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016780 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 /* Set l:self to "selfdict". */
16782 v = &fc.fixvar[fixvar_idx++].var;
16783 STRCPY(v->di_key, "self");
16784 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16785 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16786 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016787 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016788 v->di_tv.vval.v_dict = selfdict;
16789 ++selfdict->dv_refcount;
16790 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016791
Bram Moolenaar33570922005-01-25 22:26:29 +000016792 /*
16793 * Init a: variables.
16794 * Set a:0 to "argcount".
16795 * Set a:000 to a list with room for the "..." arguments.
16796 */
16797 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16798 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016799 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 v = &fc.fixvar[fixvar_idx++].var;
16801 STRCPY(v->di_key, "000");
16802 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16803 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16804 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016805 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016806 v->di_tv.vval.v_list = &fc.l_varlist;
16807 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16808 fc.l_varlist.lv_refcount = 99999;
16809
16810 /*
16811 * Set a:firstline to "firstline" and a:lastline to "lastline".
16812 * Set a:name to named arguments.
16813 * Set a:N to the "..." arguments.
16814 */
16815 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16816 (varnumber_T)firstline);
16817 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16818 (varnumber_T)lastline);
16819 for (i = 0; i < argcount; ++i)
16820 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016821 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 if (ai < 0)
16823 /* named argument a:name */
16824 name = FUNCARG(fp, i);
16825 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016826 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 /* "..." argument a:1, a:2, etc. */
16828 sprintf((char *)numbuf, "%d", ai + 1);
16829 name = numbuf;
16830 }
16831 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16832 {
16833 v = &fc.fixvar[fixvar_idx++].var;
16834 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16835 }
16836 else
16837 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016838 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16839 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 if (v == NULL)
16841 break;
16842 v->di_flags = DI_FLAGS_RO;
16843 }
16844 STRCPY(v->di_key, name);
16845 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016847 /* Note: the values are copied directly to avoid alloc/free.
16848 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016850 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016851
16852 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16853 {
16854 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16855 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016856 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016857 }
16858 }
16859
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 /* Don't redraw while executing the function. */
16861 ++RedrawingDisabled;
16862 save_sourcing_name = sourcing_name;
16863 save_sourcing_lnum = sourcing_lnum;
16864 sourcing_lnum = 1;
16865 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016866 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 if (sourcing_name != NULL)
16868 {
16869 if (save_sourcing_name != NULL
16870 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16871 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16872 else
16873 STRCPY(sourcing_name, "function ");
16874 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16875
16876 if (p_verbose >= 12)
16877 {
16878 ++no_wait_return;
16879 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar555b2802005-05-19 21:08:39 +000016880 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 if (p_verbose >= 14)
16882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016884 char_u numbuf[NUMBUFLEN];
16885 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
16887 msg_puts((char_u *)"(");
16888 for (i = 0; i < argcount; ++i)
16889 {
16890 if (i > 0)
16891 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016892 if (argvars[i].v_type == VAR_NUMBER)
16893 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 else
16895 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016896 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016897 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016899 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 }
16901 }
16902 msg_puts((char_u *)")");
16903 }
16904 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16905 cmdline_row = msg_row;
16906 --no_wait_return;
16907 }
16908 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016909#ifdef FEAT_PROFILE
16910 if (do_profiling)
16911 {
16912 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16913 func_do_profile(fp);
16914 if (fp->uf_profiling
16915 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16916 {
16917 ++fp->uf_tm_count;
16918 profile_start(&fp->uf_tm_start);
16919 profile_zero(&fp->uf_tm_children);
16920 }
16921 script_prof_save(&wait_start);
16922 }
16923#endif
16924
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016926 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 save_did_emsg = did_emsg;
16928 did_emsg = FALSE;
16929
16930 /* call do_cmdline() to execute the lines */
16931 do_cmdline(NULL, get_func_line, (void *)&fc,
16932 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16933
16934 --RedrawingDisabled;
16935
16936 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016937 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016939 clear_tv(rettv);
16940 rettv->v_type = VAR_NUMBER;
16941 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 }
16943
Bram Moolenaar05159a02005-02-26 23:04:13 +000016944#ifdef FEAT_PROFILE
16945 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16946 {
16947 profile_end(&fp->uf_tm_start);
16948 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16949 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16950 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16951 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16952 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16953 {
16954 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16955 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16956 }
16957 }
16958#endif
16959
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 /* when being verbose, mention the return value */
16961 if (p_verbose >= 12)
16962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 ++no_wait_return;
16964 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16965
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000016967 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016968 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000016969 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
16970 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016971 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016973 char_u buf[MSG_BUF_LEN];
16974 char_u numbuf[NUMBUFLEN];
16975 char_u *tofree;
16976
Bram Moolenaar555b2802005-05-19 21:08:39 +000016977 /* The value may be very long. Skip the middle part, so that we
16978 * have some idea how it starts and ends. smsg() would always
16979 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000016980 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016981 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000016982 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016983 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 }
16985 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16986 cmdline_row = msg_row;
16987 --no_wait_return;
16988 }
16989
16990 vim_free(sourcing_name);
16991 sourcing_name = save_sourcing_name;
16992 sourcing_lnum = save_sourcing_lnum;
16993 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016994#ifdef FEAT_PROFILE
16995 if (do_profiling)
16996 script_prof_restore(&wait_start);
16997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998
16999 if (p_verbose >= 12 && sourcing_name != NULL)
17000 {
17001 ++no_wait_return;
17002 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar555b2802005-05-19 21:08:39 +000017003 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 msg_puts((char_u *)"\n"); /* don't overwrite this either */
17005 cmdline_row = msg_row;
17006 --no_wait_return;
17007 }
17008
17009 did_emsg |= save_did_emsg;
17010 current_funccal = save_fcp;
17011
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 /* The a: variables typevals were not alloced, only free the allocated
17013 * variables. */
17014 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17015
17016 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 --depth;
17018}
17019
17020/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 * Add a number variable "name" to dict "dp" with value "nr".
17022 */
17023 static void
17024add_nr_var(dp, v, name, nr)
17025 dict_T *dp;
17026 dictitem_T *v;
17027 char *name;
17028 varnumber_T nr;
17029{
17030 STRCPY(v->di_key, name);
17031 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17032 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17033 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017034 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017035 v->di_tv.vval.v_number = nr;
17036}
17037
17038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 * ":return [expr]"
17040 */
17041 void
17042ex_return(eap)
17043 exarg_T *eap;
17044{
17045 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 int returning = FALSE;
17048
17049 if (current_funccal == NULL)
17050 {
17051 EMSG(_("E133: :return not inside a function"));
17052 return;
17053 }
17054
17055 if (eap->skip)
17056 ++emsg_skip;
17057
17058 eap->nextcmd = NULL;
17059 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 {
17062 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 }
17067 /* It's safer to return also on error. */
17068 else if (!eap->skip)
17069 {
17070 /*
17071 * Return unless the expression evaluation has been cancelled due to an
17072 * aborting error, an interrupt, or an exception.
17073 */
17074 if (!aborting())
17075 returning = do_return(eap, FALSE, TRUE, NULL);
17076 }
17077
17078 /* When skipping or the return gets pending, advance to the next command
17079 * in this line (!returning). Otherwise, ignore the rest of the line.
17080 * Following lines will be ignored by get_func_line(). */
17081 if (returning)
17082 eap->nextcmd = NULL;
17083 else if (eap->nextcmd == NULL) /* no argument */
17084 eap->nextcmd = check_nextcmd(arg);
17085
17086 if (eap->skip)
17087 --emsg_skip;
17088}
17089
17090/*
17091 * Return from a function. Possibly makes the return pending. Also called
17092 * for a pending return at the ":endtry" or after returning from an extra
17093 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017094 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017095 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 * FALSE when the return gets pending.
17097 */
17098 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017099do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 exarg_T *eap;
17101 int reanimate;
17102 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017103 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104{
17105 int idx;
17106 struct condstack *cstack = eap->cstack;
17107
17108 if (reanimate)
17109 /* Undo the return. */
17110 current_funccal->returned = FALSE;
17111
17112 /*
17113 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17114 * not in its finally clause (which then is to be executed next) is found.
17115 * In this case, make the ":return" pending for execution at the ":endtry".
17116 * Otherwise, return normally.
17117 */
17118 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17119 if (idx >= 0)
17120 {
17121 cstack->cs_pending[idx] = CSTP_RETURN;
17122
17123 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 /* A pending return again gets pending. "rettv" points to an
17125 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017127 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 else
17129 {
17130 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 {
17137 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017138 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017139 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 else
17141 EMSG(_(e_outofmem));
17142 }
17143 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017144 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145
17146 if (reanimate)
17147 {
17148 /* The pending return value could be overwritten by a ":return"
17149 * without argument in a finally clause; reset the default
17150 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017151 current_funccal->rettv->v_type = VAR_NUMBER;
17152 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 }
17154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017155 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 }
17157 else
17158 {
17159 current_funccal->returned = TRUE;
17160
17161 /* If the return is carried out now, store the return value. For
17162 * a return immediately after reanimation, the value is already
17163 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017164 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017166 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 }
17171 }
17172
17173 return idx < 0;
17174}
17175
17176/*
17177 * Free the variable with a pending return value.
17178 */
17179 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180discard_pending_return(rettv)
17181 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182{
Bram Moolenaar33570922005-01-25 22:26:29 +000017183 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184}
17185
17186/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017187 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 * is an allocated string. Used by report_pending() for verbose messages.
17189 */
17190 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191get_return_cmd(rettv)
17192 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017194 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017196 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017198 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017200 if (s == NULL)
17201 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017202
17203 STRCPY(IObuff, ":return ");
17204 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17205 if (STRLEN(s) + 8 >= IOSIZE)
17206 STRCPY(IObuff + IOSIZE - 4, "...");
17207 vim_free(tofree);
17208 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209}
17210
17211/*
17212 * Get next function line.
17213 * Called by do_cmdline() to get the next line.
17214 * Returns allocated string, or NULL for end of function.
17215 */
17216/* ARGSUSED */
17217 char_u *
17218get_func_line(c, cookie, indent)
17219 int c; /* not used */
17220 void *cookie;
17221 int indent; /* not used */
17222{
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017224 ufunc_T *fp = fcp->func;
17225 char_u *retval;
17226 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227
17228 /* If breakpoints have been added/deleted need to check for it. */
17229 if (fcp->dbg_tick != debug_tick)
17230 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017231 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 sourcing_lnum);
17233 fcp->dbg_tick = debug_tick;
17234 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017235#ifdef FEAT_PROFILE
17236 if (do_profiling)
17237 func_line_end(cookie);
17238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239
Bram Moolenaar05159a02005-02-26 23:04:13 +000017240 gap = &fp->uf_lines;
17241 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 retval = NULL;
17243 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17244 retval = NULL;
17245 else
17246 {
17247 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17248 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017249#ifdef FEAT_PROFILE
17250 if (do_profiling)
17251 func_line_start(cookie);
17252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 }
17254
17255 /* Did we encounter a breakpoint? */
17256 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17257 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017258 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017260 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 sourcing_lnum);
17262 fcp->dbg_tick = debug_tick;
17263 }
17264
17265 return retval;
17266}
17267
Bram Moolenaar05159a02005-02-26 23:04:13 +000017268#if defined(FEAT_PROFILE) || defined(PROTO)
17269/*
17270 * Called when starting to read a function line.
17271 * "sourcing_lnum" must be correct!
17272 * When skipping lines it may not actually be executed, but we won't find out
17273 * until later and we need to store the time now.
17274 */
17275 void
17276func_line_start(cookie)
17277 void *cookie;
17278{
17279 funccall_T *fcp = (funccall_T *)cookie;
17280 ufunc_T *fp = fcp->func;
17281
17282 if (fp->uf_profiling && sourcing_lnum >= 1
17283 && sourcing_lnum <= fp->uf_lines.ga_len)
17284 {
17285 fp->uf_tml_idx = sourcing_lnum - 1;
17286 fp->uf_tml_execed = FALSE;
17287 profile_start(&fp->uf_tml_start);
17288 profile_zero(&fp->uf_tml_children);
17289 profile_get_wait(&fp->uf_tml_wait);
17290 }
17291}
17292
17293/*
17294 * Called when actually executing a function line.
17295 */
17296 void
17297func_line_exec(cookie)
17298 void *cookie;
17299{
17300 funccall_T *fcp = (funccall_T *)cookie;
17301 ufunc_T *fp = fcp->func;
17302
17303 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17304 fp->uf_tml_execed = TRUE;
17305}
17306
17307/*
17308 * Called when done with a function line.
17309 */
17310 void
17311func_line_end(cookie)
17312 void *cookie;
17313{
17314 funccall_T *fcp = (funccall_T *)cookie;
17315 ufunc_T *fp = fcp->func;
17316
17317 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17318 {
17319 if (fp->uf_tml_execed)
17320 {
17321 ++fp->uf_tml_count[fp->uf_tml_idx];
17322 profile_end(&fp->uf_tml_start);
17323 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17324 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17325 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17326 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17327 }
17328 fp->uf_tml_idx = -1;
17329 }
17330}
17331#endif
17332
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333/*
17334 * Return TRUE if the currently active function should be ended, because a
17335 * return was encountered or an error occured. Used inside a ":while".
17336 */
17337 int
17338func_has_ended(cookie)
17339 void *cookie;
17340{
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342
17343 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17344 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017345 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 || fcp->returned);
17347}
17348
17349/*
17350 * return TRUE if cookie indicates a function which "abort"s on errors.
17351 */
17352 int
17353func_has_abort(cookie)
17354 void *cookie;
17355{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017356 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357}
17358
17359#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17360typedef enum
17361{
17362 VAR_FLAVOUR_DEFAULT,
17363 VAR_FLAVOUR_SESSION,
17364 VAR_FLAVOUR_VIMINFO
17365} var_flavour_T;
17366
17367static var_flavour_T var_flavour __ARGS((char_u *varname));
17368
17369 static var_flavour_T
17370var_flavour(varname)
17371 char_u *varname;
17372{
17373 char_u *p = varname;
17374
17375 if (ASCII_ISUPPER(*p))
17376 {
17377 while (*(++p))
17378 if (ASCII_ISLOWER(*p))
17379 return VAR_FLAVOUR_SESSION;
17380 return VAR_FLAVOUR_VIMINFO;
17381 }
17382 else
17383 return VAR_FLAVOUR_DEFAULT;
17384}
17385#endif
17386
17387#if defined(FEAT_VIMINFO) || defined(PROTO)
17388/*
17389 * Restore global vars that start with a capital from the viminfo file
17390 */
17391 int
17392read_viminfo_varlist(virp, writing)
17393 vir_T *virp;
17394 int writing;
17395{
17396 char_u *tab;
17397 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
17400 if (!writing && (find_viminfo_parameter('!') != NULL))
17401 {
17402 tab = vim_strchr(virp->vir_line + 1, '\t');
17403 if (tab != NULL)
17404 {
17405 *tab++ = '\0'; /* isolate the variable name */
17406 if (*tab == 'S') /* string var */
17407 is_string = TRUE;
17408
17409 tab = vim_strchr(tab, '\t');
17410 if (tab != NULL)
17411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 if (is_string)
17413 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017414 tv.v_type = VAR_STRING;
17415 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 }
17418 else
17419 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017420 tv.v_type = VAR_NUMBER;
17421 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017423 set_var(virp->vir_line + 1, &tv, FALSE);
17424 if (is_string)
17425 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 }
17427 }
17428 }
17429
17430 return viminfo_readline(virp);
17431}
17432
17433/*
17434 * Write global vars that start with a capital to the viminfo file
17435 */
17436 void
17437write_viminfo_varlist(fp)
17438 FILE *fp;
17439{
Bram Moolenaar33570922005-01-25 22:26:29 +000017440 hashitem_T *hi;
17441 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017442 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017443 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017444 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017445 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017446 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447
17448 if (find_viminfo_parameter('!') == NULL)
17449 return;
17450
17451 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017452
Bram Moolenaar33570922005-01-25 22:26:29 +000017453 todo = globvarht.ht_used;
17454 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017456 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017458 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 this_var = HI2DI(hi);
17460 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017461 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017463 {
17464 case VAR_STRING: s = "STR"; break;
17465 case VAR_NUMBER: s = "NUM"; break;
17466 default: continue;
17467 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017468 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017469 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17470 if (p != NULL)
17471 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017472 vim_free(tofree);
17473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 }
17475 }
17476}
17477#endif
17478
17479#if defined(FEAT_SESSION) || defined(PROTO)
17480 int
17481store_session_globals(fd)
17482 FILE *fd;
17483{
Bram Moolenaar33570922005-01-25 22:26:29 +000017484 hashitem_T *hi;
17485 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017486 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 char_u *p, *t;
17488
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 todo = globvarht.ht_used;
17490 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017492 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017494 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 this_var = HI2DI(hi);
17496 if ((this_var->di_tv.v_type == VAR_NUMBER
17497 || this_var->di_tv.v_type == VAR_STRING)
17498 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017499 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017500 /* Escape special characters with a backslash. Turn a LF and
17501 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017502 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017503 (char_u *)"\\\"\n\r");
17504 if (p == NULL) /* out of memory */
17505 break;
17506 for (t = p; *t != NUL; ++t)
17507 if (*t == '\n')
17508 *t = 'n';
17509 else if (*t == '\r')
17510 *t = 'r';
17511 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 this_var->di_key,
17513 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17514 : ' ',
17515 p,
17516 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17517 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017518 || put_eol(fd) == FAIL)
17519 {
17520 vim_free(p);
17521 return FAIL;
17522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 vim_free(p);
17524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 }
17526 }
17527 return OK;
17528}
17529#endif
17530
17531#endif /* FEAT_EVAL */
17532
17533#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17534
17535
17536#ifdef WIN3264
17537/*
17538 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17539 */
17540static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17541static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17542static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17543
17544/*
17545 * Get the short pathname of a file.
17546 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17547 */
17548 static int
17549get_short_pathname(fnamep, bufp, fnamelen)
17550 char_u **fnamep;
17551 char_u **bufp;
17552 int *fnamelen;
17553{
17554 int l,len;
17555 char_u *newbuf;
17556
17557 len = *fnamelen;
17558
17559 l = GetShortPathName(*fnamep, *fnamep, len);
17560 if (l > len - 1)
17561 {
17562 /* If that doesn't work (not enough space), then save the string
17563 * and try again with a new buffer big enough
17564 */
17565 newbuf = vim_strnsave(*fnamep, l);
17566 if (newbuf == NULL)
17567 return 0;
17568
17569 vim_free(*bufp);
17570 *fnamep = *bufp = newbuf;
17571
17572 l = GetShortPathName(*fnamep,*fnamep,l+1);
17573
17574 /* Really should always succeed, as the buffer is big enough */
17575 }
17576
17577 *fnamelen = l;
17578 return 1;
17579}
17580
17581/*
17582 * Create a short path name. Returns the length of the buffer it needs.
17583 * Doesn't copy over the end of the buffer passed in.
17584 */
17585 static int
17586shortpath_for_invalid_fname(fname, bufp, fnamelen)
17587 char_u **fname;
17588 char_u **bufp;
17589 int *fnamelen;
17590{
17591 char_u *s, *p, *pbuf2, *pbuf3;
17592 char_u ch;
17593 int l,len,len2,plen,slen;
17594
17595 /* Make a copy */
17596 len2 = *fnamelen;
17597 pbuf2 = vim_strnsave(*fname, len2);
17598 pbuf3 = NULL;
17599
17600 s = pbuf2 + len2 - 1; /* Find the end */
17601 slen = 1;
17602 plen = len2;
17603
17604 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017605 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 {
17607 --s;
17608 ++slen;
17609 --plen;
17610 }
17611
17612 do
17613 {
17614 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017615 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 {
17617 --s;
17618 ++slen;
17619 --plen;
17620 }
17621 if (s <= pbuf2)
17622 break;
17623
17624 /* Remeber the character that is about to be blatted */
17625 ch = *s;
17626 *s = 0; /* get_short_pathname requires a null-terminated string */
17627
17628 /* Try it in situ */
17629 p = pbuf2;
17630 if (!get_short_pathname(&p, &pbuf3, &plen))
17631 {
17632 vim_free(pbuf2);
17633 return -1;
17634 }
17635 *s = ch; /* Preserve the string */
17636 } while (plen == 0);
17637
17638 if (plen > 0)
17639 {
17640 /* Remeber the length of the new string. */
17641 *fnamelen = len = plen + slen;
17642 vim_free(*bufp);
17643 if (len > len2)
17644 {
17645 /* If there's not enough space in the currently allocated string,
17646 * then copy it to a buffer big enough.
17647 */
17648 *fname= *bufp = vim_strnsave(p, len);
17649 if (*fname == NULL)
17650 return -1;
17651 }
17652 else
17653 {
17654 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17655 *fname = *bufp = pbuf2;
17656 if (p != pbuf2)
17657 strncpy(*fname, p, plen);
17658 pbuf2 = NULL;
17659 }
17660 /* Concat the next bit */
17661 strncpy(*fname + plen, s, slen);
17662 (*fname)[len] = '\0';
17663 }
17664 vim_free(pbuf3);
17665 vim_free(pbuf2);
17666 return 0;
17667}
17668
17669/*
17670 * Get a pathname for a partial path.
17671 */
17672 static int
17673shortpath_for_partial(fnamep, bufp, fnamelen)
17674 char_u **fnamep;
17675 char_u **bufp;
17676 int *fnamelen;
17677{
17678 int sepcount, len, tflen;
17679 char_u *p;
17680 char_u *pbuf, *tfname;
17681 int hasTilde;
17682
17683 /* Count up the path seperators from the RHS.. so we know which part
17684 * of the path to return.
17685 */
17686 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017687 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 if (vim_ispathsep(*p))
17689 ++sepcount;
17690
17691 /* Need full path first (use expand_env() to remove a "~/") */
17692 hasTilde = (**fnamep == '~');
17693 if (hasTilde)
17694 pbuf = tfname = expand_env_save(*fnamep);
17695 else
17696 pbuf = tfname = FullName_save(*fnamep, FALSE);
17697
17698 len = tflen = STRLEN(tfname);
17699
17700 if (!get_short_pathname(&tfname, &pbuf, &len))
17701 return -1;
17702
17703 if (len == 0)
17704 {
17705 /* Don't have a valid filename, so shorten the rest of the
17706 * path if we can. This CAN give us invalid 8.3 filenames, but
17707 * there's not a lot of point in guessing what it might be.
17708 */
17709 len = tflen;
17710 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17711 return -1;
17712 }
17713
17714 /* Count the paths backward to find the beginning of the desired string. */
17715 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017716 {
17717#ifdef FEAT_MBYTE
17718 if (has_mbyte)
17719 p -= mb_head_off(tfname, p);
17720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 if (vim_ispathsep(*p))
17722 {
17723 if (sepcount == 0 || (hasTilde && sepcount == 1))
17724 break;
17725 else
17726 sepcount --;
17727 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 if (hasTilde)
17730 {
17731 --p;
17732 if (p >= tfname)
17733 *p = '~';
17734 else
17735 return -1;
17736 }
17737 else
17738 ++p;
17739
17740 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17741 vim_free(*bufp);
17742 *fnamelen = (int)STRLEN(p);
17743 *bufp = pbuf;
17744 *fnamep = p;
17745
17746 return 0;
17747}
17748#endif /* WIN3264 */
17749
17750/*
17751 * Adjust a filename, according to a string of modifiers.
17752 * *fnamep must be NUL terminated when called. When returning, the length is
17753 * determined by *fnamelen.
17754 * Returns valid flags.
17755 * When there is an error, *fnamep is set to NULL.
17756 */
17757 int
17758modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17759 char_u *src; /* string with modifiers */
17760 int *usedlen; /* characters after src that are used */
17761 char_u **fnamep; /* file name so far */
17762 char_u **bufp; /* buffer for allocated file name or NULL */
17763 int *fnamelen; /* length of fnamep */
17764{
17765 int valid = 0;
17766 char_u *tail;
17767 char_u *s, *p, *pbuf;
17768 char_u dirname[MAXPATHL];
17769 int c;
17770 int has_fullname = 0;
17771#ifdef WIN3264
17772 int has_shortname = 0;
17773#endif
17774
17775repeat:
17776 /* ":p" - full path/file_name */
17777 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17778 {
17779 has_fullname = 1;
17780
17781 valid |= VALID_PATH;
17782 *usedlen += 2;
17783
17784 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17785 if ((*fnamep)[0] == '~'
17786#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17787 && ((*fnamep)[1] == '/'
17788# ifdef BACKSLASH_IN_FILENAME
17789 || (*fnamep)[1] == '\\'
17790# endif
17791 || (*fnamep)[1] == NUL)
17792
17793#endif
17794 )
17795 {
17796 *fnamep = expand_env_save(*fnamep);
17797 vim_free(*bufp); /* free any allocated file name */
17798 *bufp = *fnamep;
17799 if (*fnamep == NULL)
17800 return -1;
17801 }
17802
17803 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017804 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 {
17806 if (vim_ispathsep(*p)
17807 && p[1] == '.'
17808 && (p[2] == NUL
17809 || vim_ispathsep(p[2])
17810 || (p[2] == '.'
17811 && (p[3] == NUL || vim_ispathsep(p[3])))))
17812 break;
17813 }
17814
17815 /* FullName_save() is slow, don't use it when not needed. */
17816 if (*p != NUL || !vim_isAbsName(*fnamep))
17817 {
17818 *fnamep = FullName_save(*fnamep, *p != NUL);
17819 vim_free(*bufp); /* free any allocated file name */
17820 *bufp = *fnamep;
17821 if (*fnamep == NULL)
17822 return -1;
17823 }
17824
17825 /* Append a path separator to a directory. */
17826 if (mch_isdir(*fnamep))
17827 {
17828 /* Make room for one or two extra characters. */
17829 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17830 vim_free(*bufp); /* free any allocated file name */
17831 *bufp = *fnamep;
17832 if (*fnamep == NULL)
17833 return -1;
17834 add_pathsep(*fnamep);
17835 }
17836 }
17837
17838 /* ":." - path relative to the current directory */
17839 /* ":~" - path relative to the home directory */
17840 /* ":8" - shortname path - postponed till after */
17841 while (src[*usedlen] == ':'
17842 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17843 {
17844 *usedlen += 2;
17845 if (c == '8')
17846 {
17847#ifdef WIN3264
17848 has_shortname = 1; /* Postpone this. */
17849#endif
17850 continue;
17851 }
17852 pbuf = NULL;
17853 /* Need full path first (use expand_env() to remove a "~/") */
17854 if (!has_fullname)
17855 {
17856 if (c == '.' && **fnamep == '~')
17857 p = pbuf = expand_env_save(*fnamep);
17858 else
17859 p = pbuf = FullName_save(*fnamep, FALSE);
17860 }
17861 else
17862 p = *fnamep;
17863
17864 has_fullname = 0;
17865
17866 if (p != NULL)
17867 {
17868 if (c == '.')
17869 {
17870 mch_dirname(dirname, MAXPATHL);
17871 s = shorten_fname(p, dirname);
17872 if (s != NULL)
17873 {
17874 *fnamep = s;
17875 if (pbuf != NULL)
17876 {
17877 vim_free(*bufp); /* free any allocated file name */
17878 *bufp = pbuf;
17879 pbuf = NULL;
17880 }
17881 }
17882 }
17883 else
17884 {
17885 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17886 /* Only replace it when it starts with '~' */
17887 if (*dirname == '~')
17888 {
17889 s = vim_strsave(dirname);
17890 if (s != NULL)
17891 {
17892 *fnamep = s;
17893 vim_free(*bufp);
17894 *bufp = s;
17895 }
17896 }
17897 }
17898 vim_free(pbuf);
17899 }
17900 }
17901
17902 tail = gettail(*fnamep);
17903 *fnamelen = (int)STRLEN(*fnamep);
17904
17905 /* ":h" - head, remove "/file_name", can be repeated */
17906 /* Don't remove the first "/" or "c:\" */
17907 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17908 {
17909 valid |= VALID_HEAD;
17910 *usedlen += 2;
17911 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017912 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 --tail;
17914 *fnamelen = (int)(tail - *fnamep);
17915#ifdef VMS
17916 if (*fnamelen > 0)
17917 *fnamelen += 1; /* the path separator is part of the path */
17918#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017919 while (tail > s && !after_pathsep(s, tail))
17920 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 }
17922
17923 /* ":8" - shortname */
17924 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17925 {
17926 *usedlen += 2;
17927#ifdef WIN3264
17928 has_shortname = 1;
17929#endif
17930 }
17931
17932#ifdef WIN3264
17933 /* Check shortname after we have done 'heads' and before we do 'tails'
17934 */
17935 if (has_shortname)
17936 {
17937 pbuf = NULL;
17938 /* Copy the string if it is shortened by :h */
17939 if (*fnamelen < (int)STRLEN(*fnamep))
17940 {
17941 p = vim_strnsave(*fnamep, *fnamelen);
17942 if (p == 0)
17943 return -1;
17944 vim_free(*bufp);
17945 *bufp = *fnamep = p;
17946 }
17947
17948 /* Split into two implementations - makes it easier. First is where
17949 * there isn't a full name already, second is where there is.
17950 */
17951 if (!has_fullname && !vim_isAbsName(*fnamep))
17952 {
17953 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17954 return -1;
17955 }
17956 else
17957 {
17958 int l;
17959
17960 /* Simple case, already have the full-name
17961 * Nearly always shorter, so try first time. */
17962 l = *fnamelen;
17963 if (!get_short_pathname(fnamep, bufp, &l))
17964 return -1;
17965
17966 if (l == 0)
17967 {
17968 /* Couldn't find the filename.. search the paths.
17969 */
17970 l = *fnamelen;
17971 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17972 return -1;
17973 }
17974 *fnamelen = l;
17975 }
17976 }
17977#endif /* WIN3264 */
17978
17979 /* ":t" - tail, just the basename */
17980 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17981 {
17982 *usedlen += 2;
17983 *fnamelen -= (int)(tail - *fnamep);
17984 *fnamep = tail;
17985 }
17986
17987 /* ":e" - extension, can be repeated */
17988 /* ":r" - root, without extension, can be repeated */
17989 while (src[*usedlen] == ':'
17990 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17991 {
17992 /* find a '.' in the tail:
17993 * - for second :e: before the current fname
17994 * - otherwise: The last '.'
17995 */
17996 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17997 s = *fnamep - 2;
17998 else
17999 s = *fnamep + *fnamelen - 1;
18000 for ( ; s > tail; --s)
18001 if (s[0] == '.')
18002 break;
18003 if (src[*usedlen + 1] == 'e') /* :e */
18004 {
18005 if (s > tail)
18006 {
18007 *fnamelen += (int)(*fnamep - (s + 1));
18008 *fnamep = s + 1;
18009#ifdef VMS
18010 /* cut version from the extension */
18011 s = *fnamep + *fnamelen - 1;
18012 for ( ; s > *fnamep; --s)
18013 if (s[0] == ';')
18014 break;
18015 if (s > *fnamep)
18016 *fnamelen = s - *fnamep;
18017#endif
18018 }
18019 else if (*fnamep <= tail)
18020 *fnamelen = 0;
18021 }
18022 else /* :r */
18023 {
18024 if (s > tail) /* remove one extension */
18025 *fnamelen = (int)(s - *fnamep);
18026 }
18027 *usedlen += 2;
18028 }
18029
18030 /* ":s?pat?foo?" - substitute */
18031 /* ":gs?pat?foo?" - global substitute */
18032 if (src[*usedlen] == ':'
18033 && (src[*usedlen + 1] == 's'
18034 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18035 {
18036 char_u *str;
18037 char_u *pat;
18038 char_u *sub;
18039 int sep;
18040 char_u *flags;
18041 int didit = FALSE;
18042
18043 flags = (char_u *)"";
18044 s = src + *usedlen + 2;
18045 if (src[*usedlen + 1] == 'g')
18046 {
18047 flags = (char_u *)"g";
18048 ++s;
18049 }
18050
18051 sep = *s++;
18052 if (sep)
18053 {
18054 /* find end of pattern */
18055 p = vim_strchr(s, sep);
18056 if (p != NULL)
18057 {
18058 pat = vim_strnsave(s, (int)(p - s));
18059 if (pat != NULL)
18060 {
18061 s = p + 1;
18062 /* find end of substitution */
18063 p = vim_strchr(s, sep);
18064 if (p != NULL)
18065 {
18066 sub = vim_strnsave(s, (int)(p - s));
18067 str = vim_strnsave(*fnamep, *fnamelen);
18068 if (sub != NULL && str != NULL)
18069 {
18070 *usedlen = (int)(p + 1 - src);
18071 s = do_string_sub(str, pat, sub, flags);
18072 if (s != NULL)
18073 {
18074 *fnamep = s;
18075 *fnamelen = (int)STRLEN(s);
18076 vim_free(*bufp);
18077 *bufp = s;
18078 didit = TRUE;
18079 }
18080 }
18081 vim_free(sub);
18082 vim_free(str);
18083 }
18084 vim_free(pat);
18085 }
18086 }
18087 /* after using ":s", repeat all the modifiers */
18088 if (didit)
18089 goto repeat;
18090 }
18091 }
18092
18093 return valid;
18094}
18095
18096/*
18097 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18098 * "flags" can be "g" to do a global substitute.
18099 * Returns an allocated string, NULL for error.
18100 */
18101 char_u *
18102do_string_sub(str, pat, sub, flags)
18103 char_u *str;
18104 char_u *pat;
18105 char_u *sub;
18106 char_u *flags;
18107{
18108 int sublen;
18109 regmatch_T regmatch;
18110 int i;
18111 int do_all;
18112 char_u *tail;
18113 garray_T ga;
18114 char_u *ret;
18115 char_u *save_cpo;
18116
18117 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18118 save_cpo = p_cpo;
18119 p_cpo = (char_u *)"";
18120
18121 ga_init2(&ga, 1, 200);
18122
18123 do_all = (flags[0] == 'g');
18124
18125 regmatch.rm_ic = p_ic;
18126 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18127 if (regmatch.regprog != NULL)
18128 {
18129 tail = str;
18130 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18131 {
18132 /*
18133 * Get some space for a temporary buffer to do the substitution
18134 * into. It will contain:
18135 * - The text up to where the match is.
18136 * - The substituted text.
18137 * - The text after the match.
18138 */
18139 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18140 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18141 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18142 {
18143 ga_clear(&ga);
18144 break;
18145 }
18146
18147 /* copy the text up to where the match is */
18148 i = (int)(regmatch.startp[0] - tail);
18149 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18150 /* add the substituted text */
18151 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18152 + ga.ga_len + i, TRUE, TRUE, FALSE);
18153 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 /* avoid getting stuck on a match with an empty string */
18155 if (tail == regmatch.endp[0])
18156 {
18157 if (*tail == NUL)
18158 break;
18159 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18160 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 }
18162 else
18163 {
18164 tail = regmatch.endp[0];
18165 if (*tail == NUL)
18166 break;
18167 }
18168 if (!do_all)
18169 break;
18170 }
18171
18172 if (ga.ga_data != NULL)
18173 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18174
18175 vim_free(regmatch.regprog);
18176 }
18177
18178 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18179 ga_clear(&ga);
18180 p_cpo = save_cpo;
18181
18182 return ret;
18183}
18184
18185#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */