blob: c73d774b485d197c2ad7c0881ecfe8a035daaa36 [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 Moolenaar2641f772005-03-25 21:58:17 +00006237 {"setqflist", 1, 1, 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);
10604 rhs = check_map(keys, mode, exact);
10605 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{
12373 rettv->vval.v_number = -1;
12374
12375#ifdef FEAT_QUICKFIX
12376 if (argvars[0].v_type != VAR_LIST)
12377 EMSG(_(e_listreq));
12378 else
12379 {
12380 list_T *l = argvars[0].vval.v_list;
12381
12382 if (l != NULL && set_errorlist(l) == OK)
12383 rettv->vval.v_number = 0;
12384 }
12385#endif
12386}
12387
12388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 * "setreg()" function
12390 */
12391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 typval_T *argvars;
12394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395{
12396 int regname;
12397 char_u *strregname;
12398 char_u *stropt;
12399 int append;
12400 char_u yank_type;
12401 long block_len;
12402
12403 block_len = -1;
12404 yank_type = MAUTO;
12405 append = FALSE;
12406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 strregname = get_tv_string(argvars);
12408 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409
12410 regname = (strregname == NULL ? '"' : *strregname);
12411 if (regname == 0 || regname == '@')
12412 regname = '"';
12413 else if (regname == '=')
12414 return;
12415
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 switch (*stropt)
12420 {
12421 case 'a': case 'A': /* append */
12422 append = TRUE;
12423 break;
12424 case 'v': case 'c': /* character-wise selection */
12425 yank_type = MCHAR;
12426 break;
12427 case 'V': case 'l': /* line-wise selection */
12428 yank_type = MLINE;
12429 break;
12430#ifdef FEAT_VISUAL
12431 case 'b': case Ctrl_V: /* block-wise selection */
12432 yank_type = MBLOCK;
12433 if (VIM_ISDIGIT(stropt[1]))
12434 {
12435 ++stropt;
12436 block_len = getdigits(&stropt) - 1;
12437 --stropt;
12438 }
12439 break;
12440#endif
12441 }
12442 }
12443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447}
12448
12449
12450/*
12451 * "setwinvar(expr)" function
12452 */
12453/*ARGSUSED*/
12454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012456 typval_T *argvars;
12457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458{
12459 win_T *win;
12460#ifdef FEAT_WINDOWS
12461 win_T *save_curwin;
12462#endif
12463 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 char_u nbuf[NUMBUFLEN];
12466
12467 if (check_restricted() || check_secure())
12468 return;
12469 ++emsg_off;
12470 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 varp = &argvars[2];
12473
12474 if (win != NULL && varname != NULL && varp != NULL)
12475 {
12476#ifdef FEAT_WINDOWS
12477 /* set curwin to be our win, temporarily */
12478 save_curwin = curwin;
12479 curwin = win;
12480 curbuf = curwin->w_buffer;
12481#endif
12482
12483 if (*varname == '&')
12484 {
12485 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 set_option_value(varname, get_tv_number(varp),
12487 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 }
12489 else
12490 {
12491 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12492 if (winvarname != NULL)
12493 {
12494 STRCPY(winvarname, "w:");
12495 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012496 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 vim_free(winvarname);
12498 }
12499 }
12500
12501#ifdef FEAT_WINDOWS
12502 /* Restore current window, if it's still valid (autocomands can make
12503 * it invalid). */
12504 if (win_valid(save_curwin))
12505 {
12506 curwin = save_curwin;
12507 curbuf = curwin->w_buffer;
12508 }
12509#endif
12510 }
12511 --emsg_off;
12512}
12513
12514/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 */
12517 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012519 typval_T *argvars;
12520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012522 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 p = get_tv_string(&argvars[0]);
12525 rettv->vval.v_string = vim_strsave(p);
12526 simplify_filename(rettv->vval.v_string); /* simplify in place */
12527 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528}
12529
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530static int
12531#ifdef __BORLANDC__
12532 _RTLENTRYF
12533#endif
12534 item_compare __ARGS((const void *s1, const void *s2));
12535static int
12536#ifdef __BORLANDC__
12537 _RTLENTRYF
12538#endif
12539 item_compare2 __ARGS((const void *s1, const void *s2));
12540
12541static int item_compare_ic;
12542static char_u *item_compare_func;
12543#define ITEM_COMPARE_FAIL 999
12544
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012546 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 static int
12549#ifdef __BORLANDC__
12550_RTLENTRYF
12551#endif
12552item_compare(s1, s2)
12553 const void *s1;
12554 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556 char_u *p1, *p2;
12557 char_u *tofree1, *tofree2;
12558 int res;
12559 char_u numbuf1[NUMBUFLEN];
12560 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561
Bram Moolenaar33570922005-01-25 22:26:29 +000012562 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12563 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564 if (item_compare_ic)
12565 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567 res = STRCMP(p1, p2);
12568 vim_free(tofree1);
12569 vim_free(tofree2);
12570 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571}
12572
12573 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574#ifdef __BORLANDC__
12575_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577item_compare2(s1, s2)
12578 const void *s1;
12579 const void *s2;
12580{
12581 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 typval_T rettv;
12583 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012586 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12587 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012588 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12589 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590
12591 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12592 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012593 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594 clear_tv(&argv[0]);
12595 clear_tv(&argv[1]);
12596
12597 if (res == FAIL)
12598 res = ITEM_COMPARE_FAIL;
12599 else
12600 res = get_tv_number(&rettv);
12601 clear_tv(&rettv);
12602 return res;
12603}
12604
12605/*
12606 * "sort({list})" function
12607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012610 typval_T *argvars;
12611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612{
Bram Moolenaar33570922005-01-25 22:26:29 +000012613 list_T *l;
12614 listitem_T *li;
12615 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616 long len;
12617 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 rettv->vval.v_number = 0;
12620 if (argvars[0].v_type != VAR_LIST)
12621 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 else
12623 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012625 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012626 return;
12627 rettv->vval.v_list = l;
12628 rettv->v_type = VAR_LIST;
12629 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631 len = list_len(l);
12632 if (len <= 1)
12633 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635 item_compare_ic = FALSE;
12636 item_compare_func = NULL;
12637 if (argvars[1].v_type != VAR_UNKNOWN)
12638 {
12639 if (argvars[1].v_type == VAR_FUNC)
12640 item_compare_func = argvars[0].vval.v_string;
12641 else
12642 {
12643 i = get_tv_number(&argvars[1]);
12644 if (i == 1)
12645 item_compare_ic = TRUE;
12646 else
12647 item_compare_func = get_tv_string(&argvars[1]);
12648 }
12649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650
Bram Moolenaar0d660222005-01-07 21:51:51 +000012651 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653 if (ptrs == NULL)
12654 return;
12655 i = 0;
12656 for (li = l->lv_first; li != NULL; li = li->li_next)
12657 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658
Bram Moolenaar0d660222005-01-07 21:51:51 +000012659 /* test the compare function */
12660 if (item_compare_func != NULL
12661 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12662 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012663 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665 {
12666 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012667 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668 item_compare_func == NULL ? item_compare : item_compare2);
12669
12670 /* Clear the List and append the items in the sorted order. */
12671 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012672 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012673 for (i = 0; i < len; ++i)
12674 list_append(l, ptrs[i]);
12675 }
12676
12677 vim_free(ptrs);
12678 }
12679}
12680
12681 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012682f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012683 typval_T *argvars;
12684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012685{
12686 char_u *str;
12687 char_u *end;
12688 char_u *pat;
12689 regmatch_T regmatch;
12690 char_u patbuf[NUMBUFLEN];
12691 char_u *save_cpo;
12692 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 listitem_T *ni;
12694 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012695 colnr_T col = 0;
12696
12697 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12698 save_cpo = p_cpo;
12699 p_cpo = (char_u *)"";
12700
12701 str = get_tv_string(&argvars[0]);
12702 if (argvars[1].v_type == VAR_UNKNOWN)
12703 pat = (char_u *)"[\\x01- ]\\+";
12704 else
12705 pat = get_tv_string_buf(&argvars[1], patbuf);
12706
12707 l = list_alloc();
12708 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012710 rettv->v_type = VAR_LIST;
12711 rettv->vval.v_list = l;
12712 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713
Bram Moolenaar0d660222005-01-07 21:51:51 +000012714 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12715 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012717 regmatch.rm_ic = FALSE;
12718 while (*str != NUL)
12719 {
12720 match = vim_regexec_nl(&regmatch, str, col);
12721 if (match)
12722 end = regmatch.startp[0];
12723 else
12724 end = str + STRLEN(str);
12725 if (end > str)
12726 {
12727 ni = listitem_alloc();
12728 if (ni == NULL)
12729 break;
12730 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012731 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012732 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12733 list_append(l, ni);
12734 }
12735 if (!match)
12736 break;
12737 /* Advance to just after the match. */
12738 if (regmatch.endp[0] > str)
12739 col = 0;
12740 else
12741 {
12742 /* Don't get stuck at the same match. */
12743#ifdef FEAT_MBYTE
12744 col = mb_ptr2len_check(regmatch.endp[0]);
12745#else
12746 col = 1;
12747#endif
12748 }
12749 str = regmatch.endp[0];
12750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaar0d660222005-01-07 21:51:51 +000012752 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756}
12757
12758#ifdef HAVE_STRFTIME
12759/*
12760 * "strftime({format}[, {time}])" function
12761 */
12762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *argvars;
12765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766{
12767 char_u result_buf[256];
12768 struct tm *curtime;
12769 time_t seconds;
12770 char_u *p;
12771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012775 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 seconds = time(NULL);
12777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 curtime = localtime(&seconds);
12780 /* MSVC returns NULL for an invalid value of seconds. */
12781 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 else
12784 {
12785# ifdef FEAT_MBYTE
12786 vimconv_T conv;
12787 char_u *enc;
12788
12789 conv.vc_type = CONV_NONE;
12790 enc = enc_locale();
12791 convert_setup(&conv, p_enc, enc);
12792 if (conv.vc_type != CONV_NONE)
12793 p = string_convert(&conv, p, NULL);
12794# endif
12795 if (p != NULL)
12796 (void)strftime((char *)result_buf, sizeof(result_buf),
12797 (char *)p, curtime);
12798 else
12799 result_buf[0] = NUL;
12800
12801# ifdef FEAT_MBYTE
12802 if (conv.vc_type != CONV_NONE)
12803 vim_free(p);
12804 convert_setup(&conv, enc, p_enc);
12805 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 else
12808# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810
12811# ifdef FEAT_MBYTE
12812 /* Release conversion descriptors */
12813 convert_setup(&conv, NULL, NULL);
12814 vim_free(enc);
12815# endif
12816 }
12817}
12818#endif
12819
12820/*
12821 * "stridx()" function
12822 */
12823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *argvars;
12826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827{
12828 char_u buf[NUMBUFLEN];
12829 char_u *needle;
12830 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012831 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012833 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012836 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12837 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838
Bram Moolenaar33570922005-01-25 22:26:29 +000012839 if (argvars[2].v_type != VAR_UNKNOWN)
12840 {
12841 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012842 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012844 if (start_idx >= 0)
12845 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 }
12847
12848 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12849 if (pos != NULL)
12850 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851}
12852
12853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012854 * "string()" function
12855 */
12856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012858 typval_T *argvars;
12859 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012860{
12861 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012862 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012865 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012866 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868}
12869
12870/*
12871 * "strlen()" function
12872 */
12873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012875 typval_T *argvars;
12876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878 rettv->vval.v_number = (varnumber_T)(STRLEN(
12879 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
12882/*
12883 * "strpart()" function
12884 */
12885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889{
12890 char_u *p;
12891 int n;
12892 int len;
12893 int slen;
12894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 slen = (int)STRLEN(p);
12897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 else
12902 len = slen - n; /* default len: all bytes that are available. */
12903
12904 /*
12905 * Only return the overlap between the specified part and the actual
12906 * string.
12907 */
12908 if (n < 0)
12909 {
12910 len += n;
12911 n = 0;
12912 }
12913 else if (n > slen)
12914 n = slen;
12915 if (len < 0)
12916 len = 0;
12917 else if (n + len > slen)
12918 len = slen - n;
12919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 rettv->v_type = VAR_STRING;
12921 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922}
12923
12924/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012925 * "strridx()" function
12926 */
12927 static void
12928f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012929 typval_T *argvars;
12930 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931{
12932 char_u buf[NUMBUFLEN];
12933 char_u *needle;
12934 char_u *haystack;
12935 char_u *rest;
12936 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012937 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012938
12939 needle = get_tv_string(&argvars[1]);
12940 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012941 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012942 if (argvars[2].v_type != VAR_UNKNOWN)
12943 {
12944 /* Third argument: upper limit for index */
12945 end_idx = get_tv_number(&argvars[2]);
12946 if (end_idx < 0)
12947 {
12948 /* can never find a match */
12949 rettv->vval.v_number = -1;
12950 return;
12951 }
12952 }
12953 else
12954 end_idx = haystack_len;
12955
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012957 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012958 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012959 lastmatch = haystack + end_idx;
12960 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012961 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963 for (rest = haystack; *rest != '\0'; ++rest)
12964 {
12965 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012966 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012967 break;
12968 lastmatch = rest;
12969 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971
12972 if (lastmatch == NULL)
12973 rettv->vval.v_number = -1;
12974 else
12975 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12976}
12977
12978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979 * "strtrans()" function
12980 */
12981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012983 typval_T *argvars;
12984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->v_type = VAR_STRING;
12987 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988}
12989
12990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012991 * "submatch()" function
12992 */
12993 static void
12994f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012995 typval_T *argvars;
12996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012997{
12998 rettv->v_type = VAR_STRING;
12999 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
13000}
13001
13002/*
13003 * "substitute()" function
13004 */
13005 static void
13006f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013007 typval_T *argvars;
13008 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013009{
13010 char_u patbuf[NUMBUFLEN];
13011 char_u subbuf[NUMBUFLEN];
13012 char_u flagsbuf[NUMBUFLEN];
13013
13014 rettv->v_type = VAR_STRING;
13015 rettv->vval.v_string = do_string_sub(
13016 get_tv_string(&argvars[0]),
13017 get_tv_string_buf(&argvars[1], patbuf),
13018 get_tv_string_buf(&argvars[2], subbuf),
13019 get_tv_string_buf(&argvars[3], flagsbuf));
13020}
13021
13022/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013023 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 */
13025/*ARGSUSED*/
13026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013027f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013028 typval_T *argvars;
13029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030{
13031 int id = 0;
13032#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013033 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 long col;
13035 int trans;
13036
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013037 lnum = get_tv_lnum(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 col = get_tv_number(&argvars[1]) - 1;
13039 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013041 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13042 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13043 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044#endif
13045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047}
13048
13049/*
13050 * "synIDattr(id, what [, mode])" function
13051 */
13052/*ARGSUSED*/
13053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013055 typval_T *argvars;
13056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057{
13058 char_u *p = NULL;
13059#ifdef FEAT_SYN_HL
13060 int id;
13061 char_u *what;
13062 char_u *mode;
13063 char_u modebuf[NUMBUFLEN];
13064 int modec;
13065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 id = get_tv_number(&argvars[0]);
13067 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 modec = TOLOWER_ASC(mode[0]);
13072 if (modec != 't' && modec != 'c'
13073#ifdef FEAT_GUI
13074 && modec != 'g'
13075#endif
13076 )
13077 modec = 0; /* replace invalid with current */
13078 }
13079 else
13080 {
13081#ifdef FEAT_GUI
13082 if (gui.in_use)
13083 modec = 'g';
13084 else
13085#endif
13086 if (t_colors > 1)
13087 modec = 'c';
13088 else
13089 modec = 't';
13090 }
13091
13092
13093 switch (TOLOWER_ASC(what[0]))
13094 {
13095 case 'b':
13096 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13097 p = highlight_color(id, what, modec);
13098 else /* bold */
13099 p = highlight_has_attr(id, HL_BOLD, modec);
13100 break;
13101
13102 case 'f': /* fg[#] */
13103 p = highlight_color(id, what, modec);
13104 break;
13105
13106 case 'i':
13107 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13108 p = highlight_has_attr(id, HL_INVERSE, modec);
13109 else /* italic */
13110 p = highlight_has_attr(id, HL_ITALIC, modec);
13111 break;
13112
13113 case 'n': /* name */
13114 p = get_highlight_name(NULL, id - 1);
13115 break;
13116
13117 case 'r': /* reverse */
13118 p = highlight_has_attr(id, HL_INVERSE, modec);
13119 break;
13120
13121 case 's': /* standout */
13122 p = highlight_has_attr(id, HL_STANDOUT, modec);
13123 break;
13124
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013125 case 'u':
13126 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13127 /* underline */
13128 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13129 else
13130 /* undercurl */
13131 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 break;
13133 }
13134
13135 if (p != NULL)
13136 p = vim_strsave(p);
13137#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013138 rettv->v_type = VAR_STRING;
13139 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140}
13141
13142/*
13143 * "synIDtrans(id)" function
13144 */
13145/*ARGSUSED*/
13146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013148 typval_T *argvars;
13149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150{
13151 int id;
13152
13153#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155
13156 if (id > 0)
13157 id = syn_get_final_id(id);
13158 else
13159#endif
13160 id = 0;
13161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163}
13164
13165/*
13166 * "system()" function
13167 */
13168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013170 typval_T *argvars;
13171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013173 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013175 char_u *infile = NULL;
13176 char_u buf[NUMBUFLEN];
13177 int err = FALSE;
13178 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013180 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013181 {
13182 /*
13183 * Write the string to a temp file, to be used for input of the shell
13184 * command.
13185 */
13186 if ((infile = vim_tempname('i')) == NULL)
13187 {
13188 EMSG(_(e_notmp));
13189 return;
13190 }
13191
13192 fd = mch_fopen((char *)infile, WRITEBIN);
13193 if (fd == NULL)
13194 {
13195 EMSG2(_(e_notopen), infile);
13196 goto done;
13197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013199 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13200 err = TRUE;
13201 if (fclose(fd) != 0)
13202 err = TRUE;
13203 if (err)
13204 {
13205 EMSG(_("E677: Error writing temp file"));
13206 goto done;
13207 }
13208 }
13209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013211
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212#ifdef USE_CR
13213 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013214 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 {
13216 char_u *s;
13217
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013218 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 {
13220 if (*s == CAR)
13221 *s = NL;
13222 }
13223 }
13224#else
13225# ifdef USE_CRNL
13226 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013227 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 {
13229 char_u *s, *d;
13230
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013231 d = res;
13232 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 {
13234 if (s[0] == CAR && s[1] == NL)
13235 ++s;
13236 *d++ = *s;
13237 }
13238 *d = NUL;
13239 }
13240# endif
13241#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013242
13243done:
13244 if (infile != NULL)
13245 {
13246 mch_remove(infile);
13247 vim_free(infile);
13248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->v_type = VAR_STRING;
13250 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251}
13252
13253/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013254 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013255 */
13256 static void
13257f_taglist(argvars, rettv)
13258 typval_T *argvars;
13259 typval_T *rettv;
13260{
13261 char_u *tag_pattern;
13262 list_T *l;
13263
13264 tag_pattern = get_tv_string(&argvars[0]);
13265
13266 rettv->vval.v_number = FALSE;
13267
13268 l = list_alloc();
13269 if (l != NULL)
13270 {
13271 if (get_tags(l, tag_pattern) != FAIL)
13272 {
13273 rettv->vval.v_list = l;
13274 rettv->v_type = VAR_LIST;
13275 ++l->lv_refcount;
13276 }
13277 else
13278 list_free(l);
13279 }
13280}
13281
13282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 * "tempname()" function
13284 */
13285/*ARGSUSED*/
13286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *argvars;
13289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290{
13291 static int x = 'A';
13292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 rettv->v_type = VAR_STRING;
13294 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295
13296 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13297 * names. Skip 'I' and 'O', they are used for shell redirection. */
13298 do
13299 {
13300 if (x == 'Z')
13301 x = '0';
13302 else if (x == '9')
13303 x = 'A';
13304 else
13305 {
13306#ifdef EBCDIC
13307 if (x == 'I')
13308 x = 'J';
13309 else if (x == 'R')
13310 x = 'S';
13311 else
13312#endif
13313 ++x;
13314 }
13315 } while (x == 'I' || x == 'O');
13316}
13317
13318/*
13319 * "tolower(string)" function
13320 */
13321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013323 typval_T *argvars;
13324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325{
13326 char_u *p;
13327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328 p = vim_strsave(get_tv_string(&argvars[0]));
13329 rettv->v_type = VAR_STRING;
13330 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331
13332 if (p != NULL)
13333 while (*p != NUL)
13334 {
13335#ifdef FEAT_MBYTE
13336 int l;
13337
13338 if (enc_utf8)
13339 {
13340 int c, lc;
13341
13342 c = utf_ptr2char(p);
13343 lc = utf_tolower(c);
13344 l = utf_ptr2len_check(p);
13345 /* TODO: reallocate string when byte count changes. */
13346 if (utf_char2len(lc) == l)
13347 utf_char2bytes(lc, p);
13348 p += l;
13349 }
13350 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13351 p += l; /* skip multi-byte character */
13352 else
13353#endif
13354 {
13355 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13356 ++p;
13357 }
13358 }
13359}
13360
13361/*
13362 * "toupper(string)" function
13363 */
13364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013365f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *argvars;
13367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368{
13369 char_u *p;
13370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 p = vim_strsave(get_tv_string(&argvars[0]));
13372 rettv->v_type = VAR_STRING;
13373 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374
13375 if (p != NULL)
13376 while (*p != NUL)
13377 {
13378#ifdef FEAT_MBYTE
13379 int l;
13380
13381 if (enc_utf8)
13382 {
13383 int c, uc;
13384
13385 c = utf_ptr2char(p);
13386 uc = utf_toupper(c);
13387 l = utf_ptr2len_check(p);
13388 /* TODO: reallocate string when byte count changes. */
13389 if (utf_char2len(uc) == l)
13390 utf_char2bytes(uc, p);
13391 p += l;
13392 }
13393 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13394 p += l; /* skip multi-byte character */
13395 else
13396#endif
13397 {
13398 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13399 p++;
13400 }
13401 }
13402}
13403
13404/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013405 * "tr(string, fromstr, tostr)" function
13406 */
13407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013411{
13412 char_u *instr;
13413 char_u *fromstr;
13414 char_u *tostr;
13415 char_u *p;
13416#ifdef FEAT_MBYTE
13417 int inlen;
13418 int fromlen;
13419 int tolen;
13420 int idx;
13421 char_u *cpstr;
13422 int cplen;
13423 int first = TRUE;
13424#endif
13425 char_u buf[NUMBUFLEN];
13426 char_u buf2[NUMBUFLEN];
13427 garray_T ga;
13428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 instr = get_tv_string(&argvars[0]);
13430 fromstr = get_tv_string_buf(&argvars[1], buf);
13431 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013432
13433 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->v_type = VAR_STRING;
13435 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013436 ga_init2(&ga, (int)sizeof(char), 80);
13437
13438#ifdef FEAT_MBYTE
13439 if (!has_mbyte)
13440#endif
13441 /* not multi-byte: fromstr and tostr must be the same length */
13442 if (STRLEN(fromstr) != STRLEN(tostr))
13443 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013444#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013445error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013446#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013447 EMSG2(_(e_invarg2), fromstr);
13448 ga_clear(&ga);
13449 return;
13450 }
13451
13452 /* fromstr and tostr have to contain the same number of chars */
13453 while (*instr != NUL)
13454 {
13455#ifdef FEAT_MBYTE
13456 if (has_mbyte)
13457 {
13458 inlen = mb_ptr2len_check(instr);
13459 cpstr = instr;
13460 cplen = inlen;
13461 idx = 0;
13462 for (p = fromstr; *p != NUL; p += fromlen)
13463 {
13464 fromlen = mb_ptr2len_check(p);
13465 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13466 {
13467 for (p = tostr; *p != NUL; p += tolen)
13468 {
13469 tolen = mb_ptr2len_check(p);
13470 if (idx-- == 0)
13471 {
13472 cplen = tolen;
13473 cpstr = p;
13474 break;
13475 }
13476 }
13477 if (*p == NUL) /* tostr is shorter than fromstr */
13478 goto error;
13479 break;
13480 }
13481 ++idx;
13482 }
13483
13484 if (first && cpstr == instr)
13485 {
13486 /* Check that fromstr and tostr have the same number of
13487 * (multi-byte) characters. Done only once when a character
13488 * of instr doesn't appear in fromstr. */
13489 first = FALSE;
13490 for (p = tostr; *p != NUL; p += tolen)
13491 {
13492 tolen = mb_ptr2len_check(p);
13493 --idx;
13494 }
13495 if (idx != 0)
13496 goto error;
13497 }
13498
13499 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013500 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013501 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013502
13503 instr += inlen;
13504 }
13505 else
13506#endif
13507 {
13508 /* When not using multi-byte chars we can do it faster. */
13509 p = vim_strchr(fromstr, *instr);
13510 if (p != NULL)
13511 ga_append(&ga, tostr[p - fromstr]);
13512 else
13513 ga_append(&ga, *instr);
13514 ++instr;
13515 }
13516 }
13517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013519}
13520
13521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 * "type(expr)" function
13523 */
13524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013526 typval_T *argvars;
13527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013529 int n;
13530
13531 switch (argvars[0].v_type)
13532 {
13533 case VAR_NUMBER: n = 0; break;
13534 case VAR_STRING: n = 1; break;
13535 case VAR_FUNC: n = 2; break;
13536 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013537 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013538 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13539 }
13540 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541}
13542
13543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013544 * "values(dict)" function
13545 */
13546 static void
13547f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 typval_T *argvars;
13549 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013550{
13551 dict_list(argvars, rettv, 1);
13552}
13553
13554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 * "virtcol(string)" function
13556 */
13557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 typval_T *argvars;
13560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561{
13562 colnr_T vcol = 0;
13563 pos_T *fp;
13564
13565 fp = var2fpos(&argvars[0], FALSE);
13566 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13567 {
13568 getvvcol(curwin, fp, NULL, NULL, &vcol);
13569 ++vcol;
13570 }
13571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573}
13574
13575/*
13576 * "visualmode()" function
13577 */
13578/*ARGSUSED*/
13579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013581 typval_T *argvars;
13582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583{
13584#ifdef FEAT_VISUAL
13585 char_u str[2];
13586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 str[0] = curbuf->b_visual_mode_eval;
13589 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591
13592 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013593 if ((argvars[0].v_type == VAR_NUMBER
13594 && argvars[0].vval.v_number != 0)
13595 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 curbuf->b_visual_mode_eval = NUL;
13598#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600#endif
13601}
13602
13603/*
13604 * "winbufnr(nr)" function
13605 */
13606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013608 typval_T *argvars;
13609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
13611 win_T *wp;
13612
13613 wp = find_win_by_nr(&argvars[0]);
13614 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
13620/*
13621 * "wincol()" function
13622 */
13623/*ARGSUSED*/
13624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *argvars;
13627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
13629 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631}
13632
13633/*
13634 * "winheight(nr)" function
13635 */
13636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *argvars;
13639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641 win_T *wp;
13642
13643 wp = find_win_by_nr(&argvars[0]);
13644 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648}
13649
13650/*
13651 * "winline()" function
13652 */
13653/*ARGSUSED*/
13654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *argvars;
13657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658{
13659 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661}
13662
13663/*
13664 * "winnr()" function
13665 */
13666/* ARGSUSED */
13667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671{
13672 int nr = 1;
13673#ifdef FEAT_WINDOWS
13674 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013675 win_T *twin = curwin;
13676 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013681 if (STRCMP(arg, "$") == 0)
13682 twin = lastwin;
13683 else if (STRCMP(arg, "#") == 0)
13684 {
13685 twin = prevwin;
13686 if (prevwin == NULL)
13687 nr = 0;
13688 }
13689 else
13690 {
13691 EMSG2(_(e_invexpr2), arg);
13692 nr = 0;
13693 }
13694 }
13695
13696 if (nr > 0)
13697 for (wp = firstwin; wp != twin; wp = wp->w_next)
13698 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701}
13702
13703/*
13704 * "winrestcmd()" function
13705 */
13706/* ARGSUSED */
13707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013709 typval_T *argvars;
13710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
13712#ifdef FEAT_WINDOWS
13713 win_T *wp;
13714 int winnr = 1;
13715 garray_T ga;
13716 char_u buf[50];
13717
13718 ga_init2(&ga, (int)sizeof(char), 70);
13719 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13720 {
13721 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13722 ga_concat(&ga, buf);
13723# ifdef FEAT_VERTSPLIT
13724 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13725 ga_concat(&ga, buf);
13726# endif
13727 ++winnr;
13728 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013729 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736}
13737
13738/*
13739 * "winwidth(nr)" function
13740 */
13741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013743 typval_T *argvars;
13744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745{
13746 win_T *wp;
13747
13748 wp = find_win_by_nr(&argvars[0]);
13749 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 else
13752#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#endif
13757}
13758
13759 static win_T *
13760find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013761 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762{
13763#ifdef FEAT_WINDOWS
13764 win_T *wp;
13765#endif
13766 int nr;
13767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769
13770#ifdef FEAT_WINDOWS
13771 if (nr == 0)
13772 return curwin;
13773
13774 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13775 if (--nr <= 0)
13776 break;
13777 return wp;
13778#else
13779 if (nr == 0 || nr == 1)
13780 return curwin;
13781 return NULL;
13782#endif
13783}
13784
13785/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013786 * "writefile()" function
13787 */
13788 static void
13789f_writefile(argvars, rettv)
13790 typval_T *argvars;
13791 typval_T *rettv;
13792{
13793 int binary = FALSE;
13794 char_u *fname;
13795 FILE *fd;
13796 listitem_T *li;
13797 char_u *s;
13798 int ret = 0;
13799 int c;
13800
13801 if (argvars[0].v_type != VAR_LIST)
13802 {
13803 EMSG2(_(e_listarg), "writefile()");
13804 return;
13805 }
13806 if (argvars[0].vval.v_list == NULL)
13807 return;
13808
13809 if (argvars[2].v_type != VAR_UNKNOWN
13810 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13811 binary = TRUE;
13812
13813 /* Always open the file in binary mode, library functions have a mind of
13814 * their own about CR-LF conversion. */
13815 fname = get_tv_string(&argvars[1]);
13816 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13817 {
13818 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13819 ret = -1;
13820 }
13821 else
13822 {
13823 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13824 li = li->li_next)
13825 {
13826 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13827 {
13828 if (*s == '\n')
13829 c = putc(NUL, fd);
13830 else
13831 c = putc(*s, fd);
13832 if (c == EOF)
13833 {
13834 ret = -1;
13835 break;
13836 }
13837 }
13838 if (!binary || li->li_next != NULL)
13839 if (putc('\n', fd) == EOF)
13840 {
13841 ret = -1;
13842 break;
13843 }
13844 if (ret < 0)
13845 {
13846 EMSG(_(e_write));
13847 break;
13848 }
13849 }
13850 fclose(fd);
13851 }
13852
13853 rettv->vval.v_number = ret;
13854}
13855
13856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 * Translate a String variable into a position.
13858 */
13859 static pos_T *
13860var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013861 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 int lnum; /* TRUE when $ is last line */
13863{
13864 char_u *name;
13865 static pos_T pos;
13866 pos_T *pp;
13867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 if (name[0] == '.') /* cursor */
13870 return &curwin->w_cursor;
13871 if (name[0] == '\'') /* mark */
13872 {
13873 pp = getmark(name[1], FALSE);
13874 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13875 return NULL;
13876 return pp;
13877 }
13878 if (name[0] == '$') /* last column or line */
13879 {
13880 if (lnum)
13881 {
13882 pos.lnum = curbuf->b_ml.ml_line_count;
13883 pos.col = 0;
13884 }
13885 else
13886 {
13887 pos.lnum = curwin->w_cursor.lnum;
13888 pos.col = (colnr_T)STRLEN(ml_get_curline());
13889 }
13890 return &pos;
13891 }
13892 return NULL;
13893}
13894
13895/*
13896 * Get the length of an environment variable name.
13897 * Advance "arg" to the first character after the name.
13898 * Return 0 for error.
13899 */
13900 static int
13901get_env_len(arg)
13902 char_u **arg;
13903{
13904 char_u *p;
13905 int len;
13906
13907 for (p = *arg; vim_isIDc(*p); ++p)
13908 ;
13909 if (p == *arg) /* no name found */
13910 return 0;
13911
13912 len = (int)(p - *arg);
13913 *arg = p;
13914 return len;
13915}
13916
13917/*
13918 * Get the length of the name of a function or internal variable.
13919 * "arg" is advanced to the first non-white character after the name.
13920 * Return 0 if something is wrong.
13921 */
13922 static int
13923get_id_len(arg)
13924 char_u **arg;
13925{
13926 char_u *p;
13927 int len;
13928
13929 /* Find the end of the name. */
13930 for (p = *arg; eval_isnamec(*p); ++p)
13931 ;
13932 if (p == *arg) /* no name found */
13933 return 0;
13934
13935 len = (int)(p - *arg);
13936 *arg = skipwhite(p);
13937
13938 return len;
13939}
13940
13941/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013942 * Get the length of the name of a variable or function.
13943 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013945 * Return -1 if curly braces expansion failed.
13946 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 * If the name contains 'magic' {}'s, expand them and return the
13948 * expanded name in an allocated string via 'alias' - caller must free.
13949 */
13950 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013951get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 char_u **arg;
13953 char_u **alias;
13954 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013955 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956{
13957 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 char_u *p;
13959 char_u *expr_start;
13960 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
13962 *alias = NULL; /* default to no alias */
13963
13964 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13965 && (*arg)[2] == (int)KE_SNR)
13966 {
13967 /* hard coded <SNR>, already translated */
13968 *arg += 3;
13969 return get_id_len(arg) + 3;
13970 }
13971 len = eval_fname_script(*arg);
13972 if (len > 0)
13973 {
13974 /* literal "<SID>", "s:" or "<SNR>" */
13975 *arg += len;
13976 }
13977
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013981 p = find_name_end(*arg, &expr_start, &expr_end,
13982 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 if (expr_start != NULL)
13984 {
13985 char_u *temp_string;
13986
13987 if (!evaluate)
13988 {
13989 len += (int)(p - *arg);
13990 *arg = skipwhite(p);
13991 return len;
13992 }
13993
13994 /*
13995 * Include any <SID> etc in the expanded string:
13996 * Thus the -len here.
13997 */
13998 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13999 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014000 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 *alias = temp_string;
14002 *arg = skipwhite(p);
14003 return (int)STRLEN(temp_string);
14004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005
14006 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014007 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 EMSG2(_(e_invexpr2), *arg);
14009
14010 return len;
14011}
14012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013/*
14014 * Find the end of a variable or function name, taking care of magic braces.
14015 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14016 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014017 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014018 * Return a pointer to just after the name. Equal to "arg" if there is no
14019 * valid name.
14020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014022find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 char_u *arg;
14024 char_u **expr_start;
14025 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014026 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 int mb_nest = 0;
14029 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 char_u *p;
14031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 *expr_start = NULL;
14035 *expr_end = NULL;
14036 }
14037
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014038 /* Quick check for valid starting character. */
14039 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14040 return arg;
14041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 for (p = arg; *p != NUL
14043 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014044 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014045 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 || mb_nest != 0
14047 || br_nest != 0); ++p)
14048 {
14049 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051 if (*p == '[')
14052 ++br_nest;
14053 else if (*p == ']')
14054 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 if (*p == '{')
14059 {
14060 mb_nest++;
14061 if (expr_start != NULL && *expr_start == NULL)
14062 *expr_start = p;
14063 }
14064 else if (*p == '}')
14065 {
14066 mb_nest--;
14067 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14068 *expr_end = p;
14069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 }
14072
14073 return p;
14074}
14075
14076/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014077 * Expands out the 'magic' {}'s in a variable/function name.
14078 * Note that this can call itself recursively, to deal with
14079 * constructs like foo{bar}{baz}{bam}
14080 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14081 * "in_start" ^
14082 * "expr_start" ^
14083 * "expr_end" ^
14084 * "in_end" ^
14085 *
14086 * Returns a new allocated string, which the caller must free.
14087 * Returns NULL for failure.
14088 */
14089 static char_u *
14090make_expanded_name(in_start, expr_start, expr_end, in_end)
14091 char_u *in_start;
14092 char_u *expr_start;
14093 char_u *expr_end;
14094 char_u *in_end;
14095{
14096 char_u c1;
14097 char_u *retval = NULL;
14098 char_u *temp_result;
14099 char_u *nextcmd = NULL;
14100
14101 if (expr_end == NULL || in_end == NULL)
14102 return NULL;
14103 *expr_start = NUL;
14104 *expr_end = NUL;
14105 c1 = *in_end;
14106 *in_end = NUL;
14107
14108 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14109 if (temp_result != NULL && nextcmd == NULL)
14110 {
14111 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14112 + (in_end - expr_end) + 1));
14113 if (retval != NULL)
14114 {
14115 STRCPY(retval, in_start);
14116 STRCAT(retval, temp_result);
14117 STRCAT(retval, expr_end + 1);
14118 }
14119 }
14120 vim_free(temp_result);
14121
14122 *in_end = c1; /* put char back for error messages */
14123 *expr_start = '{';
14124 *expr_end = '}';
14125
14126 if (retval != NULL)
14127 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014128 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014129 if (expr_start != NULL)
14130 {
14131 /* Further expansion! */
14132 temp_result = make_expanded_name(retval, expr_start,
14133 expr_end, temp_result);
14134 vim_free(retval);
14135 retval = temp_result;
14136 }
14137 }
14138
14139 return retval;
14140}
14141
14142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014144 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 */
14146 static int
14147eval_isnamec(c)
14148 int c;
14149{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014150 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14151}
14152
14153/*
14154 * Return TRUE if character "c" can be used as the first character in a
14155 * variable or function name (excluding '{' and '}').
14156 */
14157 static int
14158eval_isnamec1(c)
14159 int c;
14160{
14161 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162}
14163
14164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165 * Set number v: variable to "val".
14166 */
14167 void
14168set_vim_var_nr(idx, val)
14169 int idx;
14170 long val;
14171{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014172 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173}
14174
14175/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014176 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 */
14178 long
14179get_vim_var_nr(idx)
14180 int idx;
14181{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014182 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014185#if defined(FEAT_AUTOCMD) || defined(PROTO)
14186/*
14187 * Get string v: variable value. Uses a static buffer, can only be used once.
14188 */
14189 char_u *
14190get_vim_var_str(idx)
14191 int idx;
14192{
14193 return get_tv_string(&vimvars[idx].vv_tv);
14194}
14195#endif
14196
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197/*
14198 * Set v:count, v:count1 and v:prevcount.
14199 */
14200 void
14201set_vcount(count, count1)
14202 long count;
14203 long count1;
14204{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014205 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14206 vimvars[VV_COUNT].vv_nr = count;
14207 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208}
14209
14210/*
14211 * Set string v: variable to a copy of "val".
14212 */
14213 void
14214set_vim_var_string(idx, val, len)
14215 int idx;
14216 char_u *val;
14217 int len; /* length of "val" to use or -1 (whole string) */
14218{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014219 /* Need to do this (at least) once, since we can't initialize a union.
14220 * Will always be invoked when "v:progname" is set. */
14221 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14222
Bram Moolenaare9a41262005-01-15 22:18:47 +000014223 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014225 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014227 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014229 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230}
14231
14232/*
14233 * Set v:register if needed.
14234 */
14235 void
14236set_reg_var(c)
14237 int c;
14238{
14239 char_u regname;
14240
14241 if (c == 0 || c == ' ')
14242 regname = '"';
14243 else
14244 regname = c;
14245 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014246 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 set_vim_var_string(VV_REG, &regname, 1);
14248}
14249
14250/*
14251 * Get or set v:exception. If "oldval" == NULL, return the current value.
14252 * Otherwise, restore the value to "oldval" and return NULL.
14253 * Must always be called in pairs to save and restore v:exception! Does not
14254 * take care of memory allocations.
14255 */
14256 char_u *
14257v_exception(oldval)
14258 char_u *oldval;
14259{
14260 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262
Bram Moolenaare9a41262005-01-15 22:18:47 +000014263 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 return NULL;
14265}
14266
14267/*
14268 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14269 * Otherwise, restore the value to "oldval" and return NULL.
14270 * Must always be called in pairs to save and restore v:throwpoint! Does not
14271 * take care of memory allocations.
14272 */
14273 char_u *
14274v_throwpoint(oldval)
14275 char_u *oldval;
14276{
14277 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014278 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279
Bram Moolenaare9a41262005-01-15 22:18:47 +000014280 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281 return NULL;
14282}
14283
14284#if defined(FEAT_AUTOCMD) || defined(PROTO)
14285/*
14286 * Set v:cmdarg.
14287 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14288 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14289 * Must always be called in pairs!
14290 */
14291 char_u *
14292set_cmdarg(eap, oldarg)
14293 exarg_T *eap;
14294 char_u *oldarg;
14295{
14296 char_u *oldval;
14297 char_u *newval;
14298 unsigned len;
14299
Bram Moolenaare9a41262005-01-15 22:18:47 +000014300 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014301 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014303 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014304 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014305 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 }
14307
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014308 if (eap->force_bin == FORCE_BIN)
14309 len = 6;
14310 else if (eap->force_bin == FORCE_NOBIN)
14311 len = 8;
14312 else
14313 len = 0;
14314 if (eap->force_ff != 0)
14315 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14316# ifdef FEAT_MBYTE
14317 if (eap->force_enc != 0)
14318 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14319# endif
14320
14321 newval = alloc(len + 1);
14322 if (newval == NULL)
14323 return NULL;
14324
14325 if (eap->force_bin == FORCE_BIN)
14326 sprintf((char *)newval, " ++bin");
14327 else if (eap->force_bin == FORCE_NOBIN)
14328 sprintf((char *)newval, " ++nobin");
14329 else
14330 *newval = NUL;
14331 if (eap->force_ff != 0)
14332 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14333 eap->cmd + eap->force_ff);
14334# ifdef FEAT_MBYTE
14335 if (eap->force_enc != 0)
14336 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14337 eap->cmd + eap->force_enc);
14338# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014339 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014340 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341}
14342#endif
14343
14344/*
14345 * Get the value of internal variable "name".
14346 * Return OK or FAIL.
14347 */
14348 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014349get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 char_u *name;
14351 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014353 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354{
14355 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 typval_T *tv = NULL;
14357 typval_T atv;
14358 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
14361 /* truncate the name, so that we can use strcmp() */
14362 cc = name[len];
14363 name[len] = NUL;
14364
14365 /*
14366 * Check for "b:changedtick".
14367 */
14368 if (STRCMP(name, "b:changedtick") == 0)
14369 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014370 atv.v_type = VAR_NUMBER;
14371 atv.vval.v_number = curbuf->b_changedtick;
14372 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 }
14374
14375 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 * Check for user-defined variables.
14377 */
14378 else
14379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014380 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 }
14384
Bram Moolenaare9a41262005-01-15 22:18:47 +000014385 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014387 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 ret = FAIL;
14390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014392 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393
14394 name[len] = cc;
14395
14396 return ret;
14397}
14398
14399/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014400 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14401 * Also handle function call with Funcref variable: func(expr)
14402 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14403 */
14404 static int
14405handle_subscript(arg, rettv, evaluate, verbose)
14406 char_u **arg;
14407 typval_T *rettv;
14408 int evaluate; /* do more than finding the end */
14409 int verbose; /* give error messages */
14410{
14411 int ret = OK;
14412 dict_T *selfdict = NULL;
14413 char_u *s;
14414 int len;
14415
14416 while (ret == OK
14417 && (**arg == '['
14418 || (**arg == '.' && rettv->v_type == VAR_DICT)
14419 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14420 && !vim_iswhite(*(*arg - 1)))
14421 {
14422 if (**arg == '(')
14423 {
14424 s = rettv->vval.v_string;
14425
14426 /* Invoke the function. Recursive! */
14427 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14428 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14429 &len, evaluate, selfdict);
14430
14431 /* Stop the expression evaluation when immediately aborting on
14432 * error, or when an interrupt occurred or an exception was thrown
14433 * but not caught. */
14434 if (aborting())
14435 {
14436 if (ret == OK)
14437 clear_tv(rettv);
14438 ret = FAIL;
14439 }
14440 dict_unref(selfdict);
14441 selfdict = NULL;
14442 }
14443 else /* **arg == '[' || **arg == '.' */
14444 {
14445 dict_unref(selfdict);
14446 if (rettv->v_type == VAR_DICT)
14447 {
14448 selfdict = rettv->vval.v_dict;
14449 if (selfdict != NULL)
14450 ++selfdict->dv_refcount;
14451 }
14452 else
14453 selfdict = NULL;
14454 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14455 {
14456 clear_tv(rettv);
14457 ret = FAIL;
14458 }
14459 }
14460 }
14461 dict_unref(selfdict);
14462 return ret;
14463}
14464
14465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014466 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14467 * value).
14468 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014469 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014471{
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014473}
14474
14475/*
14476 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 * The string "s" must have been allocated, it is consumed.
14478 * Return NULL for out of memory, the variable otherwise.
14479 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014480 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 char_u *s;
14483{
Bram Moolenaar33570922005-01-25 22:26:29 +000014484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486 rettv = alloc_tv();
14487 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->v_type = VAR_STRING;
14490 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 }
14492 else
14493 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495}
14496
14497/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014498 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 */
14500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014502 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
14504 if (varp != NULL)
14505 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014506 switch (varp->v_type)
14507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014508 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014509 func_unref(varp->vval.v_string);
14510 /*FALLTHROUGH*/
14511 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014512 vim_free(varp->vval.v_string);
14513 break;
14514 case VAR_LIST:
14515 list_unref(varp->vval.v_list);
14516 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014517 case VAR_DICT:
14518 dict_unref(varp->vval.v_dict);
14519 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014520 case VAR_NUMBER:
14521 case VAR_UNKNOWN:
14522 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014523 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014524 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014525 break;
14526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 vim_free(varp);
14528 }
14529}
14530
14531/*
14532 * Free the memory for a variable value and set the value to NULL or 0.
14533 */
14534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014536 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
14538 if (varp != NULL)
14539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014540 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014543 func_unref(varp->vval.v_string);
14544 /*FALLTHROUGH*/
14545 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014546 vim_free(varp->vval.v_string);
14547 varp->vval.v_string = NULL;
14548 break;
14549 case VAR_LIST:
14550 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014551 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014552 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014553 case VAR_DICT:
14554 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014555 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014556 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014558 varp->vval.v_number = 0;
14559 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 case VAR_UNKNOWN:
14561 break;
14562 default:
14563 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014565 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566 }
14567}
14568
14569/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570 * Set the value of a variable to NULL without freeing items.
14571 */
14572 static void
14573init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575{
14576 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578}
14579
14580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 * Get the number value of a variable.
14582 * If it is a String variable, uses vim_str2nr().
14583 */
14584 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014586 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014588 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014590 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014592 case VAR_NUMBER:
14593 n = (long)(varp->vval.v_number);
14594 break;
14595 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014596 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014597 break;
14598 case VAR_STRING:
14599 if (varp->vval.v_string != NULL)
14600 vim_str2nr(varp->vval.v_string, NULL, NULL,
14601 TRUE, TRUE, &n, NULL);
14602 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014603 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014604 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014605 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014606 case VAR_DICT:
14607 EMSG(_("E728: Using a Dictionary as a number"));
14608 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014609 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014610 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014613 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614}
14615
14616/*
14617 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14618 */
14619 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622{
Bram Moolenaar33570922005-01-25 22:26:29 +000014623 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 linenr_T lnum;
14625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 if (lnum == 0) /* no valid number, try using line() */
14628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv.v_type = VAR_NUMBER;
14630 f_line(argvars, &rettv);
14631 lnum = rettv.vval.v_number;
14632 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 }
14634 return lnum;
14635}
14636
14637/*
14638 * Get the string value of a variable.
14639 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014640 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14641 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 * If the String variable has never been set, return an empty string.
14643 * Never returns NULL;
14644 */
14645 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014646get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014647 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648{
14649 static char_u mybuf[NUMBUFLEN];
14650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652}
14653
14654 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014656 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014657 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014659 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014661 case VAR_NUMBER:
14662 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14663 return buf;
14664 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014665 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014666 break;
14667 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014668 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014669 break;
14670 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014671 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014672 break;
14673 case VAR_STRING:
14674 if (varp->vval.v_string != NULL)
14675 return varp->vval.v_string;
14676 break;
14677 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014679 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014681 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682}
14683
14684/*
14685 * Find variable "name" in the list of variables.
14686 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014687 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014688 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014689 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014691 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014692find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014697 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698
Bram Moolenaara7043832005-01-21 11:56:39 +000014699 ht = find_var_ht(name, &varname);
14700 if (htp != NULL)
14701 *htp = ht;
14702 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014704 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705}
14706
14707/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014708 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014709 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014711 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014712find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014713 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014714 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014715 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014716{
Bram Moolenaar33570922005-01-25 22:26:29 +000014717 hashitem_T *hi;
14718
14719 if (*varname == NUL)
14720 {
14721 /* Must be something like "s:", otherwise "ht" would be NULL. */
14722 switch (varname[-2])
14723 {
14724 case 's': return &SCRIPT_SV(current_SID).sv_var;
14725 case 'g': return &globvars_var;
14726 case 'v': return &vimvars_var;
14727 case 'b': return &curbuf->b_bufvar;
14728 case 'w': return &curwin->w_winvar;
14729 case 'l': return &current_funccal->l_vars_var;
14730 case 'a': return &current_funccal->l_avars_var;
14731 }
14732 return NULL;
14733 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014734
14735 hi = hash_find(ht, varname);
14736 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014737 {
14738 /* For global variables we may try auto-loading the script. If it
14739 * worked find the variable again. */
14740 if (ht == &globvarht && !writing
14741 && script_autoload(varname) && !aborting())
14742 hi = hash_find(ht, varname);
14743 if (HASHITEM_EMPTY(hi))
14744 return NULL;
14745 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014747}
14748
14749/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014751 * Set "varname" to the start of name without ':'.
14752 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014753 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014754find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 char_u *name;
14756 char_u **varname;
14757{
14758 if (name[1] != ':')
14759 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014760 /* The name must not start with a colon or #. */
14761 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 return NULL;
14763 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014764
14765 /* "version" is "v:version" in all scopes */
14766 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14767 return &compat_hashtab;
14768
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014770 return &globvarht; /* global variable */
14771 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 }
14773 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014774 if (*name == 'g') /* global variable */
14775 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014776 /* There must be no ':' or '#' in the rest of the name, unless g: is used
14777 */
14778 if (vim_strchr(name + 2, ':') != NULL
14779 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014780 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 if (*name == 'v') /* v: variable */
14786 return &vimvarht;
14787 if (*name == 'a' && current_funccal != NULL) /* function argument */
14788 return &current_funccal->l_avars.dv_hashtab;
14789 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14790 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 if (*name == 's' /* script variable */
14792 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14793 return &SCRIPT_VARS(current_SID);
14794 return NULL;
14795}
14796
14797/*
14798 * Get the string value of a (global/local) variable.
14799 * Returns NULL when it doesn't exist.
14800 */
14801 char_u *
14802get_var_value(name)
14803 char_u *name;
14804{
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806
Bram Moolenaara7043832005-01-21 11:56:39 +000014807 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 if (v == NULL)
14809 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811}
14812
14813/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815 * sourcing this script and when executing functions defined in the script.
14816 */
14817 void
14818new_script_vars(id)
14819 scid_T id;
14820{
Bram Moolenaara7043832005-01-21 11:56:39 +000014821 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014822 hashtab_T *ht;
14823 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014824
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014827 /* Re-allocating ga_data means that an ht_array pointing to
14828 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014829 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014830 for (i = 1; i <= ga_scripts.ga_len; ++i)
14831 {
14832 ht = &SCRIPT_VARS(i);
14833 if (ht->ht_mask == HT_INIT_SIZE - 1)
14834 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 sv = &SCRIPT_SV(i);
14836 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014837 }
14838
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 while (ga_scripts.ga_len < id)
14840 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014841 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14842 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 }
14845 }
14846}
14847
14848/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14850 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 */
14852 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014853init_var_dict(dict, dict_var)
14854 dict_T *dict;
14855 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856{
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 hash_init(&dict->dv_hashtab);
14858 dict->dv_refcount = 99999;
14859 dict_var->di_tv.vval.v_dict = dict;
14860 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014861 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014862 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14863 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864}
14865
14866/*
14867 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014868 * Frees all allocated variables and the value they contain.
14869 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 */
14871 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014872vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 hashtab_T *ht;
14874{
14875 vars_clear_ext(ht, TRUE);
14876}
14877
14878/*
14879 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14880 */
14881 static void
14882vars_clear_ext(ht, free_val)
14883 hashtab_T *ht;
14884 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885{
Bram Moolenaara7043832005-01-21 11:56:39 +000014886 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014887 hashitem_T *hi;
14888 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889
Bram Moolenaar33570922005-01-25 22:26:29 +000014890 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014891 todo = ht->ht_used;
14892 for (hi = ht->ht_array; todo > 0; ++hi)
14893 {
14894 if (!HASHITEM_EMPTY(hi))
14895 {
14896 --todo;
14897
Bram Moolenaar33570922005-01-25 22:26:29 +000014898 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014899 * ht_array might change then. hash_clear() takes care of it
14900 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014901 v = HI2DI(hi);
14902 if (free_val)
14903 clear_tv(&v->di_tv);
14904 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14905 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014906 }
14907 }
14908 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909}
14910
Bram Moolenaara7043832005-01-21 11:56:39 +000014911/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014912 * Delete a variable from hashtab "ht" at item "hi".
14913 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014916delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014917 hashtab_T *ht;
14918 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919{
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014921
14922 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 clear_tv(&di->di_tv);
14924 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925}
14926
14927/*
14928 * List the value of one internal variable.
14929 */
14930 static void
14931list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014932 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 char_u *prefix;
14934{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014935 char_u *tofree;
14936 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014937 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014938
Bram Moolenaar33570922005-01-25 22:26:29 +000014939 s = echo_string(&v->di_tv, &tofree, numbuf);
14940 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014941 s == NULL ? (char_u *)"" : s);
14942 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943}
14944
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 static void
14946list_one_var_a(prefix, name, type, string)
14947 char_u *prefix;
14948 char_u *name;
14949 int type;
14950 char_u *string;
14951{
14952 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14953 if (name != NULL) /* "a:" vars don't have a name stored */
14954 msg_puts(name);
14955 msg_putchar(' ');
14956 msg_advance(22);
14957 if (type == VAR_NUMBER)
14958 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014959 else if (type == VAR_FUNC)
14960 msg_putchar('*');
14961 else if (type == VAR_LIST)
14962 {
14963 msg_putchar('[');
14964 if (*string == '[')
14965 ++string;
14966 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014967 else if (type == VAR_DICT)
14968 {
14969 msg_putchar('{');
14970 if (*string == '{')
14971 ++string;
14972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 else
14974 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014975
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014977
14978 if (type == VAR_FUNC)
14979 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980}
14981
14982/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 * If the variable already exists, the value is updated.
14985 * Otherwise the variable is created.
14986 */
14987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014990 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992{
Bram Moolenaar33570922005-01-25 22:26:29 +000014993 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014995 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014997 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014998 {
14999 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15000 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15001 ? name[2] : name[0]))
15002 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015003 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015004 return;
15005 }
15006 if (function_exists(name))
15007 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015008 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15009 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015010 return;
15011 }
15012 }
15013
Bram Moolenaara7043832005-01-21 11:56:39 +000015014 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015016 {
15017 EMSG2(_("E461: Illegal variable name: %s"), name);
15018 return;
15019 }
15020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015021 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015025 if (var_check_ro(v->di_flags, name)
15026 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 return;
15028 if (v->di_tv.v_type != tv->v_type
15029 && !((v->di_tv.v_type == VAR_STRING
15030 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 && (tv->v_type == VAR_STRING
15032 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015033 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015034 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015035 return;
15036 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015037
15038 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015039 * Handle setting internal v: variables separately: we don't change
15040 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 */
15042 if (ht == &vimvarht)
15043 {
15044 if (v->di_tv.v_type == VAR_STRING)
15045 {
15046 vim_free(v->di_tv.vval.v_string);
15047 if (copy || tv->v_type != VAR_STRING)
15048 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15049 else
15050 {
15051 /* Take over the string to avoid an extra alloc/free. */
15052 v->di_tv.vval.v_string = tv->vval.v_string;
15053 tv->vval.v_string = NULL;
15054 }
15055 }
15056 else if (v->di_tv.v_type != VAR_NUMBER)
15057 EMSG2(_(e_intern2), "set_var()");
15058 else
15059 v->di_tv.vval.v_number = get_tv_number(tv);
15060 return;
15061 }
15062
15063 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 }
15065 else /* add a new variable */
15066 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015067 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15068 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015069 if (v == NULL)
15070 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015072 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015074 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 return;
15076 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015077 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015080 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015082 else
15083 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015084 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015085 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015086 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088}
15089
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015090/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15092 * Also give an error message.
15093 */
15094 static int
15095var_check_ro(flags, name)
15096 int flags;
15097 char_u *name;
15098{
15099 if (flags & DI_FLAGS_RO)
15100 {
15101 EMSG2(_(e_readonlyvar), name);
15102 return TRUE;
15103 }
15104 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15105 {
15106 EMSG2(_(e_readonlysbx), name);
15107 return TRUE;
15108 }
15109 return FALSE;
15110}
15111
15112/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015113 * Return TRUE if typeval "tv" is set to be locked (immutable).
15114 * Also give an error message, using "name".
15115 */
15116 static int
15117tv_check_lock(lock, name)
15118 int lock;
15119 char_u *name;
15120{
15121 if (lock & VAR_LOCKED)
15122 {
15123 EMSG2(_("E741: Value is locked: %s"),
15124 name == NULL ? (char_u *)_("Unknown") : name);
15125 return TRUE;
15126 }
15127 if (lock & VAR_FIXED)
15128 {
15129 EMSG2(_("E742: Cannot change value of %s"),
15130 name == NULL ? (char_u *)_("Unknown") : name);
15131 return TRUE;
15132 }
15133 return FALSE;
15134}
15135
15136/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015138 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015139 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015142copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015143 typval_T *from;
15144 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015146 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015147 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015148 switch (from->v_type)
15149 {
15150 case VAR_NUMBER:
15151 to->vval.v_number = from->vval.v_number;
15152 break;
15153 case VAR_STRING:
15154 case VAR_FUNC:
15155 if (from->vval.v_string == NULL)
15156 to->vval.v_string = NULL;
15157 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015158 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015159 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015160 if (from->v_type == VAR_FUNC)
15161 func_ref(to->vval.v_string);
15162 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015163 break;
15164 case VAR_LIST:
15165 if (from->vval.v_list == NULL)
15166 to->vval.v_list = NULL;
15167 else
15168 {
15169 to->vval.v_list = from->vval.v_list;
15170 ++to->vval.v_list->lv_refcount;
15171 }
15172 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015173 case VAR_DICT:
15174 if (from->vval.v_dict == NULL)
15175 to->vval.v_dict = NULL;
15176 else
15177 {
15178 to->vval.v_dict = from->vval.v_dict;
15179 ++to->vval.v_dict->dv_refcount;
15180 }
15181 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015182 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015184 break;
15185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
15188/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015189 * Make a copy of an item.
15190 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015191 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15192 * reference to an already copied list/dict can be used.
15193 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015194 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015195 static int
15196item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 typval_T *from;
15198 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015199 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015200 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015201{
15202 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015203 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015204
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015206 {
15207 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015208 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015209 }
15210 ++recurse;
15211
15212 switch (from->v_type)
15213 {
15214 case VAR_NUMBER:
15215 case VAR_STRING:
15216 case VAR_FUNC:
15217 copy_tv(from, to);
15218 break;
15219 case VAR_LIST:
15220 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015221 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015222 if (from->vval.v_list == NULL)
15223 to->vval.v_list = NULL;
15224 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15225 {
15226 /* use the copy made earlier */
15227 to->vval.v_list = from->vval.v_list->lv_copylist;
15228 ++to->vval.v_list->lv_refcount;
15229 }
15230 else
15231 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15232 if (to->vval.v_list == NULL)
15233 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015234 break;
15235 case VAR_DICT:
15236 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015237 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015238 if (from->vval.v_dict == NULL)
15239 to->vval.v_dict = NULL;
15240 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15241 {
15242 /* use the copy made earlier */
15243 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15244 ++to->vval.v_dict->dv_refcount;
15245 }
15246 else
15247 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15248 if (to->vval.v_dict == NULL)
15249 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015250 break;
15251 default:
15252 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015253 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015254 }
15255 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015256 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015257}
15258
15259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 * ":echo expr1 ..." print each argument separated with a space, add a
15261 * newline at the end.
15262 * ":echon expr1 ..." print each argument plain.
15263 */
15264 void
15265ex_echo(eap)
15266 exarg_T *eap;
15267{
15268 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015269 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271 char_u *p;
15272 int needclr = TRUE;
15273 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015274 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275
15276 if (eap->skip)
15277 ++emsg_skip;
15278 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15279 {
15280 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015281 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 {
15283 /*
15284 * Report the invalid expression unless the expression evaluation
15285 * has been cancelled due to an aborting error, an interrupt, or an
15286 * exception.
15287 */
15288 if (!aborting())
15289 EMSG2(_(e_invexpr2), p);
15290 break;
15291 }
15292 if (!eap->skip)
15293 {
15294 if (atstart)
15295 {
15296 atstart = FALSE;
15297 /* Call msg_start() after eval1(), evaluating the expression
15298 * may cause a message to appear. */
15299 if (eap->cmdidx == CMD_echo)
15300 msg_start();
15301 }
15302 else if (eap->cmdidx == CMD_echo)
15303 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015304 p = echo_string(&rettv, &tofree, numbuf);
15305 if (p != NULL)
15306 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015308 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015310 if (*p != TAB && needclr)
15311 {
15312 /* remove any text still there from the command */
15313 msg_clr_eos();
15314 needclr = FALSE;
15315 }
15316 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 }
15318 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015319 {
15320#ifdef FEAT_MBYTE
15321 if (has_mbyte)
15322 {
15323 int i = (*mb_ptr2len_check)(p);
15324
15325 (void)msg_outtrans_len_attr(p, i, echo_attr);
15326 p += i - 1;
15327 }
15328 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015330 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015333 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 arg = skipwhite(arg);
15337 }
15338 eap->nextcmd = check_nextcmd(arg);
15339
15340 if (eap->skip)
15341 --emsg_skip;
15342 else
15343 {
15344 /* remove text that may still be there from the command */
15345 if (needclr)
15346 msg_clr_eos();
15347 if (eap->cmdidx == CMD_echo)
15348 msg_end();
15349 }
15350}
15351
15352/*
15353 * ":echohl {name}".
15354 */
15355 void
15356ex_echohl(eap)
15357 exarg_T *eap;
15358{
15359 int id;
15360
15361 id = syn_name2id(eap->arg);
15362 if (id == 0)
15363 echo_attr = 0;
15364 else
15365 echo_attr = syn_id2attr(id);
15366}
15367
15368/*
15369 * ":execute expr1 ..." execute the result of an expression.
15370 * ":echomsg expr1 ..." Print a message
15371 * ":echoerr expr1 ..." Print an error
15372 * Each gets spaces around each argument and a newline at the end for
15373 * echo commands
15374 */
15375 void
15376ex_execute(eap)
15377 exarg_T *eap;
15378{
15379 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015380 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 int ret = OK;
15382 char_u *p;
15383 garray_T ga;
15384 int len;
15385 int save_did_emsg;
15386
15387 ga_init2(&ga, 1, 80);
15388
15389 if (eap->skip)
15390 ++emsg_skip;
15391 while (*arg != NUL && *arg != '|' && *arg != '\n')
15392 {
15393 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 {
15396 /*
15397 * Report the invalid expression unless the expression evaluation
15398 * has been cancelled due to an aborting error, an interrupt, or an
15399 * exception.
15400 */
15401 if (!aborting())
15402 EMSG2(_(e_invexpr2), p);
15403 ret = FAIL;
15404 break;
15405 }
15406
15407 if (!eap->skip)
15408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 len = (int)STRLEN(p);
15411 if (ga_grow(&ga, len + 2) == FAIL)
15412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 ret = FAIL;
15415 break;
15416 }
15417 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 ga.ga_len += len;
15421 }
15422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 arg = skipwhite(arg);
15425 }
15426
15427 if (ret != FAIL && ga.ga_data != NULL)
15428 {
15429 if (eap->cmdidx == CMD_echomsg)
15430 MSG_ATTR(ga.ga_data, echo_attr);
15431 else if (eap->cmdidx == CMD_echoerr)
15432 {
15433 /* We don't want to abort following commands, restore did_emsg. */
15434 save_did_emsg = did_emsg;
15435 EMSG((char_u *)ga.ga_data);
15436 if (!force_abort)
15437 did_emsg = save_did_emsg;
15438 }
15439 else if (eap->cmdidx == CMD_execute)
15440 do_cmdline((char_u *)ga.ga_data,
15441 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15442 }
15443
15444 ga_clear(&ga);
15445
15446 if (eap->skip)
15447 --emsg_skip;
15448
15449 eap->nextcmd = check_nextcmd(arg);
15450}
15451
15452/*
15453 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15454 * "arg" points to the "&" or '+' when called, to "option" when returning.
15455 * Returns NULL when no option name found. Otherwise pointer to the char
15456 * after the option name.
15457 */
15458 static char_u *
15459find_option_end(arg, opt_flags)
15460 char_u **arg;
15461 int *opt_flags;
15462{
15463 char_u *p = *arg;
15464
15465 ++p;
15466 if (*p == 'g' && p[1] == ':')
15467 {
15468 *opt_flags = OPT_GLOBAL;
15469 p += 2;
15470 }
15471 else if (*p == 'l' && p[1] == ':')
15472 {
15473 *opt_flags = OPT_LOCAL;
15474 p += 2;
15475 }
15476 else
15477 *opt_flags = 0;
15478
15479 if (!ASCII_ISALPHA(*p))
15480 return NULL;
15481 *arg = p;
15482
15483 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15484 p += 4; /* termcap option */
15485 else
15486 while (ASCII_ISALPHA(*p))
15487 ++p;
15488 return p;
15489}
15490
15491/*
15492 * ":function"
15493 */
15494 void
15495ex_function(eap)
15496 exarg_T *eap;
15497{
15498 char_u *theline;
15499 int j;
15500 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 char_u *name = NULL;
15503 char_u *p;
15504 char_u *arg;
15505 garray_T newargs;
15506 garray_T newlines;
15507 int varargs = FALSE;
15508 int mustend = FALSE;
15509 int flags = 0;
15510 ufunc_T *fp;
15511 int indent;
15512 int nesting;
15513 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015514 dictitem_T *v;
15515 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015516 static int func_nr = 0; /* number for nameless function */
15517 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015518 hashtab_T *ht;
15519 int todo;
15520 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521
15522 /*
15523 * ":function" without argument: list functions.
15524 */
15525 if (ends_excmd(*eap->arg))
15526 {
15527 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015528 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015529 todo = func_hashtab.ht_used;
15530 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015531 {
15532 if (!HASHITEM_EMPTY(hi))
15533 {
15534 --todo;
15535 fp = HI2UF(hi);
15536 if (!isdigit(*fp->uf_name))
15537 list_func_head(fp, FALSE);
15538 }
15539 }
15540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 eap->nextcmd = check_nextcmd(eap->arg);
15542 return;
15543 }
15544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015545 /*
15546 * Get the function name. There are these situations:
15547 * func normal function name
15548 * "name" == func, "fudi.fd_dict" == NULL
15549 * dict.func new dictionary entry
15550 * "name" == NULL, "fudi.fd_dict" set,
15551 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15552 * dict.func existing dict entry with a Funcref
15553 * "name" == fname, "fudi.fd_dict" set,
15554 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15555 * dict.func existing dict entry that's not a Funcref
15556 * "name" == NULL, "fudi.fd_dict" set,
15557 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015560 name = trans_function_name(&p, eap->skip, 0, &fudi);
15561 paren = (vim_strchr(p, '(') != NULL);
15562 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 {
15564 /*
15565 * Return on an invalid expression in braces, unless the expression
15566 * evaluation has been cancelled due to an aborting error, an
15567 * interrupt, or an exception.
15568 */
15569 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015571 if (!eap->skip && fudi.fd_newkey != NULL)
15572 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015573 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 else
15577 eap->skip = TRUE;
15578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 /* An error in a function call during evaluation of an expression in magic
15580 * braces should not cause the function not to be defined. */
15581 saved_did_emsg = did_emsg;
15582 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583
15584 /*
15585 * ":function func" with only function name: list function.
15586 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015587 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 {
15589 if (!ends_excmd(*skipwhite(p)))
15590 {
15591 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015592 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
15594 eap->nextcmd = check_nextcmd(p);
15595 if (eap->nextcmd != NULL)
15596 *p = NUL;
15597 if (!eap->skip && !got_int)
15598 {
15599 fp = find_func(name);
15600 if (fp != NULL)
15601 {
15602 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015603 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604 {
15605 msg_putchar('\n');
15606 msg_outnum((long)(j + 1));
15607 if (j < 9)
15608 msg_putchar(' ');
15609 if (j < 99)
15610 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015611 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 out_flush(); /* show a line at a time */
15613 ui_breakcheck();
15614 }
15615 if (!got_int)
15616 {
15617 msg_putchar('\n');
15618 msg_puts((char_u *)" endfunction");
15619 }
15620 }
15621 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015622 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015624 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 }
15626
15627 /*
15628 * ":function name(arg1, arg2)" Define function.
15629 */
15630 p = skipwhite(p);
15631 if (*p != '(')
15632 {
15633 if (!eap->skip)
15634 {
15635 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015636 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 }
15638 /* attempt to continue by skipping some text */
15639 if (vim_strchr(p, '(') != NULL)
15640 p = vim_strchr(p, '(');
15641 }
15642 p = skipwhite(p + 1);
15643
15644 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15645 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15646
15647 /*
15648 * Isolate the arguments: "arg1, arg2, ...)"
15649 */
15650 while (*p != ')')
15651 {
15652 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15653 {
15654 varargs = TRUE;
15655 p += 3;
15656 mustend = TRUE;
15657 }
15658 else
15659 {
15660 arg = p;
15661 while (ASCII_ISALNUM(*p) || *p == '_')
15662 ++p;
15663 if (arg == p || isdigit(*arg)
15664 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15665 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15666 {
15667 if (!eap->skip)
15668 EMSG2(_("E125: Illegal argument: %s"), arg);
15669 break;
15670 }
15671 if (ga_grow(&newargs, 1) == FAIL)
15672 goto erret;
15673 c = *p;
15674 *p = NUL;
15675 arg = vim_strsave(arg);
15676 if (arg == NULL)
15677 goto erret;
15678 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15679 *p = c;
15680 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 if (*p == ',')
15682 ++p;
15683 else
15684 mustend = TRUE;
15685 }
15686 p = skipwhite(p);
15687 if (mustend && *p != ')')
15688 {
15689 if (!eap->skip)
15690 EMSG2(_(e_invarg2), eap->arg);
15691 break;
15692 }
15693 }
15694 ++p; /* skip the ')' */
15695
Bram Moolenaare9a41262005-01-15 22:18:47 +000015696 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 for (;;)
15698 {
15699 p = skipwhite(p);
15700 if (STRNCMP(p, "range", 5) == 0)
15701 {
15702 flags |= FC_RANGE;
15703 p += 5;
15704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015705 else if (STRNCMP(p, "dict", 4) == 0)
15706 {
15707 flags |= FC_DICT;
15708 p += 4;
15709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 else if (STRNCMP(p, "abort", 5) == 0)
15711 {
15712 flags |= FC_ABORT;
15713 p += 5;
15714 }
15715 else
15716 break;
15717 }
15718
15719 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15720 EMSG(_(e_trailing));
15721
15722 /*
15723 * Read the body of the function, until ":endfunction" is found.
15724 */
15725 if (KeyTyped)
15726 {
15727 /* Check if the function already exists, don't let the user type the
15728 * whole function before telling him it doesn't work! For a script we
15729 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015730 if (!eap->skip && !eap->forceit)
15731 {
15732 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15733 EMSG(_(e_funcdict));
15734 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015735 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737
15738 msg_putchar('\n'); /* don't overwrite the function name */
15739 cmdline_row = msg_row;
15740 }
15741
15742 indent = 2;
15743 nesting = 0;
15744 for (;;)
15745 {
15746 msg_scroll = TRUE;
15747 need_wait_return = FALSE;
15748 if (eap->getline == NULL)
15749 theline = getcmdline(':', 0L, indent);
15750 else
15751 theline = eap->getline(':', eap->cookie, indent);
15752 if (KeyTyped)
15753 lines_left = Rows - 1;
15754 if (theline == NULL)
15755 {
15756 EMSG(_("E126: Missing :endfunction"));
15757 goto erret;
15758 }
15759
15760 if (skip_until != NULL)
15761 {
15762 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15763 * don't check for ":endfunc". */
15764 if (STRCMP(theline, skip_until) == 0)
15765 {
15766 vim_free(skip_until);
15767 skip_until = NULL;
15768 }
15769 }
15770 else
15771 {
15772 /* skip ':' and blanks*/
15773 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15774 ;
15775
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015776 /* Check for "endfunction". */
15777 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 {
15779 vim_free(theline);
15780 break;
15781 }
15782
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015783 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 * at "end". */
15785 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15786 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015787 else if (STRNCMP(p, "if", 2) == 0
15788 || STRNCMP(p, "wh", 2) == 0
15789 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 || STRNCMP(p, "try", 3) == 0)
15791 indent += 2;
15792
15793 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015794 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015796 if (*p == '!')
15797 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 p += eval_fname_script(p);
15799 if (ASCII_ISALPHA(*p))
15800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015801 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 if (*skipwhite(p) == '(')
15803 {
15804 ++nesting;
15805 indent += 2;
15806 }
15807 }
15808 }
15809
15810 /* Check for ":append" or ":insert". */
15811 p = skip_range(p, NULL);
15812 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15813 || (p[0] == 'i'
15814 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15815 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15816 skip_until = vim_strsave((char_u *)".");
15817
15818 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15819 arg = skipwhite(skiptowhite(p));
15820 if (arg[0] == '<' && arg[1] =='<'
15821 && ((p[0] == 'p' && p[1] == 'y'
15822 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15823 || (p[0] == 'p' && p[1] == 'e'
15824 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15825 || (p[0] == 't' && p[1] == 'c'
15826 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15827 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15828 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015829 || (p[0] == 'm' && p[1] == 'z'
15830 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 ))
15832 {
15833 /* ":python <<" continues until a dot, like ":append" */
15834 p = skipwhite(arg + 2);
15835 if (*p == NUL)
15836 skip_until = vim_strsave((char_u *)".");
15837 else
15838 skip_until = vim_strsave(p);
15839 }
15840 }
15841
15842 /* Add the line to the function. */
15843 if (ga_grow(&newlines, 1) == FAIL)
15844 goto erret;
15845 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15846 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 }
15848
15849 /* Don't define the function when skipping commands or when an error was
15850 * detected. */
15851 if (eap->skip || did_emsg)
15852 goto erret;
15853
15854 /*
15855 * If there are no errors, add the function
15856 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015857 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015858 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015859 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015860 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015861 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015862 emsg_funcname("E707: Function name conflicts with variable: %s",
15863 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015864 goto erret;
15865 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015866
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015867 fp = find_func(name);
15868 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015870 if (!eap->forceit)
15871 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015872 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015873 goto erret;
15874 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015875 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015876 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015877 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015878 name);
15879 goto erret;
15880 }
15881 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015882 ga_clear_strings(&(fp->uf_args));
15883 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015884 vim_free(name);
15885 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 }
15888 else
15889 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015890 char numbuf[20];
15891
15892 fp = NULL;
15893 if (fudi.fd_newkey == NULL && !eap->forceit)
15894 {
15895 EMSG(_(e_funcdict));
15896 goto erret;
15897 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015898 if (fudi.fd_di == NULL)
15899 {
15900 /* Can't add a function to a locked dictionary */
15901 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15902 goto erret;
15903 }
15904 /* Can't change an existing function if it is locked */
15905 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15906 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015907
15908 /* Give the function a sequential number. Can only be used with a
15909 * Funcref! */
15910 vim_free(name);
15911 sprintf(numbuf, "%d", ++func_nr);
15912 name = vim_strsave((char_u *)numbuf);
15913 if (name == NULL)
15914 goto erret;
15915 }
15916
15917 if (fp == NULL)
15918 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015919 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015920 {
15921 int slen, plen;
15922 char_u *scriptname;
15923
15924 /* Check that the autoload name matches the script name. */
15925 j = FAIL;
15926 if (sourcing_name != NULL)
15927 {
15928 scriptname = autoload_name(name);
15929 if (scriptname != NULL)
15930 {
15931 p = vim_strchr(scriptname, '/');
15932 plen = STRLEN(p);
15933 slen = STRLEN(sourcing_name);
15934 if (slen > plen && fnamecmp(p,
15935 sourcing_name + slen - plen) == 0)
15936 j = OK;
15937 vim_free(scriptname);
15938 }
15939 }
15940 if (j == FAIL)
15941 {
15942 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15943 goto erret;
15944 }
15945 }
15946
15947 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 if (fp == NULL)
15949 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015950
15951 if (fudi.fd_dict != NULL)
15952 {
15953 if (fudi.fd_di == NULL)
15954 {
15955 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015956 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015957 if (fudi.fd_di == NULL)
15958 {
15959 vim_free(fp);
15960 goto erret;
15961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015962 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15963 {
15964 vim_free(fudi.fd_di);
15965 goto erret;
15966 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015967 }
15968 else
15969 /* overwrite existing dict entry */
15970 clear_tv(&fudi.fd_di->di_tv);
15971 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015972 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015973 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015974 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015975 }
15976
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015978 STRCPY(fp->uf_name, name);
15979 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015981 fp->uf_args = newargs;
15982 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015983#ifdef FEAT_PROFILE
15984 fp->uf_tml_count = NULL;
15985 fp->uf_tml_total = NULL;
15986 fp->uf_tml_self = NULL;
15987 fp->uf_profiling = FALSE;
15988 if (prof_def_func())
15989 func_do_profile(fp);
15990#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015991 fp->uf_varargs = varargs;
15992 fp->uf_flags = flags;
15993 fp->uf_calls = 0;
15994 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015995 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996
15997erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 ga_clear_strings(&newargs);
15999 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016000ret_free:
16001 vim_free(skip_until);
16002 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005}
16006
16007/*
16008 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016009 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016011 * flags:
16012 * TFN_INT: internal function name OK
16013 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 * Advances "pp" to just after the function name (if no error).
16015 */
16016 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016017trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 char_u **pp;
16019 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016020 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016023 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 char_u *start;
16025 char_u *end;
16026 int lead;
16027 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016029 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016030
16031 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016034
16035 /* Check for hard coded <SNR>: already translated function ID (from a user
16036 * command). */
16037 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16038 && (*pp)[2] == (int)KE_SNR)
16039 {
16040 *pp += 3;
16041 len = get_id_len(pp) + 3;
16042 return vim_strnsave(start, len);
16043 }
16044
16045 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16046 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016048 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016050
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016051 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16052 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016053 if (end == start)
16054 {
16055 if (!skip)
16056 EMSG(_("E129: Function name required"));
16057 goto theend;
16058 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016059 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016060 {
16061 /*
16062 * Report an invalid expression in braces, unless the expression
16063 * evaluation has been cancelled due to an aborting error, an
16064 * interrupt, or an exception.
16065 */
16066 if (!aborting())
16067 {
16068 if (end != NULL)
16069 EMSG2(_(e_invarg2), start);
16070 }
16071 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016072 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016073 goto theend;
16074 }
16075
16076 if (lv.ll_tv != NULL)
16077 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016078 if (fdp != NULL)
16079 {
16080 fdp->fd_dict = lv.ll_dict;
16081 fdp->fd_newkey = lv.ll_newkey;
16082 lv.ll_newkey = NULL;
16083 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016085 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16086 {
16087 name = vim_strsave(lv.ll_tv->vval.v_string);
16088 *pp = end;
16089 }
16090 else
16091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016092 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16093 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016094 EMSG(_(e_funcref));
16095 else
16096 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016097 name = NULL;
16098 }
16099 goto theend;
16100 }
16101
16102 if (lv.ll_name == NULL)
16103 {
16104 /* Error found, but continue after the function name. */
16105 *pp = end;
16106 goto theend;
16107 }
16108
16109 if (lv.ll_exp_name != NULL)
16110 len = STRLEN(lv.ll_exp_name);
16111 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016112 {
16113 if (lead == 2) /* skip over "s:" */
16114 lv.ll_name += 2;
16115 len = (int)(end - lv.ll_name);
16116 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016117
16118 /*
16119 * Copy the function name to allocated memory.
16120 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16121 * Accept <SNR>123_name() outside a script.
16122 */
16123 if (skip)
16124 lead = 0; /* do nothing */
16125 else if (lead > 0)
16126 {
16127 lead = 3;
16128 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16129 {
16130 if (current_SID <= 0)
16131 {
16132 EMSG(_(e_usingsid));
16133 goto theend;
16134 }
16135 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16136 lead += (int)STRLEN(sid_buf);
16137 }
16138 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016139 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016140 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016141 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016142 goto theend;
16143 }
16144 name = alloc((unsigned)(len + lead + 1));
16145 if (name != NULL)
16146 {
16147 if (lead > 0)
16148 {
16149 name[0] = K_SPECIAL;
16150 name[1] = KS_EXTRA;
16151 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016152 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016153 STRCPY(name + 3, sid_buf);
16154 }
16155 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16156 name[len + lead] = NUL;
16157 }
16158 *pp = end;
16159
16160theend:
16161 clear_lval(&lv);
16162 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163}
16164
16165/*
16166 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16167 * Return 2 if "p" starts with "s:".
16168 * Return 0 otherwise.
16169 */
16170 static int
16171eval_fname_script(p)
16172 char_u *p;
16173{
16174 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16175 || STRNICMP(p + 1, "SNR>", 4) == 0))
16176 return 5;
16177 if (p[0] == 's' && p[1] == ':')
16178 return 2;
16179 return 0;
16180}
16181
16182/*
16183 * Return TRUE if "p" starts with "<SID>" or "s:".
16184 * Only works if eval_fname_script() returned non-zero for "p"!
16185 */
16186 static int
16187eval_fname_sid(p)
16188 char_u *p;
16189{
16190 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16191}
16192
16193/*
16194 * List the head of the function: "name(arg1, arg2)".
16195 */
16196 static void
16197list_func_head(fp, indent)
16198 ufunc_T *fp;
16199 int indent;
16200{
16201 int j;
16202
16203 msg_start();
16204 if (indent)
16205 MSG_PUTS(" ");
16206 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016207 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 {
16209 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016210 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 }
16212 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016213 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016215 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 {
16217 if (j)
16218 MSG_PUTS(", ");
16219 msg_puts(FUNCARG(fp, j));
16220 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016221 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 {
16223 if (j)
16224 MSG_PUTS(", ");
16225 MSG_PUTS("...");
16226 }
16227 msg_putchar(')');
16228}
16229
16230/*
16231 * Find a function by name, return pointer to it in ufuncs.
16232 * Return NULL for unknown function.
16233 */
16234 static ufunc_T *
16235find_func(name)
16236 char_u *name;
16237{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016238 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016240 hi = hash_find(&func_hashtab, name);
16241 if (!HASHITEM_EMPTY(hi))
16242 return HI2UF(hi);
16243 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244}
16245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016246/*
16247 * Return TRUE if a function "name" exists.
16248 */
16249 static int
16250function_exists(name)
16251 char_u *name;
16252{
16253 char_u *p = name;
16254 int n = FALSE;
16255
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016256 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016257 if (p != NULL)
16258 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016259 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016260 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016261 else
16262 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016263 vim_free(p);
16264 }
16265 return n;
16266}
16267
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016268/*
16269 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016270 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016271 */
16272 static int
16273builtin_function(name)
16274 char_u *name;
16275{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016276 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
16277 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016278}
16279
Bram Moolenaar05159a02005-02-26 23:04:13 +000016280#if defined(FEAT_PROFILE) || defined(PROTO)
16281/*
16282 * Start profiling function "fp".
16283 */
16284 static void
16285func_do_profile(fp)
16286 ufunc_T *fp;
16287{
16288 fp->uf_tm_count = 0;
16289 profile_zero(&fp->uf_tm_self);
16290 profile_zero(&fp->uf_tm_total);
16291 if (fp->uf_tml_count == NULL)
16292 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16293 (sizeof(int) * fp->uf_lines.ga_len));
16294 if (fp->uf_tml_total == NULL)
16295 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16296 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16297 if (fp->uf_tml_self == NULL)
16298 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16299 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16300 fp->uf_tml_idx = -1;
16301 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16302 || fp->uf_tml_self == NULL)
16303 return; /* out of memory */
16304
16305 fp->uf_profiling = TRUE;
16306}
16307
16308/*
16309 * Dump the profiling results for all functions in file "fd".
16310 */
16311 void
16312func_dump_profile(fd)
16313 FILE *fd;
16314{
16315 hashitem_T *hi;
16316 int todo;
16317 ufunc_T *fp;
16318 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016319 ufunc_T **sorttab;
16320 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016321
16322 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016323 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16324
Bram Moolenaar05159a02005-02-26 23:04:13 +000016325 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16326 {
16327 if (!HASHITEM_EMPTY(hi))
16328 {
16329 --todo;
16330 fp = HI2UF(hi);
16331 if (fp->uf_profiling)
16332 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016333 if (sorttab != NULL)
16334 sorttab[st_len++] = fp;
16335
Bram Moolenaar05159a02005-02-26 23:04:13 +000016336 if (fp->uf_name[0] == K_SPECIAL)
16337 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16338 else
16339 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16340 if (fp->uf_tm_count == 1)
16341 fprintf(fd, "Called 1 time\n");
16342 else
16343 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16344 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16345 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16346 fprintf(fd, "\n");
16347 fprintf(fd, "count total (s) self (s)\n");
16348
16349 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16350 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016351 prof_func_line(fd, fp->uf_tml_count[i],
16352 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016353 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16354 }
16355 fprintf(fd, "\n");
16356 }
16357 }
16358 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016359
16360 if (sorttab != NULL && st_len > 0)
16361 {
16362 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16363 prof_total_cmp);
16364 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16365 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16366 prof_self_cmp);
16367 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16368 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016369}
Bram Moolenaar73830342005-02-28 22:48:19 +000016370
16371 static void
16372prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16373 FILE *fd;
16374 ufunc_T **sorttab;
16375 int st_len;
16376 char *title;
16377 int prefer_self; /* when equal print only self time */
16378{
16379 int i;
16380 ufunc_T *fp;
16381
16382 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16383 fprintf(fd, "count total (s) self (s) function\n");
16384 for (i = 0; i < 20 && i < st_len; ++i)
16385 {
16386 fp = sorttab[i];
16387 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16388 prefer_self);
16389 if (fp->uf_name[0] == K_SPECIAL)
16390 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16391 else
16392 fprintf(fd, " %s()\n", fp->uf_name);
16393 }
16394 fprintf(fd, "\n");
16395}
16396
16397/*
16398 * Print the count and times for one function or function line.
16399 */
16400 static void
16401prof_func_line(fd, count, total, self, prefer_self)
16402 FILE *fd;
16403 int count;
16404 proftime_T *total;
16405 proftime_T *self;
16406 int prefer_self; /* when equal print only self time */
16407{
16408 if (count > 0)
16409 {
16410 fprintf(fd, "%5d ", count);
16411 if (prefer_self && profile_equal(total, self))
16412 fprintf(fd, " ");
16413 else
16414 fprintf(fd, "%s ", profile_msg(total));
16415 if (!prefer_self && profile_equal(total, self))
16416 fprintf(fd, " ");
16417 else
16418 fprintf(fd, "%s ", profile_msg(self));
16419 }
16420 else
16421 fprintf(fd, " ");
16422}
16423
16424/*
16425 * Compare function for total time sorting.
16426 */
16427 static int
16428#ifdef __BORLANDC__
16429_RTLENTRYF
16430#endif
16431prof_total_cmp(s1, s2)
16432 const void *s1;
16433 const void *s2;
16434{
16435 ufunc_T *p1, *p2;
16436
16437 p1 = *(ufunc_T **)s1;
16438 p2 = *(ufunc_T **)s2;
16439 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16440}
16441
16442/*
16443 * Compare function for self time sorting.
16444 */
16445 static int
16446#ifdef __BORLANDC__
16447_RTLENTRYF
16448#endif
16449prof_self_cmp(s1, s2)
16450 const void *s1;
16451 const void *s2;
16452{
16453 ufunc_T *p1, *p2;
16454
16455 p1 = *(ufunc_T **)s1;
16456 p2 = *(ufunc_T **)s2;
16457 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16458}
16459
Bram Moolenaar05159a02005-02-26 23:04:13 +000016460#endif
16461
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016462/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016463 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016464 * Return TRUE if a package was loaded.
16465 */
16466 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016467script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016468 char_u *name;
16469{
16470 char_u *p;
16471 char_u *scriptname;
16472 int ret = FALSE;
16473
16474 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016475 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016476 if (p == NULL || p <= name + 2)
16477 return FALSE;
16478
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016479 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16480 scriptname = autoload_name(name);
16481 if (cmd_runtime(scriptname, FALSE) == OK)
16482 ret = TRUE;
16483
16484 vim_free(scriptname);
16485 return ret;
16486}
16487
16488/*
16489 * Return the autoload script name for a function or variable name.
16490 * Returns NULL when out of memory.
16491 */
16492 static char_u *
16493autoload_name(name)
16494 char_u *name;
16495{
16496 char_u *p;
16497 char_u *scriptname;
16498
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016499 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016500 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16501 if (scriptname == NULL)
16502 return FALSE;
16503 STRCPY(scriptname, "autoload/");
16504 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016505 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016506 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016507 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016508 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016509 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016510}
16511
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16513
16514/*
16515 * Function given to ExpandGeneric() to obtain the list of user defined
16516 * function names.
16517 */
16518 char_u *
16519get_user_func_name(xp, idx)
16520 expand_T *xp;
16521 int idx;
16522{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016523 static long_u done;
16524 static hashitem_T *hi;
16525 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526
16527 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016529 done = 0;
16530 hi = func_hashtab.ht_array;
16531 }
16532 if (done < func_hashtab.ht_used)
16533 {
16534 if (done++ > 0)
16535 ++hi;
16536 while (HASHITEM_EMPTY(hi))
16537 ++hi;
16538 fp = HI2UF(hi);
16539
16540 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16541 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542
16543 cat_func_name(IObuff, fp);
16544 if (xp->xp_context != EXPAND_USER_FUNC)
16545 {
16546 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016547 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 STRCAT(IObuff, ")");
16549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 return IObuff;
16551 }
16552 return NULL;
16553}
16554
16555#endif /* FEAT_CMDL_COMPL */
16556
16557/*
16558 * Copy the function name of "fp" to buffer "buf".
16559 * "buf" must be able to hold the function name plus three bytes.
16560 * Takes care of script-local function names.
16561 */
16562 static void
16563cat_func_name(buf, fp)
16564 char_u *buf;
16565 ufunc_T *fp;
16566{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016567 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 {
16569 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016570 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 }
16572 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016573 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574}
16575
16576/*
16577 * ":delfunction {name}"
16578 */
16579 void
16580ex_delfunction(eap)
16581 exarg_T *eap;
16582{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016583 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 char_u *p;
16585 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016586 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587
16588 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016589 name = trans_function_name(&p, eap->skip, 0, &fudi);
16590 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016592 {
16593 if (fudi.fd_dict != NULL && !eap->skip)
16594 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 if (!ends_excmd(*skipwhite(p)))
16598 {
16599 vim_free(name);
16600 EMSG(_(e_trailing));
16601 return;
16602 }
16603 eap->nextcmd = check_nextcmd(p);
16604 if (eap->nextcmd != NULL)
16605 *p = NUL;
16606
16607 if (!eap->skip)
16608 fp = find_func(name);
16609 vim_free(name);
16610
16611 if (!eap->skip)
16612 {
16613 if (fp == NULL)
16614 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016615 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 return;
16617 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016618 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 {
16620 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16621 return;
16622 }
16623
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016624 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016626 /* Delete the dict item that refers to the function, it will
16627 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016628 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016630 else
16631 func_free(fp);
16632 }
16633}
16634
16635/*
16636 * Free a function and remove it from the list of functions.
16637 */
16638 static void
16639func_free(fp)
16640 ufunc_T *fp;
16641{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016642 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016643
16644 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016645 ga_clear_strings(&(fp->uf_args));
16646 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016647#ifdef FEAT_PROFILE
16648 vim_free(fp->uf_tml_count);
16649 vim_free(fp->uf_tml_total);
16650 vim_free(fp->uf_tml_self);
16651#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016652
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016653 /* remove the function from the function hashtable */
16654 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16655 if (HASHITEM_EMPTY(hi))
16656 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016657 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016658 hash_remove(&func_hashtab, hi);
16659
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016660 vim_free(fp);
16661}
16662
16663/*
16664 * Unreference a Function: decrement the reference count and free it when it
16665 * becomes zero. Only for numbered functions.
16666 */
16667 static void
16668func_unref(name)
16669 char_u *name;
16670{
16671 ufunc_T *fp;
16672
16673 if (name != NULL && isdigit(*name))
16674 {
16675 fp = find_func(name);
16676 if (fp == NULL)
16677 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016678 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016679 {
16680 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016681 * when "uf_calls" becomes zero. */
16682 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016683 func_free(fp);
16684 }
16685 }
16686}
16687
16688/*
16689 * Count a reference to a Function.
16690 */
16691 static void
16692func_ref(name)
16693 char_u *name;
16694{
16695 ufunc_T *fp;
16696
16697 if (name != NULL && isdigit(*name))
16698 {
16699 fp = find_func(name);
16700 if (fp == NULL)
16701 EMSG2(_(e_intern2), "func_ref()");
16702 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016703 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 }
16705}
16706
16707/*
16708 * Call a user function.
16709 */
16710 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016711call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 ufunc_T *fp; /* pointer to function */
16713 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016714 typval_T *argvars; /* arguments */
16715 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 linenr_T firstline; /* first line of range */
16717 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719{
Bram Moolenaar33570922005-01-25 22:26:29 +000016720 char_u *save_sourcing_name;
16721 linenr_T save_sourcing_lnum;
16722 scid_T save_current_SID;
16723 funccall_T fc;
16724 funccall_T *save_fcp = current_funccal;
16725 int save_did_emsg;
16726 static int depth = 0;
16727 dictitem_T *v;
16728 int fixvar_idx = 0; /* index in fixvar[] */
16729 int i;
16730 int ai;
16731 char_u numbuf[NUMBUFLEN];
16732 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016733#ifdef FEAT_PROFILE
16734 proftime_T wait_start;
16735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736
16737 /* If depth of calling is getting too high, don't execute the function */
16738 if (depth >= p_mfd)
16739 {
16740 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016741 rettv->v_type = VAR_NUMBER;
16742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 return;
16744 }
16745 ++depth;
16746
16747 line_breakcheck(); /* check for CTRL-C hit */
16748
Bram Moolenaar33570922005-01-25 22:26:29 +000016749 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751 fc.rettv = rettv;
16752 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 fc.linenr = 0;
16754 fc.returned = FALSE;
16755 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016757 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 fc.dbg_tick = debug_tick;
16759
Bram Moolenaar33570922005-01-25 22:26:29 +000016760 /*
16761 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16762 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16763 * each argument variable and saves a lot of time.
16764 */
16765 /*
16766 * Init l: variables.
16767 */
16768 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016769 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016770 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016771 /* Set l:self to "selfdict". */
16772 v = &fc.fixvar[fixvar_idx++].var;
16773 STRCPY(v->di_key, "self");
16774 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16775 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16776 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016777 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016778 v->di_tv.vval.v_dict = selfdict;
16779 ++selfdict->dv_refcount;
16780 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016781
Bram Moolenaar33570922005-01-25 22:26:29 +000016782 /*
16783 * Init a: variables.
16784 * Set a:0 to "argcount".
16785 * Set a:000 to a list with room for the "..." arguments.
16786 */
16787 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16788 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016789 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016790 v = &fc.fixvar[fixvar_idx++].var;
16791 STRCPY(v->di_key, "000");
16792 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16793 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16794 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016795 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016796 v->di_tv.vval.v_list = &fc.l_varlist;
16797 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16798 fc.l_varlist.lv_refcount = 99999;
16799
16800 /*
16801 * Set a:firstline to "firstline" and a:lastline to "lastline".
16802 * Set a:name to named arguments.
16803 * Set a:N to the "..." arguments.
16804 */
16805 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16806 (varnumber_T)firstline);
16807 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16808 (varnumber_T)lastline);
16809 for (i = 0; i < argcount; ++i)
16810 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016811 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016812 if (ai < 0)
16813 /* named argument a:name */
16814 name = FUNCARG(fp, i);
16815 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016816 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016817 /* "..." argument a:1, a:2, etc. */
16818 sprintf((char *)numbuf, "%d", ai + 1);
16819 name = numbuf;
16820 }
16821 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16822 {
16823 v = &fc.fixvar[fixvar_idx++].var;
16824 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16825 }
16826 else
16827 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016828 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16829 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 if (v == NULL)
16831 break;
16832 v->di_flags = DI_FLAGS_RO;
16833 }
16834 STRCPY(v->di_key, name);
16835 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16836
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016837 /* Note: the values are copied directly to avoid alloc/free.
16838 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016839 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016840 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016841
16842 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16843 {
16844 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16845 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016846 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016847 }
16848 }
16849
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 /* Don't redraw while executing the function. */
16851 ++RedrawingDisabled;
16852 save_sourcing_name = sourcing_name;
16853 save_sourcing_lnum = sourcing_lnum;
16854 sourcing_lnum = 1;
16855 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016856 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 if (sourcing_name != NULL)
16858 {
16859 if (save_sourcing_name != NULL
16860 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16861 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16862 else
16863 STRCPY(sourcing_name, "function ");
16864 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16865
16866 if (p_verbose >= 12)
16867 {
16868 ++no_wait_return;
16869 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar555b2802005-05-19 21:08:39 +000016870 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 if (p_verbose >= 14)
16872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016874 char_u numbuf[NUMBUFLEN];
16875 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876
16877 msg_puts((char_u *)"(");
16878 for (i = 0; i < argcount; ++i)
16879 {
16880 if (i > 0)
16881 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016882 if (argvars[i].v_type == VAR_NUMBER)
16883 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 else
16885 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016886 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016887 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016889 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 }
16891 }
16892 msg_puts((char_u *)")");
16893 }
16894 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16895 cmdline_row = msg_row;
16896 --no_wait_return;
16897 }
16898 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016899#ifdef FEAT_PROFILE
16900 if (do_profiling)
16901 {
16902 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16903 func_do_profile(fp);
16904 if (fp->uf_profiling
16905 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16906 {
16907 ++fp->uf_tm_count;
16908 profile_start(&fp->uf_tm_start);
16909 profile_zero(&fp->uf_tm_children);
16910 }
16911 script_prof_save(&wait_start);
16912 }
16913#endif
16914
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016916 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 save_did_emsg = did_emsg;
16918 did_emsg = FALSE;
16919
16920 /* call do_cmdline() to execute the lines */
16921 do_cmdline(NULL, get_func_line, (void *)&fc,
16922 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16923
16924 --RedrawingDisabled;
16925
16926 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016927 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016929 clear_tv(rettv);
16930 rettv->v_type = VAR_NUMBER;
16931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 }
16933
Bram Moolenaar05159a02005-02-26 23:04:13 +000016934#ifdef FEAT_PROFILE
16935 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16936 {
16937 profile_end(&fp->uf_tm_start);
16938 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16939 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16940 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16941 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16942 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16943 {
16944 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16945 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16946 }
16947 }
16948#endif
16949
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 /* when being verbose, mention the return value */
16951 if (p_verbose >= 12)
16952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 ++no_wait_return;
16954 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16955
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000016957 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000016959 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
16960 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016963 char_u buf[MSG_BUF_LEN];
16964 char_u numbuf[NUMBUFLEN];
16965 char_u *tofree;
16966
Bram Moolenaar555b2802005-05-19 21:08:39 +000016967 /* The value may be very long. Skip the middle part, so that we
16968 * have some idea how it starts and ends. smsg() would always
16969 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000016970 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016971 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000016972 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016973 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 }
16975 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16976 cmdline_row = msg_row;
16977 --no_wait_return;
16978 }
16979
16980 vim_free(sourcing_name);
16981 sourcing_name = save_sourcing_name;
16982 sourcing_lnum = save_sourcing_lnum;
16983 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016984#ifdef FEAT_PROFILE
16985 if (do_profiling)
16986 script_prof_restore(&wait_start);
16987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988
16989 if (p_verbose >= 12 && sourcing_name != NULL)
16990 {
16991 ++no_wait_return;
16992 msg_scroll = TRUE; /* always scroll up, don't overwrite */
Bram Moolenaar555b2802005-05-19 21:08:39 +000016993 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16995 cmdline_row = msg_row;
16996 --no_wait_return;
16997 }
16998
16999 did_emsg |= save_did_emsg;
17000 current_funccal = save_fcp;
17001
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 /* The a: variables typevals were not alloced, only free the allocated
17003 * variables. */
17004 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17005
17006 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 --depth;
17008}
17009
17010/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017011 * Add a number variable "name" to dict "dp" with value "nr".
17012 */
17013 static void
17014add_nr_var(dp, v, name, nr)
17015 dict_T *dp;
17016 dictitem_T *v;
17017 char *name;
17018 varnumber_T nr;
17019{
17020 STRCPY(v->di_key, name);
17021 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17022 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17023 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017024 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 v->di_tv.vval.v_number = nr;
17026}
17027
17028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 * ":return [expr]"
17030 */
17031 void
17032ex_return(eap)
17033 exarg_T *eap;
17034{
17035 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017036 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 int returning = FALSE;
17038
17039 if (current_funccal == NULL)
17040 {
17041 EMSG(_("E133: :return not inside a function"));
17042 return;
17043 }
17044
17045 if (eap->skip)
17046 ++emsg_skip;
17047
17048 eap->nextcmd = NULL;
17049 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 {
17052 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017053 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017055 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 }
17057 /* It's safer to return also on error. */
17058 else if (!eap->skip)
17059 {
17060 /*
17061 * Return unless the expression evaluation has been cancelled due to an
17062 * aborting error, an interrupt, or an exception.
17063 */
17064 if (!aborting())
17065 returning = do_return(eap, FALSE, TRUE, NULL);
17066 }
17067
17068 /* When skipping or the return gets pending, advance to the next command
17069 * in this line (!returning). Otherwise, ignore the rest of the line.
17070 * Following lines will be ignored by get_func_line(). */
17071 if (returning)
17072 eap->nextcmd = NULL;
17073 else if (eap->nextcmd == NULL) /* no argument */
17074 eap->nextcmd = check_nextcmd(arg);
17075
17076 if (eap->skip)
17077 --emsg_skip;
17078}
17079
17080/*
17081 * Return from a function. Possibly makes the return pending. Also called
17082 * for a pending return at the ":endtry" or after returning from an extra
17083 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017084 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017085 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 * FALSE when the return gets pending.
17087 */
17088 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 exarg_T *eap;
17091 int reanimate;
17092 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017093 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094{
17095 int idx;
17096 struct condstack *cstack = eap->cstack;
17097
17098 if (reanimate)
17099 /* Undo the return. */
17100 current_funccal->returned = FALSE;
17101
17102 /*
17103 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17104 * not in its finally clause (which then is to be executed next) is found.
17105 * In this case, make the ":return" pending for execution at the ":endtry".
17106 * Otherwise, return normally.
17107 */
17108 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17109 if (idx >= 0)
17110 {
17111 cstack->cs_pending[idx] = CSTP_RETURN;
17112
17113 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017114 /* A pending return again gets pending. "rettv" points to an
17115 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 else
17119 {
17120 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 {
17127 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 else
17131 EMSG(_(e_outofmem));
17132 }
17133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017134 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135
17136 if (reanimate)
17137 {
17138 /* The pending return value could be overwritten by a ":return"
17139 * without argument in a finally clause; reset the default
17140 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017141 current_funccal->rettv->v_type = VAR_NUMBER;
17142 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 }
17144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017145 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 }
17147 else
17148 {
17149 current_funccal->returned = TRUE;
17150
17151 /* If the return is carried out now, store the return value. For
17152 * a return immediately after reanimation, the value is already
17153 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 }
17161 }
17162
17163 return idx < 0;
17164}
17165
17166/*
17167 * Free the variable with a pending return value.
17168 */
17169 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170discard_pending_return(rettv)
17171 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172{
Bram Moolenaar33570922005-01-25 22:26:29 +000017173 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174}
17175
17176/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 * is an allocated string. Used by report_pending() for verbose messages.
17179 */
17180 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017181get_return_cmd(rettv)
17182 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017184 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017186 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017188 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017190 if (s == NULL)
17191 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192
17193 STRCPY(IObuff, ":return ");
17194 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17195 if (STRLEN(s) + 8 >= IOSIZE)
17196 STRCPY(IObuff + IOSIZE - 4, "...");
17197 vim_free(tofree);
17198 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199}
17200
17201/*
17202 * Get next function line.
17203 * Called by do_cmdline() to get the next line.
17204 * Returns allocated string, or NULL for end of function.
17205 */
17206/* ARGSUSED */
17207 char_u *
17208get_func_line(c, cookie, indent)
17209 int c; /* not used */
17210 void *cookie;
17211 int indent; /* not used */
17212{
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017214 ufunc_T *fp = fcp->func;
17215 char_u *retval;
17216 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217
17218 /* If breakpoints have been added/deleted need to check for it. */
17219 if (fcp->dbg_tick != debug_tick)
17220 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017221 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 sourcing_lnum);
17223 fcp->dbg_tick = debug_tick;
17224 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017225#ifdef FEAT_PROFILE
17226 if (do_profiling)
17227 func_line_end(cookie);
17228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229
Bram Moolenaar05159a02005-02-26 23:04:13 +000017230 gap = &fp->uf_lines;
17231 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 retval = NULL;
17233 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17234 retval = NULL;
17235 else
17236 {
17237 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17238 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017239#ifdef FEAT_PROFILE
17240 if (do_profiling)
17241 func_line_start(cookie);
17242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 }
17244
17245 /* Did we encounter a breakpoint? */
17246 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17247 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017248 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017250 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 sourcing_lnum);
17252 fcp->dbg_tick = debug_tick;
17253 }
17254
17255 return retval;
17256}
17257
Bram Moolenaar05159a02005-02-26 23:04:13 +000017258#if defined(FEAT_PROFILE) || defined(PROTO)
17259/*
17260 * Called when starting to read a function line.
17261 * "sourcing_lnum" must be correct!
17262 * When skipping lines it may not actually be executed, but we won't find out
17263 * until later and we need to store the time now.
17264 */
17265 void
17266func_line_start(cookie)
17267 void *cookie;
17268{
17269 funccall_T *fcp = (funccall_T *)cookie;
17270 ufunc_T *fp = fcp->func;
17271
17272 if (fp->uf_profiling && sourcing_lnum >= 1
17273 && sourcing_lnum <= fp->uf_lines.ga_len)
17274 {
17275 fp->uf_tml_idx = sourcing_lnum - 1;
17276 fp->uf_tml_execed = FALSE;
17277 profile_start(&fp->uf_tml_start);
17278 profile_zero(&fp->uf_tml_children);
17279 profile_get_wait(&fp->uf_tml_wait);
17280 }
17281}
17282
17283/*
17284 * Called when actually executing a function line.
17285 */
17286 void
17287func_line_exec(cookie)
17288 void *cookie;
17289{
17290 funccall_T *fcp = (funccall_T *)cookie;
17291 ufunc_T *fp = fcp->func;
17292
17293 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17294 fp->uf_tml_execed = TRUE;
17295}
17296
17297/*
17298 * Called when done with a function line.
17299 */
17300 void
17301func_line_end(cookie)
17302 void *cookie;
17303{
17304 funccall_T *fcp = (funccall_T *)cookie;
17305 ufunc_T *fp = fcp->func;
17306
17307 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17308 {
17309 if (fp->uf_tml_execed)
17310 {
17311 ++fp->uf_tml_count[fp->uf_tml_idx];
17312 profile_end(&fp->uf_tml_start);
17313 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17314 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17315 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17316 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17317 }
17318 fp->uf_tml_idx = -1;
17319 }
17320}
17321#endif
17322
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323/*
17324 * Return TRUE if the currently active function should be ended, because a
17325 * return was encountered or an error occured. Used inside a ":while".
17326 */
17327 int
17328func_has_ended(cookie)
17329 void *cookie;
17330{
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332
17333 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17334 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017335 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 || fcp->returned);
17337}
17338
17339/*
17340 * return TRUE if cookie indicates a function which "abort"s on errors.
17341 */
17342 int
17343func_has_abort(cookie)
17344 void *cookie;
17345{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017346 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347}
17348
17349#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17350typedef enum
17351{
17352 VAR_FLAVOUR_DEFAULT,
17353 VAR_FLAVOUR_SESSION,
17354 VAR_FLAVOUR_VIMINFO
17355} var_flavour_T;
17356
17357static var_flavour_T var_flavour __ARGS((char_u *varname));
17358
17359 static var_flavour_T
17360var_flavour(varname)
17361 char_u *varname;
17362{
17363 char_u *p = varname;
17364
17365 if (ASCII_ISUPPER(*p))
17366 {
17367 while (*(++p))
17368 if (ASCII_ISLOWER(*p))
17369 return VAR_FLAVOUR_SESSION;
17370 return VAR_FLAVOUR_VIMINFO;
17371 }
17372 else
17373 return VAR_FLAVOUR_DEFAULT;
17374}
17375#endif
17376
17377#if defined(FEAT_VIMINFO) || defined(PROTO)
17378/*
17379 * Restore global vars that start with a capital from the viminfo file
17380 */
17381 int
17382read_viminfo_varlist(virp, writing)
17383 vir_T *virp;
17384 int writing;
17385{
17386 char_u *tab;
17387 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389
17390 if (!writing && (find_viminfo_parameter('!') != NULL))
17391 {
17392 tab = vim_strchr(virp->vir_line + 1, '\t');
17393 if (tab != NULL)
17394 {
17395 *tab++ = '\0'; /* isolate the variable name */
17396 if (*tab == 'S') /* string var */
17397 is_string = TRUE;
17398
17399 tab = vim_strchr(tab, '\t');
17400 if (tab != NULL)
17401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 if (is_string)
17403 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017404 tv.v_type = VAR_STRING;
17405 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 }
17408 else
17409 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017410 tv.v_type = VAR_NUMBER;
17411 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017413 set_var(virp->vir_line + 1, &tv, FALSE);
17414 if (is_string)
17415 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 }
17417 }
17418 }
17419
17420 return viminfo_readline(virp);
17421}
17422
17423/*
17424 * Write global vars that start with a capital to the viminfo file
17425 */
17426 void
17427write_viminfo_varlist(fp)
17428 FILE *fp;
17429{
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 hashitem_T *hi;
17431 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017432 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017433 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017434 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017435 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017436 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437
17438 if (find_viminfo_parameter('!') == NULL)
17439 return;
17440
17441 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017442
Bram Moolenaar33570922005-01-25 22:26:29 +000017443 todo = globvarht.ht_used;
17444 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017446 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017448 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017449 this_var = HI2DI(hi);
17450 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017451 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017452 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017453 {
17454 case VAR_STRING: s = "STR"; break;
17455 case VAR_NUMBER: s = "NUM"; break;
17456 default: continue;
17457 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017459 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17460 if (p != NULL)
17461 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017462 vim_free(tofree);
17463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 }
17465 }
17466}
17467#endif
17468
17469#if defined(FEAT_SESSION) || defined(PROTO)
17470 int
17471store_session_globals(fd)
17472 FILE *fd;
17473{
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 hashitem_T *hi;
17475 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017476 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 char_u *p, *t;
17478
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 todo = globvarht.ht_used;
17480 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017482 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017484 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017485 this_var = HI2DI(hi);
17486 if ((this_var->di_tv.v_type == VAR_NUMBER
17487 || this_var->di_tv.v_type == VAR_STRING)
17488 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017489 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017490 /* Escape special characters with a backslash. Turn a LF and
17491 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017493 (char_u *)"\\\"\n\r");
17494 if (p == NULL) /* out of memory */
17495 break;
17496 for (t = p; *t != NUL; ++t)
17497 if (*t == '\n')
17498 *t = 'n';
17499 else if (*t == '\r')
17500 *t = 'r';
17501 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017502 this_var->di_key,
17503 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17504 : ' ',
17505 p,
17506 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17507 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017508 || put_eol(fd) == FAIL)
17509 {
17510 vim_free(p);
17511 return FAIL;
17512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 vim_free(p);
17514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 }
17516 }
17517 return OK;
17518}
17519#endif
17520
17521#endif /* FEAT_EVAL */
17522
17523#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17524
17525
17526#ifdef WIN3264
17527/*
17528 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17529 */
17530static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17531static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17532static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17533
17534/*
17535 * Get the short pathname of a file.
17536 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17537 */
17538 static int
17539get_short_pathname(fnamep, bufp, fnamelen)
17540 char_u **fnamep;
17541 char_u **bufp;
17542 int *fnamelen;
17543{
17544 int l,len;
17545 char_u *newbuf;
17546
17547 len = *fnamelen;
17548
17549 l = GetShortPathName(*fnamep, *fnamep, len);
17550 if (l > len - 1)
17551 {
17552 /* If that doesn't work (not enough space), then save the string
17553 * and try again with a new buffer big enough
17554 */
17555 newbuf = vim_strnsave(*fnamep, l);
17556 if (newbuf == NULL)
17557 return 0;
17558
17559 vim_free(*bufp);
17560 *fnamep = *bufp = newbuf;
17561
17562 l = GetShortPathName(*fnamep,*fnamep,l+1);
17563
17564 /* Really should always succeed, as the buffer is big enough */
17565 }
17566
17567 *fnamelen = l;
17568 return 1;
17569}
17570
17571/*
17572 * Create a short path name. Returns the length of the buffer it needs.
17573 * Doesn't copy over the end of the buffer passed in.
17574 */
17575 static int
17576shortpath_for_invalid_fname(fname, bufp, fnamelen)
17577 char_u **fname;
17578 char_u **bufp;
17579 int *fnamelen;
17580{
17581 char_u *s, *p, *pbuf2, *pbuf3;
17582 char_u ch;
17583 int l,len,len2,plen,slen;
17584
17585 /* Make a copy */
17586 len2 = *fnamelen;
17587 pbuf2 = vim_strnsave(*fname, len2);
17588 pbuf3 = NULL;
17589
17590 s = pbuf2 + len2 - 1; /* Find the end */
17591 slen = 1;
17592 plen = len2;
17593
17594 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017595 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 {
17597 --s;
17598 ++slen;
17599 --plen;
17600 }
17601
17602 do
17603 {
17604 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017605 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 {
17607 --s;
17608 ++slen;
17609 --plen;
17610 }
17611 if (s <= pbuf2)
17612 break;
17613
17614 /* Remeber the character that is about to be blatted */
17615 ch = *s;
17616 *s = 0; /* get_short_pathname requires a null-terminated string */
17617
17618 /* Try it in situ */
17619 p = pbuf2;
17620 if (!get_short_pathname(&p, &pbuf3, &plen))
17621 {
17622 vim_free(pbuf2);
17623 return -1;
17624 }
17625 *s = ch; /* Preserve the string */
17626 } while (plen == 0);
17627
17628 if (plen > 0)
17629 {
17630 /* Remeber the length of the new string. */
17631 *fnamelen = len = plen + slen;
17632 vim_free(*bufp);
17633 if (len > len2)
17634 {
17635 /* If there's not enough space in the currently allocated string,
17636 * then copy it to a buffer big enough.
17637 */
17638 *fname= *bufp = vim_strnsave(p, len);
17639 if (*fname == NULL)
17640 return -1;
17641 }
17642 else
17643 {
17644 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17645 *fname = *bufp = pbuf2;
17646 if (p != pbuf2)
17647 strncpy(*fname, p, plen);
17648 pbuf2 = NULL;
17649 }
17650 /* Concat the next bit */
17651 strncpy(*fname + plen, s, slen);
17652 (*fname)[len] = '\0';
17653 }
17654 vim_free(pbuf3);
17655 vim_free(pbuf2);
17656 return 0;
17657}
17658
17659/*
17660 * Get a pathname for a partial path.
17661 */
17662 static int
17663shortpath_for_partial(fnamep, bufp, fnamelen)
17664 char_u **fnamep;
17665 char_u **bufp;
17666 int *fnamelen;
17667{
17668 int sepcount, len, tflen;
17669 char_u *p;
17670 char_u *pbuf, *tfname;
17671 int hasTilde;
17672
17673 /* Count up the path seperators from the RHS.. so we know which part
17674 * of the path to return.
17675 */
17676 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017677 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 if (vim_ispathsep(*p))
17679 ++sepcount;
17680
17681 /* Need full path first (use expand_env() to remove a "~/") */
17682 hasTilde = (**fnamep == '~');
17683 if (hasTilde)
17684 pbuf = tfname = expand_env_save(*fnamep);
17685 else
17686 pbuf = tfname = FullName_save(*fnamep, FALSE);
17687
17688 len = tflen = STRLEN(tfname);
17689
17690 if (!get_short_pathname(&tfname, &pbuf, &len))
17691 return -1;
17692
17693 if (len == 0)
17694 {
17695 /* Don't have a valid filename, so shorten the rest of the
17696 * path if we can. This CAN give us invalid 8.3 filenames, but
17697 * there's not a lot of point in guessing what it might be.
17698 */
17699 len = tflen;
17700 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17701 return -1;
17702 }
17703
17704 /* Count the paths backward to find the beginning of the desired string. */
17705 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017706 {
17707#ifdef FEAT_MBYTE
17708 if (has_mbyte)
17709 p -= mb_head_off(tfname, p);
17710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 if (vim_ispathsep(*p))
17712 {
17713 if (sepcount == 0 || (hasTilde && sepcount == 1))
17714 break;
17715 else
17716 sepcount --;
17717 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 if (hasTilde)
17720 {
17721 --p;
17722 if (p >= tfname)
17723 *p = '~';
17724 else
17725 return -1;
17726 }
17727 else
17728 ++p;
17729
17730 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17731 vim_free(*bufp);
17732 *fnamelen = (int)STRLEN(p);
17733 *bufp = pbuf;
17734 *fnamep = p;
17735
17736 return 0;
17737}
17738#endif /* WIN3264 */
17739
17740/*
17741 * Adjust a filename, according to a string of modifiers.
17742 * *fnamep must be NUL terminated when called. When returning, the length is
17743 * determined by *fnamelen.
17744 * Returns valid flags.
17745 * When there is an error, *fnamep is set to NULL.
17746 */
17747 int
17748modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17749 char_u *src; /* string with modifiers */
17750 int *usedlen; /* characters after src that are used */
17751 char_u **fnamep; /* file name so far */
17752 char_u **bufp; /* buffer for allocated file name or NULL */
17753 int *fnamelen; /* length of fnamep */
17754{
17755 int valid = 0;
17756 char_u *tail;
17757 char_u *s, *p, *pbuf;
17758 char_u dirname[MAXPATHL];
17759 int c;
17760 int has_fullname = 0;
17761#ifdef WIN3264
17762 int has_shortname = 0;
17763#endif
17764
17765repeat:
17766 /* ":p" - full path/file_name */
17767 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17768 {
17769 has_fullname = 1;
17770
17771 valid |= VALID_PATH;
17772 *usedlen += 2;
17773
17774 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17775 if ((*fnamep)[0] == '~'
17776#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17777 && ((*fnamep)[1] == '/'
17778# ifdef BACKSLASH_IN_FILENAME
17779 || (*fnamep)[1] == '\\'
17780# endif
17781 || (*fnamep)[1] == NUL)
17782
17783#endif
17784 )
17785 {
17786 *fnamep = expand_env_save(*fnamep);
17787 vim_free(*bufp); /* free any allocated file name */
17788 *bufp = *fnamep;
17789 if (*fnamep == NULL)
17790 return -1;
17791 }
17792
17793 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017794 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 {
17796 if (vim_ispathsep(*p)
17797 && p[1] == '.'
17798 && (p[2] == NUL
17799 || vim_ispathsep(p[2])
17800 || (p[2] == '.'
17801 && (p[3] == NUL || vim_ispathsep(p[3])))))
17802 break;
17803 }
17804
17805 /* FullName_save() is slow, don't use it when not needed. */
17806 if (*p != NUL || !vim_isAbsName(*fnamep))
17807 {
17808 *fnamep = FullName_save(*fnamep, *p != NUL);
17809 vim_free(*bufp); /* free any allocated file name */
17810 *bufp = *fnamep;
17811 if (*fnamep == NULL)
17812 return -1;
17813 }
17814
17815 /* Append a path separator to a directory. */
17816 if (mch_isdir(*fnamep))
17817 {
17818 /* Make room for one or two extra characters. */
17819 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17820 vim_free(*bufp); /* free any allocated file name */
17821 *bufp = *fnamep;
17822 if (*fnamep == NULL)
17823 return -1;
17824 add_pathsep(*fnamep);
17825 }
17826 }
17827
17828 /* ":." - path relative to the current directory */
17829 /* ":~" - path relative to the home directory */
17830 /* ":8" - shortname path - postponed till after */
17831 while (src[*usedlen] == ':'
17832 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17833 {
17834 *usedlen += 2;
17835 if (c == '8')
17836 {
17837#ifdef WIN3264
17838 has_shortname = 1; /* Postpone this. */
17839#endif
17840 continue;
17841 }
17842 pbuf = NULL;
17843 /* Need full path first (use expand_env() to remove a "~/") */
17844 if (!has_fullname)
17845 {
17846 if (c == '.' && **fnamep == '~')
17847 p = pbuf = expand_env_save(*fnamep);
17848 else
17849 p = pbuf = FullName_save(*fnamep, FALSE);
17850 }
17851 else
17852 p = *fnamep;
17853
17854 has_fullname = 0;
17855
17856 if (p != NULL)
17857 {
17858 if (c == '.')
17859 {
17860 mch_dirname(dirname, MAXPATHL);
17861 s = shorten_fname(p, dirname);
17862 if (s != NULL)
17863 {
17864 *fnamep = s;
17865 if (pbuf != NULL)
17866 {
17867 vim_free(*bufp); /* free any allocated file name */
17868 *bufp = pbuf;
17869 pbuf = NULL;
17870 }
17871 }
17872 }
17873 else
17874 {
17875 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17876 /* Only replace it when it starts with '~' */
17877 if (*dirname == '~')
17878 {
17879 s = vim_strsave(dirname);
17880 if (s != NULL)
17881 {
17882 *fnamep = s;
17883 vim_free(*bufp);
17884 *bufp = s;
17885 }
17886 }
17887 }
17888 vim_free(pbuf);
17889 }
17890 }
17891
17892 tail = gettail(*fnamep);
17893 *fnamelen = (int)STRLEN(*fnamep);
17894
17895 /* ":h" - head, remove "/file_name", can be repeated */
17896 /* Don't remove the first "/" or "c:\" */
17897 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17898 {
17899 valid |= VALID_HEAD;
17900 *usedlen += 2;
17901 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017902 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 --tail;
17904 *fnamelen = (int)(tail - *fnamep);
17905#ifdef VMS
17906 if (*fnamelen > 0)
17907 *fnamelen += 1; /* the path separator is part of the path */
17908#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017909 while (tail > s && !after_pathsep(s, tail))
17910 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 }
17912
17913 /* ":8" - shortname */
17914 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17915 {
17916 *usedlen += 2;
17917#ifdef WIN3264
17918 has_shortname = 1;
17919#endif
17920 }
17921
17922#ifdef WIN3264
17923 /* Check shortname after we have done 'heads' and before we do 'tails'
17924 */
17925 if (has_shortname)
17926 {
17927 pbuf = NULL;
17928 /* Copy the string if it is shortened by :h */
17929 if (*fnamelen < (int)STRLEN(*fnamep))
17930 {
17931 p = vim_strnsave(*fnamep, *fnamelen);
17932 if (p == 0)
17933 return -1;
17934 vim_free(*bufp);
17935 *bufp = *fnamep = p;
17936 }
17937
17938 /* Split into two implementations - makes it easier. First is where
17939 * there isn't a full name already, second is where there is.
17940 */
17941 if (!has_fullname && !vim_isAbsName(*fnamep))
17942 {
17943 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17944 return -1;
17945 }
17946 else
17947 {
17948 int l;
17949
17950 /* Simple case, already have the full-name
17951 * Nearly always shorter, so try first time. */
17952 l = *fnamelen;
17953 if (!get_short_pathname(fnamep, bufp, &l))
17954 return -1;
17955
17956 if (l == 0)
17957 {
17958 /* Couldn't find the filename.. search the paths.
17959 */
17960 l = *fnamelen;
17961 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17962 return -1;
17963 }
17964 *fnamelen = l;
17965 }
17966 }
17967#endif /* WIN3264 */
17968
17969 /* ":t" - tail, just the basename */
17970 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17971 {
17972 *usedlen += 2;
17973 *fnamelen -= (int)(tail - *fnamep);
17974 *fnamep = tail;
17975 }
17976
17977 /* ":e" - extension, can be repeated */
17978 /* ":r" - root, without extension, can be repeated */
17979 while (src[*usedlen] == ':'
17980 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17981 {
17982 /* find a '.' in the tail:
17983 * - for second :e: before the current fname
17984 * - otherwise: The last '.'
17985 */
17986 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17987 s = *fnamep - 2;
17988 else
17989 s = *fnamep + *fnamelen - 1;
17990 for ( ; s > tail; --s)
17991 if (s[0] == '.')
17992 break;
17993 if (src[*usedlen + 1] == 'e') /* :e */
17994 {
17995 if (s > tail)
17996 {
17997 *fnamelen += (int)(*fnamep - (s + 1));
17998 *fnamep = s + 1;
17999#ifdef VMS
18000 /* cut version from the extension */
18001 s = *fnamep + *fnamelen - 1;
18002 for ( ; s > *fnamep; --s)
18003 if (s[0] == ';')
18004 break;
18005 if (s > *fnamep)
18006 *fnamelen = s - *fnamep;
18007#endif
18008 }
18009 else if (*fnamep <= tail)
18010 *fnamelen = 0;
18011 }
18012 else /* :r */
18013 {
18014 if (s > tail) /* remove one extension */
18015 *fnamelen = (int)(s - *fnamep);
18016 }
18017 *usedlen += 2;
18018 }
18019
18020 /* ":s?pat?foo?" - substitute */
18021 /* ":gs?pat?foo?" - global substitute */
18022 if (src[*usedlen] == ':'
18023 && (src[*usedlen + 1] == 's'
18024 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18025 {
18026 char_u *str;
18027 char_u *pat;
18028 char_u *sub;
18029 int sep;
18030 char_u *flags;
18031 int didit = FALSE;
18032
18033 flags = (char_u *)"";
18034 s = src + *usedlen + 2;
18035 if (src[*usedlen + 1] == 'g')
18036 {
18037 flags = (char_u *)"g";
18038 ++s;
18039 }
18040
18041 sep = *s++;
18042 if (sep)
18043 {
18044 /* find end of pattern */
18045 p = vim_strchr(s, sep);
18046 if (p != NULL)
18047 {
18048 pat = vim_strnsave(s, (int)(p - s));
18049 if (pat != NULL)
18050 {
18051 s = p + 1;
18052 /* find end of substitution */
18053 p = vim_strchr(s, sep);
18054 if (p != NULL)
18055 {
18056 sub = vim_strnsave(s, (int)(p - s));
18057 str = vim_strnsave(*fnamep, *fnamelen);
18058 if (sub != NULL && str != NULL)
18059 {
18060 *usedlen = (int)(p + 1 - src);
18061 s = do_string_sub(str, pat, sub, flags);
18062 if (s != NULL)
18063 {
18064 *fnamep = s;
18065 *fnamelen = (int)STRLEN(s);
18066 vim_free(*bufp);
18067 *bufp = s;
18068 didit = TRUE;
18069 }
18070 }
18071 vim_free(sub);
18072 vim_free(str);
18073 }
18074 vim_free(pat);
18075 }
18076 }
18077 /* after using ":s", repeat all the modifiers */
18078 if (didit)
18079 goto repeat;
18080 }
18081 }
18082
18083 return valid;
18084}
18085
18086/*
18087 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18088 * "flags" can be "g" to do a global substitute.
18089 * Returns an allocated string, NULL for error.
18090 */
18091 char_u *
18092do_string_sub(str, pat, sub, flags)
18093 char_u *str;
18094 char_u *pat;
18095 char_u *sub;
18096 char_u *flags;
18097{
18098 int sublen;
18099 regmatch_T regmatch;
18100 int i;
18101 int do_all;
18102 char_u *tail;
18103 garray_T ga;
18104 char_u *ret;
18105 char_u *save_cpo;
18106
18107 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18108 save_cpo = p_cpo;
18109 p_cpo = (char_u *)"";
18110
18111 ga_init2(&ga, 1, 200);
18112
18113 do_all = (flags[0] == 'g');
18114
18115 regmatch.rm_ic = p_ic;
18116 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18117 if (regmatch.regprog != NULL)
18118 {
18119 tail = str;
18120 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18121 {
18122 /*
18123 * Get some space for a temporary buffer to do the substitution
18124 * into. It will contain:
18125 * - The text up to where the match is.
18126 * - The substituted text.
18127 * - The text after the match.
18128 */
18129 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18130 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18131 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18132 {
18133 ga_clear(&ga);
18134 break;
18135 }
18136
18137 /* copy the text up to where the match is */
18138 i = (int)(regmatch.startp[0] - tail);
18139 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18140 /* add the substituted text */
18141 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18142 + ga.ga_len + i, TRUE, TRUE, FALSE);
18143 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 /* avoid getting stuck on a match with an empty string */
18145 if (tail == regmatch.endp[0])
18146 {
18147 if (*tail == NUL)
18148 break;
18149 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18150 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 }
18152 else
18153 {
18154 tail = regmatch.endp[0];
18155 if (*tail == NUL)
18156 break;
18157 }
18158 if (!do_all)
18159 break;
18160 }
18161
18162 if (ga.ga_data != NULL)
18163 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18164
18165 vim_free(regmatch.regprog);
18166 }
18167
18168 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18169 ga_clear(&ga);
18170 p_cpo = save_cpo;
18171
18172 return ret;
18173}
18174
18175#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */