blob: 1051beb6098dcd4aa90cfc7981b54cb3db731065 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000110
111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000126 * Array to hold the hashtab with variables local to each sourced script.
127 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000129typedef struct
130{
131 dictitem_T sv_var;
132 dict_T sv_dict;
133} scriptvar_T;
134
135static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
136#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
137#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139static int echo_attr = 0; /* attributes used for ":echo" */
140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000141/* Values for trans_function_name() argument: */
142#define TFN_INT 1 /* internal function name OK */
143#define TFN_QUIET 2 /* no error messages */
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
146 * Structure to hold info for a user function.
147 */
148typedef struct ufunc ufunc_T;
149
150struct ufunc
151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000152 int uf_varargs; /* variable nr of arguments */
153 int uf_flags;
154 int uf_calls; /* nr of active calls */
155 garray_T uf_args; /* arguments */
156 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000157#ifdef FEAT_PROFILE
158 int uf_profiling; /* TRUE when func is being profiled */
159 /* profiling the function as a whole */
160 int uf_tm_count; /* nr of calls */
161 proftime_T uf_tm_total; /* time spend in function + children */
162 proftime_T uf_tm_self; /* time spend in function itself */
163 proftime_T uf_tm_start; /* time at function call */
164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
167 proftime_T *uf_tml_total; /* time spend in a line + children */
168 proftime_T *uf_tml_self; /* time spend in a line itself */
169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000191static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
267#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
272 int vv_len; /* length of name */
273 dictitem_T vv_di; /* value and name for key */
274 char vv_filler[16]; /* space for LONGEST name below!!! */
275 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
276} vimvars[VV_LEN] =
277{
278 /*
279 * The order here must match the VV_ defines in vim.h!
280 * Initializing a union does not work, leave tv.vval empty to get zero's.
281 */
282 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
283 {VV_NAME("count1", VAR_NUMBER), VV_RO},
284 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
285 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
286 {VV_NAME("warningmsg", VAR_STRING), 0},
287 {VV_NAME("statusmsg", VAR_STRING), 0},
288 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
289 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
290 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
291 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
292 {VV_NAME("termresponse", VAR_STRING), VV_RO},
293 {VV_NAME("fname", VAR_STRING), VV_RO},
294 {VV_NAME("lang", VAR_STRING), VV_RO},
295 {VV_NAME("lc_time", VAR_STRING), VV_RO},
296 {VV_NAME("ctype", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
298 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
299 {VV_NAME("fname_in", VAR_STRING), VV_RO},
300 {VV_NAME("fname_out", VAR_STRING), VV_RO},
301 {VV_NAME("fname_new", VAR_STRING), VV_RO},
302 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
303 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
304 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
307 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
308 {VV_NAME("progname", VAR_STRING), VV_RO},
309 {VV_NAME("servername", VAR_STRING), VV_RO},
310 {VV_NAME("dying", VAR_NUMBER), VV_RO},
311 {VV_NAME("exception", VAR_STRING), VV_RO},
312 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
313 {VV_NAME("register", VAR_STRING), VV_RO},
314 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
315 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000316 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
317 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000318 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000319 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
320 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000321 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
322 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
323 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
324 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
325 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000326};
327
328/* shorthand */
329#define vv_type vv_di.di_tv.v_type
330#define vv_nr vv_di.di_tv.vval.v_number
331#define vv_str vv_di.di_tv.vval.v_string
332#define vv_tv vv_di.di_tv
333
334/*
335 * The v: variables are stored in dictionary "vimvardict".
336 * "vimvars_var" is the variable that is used for the "l:" scope.
337 */
338static dict_T vimvardict;
339static dictitem_T vimvars_var;
340#define vimvarht vimvardict.dv_hashtab
341
342static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
343static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
344static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
345static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
346static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
347static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
348static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
349static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000350static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000351static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
352static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
353static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
354static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
355static list_T *list_alloc __ARGS((void));
356static void list_unref __ARGS((list_T *l));
357static void list_free __ARGS((list_T *l));
358static listitem_T *listitem_alloc __ARGS((void));
359static void listitem_free __ARGS((listitem_T *item));
360static void listitem_remove __ARGS((list_T *l, listitem_T *item));
361static long list_len __ARGS((list_T *l));
362static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
363static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
364static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
365static int string_isa_number __ARGS((char_u *s));
366static listitem_T *list_find __ARGS((list_T *l, long n));
367static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000368static void list_append __ARGS((list_T *l, listitem_T *item));
369static int list_append_tv __ARGS((list_T *l, typval_T *tv));
370static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
371static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
372static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000373static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000374static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
375static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000376static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar33570922005-01-25 22:26:29 +0000378static void dict_unref __ARGS((dict_T *d));
379static void dict_free __ARGS((dict_T *d));
380static dictitem_T *dictitem_alloc __ARGS((char_u *key));
381static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
382static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
383static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000384static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int dict_add __ARGS((dict_T *d, dictitem_T *item));
386static long dict_len __ARGS((dict_T *d));
387static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
388static char_u *dict2string __ARGS((typval_T *tv));
389static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390
391static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
392static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
393static char_u *string_quote __ARGS((char_u *str, int function));
394static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int find_internal_func __ARGS((char_u *name));
396static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
397static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
398static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000399static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400
401static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
425static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000464static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000492static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000509static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513#ifdef vim_mkdir
514static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000521static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000539static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
545#ifdef HAVE_STRFTIME
546static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
547#endif
548static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000560static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577
578static win_T *find_win_by_nr __ARGS((typval_T *vp));
579static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
580static int get_env_len __ARGS((char_u **arg));
581static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000583static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
584#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
585#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
586 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000588static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000589static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
590static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static typval_T *alloc_tv __ARGS((void));
592static typval_T *alloc_string_tv __ARGS((char_u *string));
593static void free_tv __ARGS((typval_T *varp));
594static void clear_tv __ARGS((typval_T *varp));
595static void init_tv __ARGS((typval_T *varp));
596static long get_tv_number __ARGS((typval_T *varp));
597static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
598static char_u *get_tv_string __ARGS((typval_T *varp));
599static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
600static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000601static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
603static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
604static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
605static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
606static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
607static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
608static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000609static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000611static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
613static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
614static int eval_fname_script __ARGS((char_u *p));
615static int eval_fname_sid __ARGS((char_u *p));
616static void list_func_head __ARGS((ufunc_T *fp, int indent));
617static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
618static ufunc_T *find_func __ARGS((char_u *name));
619static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000620static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000621#ifdef FEAT_PROFILE
622static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000623static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
624static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
625static int
626# ifdef __BORLANDC__
627 _RTLENTRYF
628# endif
629 prof_total_cmp __ARGS((const void *s1, const void *s2));
630static int
631# ifdef __BORLANDC__
632 _RTLENTRYF
633# endif
634 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000635#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636static int script_autoload __ARGS((char_u *name));
637static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void func_free __ARGS((ufunc_T *fp));
639static void func_unref __ARGS((char_u *name));
640static void func_ref __ARGS((char_u *name));
641static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
642static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
643
644static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
645
646static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
647static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
648static char_u *skip_var_one __ARGS((char_u *arg));
649static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
650static void list_glob_vars __ARGS((void));
651static void list_buf_vars __ARGS((void));
652static void list_win_vars __ARGS((void));
653static void list_vim_vars __ARGS((void));
654static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
655static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
656static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000657static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void clear_lval __ARGS((lval_T *lp));
659static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
660static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
661static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
662static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
663static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000664static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000666static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
667static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000668static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000670/* Character used as separated in autoload function/variable names. */
671#define AUTOLOAD_CHAR '#'
672
Bram Moolenaar33570922005-01-25 22:26:29 +0000673/*
674 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000675 */
676 void
677eval_init()
678{
Bram Moolenaar33570922005-01-25 22:26:29 +0000679 int i;
680 struct vimvar *p;
681
682 init_var_dict(&globvardict, &globvars_var);
683 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000684 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000685 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000686
687 for (i = 0; i < VV_LEN; ++i)
688 {
689 p = &vimvars[i];
690 STRCPY(p->vv_di.di_key, p->vv_name);
691 if (p->vv_flags & VV_RO)
692 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
693 else if (p->vv_flags & VV_RO_SBX)
694 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
695 else
696 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000697
698 /* add to v: scope dict, unless the value is not always available */
699 if (p->vv_type != VAR_UNKNOWN)
700 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000701 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000702 /* add to compat scope dict */
703 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000704 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000705}
706
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 * Return the name of the executed function.
709 */
710 char_u *
711func_name(cookie)
712 void *cookie;
713{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000714 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715}
716
717/*
718 * Return the address holding the next breakpoint line for a funccall cookie.
719 */
720 linenr_T *
721func_breakpoint(cookie)
722 void *cookie;
723{
Bram Moolenaar33570922005-01-25 22:26:29 +0000724 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725}
726
727/*
728 * Return the address holding the debug tick for a funccall cookie.
729 */
730 int *
731func_dbg_tick(cookie)
732 void *cookie;
733{
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735}
736
737/*
738 * Return the nesting level for a funccall cookie.
739 */
740 int
741func_level(cookie)
742 void *cookie;
743{
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746
747/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000748funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750/*
751 * Return TRUE when a function was ended by a ":return" command.
752 */
753 int
754current_func_returned()
755{
756 return current_funccal->returned;
757}
758
759
760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * Set an internal variable to a string value. Creates the variable if it does
762 * not already exist.
763 */
764 void
765set_internal_string_var(name, value)
766 char_u *name;
767 char_u *value;
768{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000769 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000770 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 val = vim_strsave(value);
773 if (val != NULL)
774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000775 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000776 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000778 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000779 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 }
782}
783
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static lval_T *redir_lval = NULL;
785static char_u *redir_endp = NULL;
786static char_u *redir_varname = NULL;
787
788/*
789 * Start recording command output to a variable
790 * Returns OK if successfully completed the setup. FAIL otherwise.
791 */
792 int
793var_redir_start(name, append)
794 char_u *name;
795 int append; /* append to an existing variable */
796{
797 int save_emsg;
798 int err;
799 typval_T tv;
800
801 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000802 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000803 {
804 EMSG(_(e_invarg));
805 return FAIL;
806 }
807
808 redir_varname = vim_strsave(name);
809 if (redir_varname == NULL)
810 return FAIL;
811
812 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
813 if (redir_lval == NULL)
814 {
815 var_redir_stop();
816 return FAIL;
817 }
818
819 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000820 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
821 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000822 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
823 {
824 if (redir_endp != NULL && *redir_endp != NUL)
825 /* Trailing characters are present after the variable name */
826 EMSG(_(e_trailing));
827 else
828 EMSG(_(e_invarg));
829 var_redir_stop();
830 return FAIL;
831 }
832
833 /* check if we can write to the variable: set it to or append an empty
834 * string */
835 save_emsg = did_emsg;
836 did_emsg = FALSE;
837 tv.v_type = VAR_STRING;
838 tv.vval.v_string = (char_u *)"";
839 if (append)
840 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
841 else
842 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
843 err = did_emsg;
844 did_emsg += save_emsg;
845 if (err)
846 {
847 var_redir_stop();
848 return FAIL;
849 }
850 if (redir_lval->ll_newkey != NULL)
851 {
852 /* Dictionary item was created, don't do it again. */
853 vim_free(redir_lval->ll_newkey);
854 redir_lval->ll_newkey = NULL;
855 }
856
857 return OK;
858}
859
860/*
861 * Append "value[len]" to the variable set by var_redir_start().
862 */
863 void
864var_redir_str(value, len)
865 char_u *value;
866 int len;
867{
868 char_u *val;
869 typval_T tv;
870 int save_emsg;
871 int err;
872
873 if (redir_lval == NULL)
874 return;
875
876 if (len == -1)
877 /* Append the entire string */
878 val = vim_strsave(value);
879 else
880 /* Append only the specified number of characters */
881 val = vim_strnsave(value, len);
882 if (val == NULL)
883 return;
884
885 tv.v_type = VAR_STRING;
886 tv.vval.v_string = val;
887
888 save_emsg = did_emsg;
889 did_emsg = FALSE;
890 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
891 err = did_emsg;
892 did_emsg += save_emsg;
893 if (err)
894 var_redir_stop();
895
896 vim_free(tv.vval.v_string);
897}
898
899/*
900 * Stop redirecting command output to a variable.
901 */
902 void
903var_redir_stop()
904{
905 if (redir_lval != NULL)
906 {
907 clear_lval(redir_lval);
908 vim_free(redir_lval);
909 redir_lval = NULL;
910 }
911 vim_free(redir_varname);
912 redir_varname = NULL;
913}
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915# if defined(FEAT_MBYTE) || defined(PROTO)
916 int
917eval_charconvert(enc_from, enc_to, fname_from, fname_to)
918 char_u *enc_from;
919 char_u *enc_to;
920 char_u *fname_from;
921 char_u *fname_to;
922{
923 int err = FALSE;
924
925 set_vim_var_string(VV_CC_FROM, enc_from, -1);
926 set_vim_var_string(VV_CC_TO, enc_to, -1);
927 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
928 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
929 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
930 err = TRUE;
931 set_vim_var_string(VV_CC_FROM, NULL, -1);
932 set_vim_var_string(VV_CC_TO, NULL, -1);
933 set_vim_var_string(VV_FNAME_IN, NULL, -1);
934 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
935
936 if (err)
937 return FAIL;
938 return OK;
939}
940# endif
941
942# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
943 int
944eval_printexpr(fname, args)
945 char_u *fname;
946 char_u *args;
947{
948 int err = FALSE;
949
950 set_vim_var_string(VV_FNAME_IN, fname, -1);
951 set_vim_var_string(VV_CMDARG, args, -1);
952 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
953 err = TRUE;
954 set_vim_var_string(VV_FNAME_IN, NULL, -1);
955 set_vim_var_string(VV_CMDARG, NULL, -1);
956
957 if (err)
958 {
959 mch_remove(fname);
960 return FAIL;
961 }
962 return OK;
963}
964# endif
965
966# if defined(FEAT_DIFF) || defined(PROTO)
967 void
968eval_diff(origfile, newfile, outfile)
969 char_u *origfile;
970 char_u *newfile;
971 char_u *outfile;
972{
973 int err = FALSE;
974
975 set_vim_var_string(VV_FNAME_IN, origfile, -1);
976 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
977 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
978 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
979 set_vim_var_string(VV_FNAME_IN, NULL, -1);
980 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
981 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
982}
983
984 void
985eval_patch(origfile, difffile, outfile)
986 char_u *origfile;
987 char_u *difffile;
988 char_u *outfile;
989{
990 int err;
991
992 set_vim_var_string(VV_FNAME_IN, origfile, -1);
993 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
994 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
995 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
996 set_vim_var_string(VV_FNAME_IN, NULL, -1);
997 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
998 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
999}
1000# endif
1001
1002/*
1003 * Top level evaluation function, returning a boolean.
1004 * Sets "error" to TRUE if there was an error.
1005 * Return TRUE or FALSE.
1006 */
1007 int
1008eval_to_bool(arg, error, nextcmd, skip)
1009 char_u *arg;
1010 int *error;
1011 char_u **nextcmd;
1012 int skip; /* only parse, don't execute */
1013{
Bram Moolenaar33570922005-01-25 22:26:29 +00001014 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 int retval = FALSE;
1016
1017 if (skip)
1018 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 else
1022 {
1023 *error = FALSE;
1024 if (!skip)
1025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 retval = (get_tv_number(&tv) != 0);
1027 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 }
1030 if (skip)
1031 --emsg_skip;
1032
1033 return retval;
1034}
1035
1036/*
1037 * Top level evaluation function, returning a string. If "skip" is TRUE,
1038 * only parsing to "nextcmd" is done, without reporting errors. Return
1039 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1040 */
1041 char_u *
1042eval_to_string_skip(arg, nextcmd, skip)
1043 char_u *arg;
1044 char_u **nextcmd;
1045 int skip; /* only parse, don't execute */
1046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 char_u *retval;
1049
1050 if (skip)
1051 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001052 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 retval = NULL;
1054 else
1055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 retval = vim_strsave(get_tv_string(&tv));
1057 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 if (skip)
1060 --emsg_skip;
1061
1062 return retval;
1063}
1064
1065/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001066 * Skip over an expression at "*pp".
1067 * Return FAIL for an error, OK otherwise.
1068 */
1069 int
1070skip_expr(pp)
1071 char_u **pp;
1072{
Bram Moolenaar33570922005-01-25 22:26:29 +00001073 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001074
1075 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001077}
1078
1079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * Top level evaluation function, returning a string.
1081 * Return pointer to allocated memory, or NULL for failure.
1082 */
1083 char_u *
1084eval_to_string(arg, nextcmd)
1085 char_u *arg;
1086 char_u **nextcmd;
1087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 char_u *retval;
1090
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 retval = NULL;
1093 else
1094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 retval = vim_strsave(get_tv_string(&tv));
1096 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098
1099 return retval;
1100}
1101
1102/*
1103 * Call eval_to_string() with "sandbox" set and not using local variables.
1104 */
1105 char_u *
1106eval_to_string_safe(arg, nextcmd)
1107 char_u *arg;
1108 char_u **nextcmd;
1109{
1110 char_u *retval;
1111 void *save_funccalp;
1112
1113 save_funccalp = save_funccal();
1114 ++sandbox;
1115 retval = eval_to_string(arg, nextcmd);
1116 --sandbox;
1117 restore_funccal(save_funccalp);
1118 return retval;
1119}
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121/*
1122 * Top level evaluation function, returning a number.
1123 * Evaluates "expr" silently.
1124 * Returns -1 for an error.
1125 */
1126 int
1127eval_to_number(expr)
1128 char_u *expr;
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001132 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 ++emsg_off;
1135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 retval = -1;
1138 else
1139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 retval = get_tv_number(&rettv);
1141 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 --emsg_off;
1144
1145 return retval;
1146}
1147
1148#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1149/*
1150 * Call some vimL function and return the result as a string
1151 * Uses argv[argc] for the function arguments.
1152 */
1153 char_u *
1154call_vim_function(func, argc, argv, safe)
1155 char_u *func;
1156 int argc;
1157 char_u **argv;
1158 int safe; /* use the sandbox */
1159{
1160 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T rettv;
1162 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 long n;
1164 int len;
1165 int i;
1166 int doesrange;
1167 void *save_funccalp = NULL;
1168
Bram Moolenaar33570922005-01-25 22:26:29 +00001169 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (argvars == NULL)
1171 return NULL;
1172
1173 for (i = 0; i < argc; i++)
1174 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001175 /* Pass a NULL or empty argument as an empty string */
1176 if (argv[i] == NULL || *argv[i] == NUL)
1177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001178 argvars[i].v_type = VAR_STRING;
1179 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001180 continue;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Recognize a number argument, the others must be strings. */
1184 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1185 if (len != 0 && len == (int)STRLEN(argv[i]))
1186 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001187 argvars[i].v_type = VAR_NUMBER;
1188 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else
1191 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001192 argvars[i].v_type = VAR_STRING;
1193 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 }
1196
1197 if (safe)
1198 {
1199 save_funccalp = save_funccal();
1200 ++sandbox;
1201 }
1202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1204 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001206 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 vim_free(argvars);
1211
1212 if (safe)
1213 {
1214 --sandbox;
1215 restore_funccal(save_funccalp);
1216 }
1217 return retval;
1218}
1219#endif
1220
1221/*
1222 * Save the current function call pointer, and set it to NULL.
1223 * Used when executing autocommands and for ":source".
1224 */
1225 void *
1226save_funccal()
1227{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001228 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 current_funccal = NULL;
1231 return (void *)fc;
1232}
1233
1234 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001235restore_funccal(vfc)
1236 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001238 funccall_T *fc = (funccall_T *)vfc;
1239
1240 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241}
1242
Bram Moolenaar05159a02005-02-26 23:04:13 +00001243#if defined(FEAT_PROFILE) || defined(PROTO)
1244/*
1245 * Prepare profiling for entering a child or something else that is not
1246 * counted for the script/function itself.
1247 * Should always be called in pair with prof_child_exit().
1248 */
1249 void
1250prof_child_enter(tm)
1251 proftime_T *tm; /* place to store waittime */
1252{
1253 funccall_T *fc = current_funccal;
1254
1255 if (fc != NULL && fc->func->uf_profiling)
1256 profile_start(&fc->prof_child);
1257 script_prof_save(tm);
1258}
1259
1260/*
1261 * Take care of time spent in a child.
1262 * Should always be called after prof_child_enter().
1263 */
1264 void
1265prof_child_exit(tm)
1266 proftime_T *tm; /* where waittime was stored */
1267{
1268 funccall_T *fc = current_funccal;
1269
1270 if (fc != NULL && fc->func->uf_profiling)
1271 {
1272 profile_end(&fc->prof_child);
1273 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1274 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1275 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1276 }
1277 script_prof_restore(tm);
1278}
1279#endif
1280
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#ifdef FEAT_FOLDING
1283/*
1284 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1285 * it in "*cp". Doesn't give error messages.
1286 */
1287 int
1288eval_foldexpr(arg, cp)
1289 char_u *arg;
1290 int *cp;
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int retval;
1294 char_u *s;
1295
1296 ++emsg_off;
1297 ++sandbox;
1298 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 retval = 0;
1301 else
1302 {
1303 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (tv.v_type == VAR_NUMBER)
1305 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001306 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = 0;
1308 else
1309 {
1310 /* If the result is a string, check if there is a non-digit before
1311 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (!VIM_ISDIGIT(*s) && *s != '-')
1314 *cp = *s++;
1315 retval = atol((char *)s);
1316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 --emsg_off;
1320 --sandbox;
1321
1322 return retval;
1323}
1324#endif
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 * ":let" list all variable values
1328 * ":let var1 var2" list variable values
1329 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 * ":let var += expr" assignment command.
1331 * ":let var -= expr" assignment command.
1332 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 void
1336ex_let(eap)
1337 exarg_T *eap;
1338{
1339 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 int var_count = 0;
1344 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001345 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001347 expr = skip_var_list(arg, &var_count, &semicolon);
1348 if (expr == NULL)
1349 return;
1350 expr = vim_strchr(expr, '=');
1351 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001353 /*
1354 * ":let" without "=": list variables
1355 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356 if (*arg == '[')
1357 EMSG(_(e_invarg));
1358 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 /* ":let var1 var2" */
1360 arg = list_arg_vars(eap, arg);
1361 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001364 list_glob_vars();
1365 list_buf_vars();
1366 list_win_vars();
1367 list_vim_vars();
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 eap->nextcmd = check_nextcmd(arg);
1370 }
1371 else
1372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 op[0] = '=';
1374 op[1] = NUL;
1375 if (expr > arg)
1376 {
1377 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1378 op[0] = expr[-1]; /* +=, -= or .= */
1379 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001380 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (eap->skip)
1383 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001384 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (eap->skip)
1386 {
1387 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 --emsg_skip;
1390 }
1391 else if (i != FAIL)
1392 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001393 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 }
1398}
1399
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001400/*
1401 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1402 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 * When "nextchars" is not NULL it points to a string with characters that
1404 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1405 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001406 * Returns OK or FAIL;
1407 */
1408 static int
1409ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1410 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412 int copy; /* copy values from "tv", don't move */
1413 int semicolon; /* from skip_var_list() */
1414 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416{
1417 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 listitem_T *item;
1421 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422
1423 if (*arg != '[')
1424 {
1425 /*
1426 * ":let var = expr" or ":for var in list"
1427 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001428 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 return FAIL;
1430 return OK;
1431 }
1432
1433 /*
1434 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1435 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001436 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 {
1438 EMSG(_(e_listreq));
1439 return FAIL;
1440 }
1441
1442 i = list_len(l);
1443 if (semicolon == 0 && var_count < i)
1444 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001445 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001446 return FAIL;
1447 }
1448 if (var_count - semicolon > i)
1449 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001450 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001451 return FAIL;
1452 }
1453
1454 item = l->lv_first;
1455 while (*arg != ']')
1456 {
1457 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001459 item = item->li_next;
1460 if (arg == NULL)
1461 return FAIL;
1462
1463 arg = skipwhite(arg);
1464 if (*arg == ';')
1465 {
1466 /* Put the rest of the list (may be empty) in the var after ';'.
1467 * Create a new list for this. */
1468 l = list_alloc();
1469 if (l == NULL)
1470 return FAIL;
1471 while (item != NULL)
1472 {
1473 list_append_tv(l, &item->li_tv);
1474 item = item->li_next;
1475 }
1476
1477 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001478 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 ltv.vval.v_list = l;
1480 l->lv_refcount = 1;
1481
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001482 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1483 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 clear_tv(&ltv);
1485 if (arg == NULL)
1486 return FAIL;
1487 break;
1488 }
1489 else if (*arg != ',' && *arg != ']')
1490 {
1491 EMSG2(_(e_intern2), "ex_let_vars()");
1492 return FAIL;
1493 }
1494 }
1495
1496 return OK;
1497}
1498
1499/*
1500 * Skip over assignable variable "var" or list of variables "[var, var]".
1501 * Used for ":let varvar = expr" and ":for varvar in expr".
1502 * For "[var, var]" increment "*var_count" for each variable.
1503 * for "[var, var; var]" set "semicolon".
1504 * Return NULL for an error.
1505 */
1506 static char_u *
1507skip_var_list(arg, var_count, semicolon)
1508 char_u *arg;
1509 int *var_count;
1510 int *semicolon;
1511{
1512 char_u *p, *s;
1513
1514 if (*arg == '[')
1515 {
1516 /* "[var, var]": find the matching ']'. */
1517 p = arg;
1518 while (1)
1519 {
1520 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1521 s = skip_var_one(p);
1522 if (s == p)
1523 {
1524 EMSG2(_(e_invarg2), p);
1525 return NULL;
1526 }
1527 ++*var_count;
1528
1529 p = skipwhite(s);
1530 if (*p == ']')
1531 break;
1532 else if (*p == ';')
1533 {
1534 if (*semicolon == 1)
1535 {
1536 EMSG(_("Double ; in list of variables"));
1537 return NULL;
1538 }
1539 *semicolon = 1;
1540 }
1541 else if (*p != ',')
1542 {
1543 EMSG2(_(e_invarg2), p);
1544 return NULL;
1545 }
1546 }
1547 return p + 1;
1548 }
1549 else
1550 return skip_var_one(arg);
1551}
1552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001553/*
1554 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1555 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 static char_u *
1557skip_var_one(arg)
1558 char_u *arg;
1559{
1560 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1561 ++arg;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001562 return find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563}
1564
Bram Moolenaara7043832005-01-21 11:56:39 +00001565/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 * List variables for hashtab "ht" with prefix "prefix".
1567 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001568 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001570list_hashtable_vars(ht, prefix, empty)
1571 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001572 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001573 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001574{
Bram Moolenaar33570922005-01-25 22:26:29 +00001575 hashitem_T *hi;
1576 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 int todo;
1578
1579 todo = ht->ht_used;
1580 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1581 {
1582 if (!HASHITEM_EMPTY(hi))
1583 {
1584 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001585 di = HI2DI(hi);
1586 if (empty || di->di_tv.v_type != VAR_STRING
1587 || di->di_tv.vval.v_string != NULL)
1588 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001589 }
1590 }
1591}
1592
1593/*
1594 * List global variables.
1595 */
1596 static void
1597list_glob_vars()
1598{
Bram Moolenaar33570922005-01-25 22:26:29 +00001599 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001600}
1601
1602/*
1603 * List buffer variables.
1604 */
1605 static void
1606list_buf_vars()
1607{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001608 char_u numbuf[NUMBUFLEN];
1609
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001611
1612 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1613 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001614}
1615
1616/*
1617 * List window variables.
1618 */
1619 static void
1620list_win_vars()
1621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001623}
1624
1625/*
1626 * List Vim variables.
1627 */
1628 static void
1629list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632}
1633
1634/*
1635 * List variables in "arg".
1636 */
1637 static char_u *
1638list_arg_vars(eap, arg)
1639 exarg_T *eap;
1640 char_u *arg;
1641{
1642 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001643 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001645 char_u *name_start;
1646 char_u *arg_subsc;
1647 char_u *tofree;
1648 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649
1650 while (!ends_excmd(*arg) && !got_int)
1651 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001652 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001654 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1656 {
1657 emsg_severe = TRUE;
1658 EMSG(_(e_trailing));
1659 break;
1660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664 /* get_name_len() takes care of expanding curly braces */
1665 name_start = name = arg;
1666 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1667 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001669 /* This is mainly to keep test 49 working: when expanding
1670 * curly braces fails overrule the exception error message. */
1671 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001673 emsg_severe = TRUE;
1674 EMSG2(_(e_invarg2), arg);
1675 break;
1676 }
1677 error = TRUE;
1678 }
1679 else
1680 {
1681 if (tofree != NULL)
1682 name = tofree;
1683 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 else
1686 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001687 /* handle d.key, l[idx], f(expr) */
1688 arg_subsc = arg;
1689 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001690 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001692 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001693 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001695 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001696 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001697 case 'g': list_glob_vars(); break;
1698 case 'b': list_buf_vars(); break;
1699 case 'w': list_win_vars(); break;
1700 case 'v': list_vim_vars(); break;
1701 default:
1702 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001703 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001704 }
1705 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001706 {
1707 char_u numbuf[NUMBUFLEN];
1708 char_u *tf;
1709 int c;
1710 char_u *s;
1711
1712 s = echo_string(&tv, &tf, numbuf);
1713 c = *arg;
1714 *arg = NUL;
1715 list_one_var_a((char_u *)"",
1716 arg == arg_subsc ? name : name_start,
1717 tv.v_type, s == NULL ? (char_u *)"" : s);
1718 *arg = c;
1719 vim_free(tf);
1720 }
1721 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 }
1724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001725
1726 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001728
1729 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 }
1731
1732 return arg;
1733}
1734
1735/*
1736 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1737 * Returns a pointer to the char just after the var name.
1738 * Returns NULL if there is an error.
1739 */
1740 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001741ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001743 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747{
1748 int c1;
1749 char_u *name;
1750 char_u *p;
1751 char_u *arg_end = NULL;
1752 int len;
1753 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001754 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755
1756 /*
1757 * ":let $VAR = expr": Set environment variable.
1758 */
1759 if (*arg == '$')
1760 {
1761 /* Find the end of the name. */
1762 ++arg;
1763 name = arg;
1764 len = get_env_len(&arg);
1765 if (len == 0)
1766 EMSG2(_(e_invarg2), name - 1);
1767 else
1768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 if (op != NULL && (*op == '+' || *op == '-'))
1770 EMSG2(_(e_letwrong), op);
1771 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001773 EMSG(_(e_letunexp));
1774 else
1775 {
1776 c1 = name[len];
1777 name[len] = NUL;
1778 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 if (op != NULL && *op == '.')
1780 {
1781 int mustfree = FALSE;
1782 char_u *s = vim_getenv(name, &mustfree);
1783
1784 if (s != NULL)
1785 {
1786 p = tofree = concat_str(s, p);
1787 if (mustfree)
1788 vim_free(s);
1789 }
1790 }
1791 if (p != NULL)
1792 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (STRICMP(name, "HOME") == 0)
1794 init_homedir();
1795 else if (didset_vim && STRICMP(name, "VIM") == 0)
1796 didset_vim = FALSE;
1797 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1798 didset_vimruntime = FALSE;
1799 name[len] = c1;
1800 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 }
1803 }
1804 }
1805
1806 /*
1807 * ":let &option = expr": Set option value.
1808 * ":let &l:option = expr": Set local option value.
1809 * ":let &g:option = expr": Set global option value.
1810 */
1811 else if (*arg == '&')
1812 {
1813 /* Find the end of the name. */
1814 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 if (p == NULL || (endchars != NULL
1816 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 EMSG(_(e_letunexp));
1818 else
1819 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 long n;
1821 int opt_type;
1822 long numval;
1823 char_u *stringval = NULL;
1824 char_u *s;
1825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 c1 = *p;
1827 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828
1829 n = get_tv_number(tv);
1830 s = get_tv_string(tv);
1831 if (op != NULL && *op != '=')
1832 {
1833 opt_type = get_option_value(arg, &numval,
1834 &stringval, opt_flags);
1835 if ((opt_type == 1 && *op == '.')
1836 || (opt_type == 0 && *op != '.'))
1837 EMSG2(_(e_letwrong), op);
1838 else
1839 {
1840 if (opt_type == 1) /* number */
1841 {
1842 if (*op == '+')
1843 n = numval + n;
1844 else
1845 n = numval - n;
1846 }
1847 else if (opt_type == 0 && stringval != NULL) /* string */
1848 {
1849 s = concat_str(stringval, s);
1850 vim_free(stringval);
1851 stringval = s;
1852 }
1853 }
1854 }
1855 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 *p = c1;
1857 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001858 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 }
1860 }
1861
1862 /*
1863 * ":let @r = expr": Set register contents.
1864 */
1865 else if (*arg == '@')
1866 {
1867 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 if (op != NULL && (*op == '+' || *op == '-'))
1869 EMSG2(_(e_letwrong), op);
1870 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 EMSG(_(e_letunexp));
1873 else
1874 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 char_u *tofree = NULL;
1876 char_u *s;
1877
1878 p = get_tv_string(tv);
1879 if (op != NULL && *op == '.')
1880 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00001881 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 if (s != NULL)
1883 {
1884 p = tofree = concat_str(s, p);
1885 vim_free(s);
1886 }
1887 }
1888 if (p != NULL)
1889 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 }
1893 }
1894
1895 /*
1896 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001899 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001901 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001903 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001904 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001906 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1907 EMSG(_(e_letunexp));
1908 else
1909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 arg_end = p;
1912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 }
1916
1917 else
1918 EMSG2(_(e_invarg2), arg);
1919
1920 return arg_end;
1921}
1922
1923/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001924 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1925 */
1926 static int
1927check_changedtick(arg)
1928 char_u *arg;
1929{
1930 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1931 {
1932 EMSG2(_(e_readonlyvar), arg);
1933 return TRUE;
1934 }
1935 return FALSE;
1936}
1937
1938/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 * Get an lval: variable, Dict item or List item that can be assigned a value
1940 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1941 * "name.key", "name.key[expr]" etc.
1942 * Indexing only works if "name" is an existing List or Dictionary.
1943 * "name" points to the start of the name.
1944 * If "rettv" is not NULL it points to the value to be assigned.
1945 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1946 * wrong; must end in space or cmd separator.
1947 *
1948 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001949 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951 */
1952 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001953get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 typval_T *rettv;
1956 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001957 int unlet;
1958 int skip;
1959 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001960 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 char_u *expr_start, *expr_end;
1964 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 dictitem_T *v;
1966 typval_T var1;
1967 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001968 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001971 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001972 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001974 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976
1977 if (skip)
1978 {
1979 /* When skipping just find the end of the name. */
1980 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001981 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001982 }
1983
1984 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001985 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001986 if (expr_start != NULL)
1987 {
1988 /* Don't expand the name when we already know there is an error. */
1989 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1990 && *p != '[' && *p != '.')
1991 {
1992 EMSG(_(e_trailing));
1993 return NULL;
1994 }
1995
1996 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1997 if (lp->ll_exp_name == NULL)
1998 {
1999 /* Report an invalid expression in braces, unless the
2000 * expression evaluation has been cancelled due to an
2001 * aborting error, an interrupt, or an exception. */
2002 if (!aborting() && !quiet)
2003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002004 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002005 EMSG2(_(e_invarg2), name);
2006 return NULL;
2007 }
2008 }
2009 lp->ll_name = lp->ll_exp_name;
2010 }
2011 else
2012 lp->ll_name = name;
2013
2014 /* Without [idx] or .key we are done. */
2015 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2016 return p;
2017
2018 cc = *p;
2019 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002020 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 if (v == NULL && !quiet)
2022 EMSG2(_(e_undefvar), lp->ll_name);
2023 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 if (v == NULL)
2025 return NULL;
2026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 /*
2028 * Loop until no more [idx] or .key is following.
2029 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2034 && !(lp->ll_tv->v_type == VAR_DICT
2035 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002037 if (!quiet)
2038 EMSG(_("E689: Can only index a List or Dictionary"));
2039 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002041 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 if (!quiet)
2044 EMSG(_("E708: [:] must come last"));
2045 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002047
Bram Moolenaar8c711452005-01-14 21:53:12 +00002048 len = -1;
2049 if (*p == '.')
2050 {
2051 key = p + 1;
2052 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2053 ;
2054 if (len == 0)
2055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002056 if (!quiet)
2057 EMSG(_(e_emptykey));
2058 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 }
2060 p = key + len;
2061 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002062 else
2063 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002064 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002065 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 if (*p == ':')
2067 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002068 else
2069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002070 empty1 = FALSE;
2071 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002073 }
2074
2075 /* Optionally get the second index [ :expr]. */
2076 if (*p == ':')
2077 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002079 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002081 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002082 if (!empty1)
2083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002084 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002085 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 if (rettv != NULL && (rettv->v_type != VAR_LIST
2087 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002089 if (!quiet)
2090 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 if (!empty1)
2092 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002094 }
2095 p = skipwhite(p + 1);
2096 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002098 else
2099 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002100 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 if (!empty1)
2104 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 }
2107 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002112
Bram Moolenaar8c711452005-01-14 21:53:12 +00002113 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002114 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002115 if (!quiet)
2116 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002117 if (!empty1)
2118 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002120 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002122 }
2123
2124 /* Skip to past ']'. */
2125 ++p;
2126 }
2127
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002129 {
2130 if (len == -1)
2131 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002132 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002133 key = get_tv_string(&var1);
2134 if (*key == NUL)
2135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 if (!quiet)
2137 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002140 }
2141 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 lp->ll_list = NULL;
2143 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002144 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002145 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002147 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002148 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002150 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 if (len == -1)
2153 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002155 }
2156 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002158 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002159 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002160 if (len == -1)
2161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002163 p = NULL;
2164 break;
2165 }
2166 if (len == -1)
2167 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002168 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002169 }
2170 else
2171 {
2172 /*
2173 * Get the number and item for the only or first index of the List.
2174 */
2175 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002177 else
2178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 clear_tv(&var1);
2181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002182 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 lp->ll_list = lp->ll_tv->vval.v_list;
2184 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2185 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002186 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (!quiet)
2188 EMSGN(_(e_listidx), lp->ll_n1);
2189 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002190 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002192 }
2193
2194 /*
2195 * May need to find the item or absolute index for the second
2196 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 * When no index given: "lp->ll_empty2" is TRUE.
2198 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002199 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002202 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 if (ni == NULL)
2208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (!quiet)
2210 EMSGN(_(e_listidx), lp->ll_n2);
2211 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002212 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002213 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002214 }
2215
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002216 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2217 if (lp->ll_n1 < 0)
2218 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2219 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002220 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 if (!quiet)
2222 EMSGN(_(e_listidx), lp->ll_n2);
2223 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002224 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002225 }
2226
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 }
2230
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 return p;
2232}
2233
2234/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002235 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 */
2237 static void
2238clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002239 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240{
2241 vim_free(lp->ll_exp_name);
2242 vim_free(lp->ll_newkey);
2243}
2244
2245/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002246 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 */
2250 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002254 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257{
2258 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002259 listitem_T *ni;
2260 listitem_T *ri;
2261 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262
2263 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 cc = *endp;
2268 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 if (op != NULL && *op != '=')
2270 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002271 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002273 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2275 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 {
2277 if (tv_op(&tv, rettv, op) == OK)
2278 set_var(lp->ll_name, &tv, FALSE);
2279 clear_tv(&tv);
2280 }
2281 }
2282 else
2283 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002287 else if (tv_check_lock(lp->ll_newkey == NULL
2288 ? lp->ll_tv->v_lock
2289 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2290 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 else if (lp->ll_range)
2292 {
2293 /*
2294 * Assign the List values to the list items.
2295 */
2296 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002297 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002298 if (op != NULL && *op != '=')
2299 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2300 else
2301 {
2302 clear_tv(&lp->ll_li->li_tv);
2303 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2304 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002305 ri = ri->li_next;
2306 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2307 break;
2308 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 /* Need to add an empty item. */
2311 ni = listitem_alloc();
2312 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 ri = NULL;
2315 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002318 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 ni->li_tv.vval.v_number = 0;
2320 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002321 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 lp->ll_li = lp->ll_li->li_next;
2323 ++lp->ll_n1;
2324 }
2325 if (ri != NULL)
2326 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002327 else if (lp->ll_empty2
2328 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 : lp->ll_n1 != lp->ll_n2)
2330 EMSG(_("E711: List value has not enough items"));
2331 }
2332 else
2333 {
2334 /*
2335 * Assign to a List or Dictionary item.
2336 */
2337 if (lp->ll_newkey != NULL)
2338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 if (op != NULL && *op != '=')
2340 {
2341 EMSG2(_(e_letwrong), op);
2342 return;
2343 }
2344
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002346 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 if (di == NULL)
2348 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002349 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2350 {
2351 vim_free(di);
2352 return;
2353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002355 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 else if (op != NULL && *op != '=')
2357 {
2358 tv_op(lp->ll_tv, rettv, op);
2359 return;
2360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 /*
2365 * Assign the value to the variable or list item.
2366 */
2367 if (copy)
2368 copy_tv(rettv, lp->ll_tv);
2369 else
2370 {
2371 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002372 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
2375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376}
2377
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2380 * Returns OK or FAIL.
2381 */
2382 static int
2383tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002384 typval_T *tv1;
2385 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 char_u *op;
2387{
2388 long n;
2389 char_u numbuf[NUMBUFLEN];
2390 char_u *s;
2391
2392 /* Can't do anything with a Funcref or a Dict on the right. */
2393 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2394 {
2395 switch (tv1->v_type)
2396 {
2397 case VAR_DICT:
2398 case VAR_FUNC:
2399 break;
2400
2401 case VAR_LIST:
2402 if (*op != '+' || tv2->v_type != VAR_LIST)
2403 break;
2404 /* List += List */
2405 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2406 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2407 return OK;
2408
2409 case VAR_NUMBER:
2410 case VAR_STRING:
2411 if (tv2->v_type == VAR_LIST)
2412 break;
2413 if (*op == '+' || *op == '-')
2414 {
2415 /* nr += nr or nr -= nr*/
2416 n = get_tv_number(tv1);
2417 if (*op == '+')
2418 n += get_tv_number(tv2);
2419 else
2420 n -= get_tv_number(tv2);
2421 clear_tv(tv1);
2422 tv1->v_type = VAR_NUMBER;
2423 tv1->vval.v_number = n;
2424 }
2425 else
2426 {
2427 /* str .= str */
2428 s = get_tv_string(tv1);
2429 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2430 clear_tv(tv1);
2431 tv1->v_type = VAR_STRING;
2432 tv1->vval.v_string = s;
2433 }
2434 return OK;
2435 }
2436 }
2437
2438 EMSG2(_(e_letwrong), op);
2439 return FAIL;
2440}
2441
2442/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443 * Add a watcher to a list.
2444 */
2445 static void
2446list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 list_T *l;
2448 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449{
2450 lw->lw_next = l->lv_watch;
2451 l->lv_watch = lw;
2452}
2453
2454/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002455 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002456 * No warning when it isn't found...
2457 */
2458 static void
2459list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002460 list_T *l;
2461 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002462{
Bram Moolenaar33570922005-01-25 22:26:29 +00002463 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464
2465 lwp = &l->lv_watch;
2466 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2467 {
2468 if (lw == lwrem)
2469 {
2470 *lwp = lw->lw_next;
2471 break;
2472 }
2473 lwp = &lw->lw_next;
2474 }
2475}
2476
2477/*
2478 * Just before removing an item from a list: advance watchers to the next
2479 * item.
2480 */
2481 static void
2482list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 list_T *l;
2484 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485{
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487
2488 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2489 if (lw->lw_item == item)
2490 lw->lw_item = item->li_next;
2491}
2492
2493/*
2494 * Evaluate the expression used in a ":for var in expr" command.
2495 * "arg" points to "var".
2496 * Set "*errp" to TRUE for an error, FALSE otherwise;
2497 * Return a pointer that holds the info. Null when there is an error.
2498 */
2499 void *
2500eval_for_line(arg, errp, nextcmdp, skip)
2501 char_u *arg;
2502 int *errp;
2503 char_u **nextcmdp;
2504 int skip;
2505{
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002507 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 typval_T tv;
2509 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002510
2511 *errp = TRUE; /* default: there is an error */
2512
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514 if (fi == NULL)
2515 return NULL;
2516
2517 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2518 if (expr == NULL)
2519 return fi;
2520
2521 expr = skipwhite(expr);
2522 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2523 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002524 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525 return fi;
2526 }
2527
2528 if (skip)
2529 ++emsg_skip;
2530 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2531 {
2532 *errp = FALSE;
2533 if (!skip)
2534 {
2535 l = tv.vval.v_list;
2536 if (tv.v_type != VAR_LIST || l == NULL)
2537 EMSG(_(e_listreq));
2538 else
2539 {
2540 fi->fi_list = l;
2541 list_add_watch(l, &fi->fi_lw);
2542 fi->fi_lw.lw_item = l->lv_first;
2543 }
2544 }
2545 }
2546 if (skip)
2547 --emsg_skip;
2548
2549 return fi;
2550}
2551
2552/*
2553 * Use the first item in a ":for" list. Advance to the next.
2554 * Assign the values to the variable (list). "arg" points to the first one.
2555 * Return TRUE when a valid item was found, FALSE when at end of list or
2556 * something wrong.
2557 */
2558 int
2559next_for_item(fi_void, arg)
2560 void *fi_void;
2561 char_u *arg;
2562{
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002564 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002566
2567 item = fi->fi_lw.lw_item;
2568 if (item == NULL)
2569 result = FALSE;
2570 else
2571 {
2572 fi->fi_lw.lw_item = item->li_next;
2573 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2574 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2575 }
2576 return result;
2577}
2578
2579/*
2580 * Free the structure used to store info used by ":for".
2581 */
2582 void
2583free_for_info(fi_void)
2584 void *fi_void;
2585{
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002587
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002588 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002589 list_rem_watch(fi->fi_list, &fi->fi_lw);
2590 vim_free(fi);
2591}
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2594
2595 void
2596set_context_for_expression(xp, arg, cmdidx)
2597 expand_T *xp;
2598 char_u *arg;
2599 cmdidx_T cmdidx;
2600{
2601 int got_eq = FALSE;
2602 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002603 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002605 if (cmdidx == CMD_let)
2606 {
2607 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002608 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002609 {
2610 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002611 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002612 {
2613 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002614 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002615 if (vim_iswhite(*p))
2616 break;
2617 }
2618 return;
2619 }
2620 }
2621 else
2622 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2623 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 while ((xp->xp_pattern = vim_strpbrk(arg,
2625 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2626 {
2627 c = *xp->xp_pattern;
2628 if (c == '&')
2629 {
2630 c = xp->xp_pattern[1];
2631 if (c == '&')
2632 {
2633 ++xp->xp_pattern;
2634 xp->xp_context = cmdidx != CMD_let || got_eq
2635 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2636 }
2637 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002640 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2641 xp->xp_pattern += 2;
2642
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645 else if (c == '$')
2646 {
2647 /* environment variable */
2648 xp->xp_context = EXPAND_ENV_VARS;
2649 }
2650 else if (c == '=')
2651 {
2652 got_eq = TRUE;
2653 xp->xp_context = EXPAND_EXPRESSION;
2654 }
2655 else if (c == '<'
2656 && xp->xp_context == EXPAND_FUNCTIONS
2657 && vim_strchr(xp->xp_pattern, '(') == NULL)
2658 {
2659 /* Function name can start with "<SNR>" */
2660 break;
2661 }
2662 else if (cmdidx != CMD_let || got_eq)
2663 {
2664 if (c == '"') /* string */
2665 {
2666 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2667 if (c == '\\' && xp->xp_pattern[1] != NUL)
2668 ++xp->xp_pattern;
2669 xp->xp_context = EXPAND_NOTHING;
2670 }
2671 else if (c == '\'') /* literal string */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2675 /* skip */ ;
2676 xp->xp_context = EXPAND_NOTHING;
2677 }
2678 else if (c == '|')
2679 {
2680 if (xp->xp_pattern[1] == '|')
2681 {
2682 ++xp->xp_pattern;
2683 xp->xp_context = EXPAND_EXPRESSION;
2684 }
2685 else
2686 xp->xp_context = EXPAND_COMMANDS;
2687 }
2688 else
2689 xp->xp_context = EXPAND_EXPRESSION;
2690 }
2691 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002692 /* Doesn't look like something valid, expand as an expression
2693 * anyway. */
2694 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 arg = xp->xp_pattern;
2696 if (*arg != NUL)
2697 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2698 /* skip */ ;
2699 }
2700 xp->xp_pattern = arg;
2701}
2702
2703#endif /* FEAT_CMDL_COMPL */
2704
2705/*
2706 * ":1,25call func(arg1, arg2)" function call.
2707 */
2708 void
2709ex_call(eap)
2710 exarg_T *eap;
2711{
2712 char_u *arg = eap->arg;
2713 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002717 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 linenr_T lnum;
2719 int doesrange;
2720 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002721 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002723 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2724 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 if (tofree == NULL)
2726 return;
2727
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002728 /* Increase refcount on dictionary, it could get deleted when evaluating
2729 * the arguments. */
2730 if (fudi.fd_dict != NULL)
2731 ++fudi.fd_dict->dv_refcount;
2732
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002733 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2734 len = STRLEN(tofree);
2735 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar532c7802005-01-27 14:44:31 +00002737 /* Skip white space to allow ":call func ()". Not good, but required for
2738 * backward compatibility. */
2739 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002740 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 if (*startarg != '(')
2743 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002744 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 goto end;
2746 }
2747
2748 /*
2749 * When skipping, evaluate the function once, to find the end of the
2750 * arguments.
2751 * When the function takes a range, this is discovered after the first
2752 * call, and the loop is broken.
2753 */
2754 if (eap->skip)
2755 {
2756 ++emsg_skip;
2757 lnum = eap->line2; /* do it once, also with an invalid range */
2758 }
2759 else
2760 lnum = eap->line1;
2761 for ( ; lnum <= eap->line2; ++lnum)
2762 {
2763 if (!eap->skip && eap->addr_count > 0)
2764 {
2765 curwin->w_cursor.lnum = lnum;
2766 curwin->w_cursor.col = 0;
2767 }
2768 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002770 eap->line1, eap->line2, &doesrange,
2771 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 failed = TRUE;
2774 break;
2775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (doesrange || eap->skip)
2778 break;
2779 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002780 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002781 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002782 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (aborting())
2784 break;
2785 }
2786 if (eap->skip)
2787 --emsg_skip;
2788
2789 if (!failed)
2790 {
2791 /* Check for trailing illegal characters and a following command. */
2792 if (!ends_excmd(*arg))
2793 {
2794 emsg_severe = TRUE;
2795 EMSG(_(e_trailing));
2796 }
2797 else
2798 eap->nextcmd = check_nextcmd(arg);
2799 }
2800
2801end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002802 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804}
2805
2806/*
2807 * ":unlet[!] var1 ... " command.
2808 */
2809 void
2810ex_unlet(eap)
2811 exarg_T *eap;
2812{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002813 ex_unletlock(eap, eap->arg, 0);
2814}
2815
2816/*
2817 * ":lockvar" and ":unlockvar" commands
2818 */
2819 void
2820ex_lockvar(eap)
2821 exarg_T *eap;
2822{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002824 int deep = 2;
2825
2826 if (eap->forceit)
2827 deep = -1;
2828 else if (vim_isdigit(*arg))
2829 {
2830 deep = getdigits(&arg);
2831 arg = skipwhite(arg);
2832 }
2833
2834 ex_unletlock(eap, arg, deep);
2835}
2836
2837/*
2838 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2839 */
2840 static void
2841ex_unletlock(eap, argstart, deep)
2842 exarg_T *eap;
2843 char_u *argstart;
2844 int deep;
2845{
2846 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002849 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 do
2852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002854 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
2855 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lv.ll_name == NULL)
2857 error = TRUE; /* error but continue parsing */
2858 if (name_end == NULL || (!vim_iswhite(*name_end)
2859 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 if (name_end != NULL)
2862 {
2863 emsg_severe = TRUE;
2864 EMSG(_(e_trailing));
2865 }
2866 if (!(eap->skip || error))
2867 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 break;
2869 }
2870
2871 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002872 {
2873 if (eap->cmdidx == CMD_unlet)
2874 {
2875 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2876 error = TRUE;
2877 }
2878 else
2879 {
2880 if (do_lock_var(&lv, name_end, deep,
2881 eap->cmdidx == CMD_lockvar) == FAIL)
2882 error = TRUE;
2883 }
2884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (!eap->skip)
2887 clear_lval(&lv);
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 arg = skipwhite(name_end);
2890 } while (!ends_excmd(*arg));
2891
2892 eap->nextcmd = check_nextcmd(arg);
2893}
2894
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 int forceit;
2900{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 int ret = OK;
2902 int cc;
2903
2904 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *name_end;
2907 *name_end = NUL;
2908
2909 /* Normal name or expanded name. */
2910 if (check_changedtick(lp->ll_name))
2911 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002915 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2917 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 else if (lp->ll_range)
2919 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921
2922 /* Delete a range of List items. */
2923 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2924 {
2925 li = lp->ll_li->li_next;
2926 listitem_remove(lp->ll_list, lp->ll_li);
2927 lp->ll_li = li;
2928 ++lp->ll_n1;
2929 }
2930 }
2931 else
2932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 /* unlet a List item. */
2935 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002938 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 }
2940
2941 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002942}
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944/*
2945 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 */
2948 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002949do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 hashtab_T *ht;
2954 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002955 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 ht = find_var_ht(name, &varname);
2958 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 hi = hash_find(ht, varname);
2961 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002962 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 if (var_check_ro(HI2DI(hi)->di_flags, name))
2964 return FAIL;
2965 delete_var(ht, hi);
2966 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 if (forceit)
2970 return OK;
2971 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 return FAIL;
2973}
2974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975/*
2976 * Lock or unlock variable indicated by "lp".
2977 * "deep" is the levels to go (-1 for unlimited);
2978 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2979 */
2980 static int
2981do_lock_var(lp, name_end, deep, lock)
2982 lval_T *lp;
2983 char_u *name_end;
2984 int deep;
2985 int lock;
2986{
2987 int ret = OK;
2988 int cc;
2989 dictitem_T *di;
2990
2991 if (deep == 0) /* nothing to do */
2992 return OK;
2993
2994 if (lp->ll_tv == NULL)
2995 {
2996 cc = *name_end;
2997 *name_end = NUL;
2998
2999 /* Normal name or expanded name. */
3000 if (check_changedtick(lp->ll_name))
3001 ret = FAIL;
3002 else
3003 {
3004 di = find_var(lp->ll_name, NULL);
3005 if (di == NULL)
3006 ret = FAIL;
3007 else
3008 {
3009 if (lock)
3010 di->di_flags |= DI_FLAGS_LOCK;
3011 else
3012 di->di_flags &= ~DI_FLAGS_LOCK;
3013 item_lock(&di->di_tv, deep, lock);
3014 }
3015 }
3016 *name_end = cc;
3017 }
3018 else if (lp->ll_range)
3019 {
3020 listitem_T *li = lp->ll_li;
3021
3022 /* (un)lock a range of List items. */
3023 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3024 {
3025 item_lock(&li->li_tv, deep, lock);
3026 li = li->li_next;
3027 ++lp->ll_n1;
3028 }
3029 }
3030 else if (lp->ll_list != NULL)
3031 /* (un)lock a List item. */
3032 item_lock(&lp->ll_li->li_tv, deep, lock);
3033 else
3034 /* un(lock) a Dictionary item. */
3035 item_lock(&lp->ll_di->di_tv, deep, lock);
3036
3037 return ret;
3038}
3039
3040/*
3041 * Lock or unlock an item. "deep" is nr of levels to go.
3042 */
3043 static void
3044item_lock(tv, deep, lock)
3045 typval_T *tv;
3046 int deep;
3047 int lock;
3048{
3049 static int recurse = 0;
3050 list_T *l;
3051 listitem_T *li;
3052 dict_T *d;
3053 hashitem_T *hi;
3054 int todo;
3055
3056 if (recurse >= DICT_MAXNEST)
3057 {
3058 EMSG(_("E743: variable nested too deep for (un)lock"));
3059 return;
3060 }
3061 if (deep == 0)
3062 return;
3063 ++recurse;
3064
3065 /* lock/unlock the item itself */
3066 if (lock)
3067 tv->v_lock |= VAR_LOCKED;
3068 else
3069 tv->v_lock &= ~VAR_LOCKED;
3070
3071 switch (tv->v_type)
3072 {
3073 case VAR_LIST:
3074 if ((l = tv->vval.v_list) != NULL)
3075 {
3076 if (lock)
3077 l->lv_lock |= VAR_LOCKED;
3078 else
3079 l->lv_lock &= ~VAR_LOCKED;
3080 if (deep < 0 || deep > 1)
3081 /* recursive: lock/unlock the items the List contains */
3082 for (li = l->lv_first; li != NULL; li = li->li_next)
3083 item_lock(&li->li_tv, deep - 1, lock);
3084 }
3085 break;
3086 case VAR_DICT:
3087 if ((d = tv->vval.v_dict) != NULL)
3088 {
3089 if (lock)
3090 d->dv_lock |= VAR_LOCKED;
3091 else
3092 d->dv_lock &= ~VAR_LOCKED;
3093 if (deep < 0 || deep > 1)
3094 {
3095 /* recursive: lock/unlock the items the List contains */
3096 todo = d->dv_hashtab.ht_used;
3097 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3098 {
3099 if (!HASHITEM_EMPTY(hi))
3100 {
3101 --todo;
3102 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3103 }
3104 }
3105 }
3106 }
3107 }
3108 --recurse;
3109}
3110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3112/*
3113 * Delete all "menutrans_" variables.
3114 */
3115 void
3116del_menutrans_vars()
3117{
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003119 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 hash_lock(&globvarht);
3122 todo = globvarht.ht_used;
3123 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003124 {
3125 if (!HASHITEM_EMPTY(hi))
3126 {
3127 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3129 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003130 }
3131 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134#endif
3135
3136#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3137
3138/*
3139 * Local string buffer for the next two functions to store a variable name
3140 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3141 * get_user_var_name().
3142 */
3143
3144static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3145
3146static char_u *varnamebuf = NULL;
3147static int varnamebuflen = 0;
3148
3149/*
3150 * Function to concatenate a prefix and a variable name.
3151 */
3152 static char_u *
3153cat_prefix_varname(prefix, name)
3154 int prefix;
3155 char_u *name;
3156{
3157 int len;
3158
3159 len = (int)STRLEN(name) + 3;
3160 if (len > varnamebuflen)
3161 {
3162 vim_free(varnamebuf);
3163 len += 10; /* some additional space */
3164 varnamebuf = alloc(len);
3165 if (varnamebuf == NULL)
3166 {
3167 varnamebuflen = 0;
3168 return NULL;
3169 }
3170 varnamebuflen = len;
3171 }
3172 *varnamebuf = prefix;
3173 varnamebuf[1] = ':';
3174 STRCPY(varnamebuf + 2, name);
3175 return varnamebuf;
3176}
3177
3178/*
3179 * Function given to ExpandGeneric() to obtain the list of user defined
3180 * (global/buffer/window/built-in) variable names.
3181 */
3182/*ARGSUSED*/
3183 char_u *
3184get_user_var_name(xp, idx)
3185 expand_T *xp;
3186 int idx;
3187{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003188 static long_u gdone;
3189 static long_u bdone;
3190 static long_u wdone;
3191 static int vidx;
3192 static hashitem_T *hi;
3193 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003196 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003197
3198 /* Global variables */
3199 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003201 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003203 else
3204 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003205 while (HASHITEM_EMPTY(hi))
3206 ++hi;
3207 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3208 return cat_prefix_varname('g', hi->hi_key);
3209 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003211
3212 /* b: variables */
3213 ht = &curbuf->b_vars.dv_hashtab;
3214 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003216 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003218 else
3219 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003220 while (HASHITEM_EMPTY(hi))
3221 ++hi;
3222 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003226 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return (char_u *)"b:changedtick";
3228 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003229
3230 /* w: variables */
3231 ht = &curwin->w_vars.dv_hashtab;
3232 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003234 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003236 else
3237 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003238 while (HASHITEM_EMPTY(hi))
3239 ++hi;
3240 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003242
3243 /* v: variables */
3244 if (vidx < VV_LEN)
3245 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
3247 vim_free(varnamebuf);
3248 varnamebuf = NULL;
3249 varnamebuflen = 0;
3250 return NULL;
3251}
3252
3253#endif /* FEAT_CMDL_COMPL */
3254
3255/*
3256 * types for expressions.
3257 */
3258typedef enum
3259{
3260 TYPE_UNKNOWN = 0
3261 , TYPE_EQUAL /* == */
3262 , TYPE_NEQUAL /* != */
3263 , TYPE_GREATER /* > */
3264 , TYPE_GEQUAL /* >= */
3265 , TYPE_SMALLER /* < */
3266 , TYPE_SEQUAL /* <= */
3267 , TYPE_MATCH /* =~ */
3268 , TYPE_NOMATCH /* !~ */
3269} exptype_T;
3270
3271/*
3272 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3275 */
3276
3277/*
3278 * Handle zero level expression.
3279 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 * Return OK or FAIL.
3282 */
3283 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003284eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 char_u **nextcmd;
3288 int evaluate;
3289{
3290 int ret;
3291 char_u *p;
3292
3293 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (ret == FAIL || !ends_excmd(*p))
3296 {
3297 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003298 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 /*
3300 * Report the invalid expression unless the expression evaluation has
3301 * been cancelled due to an aborting error, an interrupt, or an
3302 * exception.
3303 */
3304 if (!aborting())
3305 EMSG2(_(e_invexpr2), arg);
3306 ret = FAIL;
3307 }
3308 if (nextcmd != NULL)
3309 *nextcmd = check_nextcmd(p);
3310
3311 return ret;
3312}
3313
3314/*
3315 * Handle top level expression:
3316 * expr1 ? expr0 : expr0
3317 *
3318 * "arg" must point to the first non-white of the expression.
3319 * "arg" is advanced to the next non-white after the recognized expression.
3320 *
3321 * Return OK or FAIL.
3322 */
3323 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 int evaluate;
3328{
3329 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
3332 /*
3333 * Get the first variable.
3334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return FAIL;
3337
3338 if ((*arg)[0] == '?')
3339 {
3340 result = FALSE;
3341 if (evaluate)
3342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003343 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
3347
3348 /*
3349 * Get the second variable.
3350 */
3351 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 return FAIL;
3354
3355 /*
3356 * Check for the ":".
3357 */
3358 if ((*arg)[0] != ':')
3359 {
3360 EMSG(_("E109: Missing ':' after '?'"));
3361 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 return FAIL;
3364 }
3365
3366 /*
3367 * Get the third variable.
3368 */
3369 *arg = skipwhite(*arg + 1);
3370 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3371 {
3372 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return FAIL;
3375 }
3376 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379
3380 return OK;
3381}
3382
3383/*
3384 * Handle first level expression:
3385 * expr2 || expr2 || expr2 logical OR
3386 *
3387 * "arg" must point to the first non-white of the expression.
3388 * "arg" is advanced to the next non-white after the recognized expression.
3389 *
3390 * Return OK or FAIL.
3391 */
3392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 int evaluate;
3397{
Bram Moolenaar33570922005-01-25 22:26:29 +00003398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 long result;
3400 int first;
3401
3402 /*
3403 * Get the first variable.
3404 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return FAIL;
3407
3408 /*
3409 * Repeat until there is no following "||".
3410 */
3411 first = TRUE;
3412 result = FALSE;
3413 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3414 {
3415 if (evaluate && first)
3416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003417 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003419 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 first = FALSE;
3421 }
3422
3423 /*
3424 * Get the second variable.
3425 */
3426 *arg = skipwhite(*arg + 2);
3427 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3428 return FAIL;
3429
3430 /*
3431 * Compute the result.
3432 */
3433 if (evaluate && !result)
3434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 if (evaluate)
3440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003441 rettv->v_type = VAR_NUMBER;
3442 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 }
3445
3446 return OK;
3447}
3448
3449/*
3450 * Handle second level expression:
3451 * expr3 && expr3 && expr3 logical AND
3452 *
3453 * "arg" must point to the first non-white of the expression.
3454 * "arg" is advanced to the next non-white after the recognized expression.
3455 *
3456 * Return OK or FAIL.
3457 */
3458 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int evaluate;
3463{
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 long result;
3466 int first;
3467
3468 /*
3469 * Get the first variable.
3470 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 return FAIL;
3473
3474 /*
3475 * Repeat until there is no following "&&".
3476 */
3477 first = TRUE;
3478 result = TRUE;
3479 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3480 {
3481 if (evaluate && first)
3482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 first = FALSE;
3487 }
3488
3489 /*
3490 * Get the second variable.
3491 */
3492 *arg = skipwhite(*arg + 2);
3493 if (eval4(arg, &var2, evaluate && result) == FAIL)
3494 return FAIL;
3495
3496 /*
3497 * Compute the result.
3498 */
3499 if (evaluate && result)
3500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 if (evaluate)
3506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003507 rettv->v_type = VAR_NUMBER;
3508 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 }
3511
3512 return OK;
3513}
3514
3515/*
3516 * Handle third level expression:
3517 * var1 == var2
3518 * var1 =~ var2
3519 * var1 != var2
3520 * var1 !~ var2
3521 * var1 > var2
3522 * var1 >= var2
3523 * var1 < var2
3524 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003525 * var1 is var2
3526 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 *
3528 * "arg" must point to the first non-white of the expression.
3529 * "arg" is advanced to the next non-white after the recognized expression.
3530 *
3531 * Return OK or FAIL.
3532 */
3533 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 int evaluate;
3538{
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_u *p;
3541 int i;
3542 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003543 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 int len = 2;
3545 long n1, n2;
3546 char_u *s1, *s2;
3547 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3548 regmatch_T regmatch;
3549 int ic;
3550 char_u *save_cpo;
3551
3552 /*
3553 * Get the first variable.
3554 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 return FAIL;
3557
3558 p = *arg;
3559 switch (p[0])
3560 {
3561 case '=': if (p[1] == '=')
3562 type = TYPE_EQUAL;
3563 else if (p[1] == '~')
3564 type = TYPE_MATCH;
3565 break;
3566 case '!': if (p[1] == '=')
3567 type = TYPE_NEQUAL;
3568 else if (p[1] == '~')
3569 type = TYPE_NOMATCH;
3570 break;
3571 case '>': if (p[1] != '=')
3572 {
3573 type = TYPE_GREATER;
3574 len = 1;
3575 }
3576 else
3577 type = TYPE_GEQUAL;
3578 break;
3579 case '<': if (p[1] != '=')
3580 {
3581 type = TYPE_SMALLER;
3582 len = 1;
3583 }
3584 else
3585 type = TYPE_SEQUAL;
3586 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003587 case 'i': if (p[1] == 's')
3588 {
3589 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3590 len = 5;
3591 if (!vim_isIDc(p[len]))
3592 {
3593 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3594 type_is = TRUE;
3595 }
3596 }
3597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /*
3601 * If there is a comparitive operator, use it.
3602 */
3603 if (type != TYPE_UNKNOWN)
3604 {
3605 /* extra question mark appended: ignore case */
3606 if (p[len] == '?')
3607 {
3608 ic = TRUE;
3609 ++len;
3610 }
3611 /* extra '#' appended: match case */
3612 else if (p[len] == '#')
3613 {
3614 ic = FALSE;
3615 ++len;
3616 }
3617 /* nothing appened: use 'ignorecase' */
3618 else
3619 ic = p_ic;
3620
3621 /*
3622 * Get the second variable.
3623 */
3624 *arg = skipwhite(p + len);
3625 if (eval5(arg, &var2, evaluate) == FAIL)
3626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return FAIL;
3629 }
3630
3631 if (evaluate)
3632 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003633 if (type_is && rettv->v_type != var2.v_type)
3634 {
3635 /* For "is" a different type always means FALSE, for "notis"
3636 * it means TRUE. */
3637 n1 = (type == TYPE_NEQUAL);
3638 }
3639 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3640 {
3641 if (type_is)
3642 {
3643 n1 = (rettv->v_type == var2.v_type
3644 && rettv->vval.v_list == var2.vval.v_list);
3645 if (type == TYPE_NEQUAL)
3646 n1 = !n1;
3647 }
3648 else if (rettv->v_type != var2.v_type
3649 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3650 {
3651 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003652 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003653 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003654 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003655 clear_tv(rettv);
3656 clear_tv(&var2);
3657 return FAIL;
3658 }
3659 else
3660 {
3661 /* Compare two Lists for being equal or unequal. */
3662 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3663 if (type == TYPE_NEQUAL)
3664 n1 = !n1;
3665 }
3666 }
3667
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003668 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3669 {
3670 if (type_is)
3671 {
3672 n1 = (rettv->v_type == var2.v_type
3673 && rettv->vval.v_dict == var2.vval.v_dict);
3674 if (type == TYPE_NEQUAL)
3675 n1 = !n1;
3676 }
3677 else if (rettv->v_type != var2.v_type
3678 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3679 {
3680 if (rettv->v_type != var2.v_type)
3681 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3682 else
3683 EMSG(_("E736: Invalid operation for Dictionary"));
3684 clear_tv(rettv);
3685 clear_tv(&var2);
3686 return FAIL;
3687 }
3688 else
3689 {
3690 /* Compare two Dictionaries for being equal or unequal. */
3691 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3692 if (type == TYPE_NEQUAL)
3693 n1 = !n1;
3694 }
3695 }
3696
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003697 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3698 {
3699 if (rettv->v_type != var2.v_type
3700 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3701 {
3702 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003703 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003704 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003705 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003706 clear_tv(rettv);
3707 clear_tv(&var2);
3708 return FAIL;
3709 }
3710 else
3711 {
3712 /* Compare two Funcrefs for being equal or unequal. */
3713 if (rettv->vval.v_string == NULL
3714 || var2.vval.v_string == NULL)
3715 n1 = FALSE;
3716 else
3717 n1 = STRCMP(rettv->vval.v_string,
3718 var2.vval.v_string) == 0;
3719 if (type == TYPE_NEQUAL)
3720 n1 = !n1;
3721 }
3722 }
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 /*
3725 * If one of the two variables is a number, compare as a number.
3726 * When using "=~" or "!~", always compare as string.
3727 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003728 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003731 n1 = get_tv_number(rettv);
3732 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 switch (type)
3734 {
3735 case TYPE_EQUAL: n1 = (n1 == n2); break;
3736 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3737 case TYPE_GREATER: n1 = (n1 > n2); break;
3738 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3739 case TYPE_SMALLER: n1 = (n1 < n2); break;
3740 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3741 case TYPE_UNKNOWN:
3742 case TYPE_MATCH:
3743 case TYPE_NOMATCH: break; /* avoid gcc warning */
3744 }
3745 }
3746 else
3747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003748 s1 = get_tv_string_buf(rettv, buf1);
3749 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3751 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3752 else
3753 i = 0;
3754 n1 = FALSE;
3755 switch (type)
3756 {
3757 case TYPE_EQUAL: n1 = (i == 0); break;
3758 case TYPE_NEQUAL: n1 = (i != 0); break;
3759 case TYPE_GREATER: n1 = (i > 0); break;
3760 case TYPE_GEQUAL: n1 = (i >= 0); break;
3761 case TYPE_SMALLER: n1 = (i < 0); break;
3762 case TYPE_SEQUAL: n1 = (i <= 0); break;
3763
3764 case TYPE_MATCH:
3765 case TYPE_NOMATCH:
3766 /* avoid 'l' flag in 'cpoptions' */
3767 save_cpo = p_cpo;
3768 p_cpo = (char_u *)"";
3769 regmatch.regprog = vim_regcomp(s2,
3770 RE_MAGIC + RE_STRING);
3771 regmatch.rm_ic = ic;
3772 if (regmatch.regprog != NULL)
3773 {
3774 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3775 vim_free(regmatch.regprog);
3776 if (type == TYPE_NOMATCH)
3777 n1 = !n1;
3778 }
3779 p_cpo = save_cpo;
3780 break;
3781
3782 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3783 }
3784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003785 clear_tv(rettv);
3786 clear_tv(&var2);
3787 rettv->v_type = VAR_NUMBER;
3788 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791
3792 return OK;
3793}
3794
3795/*
3796 * Handle fourth level expression:
3797 * + number addition
3798 * - number subtraction
3799 * . string concatenation
3800 *
3801 * "arg" must point to the first non-white of the expression.
3802 * "arg" is advanced to the next non-white after the recognized expression.
3803 *
3804 * Return OK or FAIL.
3805 */
3806 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int evaluate;
3811{
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 typval_T var2;
3813 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 int op;
3815 long n1, n2;
3816 char_u *s1, *s2;
3817 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3818 char_u *p;
3819
3820 /*
3821 * Get the first variable.
3822 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 return FAIL;
3825
3826 /*
3827 * Repeat computing, until no '+', '-' or '.' is following.
3828 */
3829 for (;;)
3830 {
3831 op = **arg;
3832 if (op != '+' && op != '-' && op != '.')
3833 break;
3834
3835 /*
3836 * Get the second variable.
3837 */
3838 *arg = skipwhite(*arg + 1);
3839 if (eval6(arg, &var2, evaluate) == FAIL)
3840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 return FAIL;
3843 }
3844
3845 if (evaluate)
3846 {
3847 /*
3848 * Compute the result.
3849 */
3850 if (op == '.')
3851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 s1 = get_tv_string_buf(rettv, buf1);
3853 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003854 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 clear_tv(rettv);
3856 rettv->v_type = VAR_STRING;
3857 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003859 else if (op == '+' && rettv->v_type == VAR_LIST
3860 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003861 {
3862 /* concatenate Lists */
3863 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3864 &var3) == FAIL)
3865 {
3866 clear_tv(rettv);
3867 clear_tv(&var2);
3868 return FAIL;
3869 }
3870 clear_tv(rettv);
3871 *rettv = var3;
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 else
3874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003875 n1 = get_tv_number(rettv);
3876 n2 = get_tv_number(&var2);
3877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (op == '+')
3879 n1 = n1 + n2;
3880 else
3881 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 rettv->v_type = VAR_NUMBER;
3883 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887 }
3888 return OK;
3889}
3890
3891/*
3892 * Handle fifth level expression:
3893 * * number multiplication
3894 * / number division
3895 * % number modulo
3896 *
3897 * "arg" must point to the first non-white of the expression.
3898 * "arg" is advanced to the next non-white after the recognized expression.
3899 *
3900 * Return OK or FAIL.
3901 */
3902 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 int evaluate;
3907{
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 int op;
3910 long n1, n2;
3911
3912 /*
3913 * Get the first variable.
3914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 return FAIL;
3917
3918 /*
3919 * Repeat computing, until no '*', '/' or '%' is following.
3920 */
3921 for (;;)
3922 {
3923 op = **arg;
3924 if (op != '*' && op != '/' && op != '%')
3925 break;
3926
3927 if (evaluate)
3928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003929 n1 = get_tv_number(rettv);
3930 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 else
3933 n1 = 0;
3934
3935 /*
3936 * Get the second variable.
3937 */
3938 *arg = skipwhite(*arg + 1);
3939 if (eval7(arg, &var2, evaluate) == FAIL)
3940 return FAIL;
3941
3942 if (evaluate)
3943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 n2 = get_tv_number(&var2);
3945 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 /*
3948 * Compute the result.
3949 */
3950 if (op == '*')
3951 n1 = n1 * n2;
3952 else if (op == '/')
3953 {
3954 if (n2 == 0) /* give an error message? */
3955 n1 = 0x7fffffffL;
3956 else
3957 n1 = n1 / n2;
3958 }
3959 else
3960 {
3961 if (n2 == 0) /* give an error message? */
3962 n1 = 0;
3963 else
3964 n1 = n1 % n2;
3965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003966 rettv->v_type = VAR_NUMBER;
3967 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970
3971 return OK;
3972}
3973
3974/*
3975 * Handle sixth level expression:
3976 * number number constant
3977 * "string" string contstant
3978 * 'string' literal string contstant
3979 * &option-name option value
3980 * @r register contents
3981 * identifier variable value
3982 * function() function call
3983 * $VAR environment variable
3984 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003985 * [expr, expr] List
3986 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 *
3988 * Also handle:
3989 * ! in front logical NOT
3990 * - in front unary minus
3991 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003992 * trailing [] subscript in String or List
3993 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 *
3995 * "arg" must point to the first non-white of the expression.
3996 * "arg" is advanced to the next non-white after the recognized expression.
3997 *
3998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int evaluate;
4005{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 long n;
4007 int len;
4008 char_u *s;
4009 int val;
4010 char_u *start_leader, *end_leader;
4011 int ret = OK;
4012 char_u *alias;
4013
4014 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004016 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020 /*
4021 * Skip '!' and '-' characters. They are handled later.
4022 */
4023 start_leader = *arg;
4024 while (**arg == '!' || **arg == '-' || **arg == '+')
4025 *arg = skipwhite(*arg + 1);
4026 end_leader = *arg;
4027
4028 switch (**arg)
4029 {
4030 /*
4031 * Number constant.
4032 */
4033 case '0':
4034 case '1':
4035 case '2':
4036 case '3':
4037 case '4':
4038 case '5':
4039 case '6':
4040 case '7':
4041 case '8':
4042 case '9':
4043 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4044 *arg += len;
4045 if (evaluate)
4046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 rettv->v_type = VAR_NUMBER;
4048 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 break;
4051
4052 /*
4053 * String constant: "string".
4054 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 break;
4057
4058 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004059 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 break;
4063
4064 /*
4065 * List: [expr, expr]
4066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 break;
4069
4070 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 * Dictionary: {key: val, key: val}
4072 */
4073 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4074 break;
4075
4076 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004077 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004079 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 break;
4081
4082 /*
4083 * Environment variable: $VAR.
4084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 break;
4087
4088 /*
4089 * Register contents: @r.
4090 */
4091 case '@': ++*arg;
4092 if (evaluate)
4093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 rettv->v_type = VAR_STRING;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00004095 rettv->vval.v_string = get_reg_contents(**arg,
4096 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 if (**arg != NUL)
4099 ++*arg;
4100 break;
4101
4102 /*
4103 * nested expression: (expression).
4104 */
4105 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (**arg == ')')
4108 ++*arg;
4109 else if (ret == OK)
4110 {
4111 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 ret = FAIL;
4114 }
4115 break;
4116
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004120
4121 if (ret == NOTDONE)
4122 {
4123 /*
4124 * Must be a variable or function name.
4125 * Can also be a curly-braces kind of name: {expr}.
4126 */
4127 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004128 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004129 if (alias != NULL)
4130 s = alias;
4131
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004132 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004133 ret = FAIL;
4134 else
4135 {
4136 if (**arg == '(') /* recursive! */
4137 {
4138 /* If "s" is the name of a variable of type VAR_FUNC
4139 * use its contents. */
4140 s = deref_func_name(s, &len);
4141
4142 /* Invoke the function. */
4143 ret = get_func_tv(s, len, rettv, arg,
4144 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004145 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 /* Stop the expression evaluation when immediately
4147 * aborting on error, or when an interrupt occurred or
4148 * an exception was thrown but not caught. */
4149 if (aborting())
4150 {
4151 if (ret == OK)
4152 clear_tv(rettv);
4153 ret = FAIL;
4154 }
4155 }
4156 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004157 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004158 else
4159 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 }
4161
4162 if (alias != NULL)
4163 vim_free(alias);
4164 }
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 *arg = skipwhite(*arg);
4167
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004168 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4169 * expr(expr). */
4170 if (ret == OK)
4171 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4175 */
4176 if (ret == OK && evaluate && end_leader > start_leader)
4177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 while (end_leader > start_leader)
4180 {
4181 --end_leader;
4182 if (*end_leader == '!')
4183 val = !val;
4184 else if (*end_leader == '-')
4185 val = -val;
4186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
4188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191
4192 return ret;
4193}
4194
4195/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004196 * Evaluate an "[expr]" or "[expr:expr]" index.
4197 * "*arg" points to the '['.
4198 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4199 */
4200 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004201eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004202 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004204 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004205 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206{
4207 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004208 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 long len = -1;
4211 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004213 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004214
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004217 if (verbose)
4218 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004219 return FAIL;
4220 }
4221
Bram Moolenaar8c711452005-01-14 21:53:12 +00004222 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004224 /*
4225 * dict.name
4226 */
4227 key = *arg + 1;
4228 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4229 ;
4230 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004232 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004233 }
4234 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004236 /*
4237 * something[idx]
4238 *
4239 * Get the (first) variable from inside the [].
4240 */
4241 *arg = skipwhite(*arg + 1);
4242 if (**arg == ':')
4243 empty1 = TRUE;
4244 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4245 return FAIL;
4246
4247 /*
4248 * Get the second variable from inside the [:].
4249 */
4250 if (**arg == ':')
4251 {
4252 range = TRUE;
4253 *arg = skipwhite(*arg + 1);
4254 if (**arg == ']')
4255 empty2 = TRUE;
4256 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4257 {
4258 clear_tv(&var1);
4259 return FAIL;
4260 }
4261 }
4262
4263 /* Check for the ']'. */
4264 if (**arg != ']')
4265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004266 if (verbose)
4267 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004268 clear_tv(&var1);
4269 if (range)
4270 clear_tv(&var2);
4271 return FAIL;
4272 }
4273 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 }
4275
4276 if (evaluate)
4277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 n1 = 0;
4279 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 n1 = get_tv_number(&var1);
4282 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004283 }
4284 if (range)
4285 {
4286 if (empty2)
4287 n2 = -1;
4288 else
4289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 n2 = get_tv_number(&var2);
4291 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 }
4293 }
4294
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004296 {
4297 case VAR_NUMBER:
4298 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004300 len = (long)STRLEN(s);
4301 if (range)
4302 {
4303 /* The resulting variable is a substring. If the indexes
4304 * are out of range the result is empty. */
4305 if (n1 < 0)
4306 {
4307 n1 = len + n1;
4308 if (n1 < 0)
4309 n1 = 0;
4310 }
4311 if (n2 < 0)
4312 n2 = len + n2;
4313 else if (n2 >= len)
4314 n2 = len;
4315 if (n1 >= len || n2 < 0 || n1 > n2)
4316 s = NULL;
4317 else
4318 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4319 }
4320 else
4321 {
4322 /* The resulting variable is a string of a single
4323 * character. If the index is too big or negative the
4324 * result is empty. */
4325 if (n1 >= len || n1 < 0)
4326 s = NULL;
4327 else
4328 s = vim_strnsave(s + n1, 1);
4329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
4331 rettv->v_type = VAR_STRING;
4332 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004333 break;
4334
4335 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004337 if (n1 < 0)
4338 n1 = len + n1;
4339 if (!empty1 && (n1 < 0 || n1 >= len))
4340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004341 if (verbose)
4342 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004343 return FAIL;
4344 }
4345 if (range)
4346 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004347 list_T *l;
4348 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004349
4350 if (n2 < 0)
4351 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004353 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004354 if (verbose)
4355 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004356 return FAIL;
4357 }
4358 l = list_alloc();
4359 if (l == NULL)
4360 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004362 n1 <= n2; ++n1)
4363 {
4364 if (list_append_tv(l, &item->li_tv) == FAIL)
4365 {
4366 list_free(l);
4367 return FAIL;
4368 }
4369 item = item->li_next;
4370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
4372 rettv->v_type = VAR_LIST;
4373 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004374 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004375 }
4376 else
4377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
4381 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004382 }
4383 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384
4385 case VAR_DICT:
4386 if (range)
4387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004388 if (verbose)
4389 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390 if (len == -1)
4391 clear_tv(&var1);
4392 return FAIL;
4393 }
4394 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004396
4397 if (len == -1)
4398 {
4399 key = get_tv_string(&var1);
4400 if (*key == NUL)
4401 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004402 if (verbose)
4403 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004404 clear_tv(&var1);
4405 return FAIL;
4406 }
4407 }
4408
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004409 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004411 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004412 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004413 if (len == -1)
4414 clear_tv(&var1);
4415 if (item == NULL)
4416 return FAIL;
4417
4418 copy_tv(&item->di_tv, &var1);
4419 clear_tv(rettv);
4420 *rettv = var1;
4421 }
4422 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 }
4424 }
4425
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426 return OK;
4427}
4428
4429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 * Get an option value.
4431 * "arg" points to the '&' or '+' before the option name.
4432 * "arg" is advanced to character after the option name.
4433 * Return OK or FAIL.
4434 */
4435 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004438 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 int evaluate;
4440{
4441 char_u *option_end;
4442 long numval;
4443 char_u *stringval;
4444 int opt_type;
4445 int c;
4446 int working = (**arg == '+'); /* has("+option") */
4447 int ret = OK;
4448 int opt_flags;
4449
4450 /*
4451 * Isolate the option name and find its value.
4452 */
4453 option_end = find_option_end(arg, &opt_flags);
4454 if (option_end == NULL)
4455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 EMSG2(_("E112: Option name missing: %s"), *arg);
4458 return FAIL;
4459 }
4460
4461 if (!evaluate)
4462 {
4463 *arg = option_end;
4464 return OK;
4465 }
4466
4467 c = *option_end;
4468 *option_end = NUL;
4469 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 if (opt_type == -3) /* invalid name */
4473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 EMSG2(_("E113: Unknown option: %s"), *arg);
4476 ret = FAIL;
4477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 if (opt_type == -2) /* hidden string option */
4481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 rettv->v_type = VAR_STRING;
4483 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else if (opt_type == -1) /* hidden number option */
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 rettv->v_type = VAR_NUMBER;
4488 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 else if (opt_type == 1) /* number option */
4491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004492 rettv->v_type = VAR_NUMBER;
4493 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 else /* string option */
4496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004497 rettv->v_type = VAR_STRING;
4498 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 }
4501 else if (working && (opt_type == -2 || opt_type == -1))
4502 ret = FAIL;
4503
4504 *option_end = c; /* put back for error messages */
4505 *arg = option_end;
4506
4507 return ret;
4508}
4509
4510/*
4511 * Allocate a variable for a string constant.
4512 * Return OK or FAIL.
4513 */
4514 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 int evaluate;
4519{
4520 char_u *p;
4521 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 int extra = 0;
4523
4524 /*
4525 * Find the end of the string, skipping backslashed characters.
4526 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004527 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 if (*p == '\\' && p[1] != NUL)
4530 {
4531 ++p;
4532 /* A "\<x>" form occupies at least 4 characters, and produces up
4533 * to 6 characters: reserve space for 2 extra */
4534 if (*p == '<')
4535 extra += 2;
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
4539 if (*p != '"')
4540 {
4541 EMSG2(_("E114: Missing quote: %s"), *arg);
4542 return FAIL;
4543 }
4544
4545 /* If only parsing, set *arg and return here */
4546 if (!evaluate)
4547 {
4548 *arg = p + 1;
4549 return OK;
4550 }
4551
4552 /*
4553 * Copy the string into allocated memory, handling backslashed
4554 * characters.
4555 */
4556 name = alloc((unsigned)(p - *arg + extra));
4557 if (name == NULL)
4558 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 rettv->v_type = VAR_STRING;
4560 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 if (*p == '\\')
4565 {
4566 switch (*++p)
4567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 case 'b': *name++ = BS; ++p; break;
4569 case 'e': *name++ = ESC; ++p; break;
4570 case 'f': *name++ = FF; ++p; break;
4571 case 'n': *name++ = NL; ++p; break;
4572 case 'r': *name++ = CAR; ++p; break;
4573 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 case 'X': /* hex: "\x1", "\x12" */
4576 case 'x':
4577 case 'u': /* Unicode: "\u0023" */
4578 case 'U':
4579 if (vim_isxdigit(p[1]))
4580 {
4581 int n, nr;
4582 int c = toupper(*p);
4583
4584 if (c == 'X')
4585 n = 2;
4586 else
4587 n = 4;
4588 nr = 0;
4589 while (--n >= 0 && vim_isxdigit(p[1]))
4590 {
4591 ++p;
4592 nr = (nr << 4) + hex2nr(*p);
4593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_MBYTE
4596 /* For "\u" store the number according to
4597 * 'encoding'. */
4598 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 else
4601#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 break;
4605
4606 /* octal: "\1", "\12", "\123" */
4607 case '0':
4608 case '1':
4609 case '2':
4610 case '3':
4611 case '4':
4612 case '5':
4613 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 case '7': *name = *p++ - '0';
4615 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 *name = (*name << 3) + *p++ - '0';
4618 if (*p >= '0' && *p <= '7')
4619 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623
4624 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004625 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (extra != 0)
4627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630 }
4631 /* FALLTHROUGH */
4632
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 break;
4635 }
4636 }
4637 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 *arg = p + 1;
4643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return OK;
4645}
4646
4647/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 * Return OK or FAIL.
4650 */
4651 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 int evaluate;
4656{
4657 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004659 int reduce = 0;
4660
4661 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004663 */
4664 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004667 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004669 break;
4670 ++reduce;
4671 ++p;
4672 }
4673 }
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004678 return FAIL;
4679 }
4680
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004682 if (!evaluate)
4683 {
4684 *arg = p + 1;
4685 return OK;
4686 }
4687
4688 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004690 */
4691 str = alloc((unsigned)((p - *arg) - reduce));
4692 if (str == NULL)
4693 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 rettv->v_type = VAR_STRING;
4695 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004696
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004699 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004702 break;
4703 ++p;
4704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004708 *arg = p + 1;
4709
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004710 return OK;
4711}
4712
4713/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 * Allocate a variable for a List and fill it from "*arg".
4715 * Return OK or FAIL.
4716 */
4717 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004720 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 int evaluate;
4722{
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 list_T *l = NULL;
4724 typval_T tv;
4725 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726
4727 if (evaluate)
4728 {
4729 l = list_alloc();
4730 if (l == NULL)
4731 return FAIL;
4732 }
4733
4734 *arg = skipwhite(*arg + 1);
4735 while (**arg != ']' && **arg != NUL)
4736 {
4737 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4738 goto failret;
4739 if (evaluate)
4740 {
4741 item = listitem_alloc();
4742 if (item != NULL)
4743 {
4744 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004745 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 list_append(l, item);
4747 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004748 else
4749 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 }
4751
4752 if (**arg == ']')
4753 break;
4754 if (**arg != ',')
4755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 goto failret;
4758 }
4759 *arg = skipwhite(*arg + 1);
4760 }
4761
4762 if (**arg != ']')
4763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765failret:
4766 if (evaluate)
4767 list_free(l);
4768 return FAIL;
4769 }
4770
4771 *arg = skipwhite(*arg + 1);
4772 if (evaluate)
4773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 rettv->v_type = VAR_LIST;
4775 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 ++l->lv_refcount;
4777 }
4778
4779 return OK;
4780}
4781
4782/*
4783 * Allocate an empty header for a list.
4784 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786list_alloc()
4787{
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789}
4790
4791/*
4792 * Unreference a list: decrement the reference count and free it when it
4793 * becomes zero.
4794 */
4795 static void
4796list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004797 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798{
4799 if (l != NULL && --l->lv_refcount <= 0)
4800 list_free(l);
4801}
4802
4803/*
4804 * Free a list, including all items it points to.
4805 * Ignores the reference count.
4806 */
4807 static void
4808list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004809 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810{
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 listitem_T *item;
4812 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813
4814 for (item = l->lv_first; item != NULL; item = next)
4815 {
4816 next = item->li_next;
4817 listitem_free(item);
4818 }
4819 vim_free(l);
4820}
4821
4822/*
4823 * Allocate a list item.
4824 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004825 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826listitem_alloc()
4827{
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829}
4830
4831/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004832 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 */
4834 static void
4835listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004836 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 vim_free(item);
4840}
4841
4842/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004843 * Remove a list item from a List and free it. Also clears the value.
4844 */
4845 static void
4846listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004847 list_T *l;
4848 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004849{
4850 list_remove(l, item, item);
4851 listitem_free(item);
4852}
4853
4854/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 * Get the number of items in a list.
4856 */
4857 static long
4858list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004859 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 if (l == NULL)
4862 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004863 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864}
4865
4866/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004867 * Return TRUE when two lists have exactly the same values.
4868 */
4869 static int
4870list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 list_T *l1;
4872 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004873 int ic; /* ignore case for strings */
4874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004876
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004877 if (list_len(l1) != list_len(l2))
4878 return FALSE;
4879
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004880 for (item1 = l1->lv_first, item2 = l2->lv_first;
4881 item1 != NULL && item2 != NULL;
4882 item1 = item1->li_next, item2 = item2->li_next)
4883 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4884 return FALSE;
4885 return item1 == NULL && item2 == NULL;
4886}
4887
4888/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004889 * Return TRUE when two dictionaries have exactly the same key/values.
4890 */
4891 static int
4892dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004893 dict_T *d1;
4894 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004895 int ic; /* ignore case for strings */
4896{
Bram Moolenaar33570922005-01-25 22:26:29 +00004897 hashitem_T *hi;
4898 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004899 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004900
4901 if (dict_len(d1) != dict_len(d2))
4902 return FALSE;
4903
Bram Moolenaar33570922005-01-25 22:26:29 +00004904 todo = d1->dv_hashtab.ht_used;
4905 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004907 if (!HASHITEM_EMPTY(hi))
4908 {
4909 item2 = dict_find(d2, hi->hi_key, -1);
4910 if (item2 == NULL)
4911 return FALSE;
4912 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4913 return FALSE;
4914 --todo;
4915 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004916 }
4917 return TRUE;
4918}
4919
4920/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004921 * Return TRUE if "tv1" and "tv2" have the same value.
4922 * Compares the items just like "==" would compare them.
4923 */
4924 static int
4925tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004926 typval_T *tv1;
4927 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004928 int ic; /* ignore case */
4929{
4930 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4931
4932 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4933 {
4934 /* recursive! */
4935 if (tv1->v_type != tv2->v_type
4936 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4937 return FALSE;
4938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004939 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4940 {
4941 /* recursive! */
4942 if (tv1->v_type != tv2->v_type
4943 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4944 return FALSE;
4945 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004946 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4947 {
4948 if (tv1->v_type != tv2->v_type
4949 || tv1->vval.v_string == NULL
4950 || tv2->vval.v_string == NULL
4951 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4952 return FALSE;
4953 }
4954 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4955 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4957 * Don't consider "4x" to be equal to 4. */
4958 if ((tv1->v_type == VAR_STRING
4959 && !string_isa_number(tv1->vval.v_string))
4960 || (tv2->v_type == VAR_STRING
4961 && !string_isa_number(tv2->vval.v_string)))
4962 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004963 if (get_tv_number(tv1) != get_tv_number(tv2))
4964 return FALSE;
4965 }
4966 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4967 get_tv_string_buf(tv2, buf2)) != 0)
4968 return FALSE;
4969 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4970 get_tv_string_buf(tv2, buf2)) != 0)
4971 return FALSE;
4972 return TRUE;
4973}
4974
4975/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004976 * Return TRUE if "tv" is a number without other non-white characters.
4977 */
4978 static int
4979string_isa_number(s)
4980 char_u *s;
4981{
4982 int len;
4983
4984 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4985 return len > 0 && *skipwhite(s + len) == NUL;
4986}
4987
4988/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 * Locate item with index "n" in list "l" and return it.
4990 * A negative index is counted from the end; -1 is the last item.
4991 * Returns NULL when "n" is out of range.
4992 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004993 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004995 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 long n;
4997{
Bram Moolenaar33570922005-01-25 22:26:29 +00004998 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004999 long idx;
5000
5001 if (l == NULL)
5002 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005003
5004 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005006 n = l->lv_len + n;
5007
5008 /* Check for index out of range. */
5009 if (n < 0 || n >= l->lv_len)
5010 return NULL;
5011
5012 /* When there is a cached index may start search from there. */
5013 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005015 if (n < l->lv_idx / 2)
5016 {
5017 /* closest to the start of the list */
5018 item = l->lv_first;
5019 idx = 0;
5020 }
5021 else if (n > (l->lv_idx + l->lv_len) / 2)
5022 {
5023 /* closest to the end of the list */
5024 item = l->lv_last;
5025 idx = l->lv_len - 1;
5026 }
5027 else
5028 {
5029 /* closest to the cached index */
5030 item = l->lv_idx_item;
5031 idx = l->lv_idx;
5032 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005033 }
5034 else
5035 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005036 if (n < l->lv_len / 2)
5037 {
5038 /* closest to the start of the list */
5039 item = l->lv_first;
5040 idx = 0;
5041 }
5042 else
5043 {
5044 /* closest to the end of the list */
5045 item = l->lv_last;
5046 idx = l->lv_len - 1;
5047 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005048 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005049
5050 while (n > idx)
5051 {
5052 /* search forward */
5053 item = item->li_next;
5054 ++idx;
5055 }
5056 while (n < idx)
5057 {
5058 /* search backward */
5059 item = item->li_prev;
5060 --idx;
5061 }
5062
5063 /* cache the used index */
5064 l->lv_idx = idx;
5065 l->lv_idx_item = item;
5066
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005067 return item;
5068}
5069
5070/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005071 * Locate "item" list "l" and return its index.
5072 * Returns -1 when "item" is not in the list.
5073 */
5074 static long
5075list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 list_T *l;
5077 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005078{
5079 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005081
5082 if (l == NULL)
5083 return -1;
5084 idx = 0;
5085 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5086 ++idx;
5087 if (li == NULL)
5088 return -1;
5089 return idx;;
5090}
5091
5092/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005093 * Append item "item" to the end of list "l".
5094 */
5095 static void
5096list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005097 list_T *l;
5098 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099{
5100 if (l->lv_last == NULL)
5101 {
5102 /* empty list */
5103 l->lv_first = item;
5104 l->lv_last = item;
5105 item->li_prev = NULL;
5106 }
5107 else
5108 {
5109 l->lv_last->li_next = item;
5110 item->li_prev = l->lv_last;
5111 l->lv_last = item;
5112 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005113 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 item->li_next = NULL;
5115}
5116
5117/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005118 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005119 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 */
5121 static int
5122list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 list_T *l;
5124 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005126 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127
Bram Moolenaar05159a02005-02-26 23:04:13 +00005128 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005130 copy_tv(tv, &li->li_tv);
5131 list_append(l, li);
5132 return OK;
5133}
5134
5135/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005136 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005137 * Return FAIL when out of memory.
5138 */
5139 int
5140list_append_dict(list, dict)
5141 list_T *list;
5142 dict_T *dict;
5143{
5144 listitem_T *li = listitem_alloc();
5145
5146 if (li == NULL)
5147 return FAIL;
5148 li->li_tv.v_type = VAR_DICT;
5149 li->li_tv.v_lock = 0;
5150 li->li_tv.vval.v_dict = dict;
5151 list_append(list, li);
5152 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 return OK;
5154}
5155
5156/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005157 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005158 * If "item" is NULL append at the end.
5159 * Return FAIL when out of memory.
5160 */
5161 static int
5162list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 list_T *l;
5164 typval_T *tv;
5165 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005166{
Bram Moolenaar33570922005-01-25 22:26:29 +00005167 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005168
5169 if (ni == NULL)
5170 return FAIL;
5171 copy_tv(tv, &ni->li_tv);
5172 if (item == NULL)
5173 /* Append new item at end of list. */
5174 list_append(l, ni);
5175 else
5176 {
5177 /* Insert new item before existing item. */
5178 ni->li_prev = item->li_prev;
5179 ni->li_next = item;
5180 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005181 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005182 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005183 ++l->lv_idx;
5184 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005185 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005186 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005187 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005188 l->lv_idx_item = NULL;
5189 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005190 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005191 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192 }
5193 return OK;
5194}
5195
5196/*
5197 * Extend "l1" with "l2".
5198 * If "bef" is NULL append at the end, otherwise insert before this item.
5199 * Returns FAIL when out of memory.
5200 */
5201 static int
5202list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 list_T *l1;
5204 list_T *l2;
5205 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005206{
Bram Moolenaar33570922005-01-25 22:26:29 +00005207 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005208
5209 for (item = l2->lv_first; item != NULL; item = item->li_next)
5210 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5211 return FAIL;
5212 return OK;
5213}
5214
5215/*
5216 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5217 * Return FAIL when out of memory.
5218 */
5219 static int
5220list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005221 list_T *l1;
5222 list_T *l2;
5223 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005224{
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005226
5227 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005228 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005229 if (l == NULL)
5230 return FAIL;
5231 tv->v_type = VAR_LIST;
5232 tv->vval.v_list = l;
5233
5234 /* append all items from the second list */
5235 return list_extend(l, l2, NULL);
5236}
5237
5238/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005241 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 * Returns NULL when out of memory.
5243 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005245list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005246 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005248 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249{
Bram Moolenaar33570922005-01-25 22:26:29 +00005250 list_T *copy;
5251 listitem_T *item;
5252 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253
5254 if (orig == NULL)
5255 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256
5257 copy = list_alloc();
5258 if (copy != NULL)
5259 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005260 if (copyID != 0)
5261 {
5262 /* Do this before adding the items, because one of the items may
5263 * refer back to this list. */
5264 orig->lv_copyID = copyID;
5265 orig->lv_copylist = copy;
5266 }
5267 for (item = orig->lv_first; item != NULL && !got_int;
5268 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 {
5270 ni = listitem_alloc();
5271 if (ni == NULL)
5272 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005273 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005274 {
5275 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5276 {
5277 vim_free(ni);
5278 break;
5279 }
5280 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 list_append(copy, ni);
5284 }
5285 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005286 if (item != NULL)
5287 {
5288 list_unref(copy);
5289 copy = NULL;
5290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 return copy;
5294}
5295
5296/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005298 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005301list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 list_T *l;
5303 listitem_T *item;
5304 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305{
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005308 /* notify watchers */
5309 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005311 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312 list_fix_watch(l, ip);
5313 if (ip == item2)
5314 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005316
5317 if (item2->li_next == NULL)
5318 l->lv_last = item->li_prev;
5319 else
5320 item2->li_next->li_prev = item->li_prev;
5321 if (item->li_prev == NULL)
5322 l->lv_first = item2->li_next;
5323 else
5324 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005325 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326}
5327
5328/*
5329 * Return an allocated string with the string representation of a list.
5330 * May return NULL.
5331 */
5332 static char_u *
5333list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335{
5336 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337
5338 if (tv->vval.v_list == NULL)
5339 return NULL;
5340 ga_init2(&ga, (int)sizeof(char), 80);
5341 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005342 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5343 {
5344 vim_free(ga.ga_data);
5345 return NULL;
5346 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 ga_append(&ga, ']');
5348 ga_append(&ga, NUL);
5349 return (char_u *)ga.ga_data;
5350}
5351
5352/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005353 * Join list "l" into a string in "*gap", using separator "sep".
5354 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005355 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005356 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005357 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005358list_join(gap, l, sep, echo)
5359 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005361 char_u *sep;
5362 int echo;
5363{
5364 int first = TRUE;
5365 char_u *tofree;
5366 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005368 char_u *s;
5369
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005370 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005371 {
5372 if (first)
5373 first = FALSE;
5374 else
5375 ga_concat(gap, sep);
5376
5377 if (echo)
5378 s = echo_string(&item->li_tv, &tofree, numbuf);
5379 else
5380 s = tv2string(&item->li_tv, &tofree, numbuf);
5381 if (s != NULL)
5382 ga_concat(gap, s);
5383 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005384 if (s == NULL)
5385 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005386 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005387 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005388}
5389
5390/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 * Allocate an empty header for a dictionary.
5392 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005393 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394dict_alloc()
5395{
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005397
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005399 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005400 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005401 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005402 d->dv_lock = 0;
5403 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005404 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005405 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005406 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407}
5408
5409/*
5410 * Unreference a Dictionary: decrement the reference count and free it when it
5411 * becomes zero.
5412 */
5413 static void
5414dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416{
5417 if (d != NULL && --d->dv_refcount <= 0)
5418 dict_free(d);
5419}
5420
5421/*
5422 * Free a Dictionary, including all items it contains.
5423 * Ignores the reference count.
5424 */
5425 static void
5426dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005429 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005432 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005433 * hashtab. Must not try to resize the hashtab! */
5434 todo = d->dv_hashtab.ht_used;
5435 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005437 if (!HASHITEM_EMPTY(hi))
5438 {
5439 dictitem_free(HI2DI(hi));
5440 --todo;
5441 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 vim_free(d);
5445}
5446
5447/*
5448 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005449 * The "key" is copied to the new item.
5450 * Note that the value of the item "di_tv" still needs to be initialized!
5451 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005454dictitem_alloc(key)
5455 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456{
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005458
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005460 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005462 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005463 di->di_flags = 0;
5464 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005465 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466}
5467
5468/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005469 * Make a copy of a Dictionary item.
5470 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005471 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005472dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005474{
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005476
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005478 if (di != NULL)
5479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005480 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005481 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005482 copy_tv(&org->di_tv, &di->di_tv);
5483 }
5484 return di;
5485}
5486
5487/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005488 * Remove item "item" from Dictionary "dict" and free it.
5489 */
5490 static void
5491dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dict_T *dict;
5493 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005494{
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005496
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005498 if (HASHITEM_EMPTY(hi))
5499 EMSG2(_(e_intern2), "dictitem_remove()");
5500 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005502 dictitem_free(item);
5503}
5504
5505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 * Free a dict item. Also clears the value.
5507 */
5508 static void
5509dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005512 clear_tv(&item->di_tv);
5513 vim_free(item);
5514}
5515
5516/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005517 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5518 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005519 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520 * Returns NULL when out of memory.
5521 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005523dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005525 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005526 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005527{
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 dict_T *copy;
5529 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005530 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005531 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005532
5533 if (orig == NULL)
5534 return NULL;
5535
5536 copy = dict_alloc();
5537 if (copy != NULL)
5538 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005539 if (copyID != 0)
5540 {
5541 orig->dv_copyID = copyID;
5542 orig->dv_copydict = copy;
5543 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005545 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005547 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005548 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005549 --todo;
5550
5551 di = dictitem_alloc(hi->hi_key);
5552 if (di == NULL)
5553 break;
5554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005555 {
5556 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5557 copyID) == FAIL)
5558 {
5559 vim_free(di);
5560 break;
5561 }
5562 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005563 else
5564 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5565 if (dict_add(copy, di) == FAIL)
5566 {
5567 dictitem_free(di);
5568 break;
5569 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005570 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005571 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005572
Bram Moolenaare9a41262005-01-15 22:18:47 +00005573 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005574 if (todo > 0)
5575 {
5576 dict_unref(copy);
5577 copy = NULL;
5578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005579 }
5580
5581 return copy;
5582}
5583
5584/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005586 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005588 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005590 dict_T *d;
5591 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592{
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594}
5595
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005597 * Add a number or string entry to dictionary "d".
5598 * When "str" is NULL use number "nr", otherwise use "str".
5599 * Returns FAIL when out of memory and when key already exists.
5600 */
5601 int
5602dict_add_nr_str(d, key, nr, str)
5603 dict_T *d;
5604 char *key;
5605 long nr;
5606 char_u *str;
5607{
5608 dictitem_T *item;
5609
5610 item = dictitem_alloc((char_u *)key);
5611 if (item == NULL)
5612 return FAIL;
5613 item->di_tv.v_lock = 0;
5614 if (str == NULL)
5615 {
5616 item->di_tv.v_type = VAR_NUMBER;
5617 item->di_tv.vval.v_number = nr;
5618 }
5619 else
5620 {
5621 item->di_tv.v_type = VAR_STRING;
5622 item->di_tv.vval.v_string = vim_strsave(str);
5623 }
5624 if (dict_add(d, item) == FAIL)
5625 {
5626 dictitem_free(item);
5627 return FAIL;
5628 }
5629 return OK;
5630}
5631
5632/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005633 * Get the number of items in a Dictionary.
5634 */
5635 static long
5636dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 if (d == NULL)
5640 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642}
5643
5644/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 * Find item "key[len]" in Dictionary "d".
5646 * If "len" is negative use strlen(key).
5647 * Returns NULL when not found.
5648 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005650dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005651 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 char_u *key;
5653 int len;
5654{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005655#define AKEYLEN 200
5656 char_u buf[AKEYLEN];
5657 char_u *akey;
5658 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005661 if (len < 0)
5662 akey = key;
5663 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005665 tofree = akey = vim_strnsave(key, len);
5666 if (akey == NULL)
5667 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005668 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005669 else
5670 {
5671 /* Avoid a malloc/free by using buf[]. */
5672 STRNCPY(buf, key, len);
5673 buf[len] = NUL;
5674 akey = buf;
5675 }
5676
Bram Moolenaar33570922005-01-25 22:26:29 +00005677 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005678 vim_free(tofree);
5679 if (HASHITEM_EMPTY(hi))
5680 return NULL;
5681 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682}
5683
5684/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005685 * Get a string item from a dictionary in allocated memory.
5686 * Returns NULL if the entry doesn't exist or out of memory.
5687 */
5688 char_u *
5689get_dict_string(d, key)
5690 dict_T *d;
5691 char_u *key;
5692{
5693 dictitem_T *di;
5694
5695 di = dict_find(d, key, -1);
5696 if (di == NULL)
5697 return NULL;
5698 return vim_strsave(get_tv_string(&di->di_tv));
5699}
5700
5701/*
5702 * Get a number item from a dictionary.
5703 * Returns 0 if the entry doesn't exist or out of memory.
5704 */
5705 long
5706get_dict_number(d, key)
5707 dict_T *d;
5708 char_u *key;
5709{
5710 dictitem_T *di;
5711
5712 di = dict_find(d, key, -1);
5713 if (di == NULL)
5714 return 0;
5715 return get_tv_number(&di->di_tv);
5716}
5717
5718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 * Return an allocated string with the string representation of a Dictionary.
5720 * May return NULL.
5721 */
5722 static char_u *
5723dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725{
5726 garray_T ga;
5727 int first = TRUE;
5728 char_u *tofree;
5729 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005733 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005735 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 return NULL;
5737 ga_init2(&ga, (int)sizeof(char), 80);
5738 ga_append(&ga, '{');
5739
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005741 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005745 --todo;
5746
5747 if (first)
5748 first = FALSE;
5749 else
5750 ga_concat(&ga, (char_u *)", ");
5751
5752 tofree = string_quote(hi->hi_key, FALSE);
5753 if (tofree != NULL)
5754 {
5755 ga_concat(&ga, tofree);
5756 vim_free(tofree);
5757 }
5758 ga_concat(&ga, (char_u *)": ");
5759 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5760 if (s != NULL)
5761 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005763 if (s == NULL)
5764 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005767 if (todo > 0)
5768 {
5769 vim_free(ga.ga_data);
5770 return NULL;
5771 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772
5773 ga_append(&ga, '}');
5774 ga_append(&ga, NUL);
5775 return (char_u *)ga.ga_data;
5776}
5777
5778/*
5779 * Allocate a variable for a Dictionary and fill it from "*arg".
5780 * Return OK or FAIL. Returns NOTDONE for {expr}.
5781 */
5782 static int
5783get_dict_tv(arg, rettv, evaluate)
5784 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 int evaluate;
5787{
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 dict_T *d = NULL;
5789 typval_T tvkey;
5790 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005794 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795
5796 /*
5797 * First check if it's not a curly-braces thing: {expr}.
5798 * Must do this without evaluating, otherwise a function may be called
5799 * twice. Unfortunately this means we need to call eval1() twice for the
5800 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005801 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 if (*start != '}')
5804 {
5805 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5806 return FAIL;
5807 if (*start == '}')
5808 return NOTDONE;
5809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810
5811 if (evaluate)
5812 {
5813 d = dict_alloc();
5814 if (d == NULL)
5815 return FAIL;
5816 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005817 tvkey.v_type = VAR_UNKNOWN;
5818 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819
5820 *arg = skipwhite(*arg + 1);
5821 while (**arg != '}' && **arg != NUL)
5822 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005823 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 goto failret;
5825 if (**arg != ':')
5826 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005827 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005828 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 goto failret;
5830 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005831 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*key == NUL)
5833 {
5834 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005835 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 goto failret;
5837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838
5839 *arg = skipwhite(*arg + 1);
5840 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5841 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005842 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 goto failret;
5844 }
5845 if (evaluate)
5846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005847 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 if (item != NULL)
5849 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00005850 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 clear_tv(&tv);
5853 goto failret;
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 item = dictitem_alloc(key);
5856 clear_tv(&tvkey);
5857 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005860 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005861 if (dict_add(d, item) == FAIL)
5862 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 }
5864 }
5865
5866 if (**arg == '}')
5867 break;
5868 if (**arg != ',')
5869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005870 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 goto failret;
5872 }
5873 *arg = skipwhite(*arg + 1);
5874 }
5875
5876 if (**arg != '}')
5877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005878 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879failret:
5880 if (evaluate)
5881 dict_free(d);
5882 return FAIL;
5883 }
5884
5885 *arg = skipwhite(*arg + 1);
5886 if (evaluate)
5887 {
5888 rettv->v_type = VAR_DICT;
5889 rettv->vval.v_dict = d;
5890 ++d->dv_refcount;
5891 }
5892
5893 return OK;
5894}
5895
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 * Return a string with the string representation of a variable.
5898 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005899 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * May return NULL;
5902 */
5903 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005909 static int recurse = 0;
5910 char_u *r = NULL;
5911
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005914 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915 *tofree = NULL;
5916 return NULL;
5917 }
5918 ++recurse;
5919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 switch (tv->v_type)
5921 {
5922 case VAR_FUNC:
5923 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005924 r = tv->vval.v_string;
5925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 case VAR_LIST:
5927 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005928 r = *tofree;
5929 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 case VAR_DICT:
5931 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005932 r = *tofree;
5933 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934 case VAR_STRING:
5935 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 *tofree = NULL;
5937 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005939 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005941 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005943
5944 --recurse;
5945 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946}
5947
5948/*
5949 * Return a string with the string representation of a variable.
5950 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5951 * "numbuf" is used for a number.
5952 * Puts quotes around strings, so that they can be parsed back by eval().
5953 * May return NULL;
5954 */
5955 static char_u *
5956tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 char_u **tofree;
5959 char_u *numbuf;
5960{
5961 switch (tv->v_type)
5962 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 case VAR_FUNC:
5964 *tofree = string_quote(tv->vval.v_string, TRUE);
5965 return *tofree;
5966 case VAR_STRING:
5967 *tofree = string_quote(tv->vval.v_string, FALSE);
5968 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005970 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977}
5978
5979/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 * Return string "str" in ' quotes, doubling ' characters.
5981 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005982 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983 */
5984 static char_u *
5985string_quote(str, function)
5986 char_u *str;
5987 int function;
5988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990 char_u *p, *r, *s;
5991
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 len = (function ? 13 : 3);
5993 if (str != NULL)
5994 {
5995 len += STRLEN(str);
5996 for (p = str; *p != NUL; mb_ptr_adv(p))
5997 if (*p == '\'')
5998 ++len;
5999 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006000 s = r = alloc(len);
6001 if (r != NULL)
6002 {
6003 if (function)
6004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006005 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006006 r += 10;
6007 }
6008 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006009 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 if (str != NULL)
6011 for (p = str; *p != NUL; )
6012 {
6013 if (*p == '\'')
6014 *r++ = '\'';
6015 MB_COPY_CHAR(p, r);
6016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006017 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 if (function)
6019 *r++ = ')';
6020 *r++ = NUL;
6021 }
6022 return s;
6023}
6024
6025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 * Get the value of an environment variable.
6027 * "arg" is pointing to the '$'. It is advanced to after the name.
6028 * If the environment variable was not set, silently assume it is empty.
6029 * Always return OK.
6030 */
6031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006032get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 int evaluate;
6036{
6037 char_u *string = NULL;
6038 int len;
6039 int cc;
6040 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006041 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042
6043 ++*arg;
6044 name = *arg;
6045 len = get_env_len(arg);
6046 if (evaluate)
6047 {
6048 if (len != 0)
6049 {
6050 cc = name[len];
6051 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006052 /* first try vim_getenv(), fast for normal environment vars */
6053 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006055 {
6056 if (!mustfree)
6057 string = vim_strsave(string);
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 else
6060 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006061 if (mustfree)
6062 vim_free(string);
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 /* next try expanding things like $VIM and ${HOME} */
6065 string = expand_env_save(name - 1);
6066 if (string != NULL && *string == '$')
6067 {
6068 vim_free(string);
6069 string = NULL;
6070 }
6071 }
6072 name[len] = cc;
6073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 rettv->v_type = VAR_STRING;
6075 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077
6078 return OK;
6079}
6080
6081/*
6082 * Array with names and number of arguments of all internal functions
6083 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6084 */
6085static struct fst
6086{
6087 char *f_name; /* function name */
6088 char f_min_argc; /* minimal number of arguments */
6089 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092} functions[] =
6093{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006094 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {"append", 2, 2, f_append},
6096 {"argc", 0, 0, f_argc},
6097 {"argidx", 0, 0, f_argidx},
6098 {"argv", 1, 1, f_argv},
6099 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006100 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {"bufexists", 1, 1, f_bufexists},
6102 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6103 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6104 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6105 {"buflisted", 1, 1, f_buflisted},
6106 {"bufloaded", 1, 1, f_bufloaded},
6107 {"bufname", 1, 1, f_bufname},
6108 {"bufnr", 1, 1, f_bufnr},
6109 {"bufwinnr", 1, 1, f_bufwinnr},
6110 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006111 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {"char2nr", 1, 1, f_char2nr},
6114 {"cindent", 1, 1, f_cindent},
6115 {"col", 1, 1, f_col},
6116 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006118 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {"cscope_connection",0,3, f_cscope_connection},
6120 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006121 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {"delete", 1, 1, f_delete},
6123 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006124 {"diff_filler", 1, 1, f_diff_filler},
6125 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006126 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006128 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {"eventhandler", 0, 0, f_eventhandler},
6130 {"executable", 1, 1, f_executable},
6131 {"exists", 1, 1, f_exists},
6132 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6135 {"filereadable", 1, 1, f_filereadable},
6136 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006137 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006138 {"finddir", 1, 3, f_finddir},
6139 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 {"fnamemodify", 2, 2, f_fnamemodify},
6141 {"foldclosed", 1, 1, f_foldclosed},
6142 {"foldclosedend", 1, 1, f_foldclosedend},
6143 {"foldlevel", 1, 1, f_foldlevel},
6144 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006145 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006148 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {"getbufvar", 2, 2, f_getbufvar},
6150 {"getchar", 0, 1, f_getchar},
6151 {"getcharmod", 0, 0, f_getcharmod},
6152 {"getcmdline", 0, 0, f_getcmdline},
6153 {"getcmdpos", 0, 0, f_getcmdpos},
6154 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006155 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006156 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {"getfsize", 1, 1, f_getfsize},
6158 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006159 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006160 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006161 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006162 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {"getregtype", 0, 1, f_getregtype},
6164 {"getwinposx", 0, 0, f_getwinposx},
6165 {"getwinposy", 0, 0, f_getwinposy},
6166 {"getwinvar", 2, 2, f_getwinvar},
6167 {"glob", 1, 1, f_glob},
6168 {"globpath", 2, 2, f_globpath},
6169 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 {"hasmapto", 1, 2, f_hasmapto},
6172 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6173 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6174 {"histadd", 2, 2, f_histadd},
6175 {"histdel", 1, 2, f_histdel},
6176 {"histget", 1, 2, f_histget},
6177 {"histnr", 1, 1, f_histnr},
6178 {"hlID", 1, 1, f_hlID},
6179 {"hlexists", 1, 1, f_hlexists},
6180 {"hostname", 0, 0, f_hostname},
6181 {"iconv", 3, 3, f_iconv},
6182 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006183 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {"input", 1, 2, f_input},
6185 {"inputdialog", 1, 3, f_inputdialog},
6186 {"inputrestore", 0, 0, f_inputrestore},
6187 {"inputsave", 0, 0, f_inputsave},
6188 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006191 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006192 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006193 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006194 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {"libcall", 3, 3, f_libcall},
6198 {"libcallnr", 3, 3, f_libcallnr},
6199 {"line", 1, 1, f_line},
6200 {"line2byte", 1, 1, f_line2byte},
6201 {"lispindent", 1, 1, f_lispindent},
6202 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006203 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 {"maparg", 1, 2, f_maparg},
6205 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006206 {"match", 2, 4, f_match},
6207 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006208 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006209 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006210 {"max", 1, 1, f_max},
6211 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006212#ifdef vim_mkdir
6213 {"mkdir", 1, 3, f_mkdir},
6214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {"mode", 0, 0, f_mode},
6216 {"nextnonblank", 1, 1, f_nextnonblank},
6217 {"nr2char", 1, 1, f_nr2char},
6218 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006219 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006220 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {"remote_expr", 2, 3, f_remote_expr},
6222 {"remote_foreground", 1, 1, f_remote_foreground},
6223 {"remote_peek", 1, 2, f_remote_peek},
6224 {"remote_read", 1, 1, f_remote_read},
6225 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006226 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006228 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006230 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {"search", 1, 2, f_search},
6232 {"searchpair", 3, 5, f_searchpair},
6233 {"server2client", 2, 2, f_server2client},
6234 {"serverlist", 0, 0, f_serverlist},
6235 {"setbufvar", 3, 3, f_setbufvar},
6236 {"setcmdpos", 1, 1, f_setcmdpos},
6237 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006238 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {"setreg", 2, 3, f_setreg},
6240 {"setwinvar", 3, 3, f_setwinvar},
6241 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006242 {"sort", 1, 2, f_sort},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006243 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244#ifdef HAVE_STRFTIME
6245 {"strftime", 1, 2, f_strftime},
6246#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006247 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {"strlen", 1, 1, f_strlen},
6250 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006251 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {"strtrans", 1, 1, f_strtrans},
6253 {"submatch", 1, 1, f_submatch},
6254 {"substitute", 4, 4, f_substitute},
6255 {"synID", 3, 3, f_synID},
6256 {"synIDattr", 2, 3, f_synIDattr},
6257 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006258 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006259 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {"tempname", 0, 0, f_tempname},
6261 {"tolower", 1, 1, f_tolower},
6262 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006263 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006265 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {"virtcol", 1, 1, f_virtcol},
6267 {"visualmode", 0, 1, f_visualmode},
6268 {"winbufnr", 1, 1, f_winbufnr},
6269 {"wincol", 0, 0, f_wincol},
6270 {"winheight", 1, 1, f_winheight},
6271 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006272 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {"winrestcmd", 0, 0, f_winrestcmd},
6274 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006275 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276};
6277
6278#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6279
6280/*
6281 * Function given to ExpandGeneric() to obtain the list of internal
6282 * or user defined function names.
6283 */
6284 char_u *
6285get_function_name(xp, idx)
6286 expand_T *xp;
6287 int idx;
6288{
6289 static int intidx = -1;
6290 char_u *name;
6291
6292 if (idx == 0)
6293 intidx = -1;
6294 if (intidx < 0)
6295 {
6296 name = get_user_func_name(xp, idx);
6297 if (name != NULL)
6298 return name;
6299 }
6300 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6301 {
6302 STRCPY(IObuff, functions[intidx].f_name);
6303 STRCAT(IObuff, "(");
6304 if (functions[intidx].f_max_argc == 0)
6305 STRCAT(IObuff, ")");
6306 return IObuff;
6307 }
6308
6309 return NULL;
6310}
6311
6312/*
6313 * Function given to ExpandGeneric() to obtain the list of internal or
6314 * user defined variable or function names.
6315 */
6316/*ARGSUSED*/
6317 char_u *
6318get_expr_name(xp, idx)
6319 expand_T *xp;
6320 int idx;
6321{
6322 static int intidx = -1;
6323 char_u *name;
6324
6325 if (idx == 0)
6326 intidx = -1;
6327 if (intidx < 0)
6328 {
6329 name = get_function_name(xp, idx);
6330 if (name != NULL)
6331 return name;
6332 }
6333 return get_user_var_name(xp, ++intidx);
6334}
6335
6336#endif /* FEAT_CMDL_COMPL */
6337
6338/*
6339 * Find internal function in table above.
6340 * Return index, or -1 if not found
6341 */
6342 static int
6343find_internal_func(name)
6344 char_u *name; /* name of the function */
6345{
6346 int first = 0;
6347 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6348 int cmp;
6349 int x;
6350
6351 /*
6352 * Find the function name in the table. Binary search.
6353 */
6354 while (first <= last)
6355 {
6356 x = first + ((unsigned)(last - first) >> 1);
6357 cmp = STRCMP(name, functions[x].f_name);
6358 if (cmp < 0)
6359 last = x - 1;
6360 else if (cmp > 0)
6361 first = x + 1;
6362 else
6363 return x;
6364 }
6365 return -1;
6366}
6367
6368/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6370 * name it contains, otherwise return "name".
6371 */
6372 static char_u *
6373deref_func_name(name, lenp)
6374 char_u *name;
6375 int *lenp;
6376{
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378 int cc;
6379
6380 cc = name[*lenp];
6381 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006382 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 {
6388 *lenp = 0;
6389 return (char_u *)""; /* just in case */
6390 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 *lenp = STRLEN(v->di_tv.vval.v_string);
6392 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 }
6394
6395 return name;
6396}
6397
6398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 * Allocate a variable for the result of a function.
6400 * Return OK or FAIL.
6401 */
6402 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006403get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6404 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 char_u *name; /* name of the function */
6406 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 char_u **arg; /* argument, pointing to the '(' */
6409 linenr_T firstline; /* first line of range */
6410 linenr_T lastline; /* last line of range */
6411 int *doesrange; /* return: function handled range */
6412 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 char_u *argp;
6416 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 int argcount = 0; /* number of arguments found */
6419
6420 /*
6421 * Get the arguments.
6422 */
6423 argp = *arg;
6424 while (argcount < MAX_FUNC_ARGS)
6425 {
6426 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6427 if (*argp == ')' || *argp == ',' || *argp == NUL)
6428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6430 {
6431 ret = FAIL;
6432 break;
6433 }
6434 ++argcount;
6435 if (*argp != ',')
6436 break;
6437 }
6438 if (*argp == ')')
6439 ++argp;
6440 else
6441 ret = FAIL;
6442
6443 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006444 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 {
6448 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006449 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006455 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
6457 *arg = skipwhite(argp);
6458 return ret;
6459}
6460
6461
6462/*
6463 * Call a function with its resolved parameters
6464 * Return OK or FAIL.
6465 */
6466 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 char_u *name; /* name of the function */
6470 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 linenr_T firstline; /* first line of range */
6475 linenr_T lastline; /* last line of range */
6476 int *doesrange; /* return: function handled range */
6477 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481#define ERROR_UNKNOWN 0
6482#define ERROR_TOOMANY 1
6483#define ERROR_TOOFEW 2
6484#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485#define ERROR_DICT 4
6486#define ERROR_NONE 5
6487#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 int error = ERROR_NONE;
6489 int i;
6490 int llen;
6491 ufunc_T *fp;
6492 int cc;
6493#define FLEN_FIXED 40
6494 char_u fname_buf[FLEN_FIXED + 1];
6495 char_u *fname;
6496
6497 /*
6498 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6499 * Change <SNR>123_name() to K_SNR 123_name().
6500 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6501 */
6502 cc = name[len];
6503 name[len] = NUL;
6504 llen = eval_fname_script(name);
6505 if (llen > 0)
6506 {
6507 fname_buf[0] = K_SPECIAL;
6508 fname_buf[1] = KS_EXTRA;
6509 fname_buf[2] = (int)KE_SNR;
6510 i = 3;
6511 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6512 {
6513 if (current_SID <= 0)
6514 error = ERROR_SCRIPT;
6515 else
6516 {
6517 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6518 i = (int)STRLEN(fname_buf);
6519 }
6520 }
6521 if (i + STRLEN(name + llen) < FLEN_FIXED)
6522 {
6523 STRCPY(fname_buf + i, name + llen);
6524 fname = fname_buf;
6525 }
6526 else
6527 {
6528 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6529 if (fname == NULL)
6530 error = ERROR_OTHER;
6531 else
6532 {
6533 mch_memmove(fname, fname_buf, (size_t)i);
6534 STRCPY(fname + i, name + llen);
6535 }
6536 }
6537 }
6538 else
6539 fname = name;
6540
6541 *doesrange = FALSE;
6542
6543
6544 /* execute the function if no errors detected and executing */
6545 if (evaluate && error == ERROR_NONE)
6546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 error = ERROR_UNKNOWN;
6549
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006550 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
6552 /*
6553 * User defined function.
6554 */
6555 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006558 /* Trigger FuncUndefined event, may load the function. */
6559 if (fp == NULL
6560 && apply_autocmds(EVENT_FUNCUNDEFINED,
6561 fname, fname, TRUE, NULL)
6562 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006564 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 fp = find_func(fname);
6566 }
6567#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006568 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006569 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006570 {
6571 /* loaded a package, search for the function again */
6572 fp = find_func(fname);
6573 }
6574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (fp != NULL)
6576 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006577 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006579 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006581 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006583 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 else
6586 {
6587 /*
6588 * Call the user function.
6589 * Save and restore search patterns, script variables and
6590 * redo buffer.
6591 */
6592 save_search_patterns();
6593 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006594 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006595 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006596 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006597 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6598 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6599 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006600 /* Function was unreferenced while being used, free it
6601 * now. */
6602 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 restoreRedobuff();
6604 restore_search_patterns();
6605 error = ERROR_NONE;
6606 }
6607 }
6608 }
6609 else
6610 {
6611 /*
6612 * Find the function name in the table, call its implementation.
6613 */
6614 i = find_internal_func(fname);
6615 if (i >= 0)
6616 {
6617 if (argcount < functions[i].f_min_argc)
6618 error = ERROR_TOOFEW;
6619 else if (argcount > functions[i].f_max_argc)
6620 error = ERROR_TOOMANY;
6621 else
6622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 error = ERROR_NONE;
6626 }
6627 }
6628 }
6629 /*
6630 * The function call (or "FuncUndefined" autocommand sequence) might
6631 * have been aborted by an error, an interrupt, or an explicitly thrown
6632 * exception that has not been caught so far. This situation can be
6633 * tested for by calling aborting(). For an error in an internal
6634 * function or for the "E132" error in call_user_func(), however, the
6635 * throw point at which the "force_abort" flag (temporarily reset by
6636 * emsg()) is normally updated has not been reached yet. We need to
6637 * update that flag first to make aborting() reliable.
6638 */
6639 update_force_abort();
6640 }
6641 if (error == ERROR_NONE)
6642 ret = OK;
6643
6644 /*
6645 * Report an error unless the argument evaluation or function call has been
6646 * cancelled due to an aborting error, an interrupt, or an exception.
6647 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 if (!aborting())
6649 {
6650 switch (error)
6651 {
6652 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006653 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 break;
6655 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657 break;
6658 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 name);
6661 break;
6662 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 name);
6665 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006666 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006667 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 name);
6669 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006670 }
6671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 name[len] = cc;
6674 if (fname != name && fname != fname_buf)
6675 vim_free(fname);
6676
6677 return ret;
6678}
6679
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006680/*
6681 * Give an error message with a function name. Handle <SNR> things.
6682 */
6683 static void
6684emsg_funcname(msg, name)
6685 char *msg;
6686 char_u *name;
6687{
6688 char_u *p;
6689
6690 if (*name == K_SPECIAL)
6691 p = concat_str((char_u *)"<SNR>", name + 3);
6692 else
6693 p = name;
6694 EMSG2(_(msg), p);
6695 if (p != name)
6696 vim_free(p);
6697}
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699/*********************************************
6700 * Implementation of the built-in functions
6701 */
6702
6703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006704 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 */
6706 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006707f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 typval_T *argvars;
6709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
Bram Moolenaar33570922005-01-25 22:26:29 +00006711 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006714 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006716 if ((l = argvars[0].vval.v_list) != NULL
6717 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6718 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006719 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720 }
6721 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006722 EMSG(_(e_listreq));
6723}
6724
6725/*
6726 * "append(lnum, string/list)" function
6727 */
6728 static void
6729f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006730 typval_T *argvars;
6731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006732{
6733 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 list_T *l = NULL;
6735 listitem_T *li = NULL;
6736 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006737 long added = 0;
6738
6739 rettv->vval.v_number = 1; /* Default: Failed */
6740 lnum = get_tv_lnum(argvars);
6741 if (lnum >= 0
6742 && lnum <= curbuf->b_ml.ml_line_count
6743 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006745 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006747 l = argvars[1].vval.v_list;
6748 if (l == NULL)
6749 return;
6750 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006752 for (;;)
6753 {
6754 if (l == NULL)
6755 tv = &argvars[1]; /* append a string */
6756 else if (li == NULL)
6757 break; /* end of list */
6758 else
6759 tv = &li->li_tv; /* append item from list */
6760 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6761 ++added;
6762 if (l == NULL)
6763 break;
6764 li = li->li_next;
6765 }
6766
6767 appended_lines_mark(lnum, added);
6768 if (curwin->w_cursor.lnum > lnum)
6769 curwin->w_cursor.lnum += added;
6770 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772}
6773
6774/*
6775 * "argc()" function
6776 */
6777/* ARGSUSED */
6778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006779f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 typval_T *argvars;
6781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006783 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
6785
6786/*
6787 * "argidx()" function
6788 */
6789/* ARGSUSED */
6790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 typval_T *argvars;
6793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796}
6797
6798/*
6799 * "argv(nr)" function
6800 */
6801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006802f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006803 typval_T *argvars;
6804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int idx;
6807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006808 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 rettv->vval.v_string = NULL;
6813 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816/*
6817 * "browse(save, title, initdir, default)" function
6818 */
6819/* ARGSUSED */
6820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006821f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 typval_T *argvars;
6823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
6825#ifdef FEAT_BROWSE
6826 int save;
6827 char_u *title;
6828 char_u *initdir;
6829 char_u *defname;
6830 char_u buf[NUMBUFLEN];
6831 char_u buf2[NUMBUFLEN];
6832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006833 save = get_tv_number(&argvars[0]);
6834 title = get_tv_string(&argvars[1]);
6835 initdir = get_tv_string_buf(&argvars[2], buf);
6836 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006838 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006839 do_browse(save ? BROWSE_SAVE : 0,
6840 title, defname, NULL, initdir, NULL, curbuf);
6841#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006842 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006843#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006845}
6846
6847/*
6848 * "browsedir(title, initdir)" function
6849 */
6850/* ARGSUSED */
6851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006852f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006853 typval_T *argvars;
6854 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006855{
6856#ifdef FEAT_BROWSE
6857 char_u *title;
6858 char_u *initdir;
6859 char_u buf[NUMBUFLEN];
6860
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006861 title = get_tv_string(&argvars[0]);
6862 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006864 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006865 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870}
6871
Bram Moolenaar33570922005-01-25 22:26:29 +00006872static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874/*
6875 * Find a buffer by number or exact name.
6876 */
6877 static buf_T *
6878find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
6881 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006883 if (avar->v_type == VAR_NUMBER)
6884 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006885 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006887 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006888 if (buf == NULL)
6889 {
6890 /* No full path name match, try a match with a URL or a "nofile"
6891 * buffer, these don't use the full path. */
6892 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6893 if (buf->b_fname != NULL
6894 && (path_with_url(buf->b_fname)
6895#ifdef FEAT_QUICKFIX
6896 || bt_nofile(buf)
6897#endif
6898 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006899 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006900 break;
6901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
6903 return buf;
6904}
6905
6906/*
6907 * "bufexists(expr)" function
6908 */
6909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006911 typval_T *argvars;
6912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006914 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915}
6916
6917/*
6918 * "buflisted(expr)" function
6919 */
6920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006921f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 typval_T *argvars;
6923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 buf_T *buf;
6926
6927 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006928 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929}
6930
6931/*
6932 * "bufloaded(expr)" function
6933 */
6934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 typval_T *argvars;
6937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 buf_T *buf;
6940
6941 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943}
6944
Bram Moolenaar33570922005-01-25 22:26:29 +00006945static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947/*
6948 * Get buffer by number or pattern.
6949 */
6950 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006951get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006954 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 int save_magic;
6956 char_u *save_cpo;
6957 buf_T *buf;
6958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006959 if (tv->v_type == VAR_NUMBER)
6960 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006961 if (tv->v_type != VAR_STRING)
6962 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (name == NULL || *name == NUL)
6964 return curbuf;
6965 if (name[0] == '$' && name[1] == NUL)
6966 return lastbuf;
6967
6968 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6969 save_magic = p_magic;
6970 p_magic = TRUE;
6971 save_cpo = p_cpo;
6972 p_cpo = (char_u *)"";
6973
6974 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6975 TRUE, FALSE));
6976
6977 p_magic = save_magic;
6978 p_cpo = save_cpo;
6979
6980 /* If not found, try expanding the name, like done for bufexists(). */
6981 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006982 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 return buf;
6985}
6986
6987/*
6988 * "bufname(expr)" function
6989 */
6990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006991f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006992 typval_T *argvars;
6993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 buf_T *buf;
6996
6997 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006998 buf = get_buf_tv(&argvars[0]);
6999 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007001 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 --emsg_off;
7005}
7006
7007/*
7008 * "bufnr(expr)" function
7009 */
7010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007011f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 typval_T *argvars;
7013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 buf_T *buf;
7016
7017 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007018 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007020 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007022 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 --emsg_off;
7024}
7025
7026/*
7027 * "bufwinnr(nr)" function
7028 */
7029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 typval_T *argvars;
7032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034#ifdef FEAT_WINDOWS
7035 win_T *wp;
7036 int winnr = 0;
7037#endif
7038 buf_T *buf;
7039
7040 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007041 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042#ifdef FEAT_WINDOWS
7043 for (wp = firstwin; wp; wp = wp->w_next)
7044 {
7045 ++winnr;
7046 if (wp->w_buffer == buf)
7047 break;
7048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007049 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007051 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#endif
7053 --emsg_off;
7054}
7055
7056/*
7057 * "byte2line(byte)" function
7058 */
7059/*ARGSUSED*/
7060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007061f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 typval_T *argvars;
7063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#else
7068 long boff = 0;
7069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007070 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007074 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 (linenr_T)0, &boff);
7076#endif
7077}
7078
7079/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007080 * "byteidx()" function
7081 */
7082/*ARGSUSED*/
7083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007084f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 typval_T *argvars;
7086 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007087{
7088#ifdef FEAT_MBYTE
7089 char_u *t;
7090#endif
7091 char_u *str;
7092 long idx;
7093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007094 str = get_tv_string(&argvars[0]);
7095 idx = get_tv_number(&argvars[1]);
7096 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007097 if (idx < 0)
7098 return;
7099
7100#ifdef FEAT_MBYTE
7101 t = str;
7102 for ( ; idx > 0; idx--)
7103 {
7104 if (*t == NUL) /* EOL reached */
7105 return;
7106 t += mb_ptr2len_check(t);
7107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007108 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007109#else
7110 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007111 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007112#endif
7113}
7114
7115/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007116 * "call(func, arglist)" function
7117 */
7118 static void
7119f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 typval_T *argvars;
7121 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007122{
7123 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007125 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007127 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007129
7130 rettv->vval.v_number = 0;
7131 if (argvars[1].v_type != VAR_LIST)
7132 {
7133 EMSG(_(e_listreq));
7134 return;
7135 }
7136 if (argvars[1].vval.v_list == NULL)
7137 return;
7138
7139 if (argvars[0].v_type == VAR_FUNC)
7140 func = argvars[0].vval.v_string;
7141 else
7142 func = get_tv_string(&argvars[0]);
7143
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 if (argvars[2].v_type != VAR_UNKNOWN)
7145 {
7146 if (argvars[2].v_type != VAR_DICT)
7147 {
7148 EMSG(_(e_dictreq));
7149 return;
7150 }
7151 selfdict = argvars[2].vval.v_dict;
7152 }
7153
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007154 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7155 item = item->li_next)
7156 {
7157 if (argc == MAX_FUNC_ARGS)
7158 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007159 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007160 break;
7161 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007162 /* Make a copy of each argument. This is needed to be able to set
7163 * v_lock to VAR_FIXED in the copy without changing the original list.
7164 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007165 copy_tv(&item->li_tv, &argv[argc++]);
7166 }
7167
7168 if (item == NULL)
7169 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7171 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007172
7173 /* Free the arguments. */
7174 while (argc > 0)
7175 clear_tv(&argv[--argc]);
7176}
7177
7178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 * "char2nr(string)" function
7180 */
7181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007182f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 typval_T *argvars;
7184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186#ifdef FEAT_MBYTE
7187 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007188 rettv->vval.v_number =
7189 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 else
7191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007192 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195/*
7196 * "cindent(lnum)" function
7197 */
7198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007199f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 typval_T *argvars;
7201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203#ifdef FEAT_CINDENT
7204 pos_T pos;
7205 linenr_T lnum;
7206
7207 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007208 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7210 {
7211 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007212 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 curwin->w_cursor = pos;
7214 }
7215 else
7216#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
7220/*
7221 * "col(string)" function
7222 */
7223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 typval_T *argvars;
7226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 colnr_T col = 0;
7229 pos_T *fp;
7230
7231 fp = var2fpos(&argvars[0], FALSE);
7232 if (fp != NULL)
7233 {
7234 if (fp->col == MAXCOL)
7235 {
7236 /* '> can be MAXCOL, get the length of the line then */
7237 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7238 col = STRLEN(ml_get(fp->lnum)) + 1;
7239 else
7240 col = MAXCOL;
7241 }
7242 else
7243 {
7244 col = fp->col + 1;
7245#ifdef FEAT_VIRTUALEDIT
7246 /* col(".") when the cursor is on the NUL at the end of the line
7247 * because of "coladd" can be seen as an extra column. */
7248 if (virtual_active() && fp == &curwin->w_cursor)
7249 {
7250 char_u *p = ml_get_cursor();
7251
7252 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7253 curwin->w_virtcol - curwin->w_cursor.coladd))
7254 {
7255# ifdef FEAT_MBYTE
7256 int l;
7257
7258 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7259 col += l;
7260# else
7261 if (*p != NUL && p[1] == NUL)
7262 ++col;
7263# endif
7264 }
7265 }
7266#endif
7267 }
7268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
7272/*
7273 * "confirm(message, buttons[, default [, type]])" function
7274 */
7275/*ARGSUSED*/
7276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 typval_T *argvars;
7279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280{
7281#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7282 char_u *message;
7283 char_u *buttons = NULL;
7284 char_u buf[NUMBUFLEN];
7285 char_u buf2[NUMBUFLEN];
7286 int def = 1;
7287 int type = VIM_GENERIC;
7288 int c;
7289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007290 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007293 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007294 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007296 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007299 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007300 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 switch (TOUPPER_ASC(c))
7302 {
7303 case 'E': type = VIM_ERROR; break;
7304 case 'Q': type = VIM_QUESTION; break;
7305 case 'I': type = VIM_INFO; break;
7306 case 'W': type = VIM_WARNING; break;
7307 case 'G': type = VIM_GENERIC; break;
7308 }
7309 }
7310 }
7311 }
7312
7313 if (buttons == NULL || *buttons == NUL)
7314 buttons = (char_u *)_("&Ok");
7315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007316 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 def, NULL);
7318#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#endif
7321}
7322
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323/*
7324 * "copy()" function
7325 */
7326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007327f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 typval_T *argvars;
7329 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007330{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007331 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007335 * "count()" function
7336 */
7337 static void
7338f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 typval_T *argvars;
7340 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007341{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007342 long n = 0;
7343 int ic = FALSE;
7344
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007346 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 listitem_T *li;
7348 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007350
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 if ((l = argvars[0].vval.v_list) != NULL)
7352 {
7353 li = l->lv_first;
7354 if (argvars[2].v_type != VAR_UNKNOWN)
7355 {
7356 ic = get_tv_number(&argvars[2]);
7357 if (argvars[3].v_type != VAR_UNKNOWN)
7358 {
7359 idx = get_tv_number(&argvars[3]);
7360 li = list_find(l, idx);
7361 if (li == NULL)
7362 EMSGN(_(e_listidx), idx);
7363 }
7364 }
7365
7366 for ( ; li != NULL; li = li->li_next)
7367 if (tv_equal(&li->li_tv, &argvars[1], ic))
7368 ++n;
7369 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 else if (argvars[0].v_type == VAR_DICT)
7372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 int todo;
7374 dict_T *d;
7375 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 if ((d = argvars[0].vval.v_dict) != NULL)
7378 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 if (argvars[2].v_type != VAR_UNKNOWN)
7380 {
7381 ic = get_tv_number(&argvars[2]);
7382 if (argvars[3].v_type != VAR_UNKNOWN)
7383 EMSG(_(e_invarg));
7384 }
7385
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 todo = d->dv_hashtab.ht_used;
7387 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 {
7389 if (!HASHITEM_EMPTY(hi))
7390 {
7391 --todo;
7392 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7393 ++n;
7394 }
7395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 }
7397 }
7398 else
7399 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007400 rettv->vval.v_number = n;
7401}
7402
7403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7405 *
7406 * Checks the existence of a cscope connection.
7407 */
7408/*ARGSUSED*/
7409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007410f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 typval_T *argvars;
7412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414#ifdef FEAT_CSCOPE
7415 int num = 0;
7416 char_u *dbpath = NULL;
7417 char_u *prepend = NULL;
7418 char_u buf[NUMBUFLEN];
7419
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007420 if (argvars[0].v_type != VAR_UNKNOWN
7421 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 num = (int)get_tv_number(&argvars[0]);
7424 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#endif
7433}
7434
7435/*
7436 * "cursor(lnum, col)" function
7437 *
7438 * Moves the cursor to the specified line and column
7439 */
7440/*ARGSUSED*/
7441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 typval_T *argvars;
7444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 long line, col;
7447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007448 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (line > 0)
7450 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007451 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 if (col > 0)
7453 curwin->w_cursor.col = col - 1;
7454#ifdef FEAT_VIRTUALEDIT
7455 curwin->w_cursor.coladd = 0;
7456#endif
7457
7458 /* Make sure the cursor is in a valid position. */
7459 check_cursor();
7460#ifdef FEAT_MBYTE
7461 /* Correct cursor for multi-byte character. */
7462 if (has_mbyte)
7463 mb_adjust_cursor();
7464#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007465
7466 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467}
7468
7469/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007470 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 */
7472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007473f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 typval_T *argvars;
7475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007477 static int copyID = 0;
7478 int noref = 0;
7479
7480 if (argvars[1].v_type != VAR_UNKNOWN)
7481 noref = get_tv_number(&argvars[1]);
7482 if (noref < 0 || noref > 1)
7483 EMSG(_(e_invarg));
7484 else
7485 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486}
7487
7488/*
7489 * "delete()" function
7490 */
7491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007492f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 typval_T *argvars;
7494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007497 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007499 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500}
7501
7502/*
7503 * "did_filetype()" function
7504 */
7505/*ARGSUSED*/
7506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 typval_T *argvars;
7509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007514 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#endif
7516}
7517
7518/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007519 * "diff_filler()" function
7520 */
7521/*ARGSUSED*/
7522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007523f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 typval_T *argvars;
7525 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007526{
7527#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007529#endif
7530}
7531
7532/*
7533 * "diff_hlID()" function
7534 */
7535/*ARGSUSED*/
7536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007537f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 typval_T *argvars;
7539 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007540{
7541#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007542 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007543 static linenr_T prev_lnum = 0;
7544 static int changedtick = 0;
7545 static int fnum = 0;
7546 static int change_start = 0;
7547 static int change_end = 0;
7548 static enum hlf_value hlID = 0;
7549 int filler_lines;
7550 int col;
7551
7552 if (lnum != prev_lnum
7553 || changedtick != curbuf->b_changedtick
7554 || fnum != curbuf->b_fnum)
7555 {
7556 /* New line, buffer, change: need to get the values. */
7557 filler_lines = diff_check(curwin, lnum);
7558 if (filler_lines < 0)
7559 {
7560 if (filler_lines == -1)
7561 {
7562 change_start = MAXCOL;
7563 change_end = -1;
7564 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7565 hlID = HLF_ADD; /* added line */
7566 else
7567 hlID = HLF_CHD; /* changed line */
7568 }
7569 else
7570 hlID = HLF_ADD; /* added line */
7571 }
7572 else
7573 hlID = (enum hlf_value)0;
7574 prev_lnum = lnum;
7575 changedtick = curbuf->b_changedtick;
7576 fnum = curbuf->b_fnum;
7577 }
7578
7579 if (hlID == HLF_CHD || hlID == HLF_TXD)
7580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007582 if (col >= change_start && col <= change_end)
7583 hlID = HLF_TXD; /* changed text */
7584 else
7585 hlID = HLF_CHD; /* changed line */
7586 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007588#endif
7589}
7590
7591/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007592 * "empty({expr})" function
7593 */
7594 static void
7595f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 typval_T *argvars;
7597 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007598{
7599 int n;
7600
7601 switch (argvars[0].v_type)
7602 {
7603 case VAR_STRING:
7604 case VAR_FUNC:
7605 n = argvars[0].vval.v_string == NULL
7606 || *argvars[0].vval.v_string == NUL;
7607 break;
7608 case VAR_NUMBER:
7609 n = argvars[0].vval.v_number == 0;
7610 break;
7611 case VAR_LIST:
7612 n = argvars[0].vval.v_list == NULL
7613 || argvars[0].vval.v_list->lv_first == NULL;
7614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 case VAR_DICT:
7616 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007619 default:
7620 EMSG2(_(e_intern2), "f_empty()");
7621 n = 0;
7622 }
7623
7624 rettv->vval.v_number = n;
7625}
7626
7627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 * "escape({string}, {chars})" function
7629 */
7630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007631f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 typval_T *argvars;
7633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634{
7635 char_u buf[NUMBUFLEN];
7636
Bram Moolenaar758711c2005-02-02 23:11:38 +00007637 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7638 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007643 * "eval()" function
7644 */
7645/*ARGSUSED*/
7646 static void
7647f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 typval_T *argvars;
7649 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650{
7651 char_u *s;
7652
7653 s = get_tv_string(&argvars[0]);
7654 s = skipwhite(s);
7655
7656 if (eval1(&s, rettv, TRUE) == FAIL)
7657 rettv->vval.v_number = 0;
7658 else if (*s != NUL)
7659 EMSG(_(e_trailing));
7660}
7661
7662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * "eventhandler()" function
7664 */
7665/*ARGSUSED*/
7666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672}
7673
7674/*
7675 * "executable()" function
7676 */
7677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683}
7684
7685/*
7686 * "exists()" function
7687 */
7688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 typval_T *argvars;
7691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 char_u *p;
7694 char_u *name;
7695 int n = FALSE;
7696 int len = 0;
7697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (*p == '$') /* environment variable */
7700 {
7701 /* first try "normal" environment variables (fast) */
7702 if (mch_getenv(p + 1) != NULL)
7703 n = TRUE;
7704 else
7705 {
7706 /* try expanding things like $VIM and ${HOME} */
7707 p = expand_env_save(p);
7708 if (p != NULL && *p != '$')
7709 n = TRUE;
7710 vim_free(p);
7711 }
7712 }
7713 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 else if (*p == '*') /* internal or user defined function */
7716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719 else if (*p == ':')
7720 {
7721 n = cmd_exists(p + 1);
7722 }
7723 else if (*p == '#')
7724 {
7725#ifdef FEAT_AUTOCMD
7726 name = p + 1;
7727 p = vim_strchr(name, '#');
7728 if (p != NULL)
7729 n = au_exists(name, p, p + 1);
7730 else
7731 n = au_exists(name, name + STRLEN(name), NULL);
7732#endif
7733 }
7734 else /* internal variable */
7735 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007736 char_u *tofree;
7737 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007739 /* get_name_len() takes care of expanding curly braces */
7740 name = p;
7741 len = get_name_len(&p, &tofree, TRUE, FALSE);
7742 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007744 if (tofree != NULL)
7745 name = tofree;
7746 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7747 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007749 /* handle d.key, l[idx], f(expr) */
7750 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7751 if (n)
7752 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007756 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761
7762/*
7763 * "expand()" function
7764 */
7765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 typval_T *argvars;
7768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770 char_u *s;
7771 int len;
7772 char_u *errormsg;
7773 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7774 expand_T xpc;
7775
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->v_type = VAR_STRING;
7777 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 if (*s == '%' || *s == '#' || *s == '<')
7779 {
7780 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 --emsg_off;
7783 }
7784 else
7785 {
7786 /* When the optional second argument is non-zero, don't remove matches
7787 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 flags |= WILD_KEEP_ALL;
7790 ExpandInit(&xpc);
7791 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 ExpandCleanup(&xpc);
7794 }
7795}
7796
7797/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007800 */
7801 static void
7802f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007805{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007806 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007808 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 list_T *l1, *l2;
7810 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007812
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 l1 = argvars[0].vval.v_list;
7814 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007815 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7816 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 {
7818 if (argvars[2].v_type != VAR_UNKNOWN)
7819 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007820 before = get_tv_number(&argvars[2]);
7821 if (before == l1->lv_len)
7822 item = NULL;
7823 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007825 item = list_find(l1, before);
7826 if (item == NULL)
7827 {
7828 EMSGN(_(e_listidx), before);
7829 return;
7830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 }
7832 }
7833 else
7834 item = NULL;
7835 list_extend(l1, l2, item);
7836
7837 ++l1->lv_refcount;
7838 copy_tv(&argvars[0], rettv);
7839 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7842 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 dict_T *d1, *d2;
7844 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 char_u *action;
7846 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007848 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849
7850 d1 = argvars[0].vval.v_dict;
7851 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007852 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7853 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 {
7855 /* Check the third argument. */
7856 if (argvars[2].v_type != VAR_UNKNOWN)
7857 {
7858 static char *(av[]) = {"keep", "force", "error"};
7859
7860 action = get_tv_string(&argvars[2]);
7861 for (i = 0; i < 3; ++i)
7862 if (STRCMP(action, av[i]) == 0)
7863 break;
7864 if (i == 3)
7865 {
7866 EMSGN(_(e_invarg2), action);
7867 return;
7868 }
7869 }
7870 else
7871 action = (char_u *)"force";
7872
7873 /* Go over all entries in the second dict and add them to the
7874 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007875 todo = d2->dv_hashtab.ht_used;
7876 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007878 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007880 --todo;
7881 di1 = dict_find(d1, hi2->hi_key, -1);
7882 if (di1 == NULL)
7883 {
7884 di1 = dictitem_copy(HI2DI(hi2));
7885 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7886 dictitem_free(di1);
7887 }
7888 else if (*action == 'e')
7889 {
7890 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7891 break;
7892 }
7893 else if (*action == 'f')
7894 {
7895 clear_tv(&di1->di_tv);
7896 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 }
7899 }
7900
7901 ++d1->dv_refcount;
7902 copy_tv(&argvars[0], rettv);
7903 }
7904 }
7905 else
7906 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907}
7908
7909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 * "filereadable()" function
7911 */
7912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007914 typval_T *argvars;
7915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 FILE *fd;
7918 char_u *p;
7919 int n;
7920
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7923 {
7924 n = TRUE;
7925 fclose(fd);
7926 }
7927 else
7928 n = FALSE;
7929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931}
7932
7933/*
7934 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7935 * rights to write into.
7936 */
7937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 typval_T *argvars;
7940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
7942 char_u *p;
7943 int retval = 0;
7944#if defined(UNIX) || defined(VMS)
7945 int perm = 0;
7946#endif
7947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949#if defined(UNIX) || defined(VMS)
7950 perm = mch_getperm(p);
7951#endif
7952#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7953 if (
7954# ifdef WIN3264
7955 mch_writable(p) &&
7956# else
7957# if defined(UNIX) || defined(VMS)
7958 (perm & 0222) &&
7959# endif
7960# endif
7961 mch_access((char *)p, W_OK) == 0
7962 )
7963#endif
7964 {
7965 ++retval;
7966 if (mch_isdir(p))
7967 ++retval;
7968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007969 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970}
7971
Bram Moolenaar33570922005-01-25 22:26:29 +00007972static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007973
7974 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007975findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 typval_T *argvars;
7977 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007978 int dir;
7979{
7980#ifdef FEAT_SEARCHPATH
7981 char_u *fname;
7982 char_u *fresult = NULL;
7983 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7984 char_u *p;
7985 char_u pathbuf[NUMBUFLEN];
7986 int count = 1;
7987 int first = TRUE;
7988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007990
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007991 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007994 if (*p != NUL)
7995 path = p;
7996
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007998 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007999 }
8000
8001 do
8002 {
8003 vim_free(fresult);
8004 fresult = find_file_in_path_option(first ? fname : NULL,
8005 first ? (int)STRLEN(fname) : 0,
8006 0, first, path, dir, NULL);
8007 first = FALSE;
8008 } while (--count > 0 && fresult != NULL);
8009
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008011#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008013#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008014 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008015}
8016
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008017static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8018static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008019static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8020static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021
8022/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008023 * Prepare v: variable "idx" to be used.
8024 * Save the current typeval in "save_tv".
8025 * When not used yet add the variable to the v: hashtable.
8026 */
8027 static void
8028prepare_vimvar(idx, save_tv)
8029 int idx;
8030 typval_T *save_tv;
8031{
8032 *save_tv = vimvars[idx].vv_tv;
8033 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8034 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8035}
8036
8037/*
8038 * Restore v: variable "idx" to typeval "save_tv".
8039 * When no longer defined, remove the variable from the v: hashtable.
8040 */
8041 static void
8042restore_vimvar(idx, save_tv)
8043 int idx;
8044 typval_T *save_tv;
8045{
8046 hashitem_T *hi;
8047
8048 clear_tv(&vimvars[idx].vv_tv);
8049 vimvars[idx].vv_tv = *save_tv;
8050 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8051 {
8052 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8053 if (HASHITEM_EMPTY(hi))
8054 EMSG2(_(e_intern2), "restore_vimvar()");
8055 else
8056 hash_remove(&vimvarht, hi);
8057 }
8058}
8059
8060/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008061 * Implementation of map() and filter().
8062 */
8063 static void
8064filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 typval_T *argvars;
8066 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008067 int map;
8068{
8069 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 listitem_T *li, *nli;
8072 list_T *l = NULL;
8073 dictitem_T *di;
8074 hashtab_T *ht;
8075 hashitem_T *hi;
8076 dict_T *d = NULL;
8077 typval_T save_val;
8078 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008080 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008081 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8082
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008083
8084 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008086 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008087 if ((l = argvars[0].vval.v_list) == NULL
8088 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008089 return;
8090 }
8091 else if (argvars[0].v_type == VAR_DICT)
8092 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008093 if ((d = argvars[0].vval.v_dict) == NULL
8094 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008095 return;
8096 }
8097 else
8098 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008099 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 return;
8101 }
8102
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008103 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105
8106 if (argvars[0].v_type == VAR_DICT)
8107 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008108 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008109 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008110
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008112 hash_lock(ht);
8113 todo = ht->ht_used;
8114 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008116 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008118 --todo;
8119 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008120 if (tv_check_lock(di->di_tv.v_lock, msg))
8121 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008122 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008123 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8124 break;
8125 if (!map && rem)
8126 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008128 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008129 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008130 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008131
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008132 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 else
8135 {
8136 for (li = l->lv_first; li != NULL; li = nli)
8137 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008138 if (tv_check_lock(li->li_tv.v_lock, msg))
8139 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008140 nli = li->li_next;
8141 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8142 break;
8143 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008144 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 }
8146 }
8147
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008148 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149
8150 copy_tv(&argvars[0], rettv);
8151}
8152
8153 static int
8154filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156 char_u *expr;
8157 int map;
8158 int *remp;
8159{
Bram Moolenaar33570922005-01-25 22:26:29 +00008160 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008161 char_u *s;
8162
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008164 s = expr;
8165 if (eval1(&s, &rettv, TRUE) == FAIL)
8166 return FAIL;
8167 if (*s != NUL) /* check for trailing chars after expr */
8168 {
8169 EMSG2(_(e_invexpr2), s);
8170 return FAIL;
8171 }
8172 if (map)
8173 {
8174 /* map(): replace the list item value */
8175 clear_tv(tv);
8176 *tv = rettv;
8177 }
8178 else
8179 {
8180 /* filter(): when expr is zero remove the item */
8181 *remp = (get_tv_number(&rettv) == 0);
8182 clear_tv(&rettv);
8183 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008185 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008186}
8187
8188/*
8189 * "filter()" function
8190 */
8191 static void
8192f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 typval_T *argvars;
8194 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008195{
8196 filter_map(argvars, rettv, FALSE);
8197}
8198
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008200 * "finddir({fname}[, {path}[, {count}]])" function
8201 */
8202 static void
8203f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 typval_T *argvars;
8205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008206{
8207 findfilendir(argvars, rettv, TRUE);
8208}
8209
8210/*
8211 * "findfile({fname}[, {path}[, {count}]])" function
8212 */
8213 static void
8214f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 typval_T *argvars;
8216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008217{
8218 findfilendir(argvars, rettv, FALSE);
8219}
8220
8221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 * "fnamemodify({fname}, {mods})" function
8223 */
8224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 typval_T *argvars;
8227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 char_u *fname;
8230 char_u *mods;
8231 int usedlen = 0;
8232 int len;
8233 char_u *fbuf = NULL;
8234 char_u buf[NUMBUFLEN];
8235
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008236 fname = get_tv_string(&argvars[0]);
8237 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 len = (int)STRLEN(fname);
8239
8240 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8241
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 vim_free(fbuf);
8248}
8249
Bram Moolenaar33570922005-01-25 22:26:29 +00008250static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252/*
8253 * "foldclosed()" function
8254 */
8255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 typval_T *argvars;
8258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 int end;
8260{
8261#ifdef FEAT_FOLDING
8262 linenr_T lnum;
8263 linenr_T first, last;
8264
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008265 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8267 {
8268 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8269 {
8270 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 return;
8275 }
8276 }
8277#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279}
8280
8281/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008282 * "foldclosed()" function
8283 */
8284 static void
8285f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 typval_T *argvars;
8287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008288{
8289 foldclosed_both(argvars, rettv, FALSE);
8290}
8291
8292/*
8293 * "foldclosedend()" function
8294 */
8295 static void
8296f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 typval_T *argvars;
8298 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008299{
8300 foldclosed_both(argvars, rettv, TRUE);
8301}
8302
8303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 * "foldlevel()" function
8305 */
8306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008307f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 typval_T *argvars;
8309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311#ifdef FEAT_FOLDING
8312 linenr_T lnum;
8313
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008314 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 else
8318#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320}
8321
8322/*
8323 * "foldtext()" function
8324 */
8325/*ARGSUSED*/
8326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008327f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *argvars;
8329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331#ifdef FEAT_FOLDING
8332 linenr_T lnum;
8333 char_u *s;
8334 char_u *r;
8335 int len;
8336 char *txt;
8337#endif
8338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 rettv->v_type = VAR_STRING;
8340 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008342 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8343 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8344 <= curbuf->b_ml.ml_line_count
8345 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {
8347 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008348 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8349 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 if (!linewhite(lnum))
8352 break;
8353 ++lnum;
8354 }
8355
8356 /* Find interesting text in this line. */
8357 s = skipwhite(ml_get(lnum));
8358 /* skip C comment-start */
8359 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008362 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008363 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008364 {
8365 s = skipwhite(ml_get(lnum + 1));
8366 if (*s == '*')
8367 s = skipwhite(s + 1);
8368 }
8369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 txt = _("+-%s%3ld lines: ");
8371 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008372 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 + 20 /* for %3ld */
8374 + STRLEN(s))); /* concatenated */
8375 if (r != NULL)
8376 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008377 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8378 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8379 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 len = (int)STRLEN(r);
8381 STRCAT(r, s);
8382 /* remove 'foldmarker' and 'commentstring' */
8383 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 }
8386 }
8387#endif
8388}
8389
8390/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008391 * "foldtextresult(lnum)" function
8392 */
8393/*ARGSUSED*/
8394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008396 typval_T *argvars;
8397 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008398{
8399#ifdef FEAT_FOLDING
8400 linenr_T lnum;
8401 char_u *text;
8402 char_u buf[51];
8403 foldinfo_T foldinfo;
8404 int fold_count;
8405#endif
8406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008407 rettv->v_type = VAR_STRING;
8408 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008409#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008410 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008411 fold_count = foldedCount(curwin, lnum, &foldinfo);
8412 if (fold_count > 0)
8413 {
8414 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8415 &foldinfo, buf);
8416 if (text == buf)
8417 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008419 }
8420#endif
8421}
8422
8423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 * "foreground()" function
8425 */
8426/*ARGSUSED*/
8427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 typval_T *argvars;
8430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008432 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433#ifdef FEAT_GUI
8434 if (gui.in_use)
8435 gui_mch_set_foreground();
8436#else
8437# ifdef WIN32
8438 win32_set_foreground();
8439# endif
8440#endif
8441}
8442
8443/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008444 * "function()" function
8445 */
8446/*ARGSUSED*/
8447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008449 typval_T *argvars;
8450 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008451{
8452 char_u *s;
8453
Bram Moolenaara7043832005-01-21 11:56:39 +00008454 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008455 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008456 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008457 EMSG2(_(e_invarg2), s);
8458 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008459 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 else
8461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->vval.v_string = vim_strsave(s);
8463 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008464 }
8465}
8466
8467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008468 * "get()" function
8469 */
8470 static void
8471f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008472 typval_T *argvars;
8473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008474{
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 listitem_T *li;
8476 list_T *l;
8477 dictitem_T *di;
8478 dict_T *d;
8479 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008480
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008483 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008484 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008485 li = list_find(l, get_tv_number(&argvars[1]));
8486 if (li != NULL)
8487 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008488 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 else if (argvars[0].v_type == VAR_DICT)
8491 {
8492 if ((d = argvars[0].vval.v_dict) != NULL)
8493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008494 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 if (di != NULL)
8496 tv = &di->di_tv;
8497 }
8498 }
8499 else
8500 EMSG2(_(e_listdictarg), "get()");
8501
8502 if (tv == NULL)
8503 {
8504 if (argvars[2].v_type == VAR_UNKNOWN)
8505 rettv->vval.v_number = 0;
8506 else
8507 copy_tv(&argvars[2], rettv);
8508 }
8509 else
8510 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008511}
8512
8513/*
8514 * "getbufvar()" function
8515 */
8516 static void
8517f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008518 typval_T *argvars;
8519 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008520{
8521 buf_T *buf;
8522 buf_T *save_curbuf;
8523 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008525
8526 ++emsg_off;
8527 buf = get_buf_tv(&argvars[0]);
8528 varname = get_tv_string(&argvars[1]);
8529
8530 rettv->v_type = VAR_STRING;
8531 rettv->vval.v_string = NULL;
8532
8533 if (buf != NULL && varname != NULL)
8534 {
8535 if (*varname == '&') /* buffer-local-option */
8536 {
8537 /* set curbuf to be our buf, temporarily */
8538 save_curbuf = curbuf;
8539 curbuf = buf;
8540
8541 get_option_tv(&varname, rettv, TRUE);
8542
8543 /* restore previous notion of curbuf */
8544 curbuf = save_curbuf;
8545 }
8546 else
8547 {
8548 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008549 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008550 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008552 }
8553 }
8554
8555 --emsg_off;
8556}
8557
8558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 * "getchar()" function
8560 */
8561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 typval_T *argvars;
8564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 varnumber_T n;
8567
8568 ++no_mapping;
8569 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 /* getchar(): blocking wait. */
8572 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 /* getchar(1): only check if char avail */
8575 n = vpeekc();
8576 else if (vpeekc() == NUL)
8577 /* getchar(0) and no char avail: return zero */
8578 n = 0;
8579 else
8580 /* getchar(0) and char avail: return char */
8581 n = safe_vgetc();
8582 --no_mapping;
8583 --allow_keys;
8584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (IS_SPECIAL(n) || mod_mask != 0)
8587 {
8588 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8589 int i = 0;
8590
8591 /* Turn a special key into three bytes, plus modifier. */
8592 if (mod_mask != 0)
8593 {
8594 temp[i++] = K_SPECIAL;
8595 temp[i++] = KS_MODIFIER;
8596 temp[i++] = mod_mask;
8597 }
8598 if (IS_SPECIAL(n))
8599 {
8600 temp[i++] = K_SPECIAL;
8601 temp[i++] = K_SECOND(n);
8602 temp[i++] = K_THIRD(n);
8603 }
8604#ifdef FEAT_MBYTE
8605 else if (has_mbyte)
8606 i += (*mb_char2bytes)(n, temp + i);
8607#endif
8608 else
8609 temp[i++] = n;
8610 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611 rettv->v_type = VAR_STRING;
8612 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614}
8615
8616/*
8617 * "getcharmod()" function
8618 */
8619/*ARGSUSED*/
8620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008622 typval_T *argvars;
8623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626}
8627
8628/*
8629 * "getcmdline()" function
8630 */
8631/*ARGSUSED*/
8632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008633f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 typval_T *argvars;
8635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637 rettv->v_type = VAR_STRING;
8638 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641/*
8642 * "getcmdpos()" function
8643 */
8644/*ARGSUSED*/
8645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 typval_T *argvars;
8648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 * "getcwd()" function
8655 */
8656/*ARGSUSED*/
8657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T *argvars;
8660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 char_u cwd[MAXPATHL];
8663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 else
8668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672#endif
8673 }
8674}
8675
8676/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008677 * "getfontname()" function
8678 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008679/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 typval_T *argvars;
8683 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 rettv->v_type = VAR_STRING;
8686 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008687#ifdef FEAT_GUI
8688 if (gui.in_use)
8689 {
8690 GuiFont font;
8691 char_u *name = NULL;
8692
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008693 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008694 {
8695 /* Get the "Normal" font. Either the name saved by
8696 * hl_set_font_name() or from the font ID. */
8697 font = gui.norm_font;
8698 name = hl_get_font_name();
8699 }
8700 else
8701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008703 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8704 return;
8705 font = gui_mch_get_font(name, FALSE);
8706 if (font == NOFONT)
8707 return; /* Invalid font name, return empty string. */
8708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008710 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008711 gui_mch_free_font(font);
8712 }
8713#endif
8714}
8715
8716/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008717 * "getfperm({fname})" function
8718 */
8719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 typval_T *argvars;
8722 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008723{
8724 char_u *fname;
8725 struct stat st;
8726 char_u *perm = NULL;
8727 char_u flags[] = "rwx";
8728 int i;
8729
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008733 if (mch_stat((char *)fname, &st) >= 0)
8734 {
8735 perm = vim_strsave((char_u *)"---------");
8736 if (perm != NULL)
8737 {
8738 for (i = 0; i < 9; i++)
8739 {
8740 if (st.st_mode & (1 << (8 - i)))
8741 perm[i] = flags[i % 3];
8742 }
8743 }
8744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008746}
8747
8748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 * "getfsize({fname})" function
8750 */
8751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008752f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008753 typval_T *argvars;
8754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
8756 char_u *fname;
8757 struct stat st;
8758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762
8763 if (mch_stat((char *)fname, &st) >= 0)
8764 {
8765 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 }
8770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772}
8773
8774/*
8775 * "getftime({fname})" function
8776 */
8777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T *argvars;
8780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 char_u *fname;
8783 struct stat st;
8784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786
8787 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792
8793/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008794 * "getftype({fname})" function
8795 */
8796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008798 typval_T *argvars;
8799 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008800{
8801 char_u *fname;
8802 struct stat st;
8803 char_u *type = NULL;
8804 char *t;
8805
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008809 if (mch_lstat((char *)fname, &st) >= 0)
8810 {
8811#ifdef S_ISREG
8812 if (S_ISREG(st.st_mode))
8813 t = "file";
8814 else if (S_ISDIR(st.st_mode))
8815 t = "dir";
8816# ifdef S_ISLNK
8817 else if (S_ISLNK(st.st_mode))
8818 t = "link";
8819# endif
8820# ifdef S_ISBLK
8821 else if (S_ISBLK(st.st_mode))
8822 t = "bdev";
8823# endif
8824# ifdef S_ISCHR
8825 else if (S_ISCHR(st.st_mode))
8826 t = "cdev";
8827# endif
8828# ifdef S_ISFIFO
8829 else if (S_ISFIFO(st.st_mode))
8830 t = "fifo";
8831# endif
8832# ifdef S_ISSOCK
8833 else if (S_ISSOCK(st.st_mode))
8834 t = "fifo";
8835# endif
8836 else
8837 t = "other";
8838#else
8839# ifdef S_IFMT
8840 switch (st.st_mode & S_IFMT)
8841 {
8842 case S_IFREG: t = "file"; break;
8843 case S_IFDIR: t = "dir"; break;
8844# ifdef S_IFLNK
8845 case S_IFLNK: t = "link"; break;
8846# endif
8847# ifdef S_IFBLK
8848 case S_IFBLK: t = "bdev"; break;
8849# endif
8850# ifdef S_IFCHR
8851 case S_IFCHR: t = "cdev"; break;
8852# endif
8853# ifdef S_IFIFO
8854 case S_IFIFO: t = "fifo"; break;
8855# endif
8856# ifdef S_IFSOCK
8857 case S_IFSOCK: t = "socket"; break;
8858# endif
8859 default: t = "other";
8860 }
8861# else
8862 if (mch_isdir(fname))
8863 t = "dir";
8864 else
8865 t = "file";
8866# endif
8867#endif
8868 type = vim_strsave((char_u *)t);
8869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008871}
8872
8873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008874 * "getline(lnum)" function
8875 */
8876 static void
8877f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 typval_T *argvars;
8879 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008880{
8881 linenr_T lnum;
8882 linenr_T end;
8883 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 list_T *l;
8885 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008886
8887 lnum = get_tv_lnum(argvars);
8888
8889 if (argvars[1].v_type == VAR_UNKNOWN)
8890 {
8891 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8892 p = ml_get(lnum);
8893 else
8894 p = (char_u *)"";
8895
8896 rettv->v_type = VAR_STRING;
8897 rettv->vval.v_string = vim_strsave(p);
8898 }
8899 else
8900 {
8901 end = get_tv_lnum(&argvars[1]);
8902 if (end < lnum)
8903 {
8904 EMSG(_(e_invrange));
8905 rettv->vval.v_number = 0;
8906 }
8907 else
8908 {
8909 l = list_alloc();
8910 if (l != NULL)
8911 {
8912 if (lnum < 1)
8913 lnum = 1;
8914 if (end > curbuf->b_ml.ml_line_count)
8915 end = curbuf->b_ml.ml_line_count;
8916 while (lnum <= end)
8917 {
8918 li = listitem_alloc();
8919 if (li == NULL)
8920 break;
8921 list_append(l, li);
8922 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008923 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008924 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8925 }
8926 rettv->vval.v_list = l;
8927 rettv->v_type = VAR_LIST;
8928 ++l->lv_refcount;
8929 }
8930 }
8931 }
8932}
8933
8934/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00008935 * "getqflist()" function
8936 */
8937/*ARGSUSED*/
8938 static void
8939f_getqflist(argvars, rettv)
8940 typval_T *argvars;
8941 typval_T *rettv;
8942{
8943#ifdef FEAT_QUICKFIX
8944 list_T *l;
8945#endif
8946
8947 rettv->vval.v_number = FALSE;
8948#ifdef FEAT_QUICKFIX
8949 l = list_alloc();
8950 if (l != NULL)
8951 {
8952 if (get_errorlist(l) != FAIL)
8953 {
8954 rettv->vval.v_list = l;
8955 rettv->v_type = VAR_LIST;
8956 ++l->lv_refcount;
8957 }
8958 else
8959 list_free(l);
8960 }
8961#endif
8962}
8963
8964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 * "getreg()" function
8966 */
8967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *argvars;
8970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 char_u *strregname;
8973 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008974 int arg2 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008979 if (argvars[1].v_type != VAR_UNKNOWN)
8980 arg2 = get_tv_number(&argvars[1]);
8981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 regname = (strregname == NULL ? '"' : *strregname);
8985 if (regname == 0)
8986 regname = '"';
8987
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988 rettv->v_type = VAR_STRING;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008989 rettv->vval.v_string = get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990}
8991
8992/*
8993 * "getregtype()" function
8994 */
8995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 typval_T *argvars;
8998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
9000 char_u *strregname;
9001 int regname;
9002 char_u buf[NUMBUFLEN + 2];
9003 long reglen = 0;
9004
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 else
9008 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
9011 regname = (strregname == NULL ? '"' : *strregname);
9012 if (regname == 0)
9013 regname = '"';
9014
9015 buf[0] = NUL;
9016 buf[1] = NUL;
9017 switch (get_reg_type(regname, &reglen))
9018 {
9019 case MLINE: buf[0] = 'V'; break;
9020 case MCHAR: buf[0] = 'v'; break;
9021#ifdef FEAT_VISUAL
9022 case MBLOCK:
9023 buf[0] = Ctrl_V;
9024 sprintf((char *)buf + 1, "%ld", reglen + 1);
9025 break;
9026#endif
9027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 rettv->v_type = VAR_STRING;
9029 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030}
9031
9032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 * "getwinposx()" function
9034 */
9035/*ARGSUSED*/
9036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *argvars;
9039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042#ifdef FEAT_GUI
9043 if (gui.in_use)
9044 {
9045 int x, y;
9046
9047 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
9050#endif
9051}
9052
9053/*
9054 * "getwinposy()" function
9055 */
9056/*ARGSUSED*/
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063#ifdef FEAT_GUI
9064 if (gui.in_use)
9065 {
9066 int x, y;
9067
9068 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
9071#endif
9072}
9073
9074/*
9075 * "getwinvar()" function
9076 */
9077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *argvars;
9080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 win_T *win, *oldcurwin;
9083 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086 ++emsg_off;
9087 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->v_type = VAR_STRING;
9091 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
9093 if (win != NULL && varname != NULL)
9094 {
9095 if (*varname == '&') /* window-local-option */
9096 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009097 /* Set curwin to be our win, temporarily. Also set curbuf, so
9098 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 oldcurwin = curwin;
9100 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009101 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 /* restore previous notion of curwin */
9106 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009107 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 }
9109 else
9110 {
9111 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009112 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009114 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
9116 }
9117
9118 --emsg_off;
9119}
9120
9121/*
9122 * "glob()" function
9123 */
9124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 typval_T *argvars;
9127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
9129 expand_T xpc;
9130
9131 ExpandInit(&xpc);
9132 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->v_type = VAR_STRING;
9134 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9136 ExpandCleanup(&xpc);
9137}
9138
9139/*
9140 * "globpath()" function
9141 */
9142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 typval_T *argvars;
9145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 char_u buf1[NUMBUFLEN];
9148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 rettv->v_type = VAR_STRING;
9150 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9151 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152}
9153
9154/*
9155 * "has()" function
9156 */
9157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009159 typval_T *argvars;
9160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 int i;
9163 char_u *name;
9164 int n = FALSE;
9165 static char *(has_list[]) =
9166 {
9167#ifdef AMIGA
9168 "amiga",
9169# ifdef FEAT_ARP
9170 "arp",
9171# endif
9172#endif
9173#ifdef __BEOS__
9174 "beos",
9175#endif
9176#ifdef MSDOS
9177# ifdef DJGPP
9178 "dos32",
9179# else
9180 "dos16",
9181# endif
9182#endif
9183#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9184 "mac",
9185#endif
9186#if defined(MACOS_X_UNIX)
9187 "macunix",
9188#endif
9189#ifdef OS2
9190 "os2",
9191#endif
9192#ifdef __QNX__
9193 "qnx",
9194#endif
9195#ifdef RISCOS
9196 "riscos",
9197#endif
9198#ifdef UNIX
9199 "unix",
9200#endif
9201#ifdef VMS
9202 "vms",
9203#endif
9204#ifdef WIN16
9205 "win16",
9206#endif
9207#ifdef WIN32
9208 "win32",
9209#endif
9210#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9211 "win32unix",
9212#endif
9213#ifdef WIN64
9214 "win64",
9215#endif
9216#ifdef EBCDIC
9217 "ebcdic",
9218#endif
9219#ifndef CASE_INSENSITIVE_FILENAME
9220 "fname_case",
9221#endif
9222#ifdef FEAT_ARABIC
9223 "arabic",
9224#endif
9225#ifdef FEAT_AUTOCMD
9226 "autocmd",
9227#endif
9228#ifdef FEAT_BEVAL
9229 "balloon_eval",
9230#endif
9231#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9232 "builtin_terms",
9233# ifdef ALL_BUILTIN_TCAPS
9234 "all_builtin_terms",
9235# endif
9236#endif
9237#ifdef FEAT_BYTEOFF
9238 "byte_offset",
9239#endif
9240#ifdef FEAT_CINDENT
9241 "cindent",
9242#endif
9243#ifdef FEAT_CLIENTSERVER
9244 "clientserver",
9245#endif
9246#ifdef FEAT_CLIPBOARD
9247 "clipboard",
9248#endif
9249#ifdef FEAT_CMDL_COMPL
9250 "cmdline_compl",
9251#endif
9252#ifdef FEAT_CMDHIST
9253 "cmdline_hist",
9254#endif
9255#ifdef FEAT_COMMENTS
9256 "comments",
9257#endif
9258#ifdef FEAT_CRYPT
9259 "cryptv",
9260#endif
9261#ifdef FEAT_CSCOPE
9262 "cscope",
9263#endif
9264#ifdef DEBUG
9265 "debug",
9266#endif
9267#ifdef FEAT_CON_DIALOG
9268 "dialog_con",
9269#endif
9270#ifdef FEAT_GUI_DIALOG
9271 "dialog_gui",
9272#endif
9273#ifdef FEAT_DIFF
9274 "diff",
9275#endif
9276#ifdef FEAT_DIGRAPHS
9277 "digraphs",
9278#endif
9279#ifdef FEAT_DND
9280 "dnd",
9281#endif
9282#ifdef FEAT_EMACS_TAGS
9283 "emacs_tags",
9284#endif
9285 "eval", /* always present, of course! */
9286#ifdef FEAT_EX_EXTRA
9287 "ex_extra",
9288#endif
9289#ifdef FEAT_SEARCH_EXTRA
9290 "extra_search",
9291#endif
9292#ifdef FEAT_FKMAP
9293 "farsi",
9294#endif
9295#ifdef FEAT_SEARCHPATH
9296 "file_in_path",
9297#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009298#if defined(UNIX) && !defined(USE_SYSTEM)
9299 "filterpipe",
9300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301#ifdef FEAT_FIND_ID
9302 "find_in_path",
9303#endif
9304#ifdef FEAT_FOLDING
9305 "folding",
9306#endif
9307#ifdef FEAT_FOOTER
9308 "footer",
9309#endif
9310#if !defined(USE_SYSTEM) && defined(UNIX)
9311 "fork",
9312#endif
9313#ifdef FEAT_GETTEXT
9314 "gettext",
9315#endif
9316#ifdef FEAT_GUI
9317 "gui",
9318#endif
9319#ifdef FEAT_GUI_ATHENA
9320# ifdef FEAT_GUI_NEXTAW
9321 "gui_neXtaw",
9322# else
9323 "gui_athena",
9324# endif
9325#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009326#ifdef FEAT_GUI_KDE
9327 "gui_kde",
9328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329#ifdef FEAT_GUI_GTK
9330 "gui_gtk",
9331# ifdef HAVE_GTK2
9332 "gui_gtk2",
9333# endif
9334#endif
9335#ifdef FEAT_GUI_MAC
9336 "gui_mac",
9337#endif
9338#ifdef FEAT_GUI_MOTIF
9339 "gui_motif",
9340#endif
9341#ifdef FEAT_GUI_PHOTON
9342 "gui_photon",
9343#endif
9344#ifdef FEAT_GUI_W16
9345 "gui_win16",
9346#endif
9347#ifdef FEAT_GUI_W32
9348 "gui_win32",
9349#endif
9350#ifdef FEAT_HANGULIN
9351 "hangul_input",
9352#endif
9353#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9354 "iconv",
9355#endif
9356#ifdef FEAT_INS_EXPAND
9357 "insert_expand",
9358#endif
9359#ifdef FEAT_JUMPLIST
9360 "jumplist",
9361#endif
9362#ifdef FEAT_KEYMAP
9363 "keymap",
9364#endif
9365#ifdef FEAT_LANGMAP
9366 "langmap",
9367#endif
9368#ifdef FEAT_LIBCALL
9369 "libcall",
9370#endif
9371#ifdef FEAT_LINEBREAK
9372 "linebreak",
9373#endif
9374#ifdef FEAT_LISP
9375 "lispindent",
9376#endif
9377#ifdef FEAT_LISTCMDS
9378 "listcmds",
9379#endif
9380#ifdef FEAT_LOCALMAP
9381 "localmap",
9382#endif
9383#ifdef FEAT_MENU
9384 "menu",
9385#endif
9386#ifdef FEAT_SESSION
9387 "mksession",
9388#endif
9389#ifdef FEAT_MODIFY_FNAME
9390 "modify_fname",
9391#endif
9392#ifdef FEAT_MOUSE
9393 "mouse",
9394#endif
9395#ifdef FEAT_MOUSESHAPE
9396 "mouseshape",
9397#endif
9398#if defined(UNIX) || defined(VMS)
9399# ifdef FEAT_MOUSE_DEC
9400 "mouse_dec",
9401# endif
9402# ifdef FEAT_MOUSE_GPM
9403 "mouse_gpm",
9404# endif
9405# ifdef FEAT_MOUSE_JSB
9406 "mouse_jsbterm",
9407# endif
9408# ifdef FEAT_MOUSE_NET
9409 "mouse_netterm",
9410# endif
9411# ifdef FEAT_MOUSE_PTERM
9412 "mouse_pterm",
9413# endif
9414# ifdef FEAT_MOUSE_XTERM
9415 "mouse_xterm",
9416# endif
9417#endif
9418#ifdef FEAT_MBYTE
9419 "multi_byte",
9420#endif
9421#ifdef FEAT_MBYTE_IME
9422 "multi_byte_ime",
9423#endif
9424#ifdef FEAT_MULTI_LANG
9425 "multi_lang",
9426#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009427#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009428#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009429 "mzscheme",
9430#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#ifdef FEAT_OLE
9433 "ole",
9434#endif
9435#ifdef FEAT_OSFILETYPE
9436 "osfiletype",
9437#endif
9438#ifdef FEAT_PATH_EXTRA
9439 "path_extra",
9440#endif
9441#ifdef FEAT_PERL
9442#ifndef DYNAMIC_PERL
9443 "perl",
9444#endif
9445#endif
9446#ifdef FEAT_PYTHON
9447#ifndef DYNAMIC_PYTHON
9448 "python",
9449#endif
9450#endif
9451#ifdef FEAT_POSTSCRIPT
9452 "postscript",
9453#endif
9454#ifdef FEAT_PRINTER
9455 "printer",
9456#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009457#ifdef FEAT_PROFILE
9458 "profile",
9459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460#ifdef FEAT_QUICKFIX
9461 "quickfix",
9462#endif
9463#ifdef FEAT_RIGHTLEFT
9464 "rightleft",
9465#endif
9466#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9467 "ruby",
9468#endif
9469#ifdef FEAT_SCROLLBIND
9470 "scrollbind",
9471#endif
9472#ifdef FEAT_CMDL_INFO
9473 "showcmd",
9474 "cmdline_info",
9475#endif
9476#ifdef FEAT_SIGNS
9477 "signs",
9478#endif
9479#ifdef FEAT_SMARTINDENT
9480 "smartindent",
9481#endif
9482#ifdef FEAT_SNIFF
9483 "sniff",
9484#endif
9485#ifdef FEAT_STL_OPT
9486 "statusline",
9487#endif
9488#ifdef FEAT_SUN_WORKSHOP
9489 "sun_workshop",
9490#endif
9491#ifdef FEAT_NETBEANS_INTG
9492 "netbeans_intg",
9493#endif
9494#ifdef FEAT_SYN_HL
9495 "syntax",
9496#endif
9497#if defined(USE_SYSTEM) || !defined(UNIX)
9498 "system",
9499#endif
9500#ifdef FEAT_TAG_BINS
9501 "tag_binary",
9502#endif
9503#ifdef FEAT_TAG_OLDSTATIC
9504 "tag_old_static",
9505#endif
9506#ifdef FEAT_TAG_ANYWHITE
9507 "tag_any_white",
9508#endif
9509#ifdef FEAT_TCL
9510# ifndef DYNAMIC_TCL
9511 "tcl",
9512# endif
9513#endif
9514#ifdef TERMINFO
9515 "terminfo",
9516#endif
9517#ifdef FEAT_TERMRESPONSE
9518 "termresponse",
9519#endif
9520#ifdef FEAT_TEXTOBJ
9521 "textobjects",
9522#endif
9523#ifdef HAVE_TGETENT
9524 "tgetent",
9525#endif
9526#ifdef FEAT_TITLE
9527 "title",
9528#endif
9529#ifdef FEAT_TOOLBAR
9530 "toolbar",
9531#endif
9532#ifdef FEAT_USR_CMDS
9533 "user-commands", /* was accidentally included in 5.4 */
9534 "user_commands",
9535#endif
9536#ifdef FEAT_VIMINFO
9537 "viminfo",
9538#endif
9539#ifdef FEAT_VERTSPLIT
9540 "vertsplit",
9541#endif
9542#ifdef FEAT_VIRTUALEDIT
9543 "virtualedit",
9544#endif
9545#ifdef FEAT_VISUAL
9546 "visual",
9547#endif
9548#ifdef FEAT_VISUALEXTRA
9549 "visualextra",
9550#endif
9551#ifdef FEAT_VREPLACE
9552 "vreplace",
9553#endif
9554#ifdef FEAT_WILDIGN
9555 "wildignore",
9556#endif
9557#ifdef FEAT_WILDMENU
9558 "wildmenu",
9559#endif
9560#ifdef FEAT_WINDOWS
9561 "windows",
9562#endif
9563#ifdef FEAT_WAK
9564 "winaltkeys",
9565#endif
9566#ifdef FEAT_WRITEBACKUP
9567 "writebackup",
9568#endif
9569#ifdef FEAT_XIM
9570 "xim",
9571#endif
9572#ifdef FEAT_XFONTSET
9573 "xfontset",
9574#endif
9575#ifdef USE_XSMP
9576 "xsmp",
9577#endif
9578#ifdef USE_XSMP_INTERACT
9579 "xsmp_interact",
9580#endif
9581#ifdef FEAT_XCLIPBOARD
9582 "xterm_clipboard",
9583#endif
9584#ifdef FEAT_XTERM_SAVE
9585 "xterm_save",
9586#endif
9587#if defined(UNIX) && defined(FEAT_X11)
9588 "X11",
9589#endif
9590 NULL
9591 };
9592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 for (i = 0; has_list[i] != NULL; ++i)
9595 if (STRICMP(name, has_list[i]) == 0)
9596 {
9597 n = TRUE;
9598 break;
9599 }
9600
9601 if (n == FALSE)
9602 {
9603 if (STRNICMP(name, "patch", 5) == 0)
9604 n = has_patch(atoi((char *)name + 5));
9605 else if (STRICMP(name, "vim_starting") == 0)
9606 n = (starting != 0);
9607#ifdef DYNAMIC_TCL
9608 else if (STRICMP(name, "tcl") == 0)
9609 n = tcl_enabled(FALSE);
9610#endif
9611#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9612 else if (STRICMP(name, "iconv") == 0)
9613 n = iconv_enabled(FALSE);
9614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009615#ifdef DYNAMIC_MZSCHEME
9616 else if (STRICMP(name, "mzscheme") == 0)
9617 n = mzscheme_enabled(FALSE);
9618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619#ifdef DYNAMIC_RUBY
9620 else if (STRICMP(name, "ruby") == 0)
9621 n = ruby_enabled(FALSE);
9622#endif
9623#ifdef DYNAMIC_PYTHON
9624 else if (STRICMP(name, "python") == 0)
9625 n = python_enabled(FALSE);
9626#endif
9627#ifdef DYNAMIC_PERL
9628 else if (STRICMP(name, "perl") == 0)
9629 n = perl_enabled(FALSE);
9630#endif
9631#ifdef FEAT_GUI
9632 else if (STRICMP(name, "gui_running") == 0)
9633 n = (gui.in_use || gui.starting);
9634# ifdef FEAT_GUI_W32
9635 else if (STRICMP(name, "gui_win32s") == 0)
9636 n = gui_is_win32s();
9637# endif
9638# ifdef FEAT_BROWSE
9639 else if (STRICMP(name, "browse") == 0)
9640 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9641# endif
9642#endif
9643#ifdef FEAT_SYN_HL
9644 else if (STRICMP(name, "syntax_items") == 0)
9645 n = syntax_present(curbuf);
9646#endif
9647#if defined(WIN3264)
9648 else if (STRICMP(name, "win95") == 0)
9649 n = mch_windows95();
9650#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009651#ifdef FEAT_NETBEANS_INTG
9652 else if (STRICMP(name, "netbeans_enabled") == 0)
9653 n = usingNetbeans;
9654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 }
9656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658}
9659
9660/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 * "has_key()" function
9662 */
9663 static void
9664f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009665 typval_T *argvars;
9666 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009667{
9668 rettv->vval.v_number = 0;
9669 if (argvars[0].v_type != VAR_DICT)
9670 {
9671 EMSG(_(e_dictreq));
9672 return;
9673 }
9674 if (argvars[0].vval.v_dict == NULL)
9675 return;
9676
9677 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009678 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679}
9680
9681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 * "hasmapto()" function
9683 */
9684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 typval_T *argvars;
9687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
9689 char_u *name;
9690 char_u *mode;
9691 char_u buf[NUMBUFLEN];
9692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009694 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 mode = (char_u *)"nvo";
9696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698
9699 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703}
9704
9705/*
9706 * "histadd()" function
9707 */
9708/*ARGSUSED*/
9709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009711 typval_T *argvars;
9712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
9714#ifdef FEAT_CMDHIST
9715 int histype;
9716 char_u *str;
9717 char_u buf[NUMBUFLEN];
9718#endif
9719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 if (check_restricted() || check_secure())
9722 return;
9723#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 if (histype >= 0)
9726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 if (*str != NUL)
9729 {
9730 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 return;
9733 }
9734 }
9735#endif
9736}
9737
9738/*
9739 * "histdel()" function
9740 */
9741/*ARGSUSED*/
9742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009744 typval_T *argvars;
9745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747#ifdef FEAT_CMDHIST
9748 int n;
9749 char_u buf[NUMBUFLEN];
9750
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009751 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009754 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9757 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 else
9759 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9761 get_tv_string_buf(&argvars[1], buf));
9762 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#endif
9766}
9767
9768/*
9769 * "histget()" function
9770 */
9771/*ARGSUSED*/
9772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009774 typval_T *argvars;
9775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
9777#ifdef FEAT_CMDHIST
9778 int type;
9779 int idx;
9780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009782 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 idx = get_history_idx(type);
9784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 idx = (int)get_tv_number(&argvars[1]);
9786 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791}
9792
9793/*
9794 * "histnr()" function
9795 */
9796/*ARGSUSED*/
9797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009799 typval_T *argvars;
9800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 int i;
9803
9804#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (i >= HIST_CMD && i < HIST_COUNT)
9807 i = get_history_idx(i);
9808 else
9809#endif
9810 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 * "highlightID(name)" function
9816 */
9817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 typval_T *argvars;
9820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823}
9824
9825/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009826 * "highlight_exists()" function
9827 */
9828 static void
9829f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009830 typval_T *argvars;
9831 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009832{
9833 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9834}
9835
9836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 * "hostname()" function
9838 */
9839/*ARGSUSED*/
9840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 typval_T *argvars;
9843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
9845 char_u hostname[256];
9846
9847 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 rettv->v_type = VAR_STRING;
9849 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850}
9851
9852/*
9853 * iconv() function
9854 */
9855/*ARGSUSED*/
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861#ifdef FEAT_MBYTE
9862 char_u buf1[NUMBUFLEN];
9863 char_u buf2[NUMBUFLEN];
9864 char_u *from, *to, *str;
9865 vimconv_T vimconv;
9866#endif
9867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->v_type = VAR_STRING;
9869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
9871#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 str = get_tv_string(&argvars[0]);
9873 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9874 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 vimconv.vc_type = CONV_NONE;
9876 convert_setup(&vimconv, from, to);
9877
9878 /* If the encodings are equal, no conversion needed. */
9879 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
9884 convert_setup(&vimconv, NULL, NULL);
9885 vim_free(from);
9886 vim_free(to);
9887#endif
9888}
9889
9890/*
9891 * "indent()" function
9892 */
9893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009895 typval_T *argvars;
9896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
9898 linenr_T lnum;
9899
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907/*
9908 * "index()" function
9909 */
9910 static void
9911f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 typval_T *argvars;
9913 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009914{
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 list_T *l;
9916 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917 long idx = 0;
9918 int ic = FALSE;
9919
9920 rettv->vval.v_number = -1;
9921 if (argvars[0].v_type != VAR_LIST)
9922 {
9923 EMSG(_(e_listreq));
9924 return;
9925 }
9926 l = argvars[0].vval.v_list;
9927 if (l != NULL)
9928 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009929 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009931 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009932 /* Start at specified item. Use the cached index that list_find()
9933 * sets, so that a negative number also works. */
9934 item = list_find(l, get_tv_number(&argvars[2]));
9935 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009936 if (argvars[3].v_type != VAR_UNKNOWN)
9937 ic = get_tv_number(&argvars[3]);
9938 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009939
Bram Moolenaar758711c2005-02-02 23:11:38 +00009940 for ( ; item != NULL; item = item->li_next, ++idx)
9941 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009942 {
9943 rettv->vval.v_number = idx;
9944 break;
9945 }
9946 }
9947}
9948
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949static int inputsecret_flag = 0;
9950
9951/*
9952 * "input()" function
9953 * Also handles inputsecret() when inputsecret is set.
9954 */
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 char_u *p = NULL;
9962 int c;
9963 char_u buf[NUMBUFLEN];
9964 int cmd_silent_save = cmd_silent;
9965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967
9968#ifdef NO_CONSOLE_INPUT
9969 /* While starting up, there is no place to enter text. */
9970 if (no_console_input())
9971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 return;
9974 }
9975#endif
9976
9977 cmd_silent = FALSE; /* Want to see the prompt. */
9978 if (prompt != NULL)
9979 {
9980 /* Only the part of the message after the last NL is considered as
9981 * prompt for the command line */
9982 p = vim_strrchr(prompt, '\n');
9983 if (p == NULL)
9984 p = prompt;
9985 else
9986 {
9987 ++p;
9988 c = *p;
9989 *p = NUL;
9990 msg_start();
9991 msg_clr_eos();
9992 msg_puts_attr(prompt, echo_attr);
9993 msg_didout = FALSE;
9994 msg_starthere();
9995 *p = c;
9996 }
9997 cmdline_row = msg_row;
9998 }
9999
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10005
10006 /* since the user typed this, no need to wait for return */
10007 need_wait_return = FALSE;
10008 msg_didout = FALSE;
10009 cmd_silent = cmd_silent_save;
10010}
10011
10012/*
10013 * "inputdialog()" function
10014 */
10015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010017 typval_T *argvars;
10018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020#if defined(FEAT_GUI_TEXTDIALOG)
10021 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10022 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10023 {
10024 char_u *message;
10025 char_u buf[NUMBUFLEN];
10026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010028 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 IObuff[IOSIZE - 1] = NUL;
10032 }
10033 else
10034 IObuff[0] = NUL;
10035 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
10036 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 else
10039 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010040 if (argvars[1].v_type != VAR_UNKNOWN
10041 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_string = vim_strsave(
10043 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049 else
10050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
10054static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10055
10056/*
10057 * "inputrestore()" function
10058 */
10059/*ARGSUSED*/
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
10065 if (ga_userinput.ga_len > 0)
10066 {
10067 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10069 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 }
10072 else if (p_verbose > 1)
10073 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010074 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 }
10077}
10078
10079/*
10080 * "inputsave()" function
10081 */
10082/*ARGSUSED*/
10083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010085 typval_T *argvars;
10086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 /* Add an entry to the stack of typehead storage. */
10089 if (ga_grow(&ga_userinput, 1) == OK)
10090 {
10091 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10092 + ga_userinput.ga_len);
10093 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 }
10096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098}
10099
10100/*
10101 * "inputsecret()" function
10102 */
10103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010105 typval_T *argvars;
10106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107{
10108 ++cmdline_star;
10109 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 --cmdline_star;
10112 --inputsecret_flag;
10113}
10114
10115/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010116 * "insert()" function
10117 */
10118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010120 typval_T *argvars;
10121 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010122{
10123 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 listitem_T *item;
10125 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010126
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010127 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010128 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010129 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010130 else if ((l = argvars[0].vval.v_list) != NULL
10131 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010132 {
10133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010135
Bram Moolenaar758711c2005-02-02 23:11:38 +000010136 if (before == l->lv_len)
10137 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010138 else
10139 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010140 item = list_find(l, before);
10141 if (item == NULL)
10142 {
10143 EMSGN(_(e_listidx), before);
10144 l = NULL;
10145 }
10146 }
10147 if (l != NULL)
10148 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010149 list_insert_tv(l, &argvars[1], item);
10150 ++l->lv_refcount;
10151 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010152 }
10153 }
10154}
10155
10156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 * "isdirectory()" function
10158 */
10159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010161 typval_T *argvars;
10162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165}
10166
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010167/*
10168 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10169 * it refers to a List or Dictionary that is locked.
10170 */
10171 static int
10172tv_islocked(tv)
10173 typval_T *tv;
10174{
10175 return (tv->v_lock & VAR_LOCKED)
10176 || (tv->v_type == VAR_LIST
10177 && tv->vval.v_list != NULL
10178 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10179 || (tv->v_type == VAR_DICT
10180 && tv->vval.v_dict != NULL
10181 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10182}
10183
10184/*
10185 * "islocked()" function
10186 */
10187 static void
10188f_islocked(argvars, rettv)
10189 typval_T *argvars;
10190 typval_T *rettv;
10191{
10192 lval_T lv;
10193 char_u *end;
10194 dictitem_T *di;
10195
10196 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010197 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10198 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010199 if (end != NULL && lv.ll_name != NULL)
10200 {
10201 if (*end != NUL)
10202 EMSG(_(e_trailing));
10203 else
10204 {
10205 if (lv.ll_tv == NULL)
10206 {
10207 if (check_changedtick(lv.ll_name))
10208 rettv->vval.v_number = 1; /* always locked */
10209 else
10210 {
10211 di = find_var(lv.ll_name, NULL);
10212 if (di != NULL)
10213 {
10214 /* Consider a variable locked when:
10215 * 1. the variable itself is locked
10216 * 2. the value of the variable is locked.
10217 * 3. the List or Dict value is locked.
10218 */
10219 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10220 || tv_islocked(&di->di_tv));
10221 }
10222 }
10223 }
10224 else if (lv.ll_range)
10225 EMSG(_("E745: Range not allowed"));
10226 else if (lv.ll_newkey != NULL)
10227 EMSG2(_(e_dictkey), lv.ll_newkey);
10228 else if (lv.ll_list != NULL)
10229 /* List item. */
10230 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10231 else
10232 /* Dictionary item. */
10233 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10234 }
10235 }
10236
10237 clear_lval(&lv);
10238}
10239
Bram Moolenaar33570922005-01-25 22:26:29 +000010240static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010241
10242/*
10243 * Turn a dict into a list:
10244 * "what" == 0: list of keys
10245 * "what" == 1: list of values
10246 * "what" == 2: list of items
10247 */
10248 static void
10249dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 typval_T *argvars;
10251 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010252 int what;
10253{
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 list_T *l;
10255 list_T *l2;
10256 dictitem_T *di;
10257 hashitem_T *hi;
10258 listitem_T *li;
10259 listitem_T *li2;
10260 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010261 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010262
10263 rettv->vval.v_number = 0;
10264 if (argvars[0].v_type != VAR_DICT)
10265 {
10266 EMSG(_(e_dictreq));
10267 return;
10268 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010269 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010270 return;
10271
10272 l = list_alloc();
10273 if (l == NULL)
10274 return;
10275 rettv->v_type = VAR_LIST;
10276 rettv->vval.v_list = l;
10277 ++l->lv_refcount;
10278
Bram Moolenaar33570922005-01-25 22:26:29 +000010279 todo = d->dv_hashtab.ht_used;
10280 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010282 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010284 --todo;
10285 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010286
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010287 li = listitem_alloc();
10288 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010289 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010290 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010291
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010292 if (what == 0)
10293 {
10294 /* keys() */
10295 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010296 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010297 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10298 }
10299 else if (what == 1)
10300 {
10301 /* values() */
10302 copy_tv(&di->di_tv, &li->li_tv);
10303 }
10304 else
10305 {
10306 /* items() */
10307 l2 = list_alloc();
10308 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010309 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010310 li->li_tv.vval.v_list = l2;
10311 if (l2 == NULL)
10312 break;
10313 ++l2->lv_refcount;
10314
10315 li2 = listitem_alloc();
10316 if (li2 == NULL)
10317 break;
10318 list_append(l2, li2);
10319 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010320 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010321 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10322
10323 li2 = listitem_alloc();
10324 if (li2 == NULL)
10325 break;
10326 list_append(l2, li2);
10327 copy_tv(&di->di_tv, &li2->li_tv);
10328 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010329 }
10330 }
10331}
10332
10333/*
10334 * "items(dict)" function
10335 */
10336 static void
10337f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010338 typval_T *argvars;
10339 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010340{
10341 dict_list(argvars, rettv, 2);
10342}
10343
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010345 * "join()" function
10346 */
10347 static void
10348f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010349 typval_T *argvars;
10350 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010351{
10352 garray_T ga;
10353 char_u *sep;
10354
10355 rettv->vval.v_number = 0;
10356 if (argvars[0].v_type != VAR_LIST)
10357 {
10358 EMSG(_(e_listreq));
10359 return;
10360 }
10361 if (argvars[0].vval.v_list == NULL)
10362 return;
10363 if (argvars[1].v_type == VAR_UNKNOWN)
10364 sep = (char_u *)" ";
10365 else
10366 sep = get_tv_string(&argvars[1]);
10367
10368 ga_init2(&ga, (int)sizeof(char), 80);
10369 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10370 ga_append(&ga, NUL);
10371
10372 rettv->v_type = VAR_STRING;
10373 rettv->vval.v_string = (char_u *)ga.ga_data;
10374}
10375
10376/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010377 * "keys()" function
10378 */
10379 static void
10380f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010381 typval_T *argvars;
10382 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010383{
10384 dict_list(argvars, rettv, 0);
10385}
10386
10387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 * "last_buffer_nr()" function.
10389 */
10390/*ARGSUSED*/
10391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010393 typval_T *argvars;
10394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395{
10396 int n = 0;
10397 buf_T *buf;
10398
10399 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10400 if (n < buf->b_fnum)
10401 n = buf->b_fnum;
10402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010404}
10405
10406/*
10407 * "len()" function
10408 */
10409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 typval_T *argvars;
10412 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010413{
10414 switch (argvars[0].v_type)
10415 {
10416 case VAR_STRING:
10417 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418 rettv->vval.v_number = (varnumber_T)STRLEN(
10419 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010420 break;
10421 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010423 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 case VAR_DICT:
10425 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10426 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010427 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010428 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010429 break;
10430 }
10431}
10432
Bram Moolenaar33570922005-01-25 22:26:29 +000010433static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010434
10435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 typval_T *argvars;
10438 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010439 int type;
10440{
10441#ifdef FEAT_LIBCALL
10442 char_u *string_in;
10443 char_u **string_result;
10444 int nr_result;
10445#endif
10446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010448 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010452
10453 if (check_restricted() || check_secure())
10454 return;
10455
10456#ifdef FEAT_LIBCALL
10457 /* The first two args must be strings, otherwise its meaningless */
10458 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10459 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010460 string_in = NULL;
10461 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010462 string_in = argvars[2].vval.v_string;
10463 if (type == VAR_NUMBER)
10464 string_result = NULL;
10465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010467 if (mch_libcall(argvars[0].vval.v_string,
10468 argvars[1].vval.v_string,
10469 string_in,
10470 argvars[2].vval.v_number,
10471 string_result,
10472 &nr_result) == OK
10473 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010474 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010475 }
10476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477}
10478
10479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010480 * "libcall()" function
10481 */
10482 static void
10483f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *argvars;
10485 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010486{
10487 libcall_common(argvars, rettv, VAR_STRING);
10488}
10489
10490/*
10491 * "libcallnr()" function
10492 */
10493 static void
10494f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 typval_T *argvars;
10496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010497{
10498 libcall_common(argvars, rettv, VAR_NUMBER);
10499}
10500
10501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 * "line(string)" function
10503 */
10504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010506 typval_T *argvars;
10507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 linenr_T lnum = 0;
10510 pos_T *fp;
10511
10512 fp = var2fpos(&argvars[0], TRUE);
10513 if (fp != NULL)
10514 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010515 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516}
10517
10518/*
10519 * "line2byte(lnum)" function
10520 */
10521/*ARGSUSED*/
10522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010524 typval_T *argvars;
10525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526{
10527#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529#else
10530 linenr_T lnum;
10531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10537 if (rettv->vval.v_number >= 0)
10538 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539#endif
10540}
10541
10542/*
10543 * "lispindent(lnum)" function
10544 */
10545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 typval_T *argvars;
10548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550#ifdef FEAT_LISP
10551 pos_T pos;
10552 linenr_T lnum;
10553
10554 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10557 {
10558 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 curwin->w_cursor = pos;
10561 }
10562 else
10563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565}
10566
10567/*
10568 * "localtime()" function
10569 */
10570/*ARGSUSED*/
10571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010573 typval_T *argvars;
10574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577}
10578
Bram Moolenaar33570922005-01-25 22:26:29 +000010579static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580
10581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *argvars;
10584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 int exact;
10586{
10587 char_u *keys;
10588 char_u *which;
10589 char_u buf[NUMBUFLEN];
10590 char_u *keys_buf = NULL;
10591 char_u *rhs;
10592 int mode;
10593 garray_T ga;
10594
10595 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 rettv->v_type = VAR_STRING;
10597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 if (*keys == NUL)
10601 return;
10602
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010603 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 else
10606 which = (char_u *)"";
10607 mode = get_map_mode(&which, 0);
10608
10609 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000010610 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 vim_free(keys_buf);
10612 if (rhs != NULL)
10613 {
10614 ga_init(&ga);
10615 ga.ga_itemsize = 1;
10616 ga.ga_growsize = 40;
10617
10618 while (*rhs != NUL)
10619 ga_concat(&ga, str2special(&rhs, FALSE));
10620
10621 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 }
10624}
10625
10626/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627 * "map()" function
10628 */
10629 static void
10630f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010633{
10634 filter_map(argvars, rettv, TRUE);
10635}
10636
10637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010638 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 */
10640 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 typval_T *argvars;
10643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010645 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646}
10647
10648/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010649 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 */
10651 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010652f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 typval_T *argvars;
10654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657}
10658
Bram Moolenaar33570922005-01-25 22:26:29 +000010659static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660
10661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010663 typval_T *argvars;
10664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 int type;
10666{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010667 char_u *str = NULL;
10668 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 char_u *pat;
10670 regmatch_T regmatch;
10671 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010672 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 char_u *save_cpo;
10674 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010675 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010676 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 list_T *l = NULL;
10678 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010679 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010680 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681
10682 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10683 save_cpo = p_cpo;
10684 p_cpo = (char_u *)"";
10685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010686 rettv->vval.v_number = -1;
10687 if (type == 3)
10688 {
10689 /* return empty list when there are no matches */
10690 if ((rettv->vval.v_list = list_alloc()) == NULL)
10691 goto theend;
10692 rettv->v_type = VAR_LIST;
10693 ++rettv->vval.v_list->lv_refcount;
10694 }
10695 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->v_type = VAR_STRING;
10698 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010701 if (argvars[0].v_type == VAR_LIST)
10702 {
10703 if ((l = argvars[0].vval.v_list) == NULL)
10704 goto theend;
10705 li = l->lv_first;
10706 }
10707 else
10708 expr = str = get_tv_string(&argvars[0]);
10709
10710 pat = get_tv_string_buf(&argvars[1], patbuf);
10711
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010715 if (l != NULL)
10716 {
10717 li = list_find(l, start);
10718 if (li == NULL)
10719 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010720 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010721 }
10722 else
10723 {
10724 if (start < 0)
10725 start = 0;
10726 if (start > (long)STRLEN(str))
10727 goto theend;
10728 str += start;
10729 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010731 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 }
10734
10735 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10736 if (regmatch.regprog != NULL)
10737 {
10738 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010739
10740 while (1)
10741 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010742 if (l != NULL)
10743 {
10744 if (li == NULL)
10745 {
10746 match = FALSE;
10747 break;
10748 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010749 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010750 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010751 if (str == NULL)
10752 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010753 }
10754
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010755 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010756
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010757 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010758 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010759 if (l == NULL && !match)
10760 break;
10761
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010762 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010763 if (l != NULL)
10764 {
10765 li = li->li_next;
10766 ++idx;
10767 }
10768 else
10769 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010770#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010771 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010772#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010773 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010774#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010775 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010776 }
10777
10778 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010780 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010781 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010782 int i;
10783
10784 /* return list with matched string and submatches */
10785 for (i = 0; i < NSUBEXP; ++i)
10786 {
10787 if (regmatch.endp[i] == NULL)
10788 break;
10789 li = listitem_alloc();
10790 if (li == NULL)
10791 break;
10792 li->li_tv.v_type = VAR_STRING;
10793 li->li_tv.v_lock = 0;
10794 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10795 (int)(regmatch.endp[i] - regmatch.startp[i]));
10796 list_append(rettv->vval.v_list, li);
10797 }
10798 }
10799 else if (type == 2)
10800 {
10801 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010802 if (l != NULL)
10803 copy_tv(&li->li_tv, rettv);
10804 else
10805 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010807 }
10808 else if (l != NULL)
10809 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 else
10811 {
10812 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 (varnumber_T)(regmatch.startp[0] - str);
10815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 }
10820 }
10821 vim_free(regmatch.regprog);
10822 }
10823
10824theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010825 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 p_cpo = save_cpo;
10827}
10828
10829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830 * "match()" function
10831 */
10832 static void
10833f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010834 typval_T *argvars;
10835 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010836{
10837 find_some_match(argvars, rettv, 1);
10838}
10839
10840/*
10841 * "matchend()" function
10842 */
10843 static void
10844f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 typval_T *argvars;
10846 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010847{
10848 find_some_match(argvars, rettv, 0);
10849}
10850
10851/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010852 * "matchlist()" function
10853 */
10854 static void
10855f_matchlist(argvars, rettv)
10856 typval_T *argvars;
10857 typval_T *rettv;
10858{
10859 find_some_match(argvars, rettv, 3);
10860}
10861
10862/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010863 * "matchstr()" function
10864 */
10865 static void
10866f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010867 typval_T *argvars;
10868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010869{
10870 find_some_match(argvars, rettv, 2);
10871}
10872
Bram Moolenaar33570922005-01-25 22:26:29 +000010873static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010874
10875 static void
10876max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010879 int domax;
10880{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010881 long n = 0;
10882 long i;
10883
10884 if (argvars[0].v_type == VAR_LIST)
10885 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 list_T *l;
10887 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010889 l = argvars[0].vval.v_list;
10890 if (l != NULL)
10891 {
10892 li = l->lv_first;
10893 if (li != NULL)
10894 {
10895 n = get_tv_number(&li->li_tv);
10896 while (1)
10897 {
10898 li = li->li_next;
10899 if (li == NULL)
10900 break;
10901 i = get_tv_number(&li->li_tv);
10902 if (domax ? i > n : i < n)
10903 n = i;
10904 }
10905 }
10906 }
10907 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010908 else if (argvars[0].v_type == VAR_DICT)
10909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010910 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010911 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010912 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010913 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010914
10915 d = argvars[0].vval.v_dict;
10916 if (d != NULL)
10917 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010918 todo = d->dv_hashtab.ht_used;
10919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010921 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010922 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010923 --todo;
10924 i = get_tv_number(&HI2DI(hi)->di_tv);
10925 if (first)
10926 {
10927 n = i;
10928 first = FALSE;
10929 }
10930 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010931 n = i;
10932 }
10933 }
10934 }
10935 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010936 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010937 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010938 rettv->vval.v_number = n;
10939}
10940
10941/*
10942 * "max()" function
10943 */
10944 static void
10945f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 typval_T *argvars;
10947 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010948{
10949 max_min(argvars, rettv, TRUE);
10950}
10951
10952/*
10953 * "min()" function
10954 */
10955 static void
10956f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010959{
10960 max_min(argvars, rettv, FALSE);
10961}
10962
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010963static int mkdir_recurse __ARGS((char_u *dir, int prot));
10964
10965/*
10966 * Create the directory in which "dir" is located, and higher levels when
10967 * needed.
10968 */
10969 static int
10970mkdir_recurse(dir, prot)
10971 char_u *dir;
10972 int prot;
10973{
10974 char_u *p;
10975 char_u *updir;
10976 int r = FAIL;
10977
10978 /* Get end of directory name in "dir".
10979 * We're done when it's "/" or "c:/". */
10980 p = gettail_sep(dir);
10981 if (p <= get_past_head(dir))
10982 return OK;
10983
10984 /* If the directory exists we're done. Otherwise: create it.*/
10985 updir = vim_strnsave(dir, (int)(p - dir));
10986 if (updir == NULL)
10987 return FAIL;
10988 if (mch_isdir(updir))
10989 r = OK;
10990 else if (mkdir_recurse(updir, prot) == OK)
10991 r = vim_mkdir_emsg(updir, prot);
10992 vim_free(updir);
10993 return r;
10994}
10995
10996#ifdef vim_mkdir
10997/*
10998 * "mkdir()" function
10999 */
11000 static void
11001f_mkdir(argvars, rettv)
11002 typval_T *argvars;
11003 typval_T *rettv;
11004{
11005 char_u *dir;
11006 char_u buf[NUMBUFLEN];
11007 int prot = 0755;
11008
11009 rettv->vval.v_number = FAIL;
11010 if (check_restricted() || check_secure())
11011 return;
11012
11013 dir = get_tv_string_buf(&argvars[0], buf);
11014 if (argvars[1].v_type != VAR_UNKNOWN)
11015 {
11016 if (argvars[2].v_type != VAR_UNKNOWN)
11017 prot = get_tv_number(&argvars[2]);
11018 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
11019 mkdir_recurse(dir, prot);
11020 }
11021 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
11022}
11023#endif
11024
Bram Moolenaar0d660222005-01-07 21:51:51 +000011025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 * "mode()" function
11027 */
11028/*ARGSUSED*/
11029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 typval_T *argvars;
11032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034 char_u buf[2];
11035
11036#ifdef FEAT_VISUAL
11037 if (VIsual_active)
11038 {
11039 if (VIsual_select)
11040 buf[0] = VIsual_mode + 's' - 'v';
11041 else
11042 buf[0] = VIsual_mode;
11043 }
11044 else
11045#endif
11046 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11047 buf[0] = 'r';
11048 else if (State & INSERT)
11049 {
11050 if (State & REPLACE_FLAG)
11051 buf[0] = 'R';
11052 else
11053 buf[0] = 'i';
11054 }
11055 else if (State & CMDLINE)
11056 buf[0] = 'c';
11057 else
11058 buf[0] = 'n';
11059
11060 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_string = vim_strsave(buf);
11062 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063}
11064
11065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011066 * "nextnonblank()" function
11067 */
11068 static void
11069f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 typval_T *argvars;
11071 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011072{
11073 linenr_T lnum;
11074
11075 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11076 {
11077 if (lnum > curbuf->b_ml.ml_line_count)
11078 {
11079 lnum = 0;
11080 break;
11081 }
11082 if (*skipwhite(ml_get(lnum)) != NUL)
11083 break;
11084 }
11085 rettv->vval.v_number = lnum;
11086}
11087
11088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 * "nr2char()" function
11090 */
11091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011093 typval_T *argvars;
11094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
11096 char_u buf[NUMBUFLEN];
11097
11098#ifdef FEAT_MBYTE
11099 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 else
11102#endif
11103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 buf[1] = NUL;
11106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 rettv->v_type = VAR_STRING;
11108 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011109}
11110
11111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112 * "prevnonblank()" function
11113 */
11114 static void
11115f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118{
11119 linenr_T lnum;
11120
11121 lnum = get_tv_lnum(argvars);
11122 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11123 lnum = 0;
11124 else
11125 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11126 --lnum;
11127 rettv->vval.v_number = lnum;
11128}
11129
Bram Moolenaar8c711452005-01-14 21:53:12 +000011130/*
11131 * "range()" function
11132 */
11133 static void
11134f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011137{
11138 long start;
11139 long end;
11140 long stride = 1;
11141 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 list_T *l;
11143 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011144
11145 start = get_tv_number(&argvars[0]);
11146 if (argvars[1].v_type == VAR_UNKNOWN)
11147 {
11148 end = start - 1;
11149 start = 0;
11150 }
11151 else
11152 {
11153 end = get_tv_number(&argvars[1]);
11154 if (argvars[2].v_type != VAR_UNKNOWN)
11155 stride = get_tv_number(&argvars[2]);
11156 }
11157
11158 rettv->vval.v_number = 0;
11159 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011160 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011161 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011162 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011163 else
11164 {
11165 l = list_alloc();
11166 if (l != NULL)
11167 {
11168 rettv->v_type = VAR_LIST;
11169 rettv->vval.v_list = l;
11170 ++l->lv_refcount;
11171
11172 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11173 {
11174 li = listitem_alloc();
11175 if (li == NULL)
11176 break;
11177 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011178 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011179 li->li_tv.vval.v_number = i;
11180 list_append(l, li);
11181 }
11182 }
11183 }
11184}
11185
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011186/*
11187 * "readfile()" function
11188 */
11189 static void
11190f_readfile(argvars, rettv)
11191 typval_T *argvars;
11192 typval_T *rettv;
11193{
11194 int binary = FALSE;
11195 char_u *fname;
11196 FILE *fd;
11197 list_T *l;
11198 listitem_T *li;
11199#define FREAD_SIZE 200 /* optimized for text lines */
11200 char_u buf[FREAD_SIZE];
11201 int readlen; /* size of last fread() */
11202 int buflen; /* nr of valid chars in buf[] */
11203 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11204 int tolist; /* first byte in buf[] still to be put in list */
11205 int chop; /* how many CR to chop off */
11206 char_u *prev = NULL; /* previously read bytes, if any */
11207 int prevlen = 0; /* length of "prev" if not NULL */
11208 char_u *s;
11209 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011210 long maxline = MAXLNUM;
11211 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011213 if (argvars[1].v_type != VAR_UNKNOWN)
11214 {
11215 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11216 binary = TRUE;
11217 if (argvars[2].v_type != VAR_UNKNOWN)
11218 maxline = get_tv_number(&argvars[2]);
11219 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011220
11221 l = list_alloc();
11222 if (l == NULL)
11223 return;
11224 rettv->v_type = VAR_LIST;
11225 rettv->vval.v_list = l;
11226 l->lv_refcount = 1;
11227
11228 /* Always open the file in binary mode, library functions have a mind of
11229 * their own about CR-LF conversion. */
11230 fname = get_tv_string(&argvars[0]);
11231 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11232 {
11233 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11234 return;
11235 }
11236
11237 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011238 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011239 {
11240 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11241 buflen = filtd + readlen;
11242 tolist = 0;
11243 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11244 {
11245 if (buf[filtd] == '\n' || readlen <= 0)
11246 {
11247 /* Only when in binary mode add an empty list item when the
11248 * last line ends in a '\n'. */
11249 if (!binary && readlen == 0 && filtd == 0)
11250 break;
11251
11252 /* Found end-of-line or end-of-file: add a text line to the
11253 * list. */
11254 chop = 0;
11255 if (!binary)
11256 while (filtd - chop - 1 >= tolist
11257 && buf[filtd - chop - 1] == '\r')
11258 ++chop;
11259 len = filtd - tolist - chop;
11260 if (prev == NULL)
11261 s = vim_strnsave(buf + tolist, len);
11262 else
11263 {
11264 s = alloc((unsigned)(prevlen + len + 1));
11265 if (s != NULL)
11266 {
11267 mch_memmove(s, prev, prevlen);
11268 vim_free(prev);
11269 prev = NULL;
11270 mch_memmove(s + prevlen, buf + tolist, len);
11271 s[prevlen + len] = NUL;
11272 }
11273 }
11274 tolist = filtd + 1;
11275
11276 li = listitem_alloc();
11277 if (li == NULL)
11278 {
11279 vim_free(s);
11280 break;
11281 }
11282 li->li_tv.v_type = VAR_STRING;
11283 li->li_tv.v_lock = 0;
11284 li->li_tv.vval.v_string = s;
11285 list_append(l, li);
11286
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011287 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011288 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011289 if (readlen <= 0)
11290 break;
11291 }
11292 else if (buf[filtd] == NUL)
11293 buf[filtd] = '\n';
11294 }
11295 if (readlen <= 0)
11296 break;
11297
11298 if (tolist == 0)
11299 {
11300 /* "buf" is full, need to move text to an allocated buffer */
11301 if (prev == NULL)
11302 {
11303 prev = vim_strnsave(buf, buflen);
11304 prevlen = buflen;
11305 }
11306 else
11307 {
11308 s = alloc((unsigned)(prevlen + buflen));
11309 if (s != NULL)
11310 {
11311 mch_memmove(s, prev, prevlen);
11312 mch_memmove(s + prevlen, buf, buflen);
11313 vim_free(prev);
11314 prev = s;
11315 prevlen += buflen;
11316 }
11317 }
11318 filtd = 0;
11319 }
11320 else
11321 {
11322 mch_memmove(buf, buf + tolist, buflen - tolist);
11323 filtd -= tolist;
11324 }
11325 }
11326
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011327 /*
11328 * For a negative line count use only the lines at the end of the file,
11329 * free the rest.
11330 */
11331 if (maxline < 0)
11332 while (cnt > -maxline)
11333 {
11334 listitem_remove(l, l->lv_first);
11335 --cnt;
11336 }
11337
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011338 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011339 fclose(fd);
11340}
11341
11342
Bram Moolenaar0d660222005-01-07 21:51:51 +000011343#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11344static void make_connection __ARGS((void));
11345static int check_connection __ARGS((void));
11346
11347 static void
11348make_connection()
11349{
11350 if (X_DISPLAY == NULL
11351# ifdef FEAT_GUI
11352 && !gui.in_use
11353# endif
11354 )
11355 {
11356 x_force_connect = TRUE;
11357 setup_term_clip();
11358 x_force_connect = FALSE;
11359 }
11360}
11361
11362 static int
11363check_connection()
11364{
11365 make_connection();
11366 if (X_DISPLAY == NULL)
11367 {
11368 EMSG(_("E240: No connection to Vim server"));
11369 return FAIL;
11370 }
11371 return OK;
11372}
11373#endif
11374
11375#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011376static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377
11378 static void
11379remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382 int expr;
11383{
11384 char_u *server_name;
11385 char_u *keys;
11386 char_u *r = NULL;
11387 char_u buf[NUMBUFLEN];
11388# ifdef WIN32
11389 HWND w;
11390# else
11391 Window w;
11392# endif
11393
11394 if (check_restricted() || check_secure())
11395 return;
11396
11397# ifdef FEAT_X11
11398 if (check_connection() == FAIL)
11399 return;
11400# endif
11401
11402 server_name = get_tv_string(&argvars[0]);
11403 keys = get_tv_string_buf(&argvars[1], buf);
11404# ifdef WIN32
11405 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11406# else
11407 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11408 < 0)
11409# endif
11410 {
11411 if (r != NULL)
11412 EMSG(r); /* sending worked but evaluation failed */
11413 else
11414 EMSG2(_("E241: Unable to send to %s"), server_name);
11415 return;
11416 }
11417
11418 rettv->vval.v_string = r;
11419
11420 if (argvars[2].v_type != VAR_UNKNOWN)
11421 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000011423 char_u str[30];
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424
11425 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 v.di_tv.v_type = VAR_STRING;
11427 v.di_tv.vval.v_string = vim_strsave(str);
11428 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11429 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011430 }
11431}
11432#endif
11433
11434/*
11435 * "remote_expr()" function
11436 */
11437/*ARGSUSED*/
11438 static void
11439f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011442{
11443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = NULL;
11445#ifdef FEAT_CLIENTSERVER
11446 remote_common(argvars, rettv, TRUE);
11447#endif
11448}
11449
11450/*
11451 * "remote_foreground()" function
11452 */
11453/*ARGSUSED*/
11454 static void
11455f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011456 typval_T *argvars;
11457 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011458{
11459 rettv->vval.v_number = 0;
11460#ifdef FEAT_CLIENTSERVER
11461# ifdef WIN32
11462 /* On Win32 it's done in this application. */
11463 serverForeground(get_tv_string(&argvars[0]));
11464# else
11465 /* Send a foreground() expression to the server. */
11466 argvars[1].v_type = VAR_STRING;
11467 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11468 argvars[2].v_type = VAR_UNKNOWN;
11469 remote_common(argvars, rettv, TRUE);
11470 vim_free(argvars[1].vval.v_string);
11471# endif
11472#endif
11473}
11474
11475/*ARGSUSED*/
11476 static void
11477f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *argvars;
11479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011480{
11481#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011483 char_u *s = NULL;
11484# ifdef WIN32
11485 int n = 0;
11486# endif
11487
11488 if (check_restricted() || check_secure())
11489 {
11490 rettv->vval.v_number = -1;
11491 return;
11492 }
11493# ifdef WIN32
11494 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11495 if (n == 0)
11496 rettv->vval.v_number = -1;
11497 else
11498 {
11499 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11500 rettv->vval.v_number = (s != NULL);
11501 }
11502# else
11503 rettv->vval.v_number = 0;
11504 if (check_connection() == FAIL)
11505 return;
11506
11507 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11508 serverStrToWin(get_tv_string(&argvars[0])), &s);
11509# endif
11510
11511 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11512 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 v.di_tv.v_type = VAR_STRING;
11514 v.di_tv.vval.v_string = vim_strsave(s);
11515 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11516 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011517 }
11518#else
11519 rettv->vval.v_number = -1;
11520#endif
11521}
11522
11523/*ARGSUSED*/
11524 static void
11525f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 typval_T *argvars;
11527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011528{
11529 char_u *r = NULL;
11530
11531#ifdef FEAT_CLIENTSERVER
11532 if (!check_restricted() && !check_secure())
11533 {
11534# ifdef WIN32
11535 /* The server's HWND is encoded in the 'id' parameter */
11536 int n = 0;
11537
11538 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11539 if (n != 0)
11540 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11541 if (r == NULL)
11542# else
11543 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11544 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11545# endif
11546 EMSG(_("E277: Unable to read a server reply"));
11547 }
11548#endif
11549 rettv->v_type = VAR_STRING;
11550 rettv->vval.v_string = r;
11551}
11552
11553/*
11554 * "remote_send()" function
11555 */
11556/*ARGSUSED*/
11557 static void
11558f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011559 typval_T *argvars;
11560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011561{
11562 rettv->v_type = VAR_STRING;
11563 rettv->vval.v_string = NULL;
11564#ifdef FEAT_CLIENTSERVER
11565 remote_common(argvars, rettv, FALSE);
11566#endif
11567}
11568
11569/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011570 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011571 */
11572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 typval_T *argvars;
11575 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011576{
Bram Moolenaar33570922005-01-25 22:26:29 +000011577 list_T *l;
11578 listitem_T *item, *item2;
11579 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011580 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011581 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011582 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011583 dict_T *d;
11584 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011585
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011586 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011587 if (argvars[0].v_type == VAR_DICT)
11588 {
11589 if (argvars[2].v_type != VAR_UNKNOWN)
11590 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011591 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011592 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011593 {
11594 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011595 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011596 if (di == NULL)
11597 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011598 else
11599 {
11600 *rettv = di->di_tv;
11601 init_tv(&di->di_tv);
11602 dictitem_remove(d, di);
11603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011604 }
11605 }
11606 else if (argvars[0].v_type != VAR_LIST)
11607 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011608 else if ((l = argvars[0].vval.v_list) != NULL
11609 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011612 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011613 if (item == NULL)
11614 EMSGN(_(e_listidx), idx);
11615 else
11616 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011617 if (argvars[2].v_type == VAR_UNKNOWN)
11618 {
11619 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011620 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011621 *rettv = item->li_tv;
11622 vim_free(item);
11623 }
11624 else
11625 {
11626 /* Remove range of items, return list with values. */
11627 end = get_tv_number(&argvars[2]);
11628 item2 = list_find(l, end);
11629 if (item2 == NULL)
11630 EMSGN(_(e_listidx), end);
11631 else
11632 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011633 int cnt = 0;
11634
11635 for (li = item; li != NULL; li = li->li_next)
11636 {
11637 ++cnt;
11638 if (li == item2)
11639 break;
11640 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011641 if (li == NULL) /* didn't find "item2" after "item" */
11642 EMSG(_(e_invrange));
11643 else
11644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011645 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011646 l = list_alloc();
11647 if (l != NULL)
11648 {
11649 rettv->v_type = VAR_LIST;
11650 rettv->vval.v_list = l;
11651 l->lv_first = item;
11652 l->lv_last = item2;
11653 l->lv_refcount = 1;
11654 item->li_prev = NULL;
11655 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011656 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011657 }
11658 }
11659 }
11660 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011661 }
11662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663}
11664
11665/*
11666 * "rename({from}, {to})" function
11667 */
11668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011670 typval_T *argvars;
11671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672{
11673 char_u buf[NUMBUFLEN];
11674
11675 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11679 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680}
11681
11682/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011683 * "repeat()" function
11684 */
11685/*ARGSUSED*/
11686 static void
11687f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011688 typval_T *argvars;
11689 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011690{
11691 char_u *p;
11692 int n;
11693 int slen;
11694 int len;
11695 char_u *r;
11696 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011698
11699 n = get_tv_number(&argvars[1]);
11700 if (argvars[0].v_type == VAR_LIST)
11701 {
11702 l = list_alloc();
11703 if (l != NULL && argvars[0].vval.v_list != NULL)
11704 {
11705 l->lv_refcount = 1;
11706 while (n-- > 0)
11707 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11708 break;
11709 }
11710 rettv->v_type = VAR_LIST;
11711 rettv->vval.v_list = l;
11712 }
11713 else
11714 {
11715 p = get_tv_string(&argvars[0]);
11716 rettv->v_type = VAR_STRING;
11717 rettv->vval.v_string = NULL;
11718
11719 slen = (int)STRLEN(p);
11720 len = slen * n;
11721 if (len <= 0)
11722 return;
11723
11724 r = alloc(len + 1);
11725 if (r != NULL)
11726 {
11727 for (i = 0; i < n; i++)
11728 mch_memmove(r + i * slen, p, (size_t)slen);
11729 r[len] = NUL;
11730 }
11731
11732 rettv->vval.v_string = r;
11733 }
11734}
11735
11736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 * "resolve()" function
11738 */
11739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011741 typval_T *argvars;
11742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743{
11744 char_u *p;
11745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011746 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747#ifdef FEAT_SHORTCUT
11748 {
11749 char_u *v = NULL;
11750
11751 v = mch_resolve_shortcut(p);
11752 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 }
11757#else
11758# ifdef HAVE_READLINK
11759 {
11760 char_u buf[MAXPATHL + 1];
11761 char_u *cpy;
11762 int len;
11763 char_u *remain = NULL;
11764 char_u *q;
11765 int is_relative_to_current = FALSE;
11766 int has_trailing_pathsep = FALSE;
11767 int limit = 100;
11768
11769 p = vim_strsave(p);
11770
11771 if (p[0] == '.' && (vim_ispathsep(p[1])
11772 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11773 is_relative_to_current = TRUE;
11774
11775 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011776 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 has_trailing_pathsep = TRUE;
11778
11779 q = getnextcomp(p);
11780 if (*q != NUL)
11781 {
11782 /* Separate the first path component in "p", and keep the
11783 * remainder (beginning with the path separator). */
11784 remain = vim_strsave(q - 1);
11785 q[-1] = NUL;
11786 }
11787
11788 for (;;)
11789 {
11790 for (;;)
11791 {
11792 len = readlink((char *)p, (char *)buf, MAXPATHL);
11793 if (len <= 0)
11794 break;
11795 buf[len] = NUL;
11796
11797 if (limit-- == 0)
11798 {
11799 vim_free(p);
11800 vim_free(remain);
11801 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 goto fail;
11804 }
11805
11806 /* Ensure that the result will have a trailing path separator
11807 * if the argument has one. */
11808 if (remain == NULL && has_trailing_pathsep)
11809 add_pathsep(buf);
11810
11811 /* Separate the first path component in the link value and
11812 * concatenate the remainders. */
11813 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11814 if (*q != NUL)
11815 {
11816 if (remain == NULL)
11817 remain = vim_strsave(q - 1);
11818 else
11819 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011820 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 if (cpy != NULL)
11822 {
11823 STRCAT(cpy, remain);
11824 vim_free(remain);
11825 remain = cpy;
11826 }
11827 }
11828 q[-1] = NUL;
11829 }
11830
11831 q = gettail(p);
11832 if (q > p && *q == NUL)
11833 {
11834 /* Ignore trailing path separator. */
11835 q[-1] = NUL;
11836 q = gettail(p);
11837 }
11838 if (q > p && !mch_isFullName(buf))
11839 {
11840 /* symlink is relative to directory of argument */
11841 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11842 if (cpy != NULL)
11843 {
11844 STRCPY(cpy, p);
11845 STRCPY(gettail(cpy), buf);
11846 vim_free(p);
11847 p = cpy;
11848 }
11849 }
11850 else
11851 {
11852 vim_free(p);
11853 p = vim_strsave(buf);
11854 }
11855 }
11856
11857 if (remain == NULL)
11858 break;
11859
11860 /* Append the first path component of "remain" to "p". */
11861 q = getnextcomp(remain + 1);
11862 len = q - remain - (*q != NUL);
11863 cpy = vim_strnsave(p, STRLEN(p) + len);
11864 if (cpy != NULL)
11865 {
11866 STRNCAT(cpy, remain, len);
11867 vim_free(p);
11868 p = cpy;
11869 }
11870 /* Shorten "remain". */
11871 if (*q != NUL)
11872 STRCPY(remain, q - 1);
11873 else
11874 {
11875 vim_free(remain);
11876 remain = NULL;
11877 }
11878 }
11879
11880 /* If the result is a relative path name, make it explicitly relative to
11881 * the current directory if and only if the argument had this form. */
11882 if (!vim_ispathsep(*p))
11883 {
11884 if (is_relative_to_current
11885 && *p != NUL
11886 && !(p[0] == '.'
11887 && (p[1] == NUL
11888 || vim_ispathsep(p[1])
11889 || (p[1] == '.'
11890 && (p[2] == NUL
11891 || vim_ispathsep(p[2]))))))
11892 {
11893 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011894 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 if (cpy != NULL)
11896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 vim_free(p);
11898 p = cpy;
11899 }
11900 }
11901 else if (!is_relative_to_current)
11902 {
11903 /* Strip leading "./". */
11904 q = p;
11905 while (q[0] == '.' && vim_ispathsep(q[1]))
11906 q += 2;
11907 if (q > p)
11908 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11909 }
11910 }
11911
11912 /* Ensure that the result will have no trailing path separator
11913 * if the argument had none. But keep "/" or "//". */
11914 if (!has_trailing_pathsep)
11915 {
11916 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011917 if (after_pathsep(p, q))
11918 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 }
11920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011921 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 }
11923# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925# endif
11926#endif
11927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929
11930#ifdef HAVE_READLINK
11931fail:
11932#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934}
11935
11936/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011937 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 */
11939 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011940f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011941 typval_T *argvars;
11942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943{
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 list_T *l;
11945 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946
Bram Moolenaar0d660222005-01-07 21:51:51 +000011947 rettv->vval.v_number = 0;
11948 if (argvars[0].v_type != VAR_LIST)
11949 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011950 else if ((l = argvars[0].vval.v_list) != NULL
11951 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011952 {
11953 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011954 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011955 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956 while (li != NULL)
11957 {
11958 ni = li->li_prev;
11959 list_append(l, li);
11960 li = ni;
11961 }
11962 rettv->vval.v_list = l;
11963 rettv->v_type = VAR_LIST;
11964 ++l->lv_refcount;
11965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966}
11967
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011968#define SP_NOMOVE 1 /* don't move cursor */
11969#define SP_REPEAT 2 /* repeat to find outer pair */
11970#define SP_RETCOUNT 4 /* return matchcount */
11971
Bram Moolenaar33570922005-01-25 22:26:29 +000011972static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011973
11974/*
11975 * Get flags for a search function.
11976 * Possibly sets "p_ws".
11977 * Returns BACKWARD, FORWARD or zero (for an error).
11978 */
11979 static int
11980get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011982 int *flagsp;
11983{
11984 int dir = FORWARD;
11985 char_u *flags;
11986 char_u nbuf[NUMBUFLEN];
11987 int mask;
11988
11989 if (varp->v_type != VAR_UNKNOWN)
11990 {
11991 flags = get_tv_string_buf(varp, nbuf);
11992 while (*flags != NUL)
11993 {
11994 switch (*flags)
11995 {
11996 case 'b': dir = BACKWARD; break;
11997 case 'w': p_ws = TRUE; break;
11998 case 'W': p_ws = FALSE; break;
11999 default: mask = 0;
12000 if (flagsp != NULL)
12001 switch (*flags)
12002 {
12003 case 'n': mask = SP_NOMOVE; break;
12004 case 'r': mask = SP_REPEAT; break;
12005 case 'm': mask = SP_RETCOUNT; break;
12006 }
12007 if (mask == 0)
12008 {
12009 EMSG2(_(e_invarg2), flags);
12010 dir = 0;
12011 }
12012 else
12013 *flagsp |= mask;
12014 }
12015 if (dir == 0)
12016 break;
12017 ++flags;
12018 }
12019 }
12020 return dir;
12021}
12022
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023/*
12024 * "search()" function
12025 */
12026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 typval_T *argvars;
12029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
12031 char_u *pat;
12032 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012033 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 int save_p_ws = p_ws;
12035 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012036 int flags = 0;
12037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012041 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12042 if (dir == 0)
12043 goto theend;
12044 if ((flags & ~SP_NOMOVE) != 0)
12045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012047 goto theend;
12048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012050 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12052 SEARCH_KEEP, RE_SEARCH) != FAIL)
12053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 curwin->w_cursor = pos;
12056 /* "/$" will put the cursor after the end of the line, may need to
12057 * correct that here */
12058 check_cursor();
12059 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012060
12061 /* If 'n' flag is used: restore cursor position. */
12062 if (flags & SP_NOMOVE)
12063 curwin->w_cursor = save_cursor;
12064theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 p_ws = save_p_ws;
12066}
12067
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068/*
12069 * "searchpair()" function
12070 */
12071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 typval_T *argvars;
12074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
12076 char_u *spat, *mpat, *epat;
12077 char_u *skip;
12078 char_u *pat, *pat2, *pat3;
12079 pos_T pos;
12080 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012081 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 pos_T save_cursor;
12083 pos_T save_pos;
12084 int save_p_ws = p_ws;
12085 char_u *save_cpo;
12086 int dir;
12087 int flags = 0;
12088 char_u nbuf1[NUMBUFLEN];
12089 char_u nbuf2[NUMBUFLEN];
12090 char_u nbuf3[NUMBUFLEN];
12091 int n;
12092 int r;
12093 int nest = 1;
12094 int err;
12095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097
12098 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12099 save_cpo = p_cpo;
12100 p_cpo = (char_u *)"";
12101
12102 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 spat = get_tv_string(&argvars[0]);
12104 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12105 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106
12107 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12108 * start/middle/end (pat3, for the top pair). */
12109 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12110 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12111 if (pat2 == NULL || pat3 == NULL)
12112 goto theend;
12113 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12114 if (*mpat == NUL)
12115 STRCPY(pat3, pat2);
12116 else
12117 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12118 spat, epat, mpat);
12119
12120 /* Handle the optional fourth argument: flags */
12121 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012122 if (dir == 0)
12123 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124
12125 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012126 if (argvars[3].v_type == VAR_UNKNOWN
12127 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 skip = (char_u *)"";
12129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131
12132 save_cursor = curwin->w_cursor;
12133 pos = curwin->w_cursor;
12134 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012135 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 pat = pat3;
12137 for (;;)
12138 {
12139 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12140 SEARCH_KEEP, RE_SEARCH);
12141 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12142 /* didn't find it or found the first match again: FAIL */
12143 break;
12144
12145 if (firstpos.lnum == 0)
12146 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012147 if (equalpos(pos, foundpos))
12148 {
12149 /* Found the same position again. Can happen with a pattern that
12150 * has "\zs" at the end and searching backwards. Advance one
12151 * character and try again. */
12152 if (dir == BACKWARD)
12153 decl(&pos);
12154 else
12155 incl(&pos);
12156 }
12157 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158
12159 /* If the skip pattern matches, ignore this match. */
12160 if (*skip != NUL)
12161 {
12162 save_pos = curwin->w_cursor;
12163 curwin->w_cursor = pos;
12164 r = eval_to_bool(skip, &err, NULL, FALSE);
12165 curwin->w_cursor = save_pos;
12166 if (err)
12167 {
12168 /* Evaluating {skip} caused an error, break here. */
12169 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 break;
12172 }
12173 if (r)
12174 continue;
12175 }
12176
12177 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12178 {
12179 /* Found end when searching backwards or start when searching
12180 * forward: nested pair. */
12181 ++nest;
12182 pat = pat2; /* nested, don't search for middle */
12183 }
12184 else
12185 {
12186 /* Found end when searching forward or start when searching
12187 * backward: end of (nested) pair; or found middle in outer pair. */
12188 if (--nest == 1)
12189 pat = pat3; /* outer level, search for middle */
12190 }
12191
12192 if (nest == 0)
12193 {
12194 /* Found the match: return matchcount or line number. */
12195 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 curwin->w_cursor = pos;
12200 if (!(flags & SP_REPEAT))
12201 break;
12202 nest = 1; /* search for next unmatched */
12203 }
12204 }
12205
12206 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 curwin->w_cursor = save_cursor;
12209
12210theend:
12211 vim_free(pat2);
12212 vim_free(pat3);
12213 p_ws = save_p_ws;
12214 p_cpo = save_cpo;
12215}
12216
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217/*ARGSUSED*/
12218 static void
12219f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012220 typval_T *argvars;
12221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223#ifdef FEAT_CLIENTSERVER
12224 char_u buf[NUMBUFLEN];
12225 char_u *server = get_tv_string(&argvars[0]);
12226 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227
Bram Moolenaar0d660222005-01-07 21:51:51 +000012228 rettv->vval.v_number = -1;
12229 if (check_restricted() || check_secure())
12230 return;
12231# ifdef FEAT_X11
12232 if (check_connection() == FAIL)
12233 return;
12234# endif
12235
12236 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238 EMSG(_("E258: Unable to send to client"));
12239 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012241 rettv->vval.v_number = 0;
12242#else
12243 rettv->vval.v_number = -1;
12244#endif
12245}
12246
12247/*ARGSUSED*/
12248 static void
12249f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012250 typval_T *argvars;
12251 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012252{
12253 char_u *r = NULL;
12254
12255#ifdef FEAT_CLIENTSERVER
12256# ifdef WIN32
12257 r = serverGetVimNames();
12258# else
12259 make_connection();
12260 if (X_DISPLAY != NULL)
12261 r = serverGetVimNames(X_DISPLAY);
12262# endif
12263#endif
12264 rettv->v_type = VAR_STRING;
12265 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266}
12267
12268/*
12269 * "setbufvar()" function
12270 */
12271/*ARGSUSED*/
12272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012274 typval_T *argvars;
12275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
12277 buf_T *buf;
12278#ifdef FEAT_AUTOCMD
12279 aco_save_T aco;
12280#else
12281 buf_T *save_curbuf;
12282#endif
12283 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 char_u nbuf[NUMBUFLEN];
12286
12287 if (check_restricted() || check_secure())
12288 return;
12289 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 buf = get_buf_tv(&argvars[0]);
12291 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 varp = &argvars[2];
12293
12294 if (buf != NULL && varname != NULL && varp != NULL)
12295 {
12296 /* set curbuf to be our buf, temporarily */
12297#ifdef FEAT_AUTOCMD
12298 aucmd_prepbuf(&aco, buf);
12299#else
12300 save_curbuf = curbuf;
12301 curbuf = buf;
12302#endif
12303
12304 if (*varname == '&')
12305 {
12306 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 set_option_value(varname, get_tv_number(varp),
12308 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 }
12310 else
12311 {
12312 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12313 if (bufvarname != NULL)
12314 {
12315 STRCPY(bufvarname, "b:");
12316 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012317 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 vim_free(bufvarname);
12319 }
12320 }
12321
12322 /* reset notion of buffer */
12323#ifdef FEAT_AUTOCMD
12324 aucmd_restbuf(&aco);
12325#else
12326 curbuf = save_curbuf;
12327#endif
12328 }
12329 --emsg_off;
12330}
12331
12332/*
12333 * "setcmdpos()" function
12334 */
12335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012337 typval_T *argvars;
12338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->vval.v_number = set_cmdline_pos(
12341 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342}
12343
12344/*
12345 * "setline()" function
12346 */
12347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012349 typval_T *argvars;
12350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351{
12352 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000012353 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012354 list_T *l = NULL;
12355 listitem_T *li = NULL;
12356 long added = 0;
12357 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012359 lnum = get_tv_lnum(&argvars[0]);
12360 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012362 l = argvars[1].vval.v_list;
12363 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012365 else
12366 line = get_tv_string(&argvars[1]);
12367
12368 rettv->vval.v_number = 0; /* OK */
12369 for (;;)
12370 {
12371 if (l != NULL)
12372 {
12373 /* list argument, get next string */
12374 if (li == NULL)
12375 break;
12376 line = get_tv_string(&li->li_tv);
12377 li = li->li_next;
12378 }
12379
12380 rettv->vval.v_number = 1; /* FAIL */
12381 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
12382 break;
12383 if (lnum <= curbuf->b_ml.ml_line_count)
12384 {
12385 /* existing line, replace it */
12386 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
12387 {
12388 changed_bytes(lnum, 0);
12389 check_cursor_col();
12390 rettv->vval.v_number = 0; /* OK */
12391 }
12392 }
12393 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
12394 {
12395 /* lnum is one past the last line, append the line */
12396 ++added;
12397 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
12398 rettv->vval.v_number = 0; /* OK */
12399 }
12400
12401 if (l == NULL) /* only one string argument */
12402 break;
12403 ++lnum;
12404 }
12405
12406 if (added > 0)
12407 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408}
12409
12410/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000012411 * "setqflist()" function
12412 */
12413/*ARGSUSED*/
12414 static void
12415f_setqflist(argvars, rettv)
12416 typval_T *argvars;
12417 typval_T *rettv;
12418{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012419 char_u *act;
12420 int action = ' ';
12421
Bram Moolenaar2641f772005-03-25 21:58:17 +000012422 rettv->vval.v_number = -1;
12423
12424#ifdef FEAT_QUICKFIX
12425 if (argvars[0].v_type != VAR_LIST)
12426 EMSG(_(e_listreq));
12427 else
12428 {
12429 list_T *l = argvars[0].vval.v_list;
12430
Bram Moolenaarf4630b62005-05-20 21:31:17 +000012431 if (argvars[1].v_type == VAR_STRING)
12432 {
12433 act = get_tv_string(&argvars[1]);
12434 if (*act == 'a' || *act == 'r')
12435 action = *act;
12436 }
12437
12438 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012439 rettv->vval.v_number = 0;
12440 }
12441#endif
12442}
12443
12444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 * "setreg()" function
12446 */
12447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451{
12452 int regname;
12453 char_u *strregname;
12454 char_u *stropt;
12455 int append;
12456 char_u yank_type;
12457 long block_len;
12458
12459 block_len = -1;
12460 yank_type = MAUTO;
12461 append = FALSE;
12462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 strregname = get_tv_string(argvars);
12464 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465
12466 regname = (strregname == NULL ? '"' : *strregname);
12467 if (regname == 0 || regname == '@')
12468 regname = '"';
12469 else if (regname == '=')
12470 return;
12471
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012472 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 switch (*stropt)
12476 {
12477 case 'a': case 'A': /* append */
12478 append = TRUE;
12479 break;
12480 case 'v': case 'c': /* character-wise selection */
12481 yank_type = MCHAR;
12482 break;
12483 case 'V': case 'l': /* line-wise selection */
12484 yank_type = MLINE;
12485 break;
12486#ifdef FEAT_VISUAL
12487 case 'b': case Ctrl_V: /* block-wise selection */
12488 yank_type = MBLOCK;
12489 if (VIM_ISDIGIT(stropt[1]))
12490 {
12491 ++stropt;
12492 block_len = getdigits(&stropt) - 1;
12493 --stropt;
12494 }
12495 break;
12496#endif
12497 }
12498 }
12499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503}
12504
12505
12506/*
12507 * "setwinvar(expr)" function
12508 */
12509/*ARGSUSED*/
12510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 typval_T *argvars;
12513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514{
12515 win_T *win;
12516#ifdef FEAT_WINDOWS
12517 win_T *save_curwin;
12518#endif
12519 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012520 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 char_u nbuf[NUMBUFLEN];
12522
12523 if (check_restricted() || check_secure())
12524 return;
12525 ++emsg_off;
12526 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012527 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 varp = &argvars[2];
12529
12530 if (win != NULL && varname != NULL && varp != NULL)
12531 {
12532#ifdef FEAT_WINDOWS
12533 /* set curwin to be our win, temporarily */
12534 save_curwin = curwin;
12535 curwin = win;
12536 curbuf = curwin->w_buffer;
12537#endif
12538
12539 if (*varname == '&')
12540 {
12541 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012542 set_option_value(varname, get_tv_number(varp),
12543 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544 }
12545 else
12546 {
12547 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12548 if (winvarname != NULL)
12549 {
12550 STRCPY(winvarname, "w:");
12551 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012552 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 vim_free(winvarname);
12554 }
12555 }
12556
12557#ifdef FEAT_WINDOWS
12558 /* Restore current window, if it's still valid (autocomands can make
12559 * it invalid). */
12560 if (win_valid(save_curwin))
12561 {
12562 curwin = save_curwin;
12563 curbuf = curwin->w_buffer;
12564 }
12565#endif
12566 }
12567 --emsg_off;
12568}
12569
12570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 */
12573 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012575 typval_T *argvars;
12576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580 p = get_tv_string(&argvars[0]);
12581 rettv->vval.v_string = vim_strsave(p);
12582 simplify_filename(rettv->vval.v_string); /* simplify in place */
12583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584}
12585
Bram Moolenaar0d660222005-01-07 21:51:51 +000012586static int
12587#ifdef __BORLANDC__
12588 _RTLENTRYF
12589#endif
12590 item_compare __ARGS((const void *s1, const void *s2));
12591static int
12592#ifdef __BORLANDC__
12593 _RTLENTRYF
12594#endif
12595 item_compare2 __ARGS((const void *s1, const void *s2));
12596
12597static int item_compare_ic;
12598static char_u *item_compare_func;
12599#define ITEM_COMPARE_FAIL 999
12600
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012602 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604 static int
12605#ifdef __BORLANDC__
12606_RTLENTRYF
12607#endif
12608item_compare(s1, s2)
12609 const void *s1;
12610 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012612 char_u *p1, *p2;
12613 char_u *tofree1, *tofree2;
12614 int res;
12615 char_u numbuf1[NUMBUFLEN];
12616 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617
Bram Moolenaar33570922005-01-25 22:26:29 +000012618 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12619 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012620 if (item_compare_ic)
12621 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012623 res = STRCMP(p1, p2);
12624 vim_free(tofree1);
12625 vim_free(tofree2);
12626 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627}
12628
12629 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630#ifdef __BORLANDC__
12631_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633item_compare2(s1, s2)
12634 const void *s1;
12635 const void *s2;
12636{
12637 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T rettv;
12639 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012640 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012642 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12643 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12645 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646
12647 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12648 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012649 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012650 clear_tv(&argv[0]);
12651 clear_tv(&argv[1]);
12652
12653 if (res == FAIL)
12654 res = ITEM_COMPARE_FAIL;
12655 else
12656 res = get_tv_number(&rettv);
12657 clear_tv(&rettv);
12658 return res;
12659}
12660
12661/*
12662 * "sort({list})" function
12663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *argvars;
12667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668{
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 list_T *l;
12670 listitem_T *li;
12671 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012672 long len;
12673 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675 rettv->vval.v_number = 0;
12676 if (argvars[0].v_type != VAR_LIST)
12677 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 else
12679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012680 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012681 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012682 return;
12683 rettv->vval.v_list = l;
12684 rettv->v_type = VAR_LIST;
12685 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686
Bram Moolenaar0d660222005-01-07 21:51:51 +000012687 len = list_len(l);
12688 if (len <= 1)
12689 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690
Bram Moolenaar0d660222005-01-07 21:51:51 +000012691 item_compare_ic = FALSE;
12692 item_compare_func = NULL;
12693 if (argvars[1].v_type != VAR_UNKNOWN)
12694 {
12695 if (argvars[1].v_type == VAR_FUNC)
12696 item_compare_func = argvars[0].vval.v_string;
12697 else
12698 {
12699 i = get_tv_number(&argvars[1]);
12700 if (i == 1)
12701 item_compare_ic = TRUE;
12702 else
12703 item_compare_func = get_tv_string(&argvars[1]);
12704 }
12705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706
Bram Moolenaar0d660222005-01-07 21:51:51 +000012707 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012708 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709 if (ptrs == NULL)
12710 return;
12711 i = 0;
12712 for (li = l->lv_first; li != NULL; li = li->li_next)
12713 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714
Bram Moolenaar0d660222005-01-07 21:51:51 +000012715 /* test the compare function */
12716 if (item_compare_func != NULL
12717 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12718 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012719 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 {
12722 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012724 item_compare_func == NULL ? item_compare : item_compare2);
12725
12726 /* Clear the List and append the items in the sorted order. */
12727 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012728 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012729 for (i = 0; i < len; ++i)
12730 list_append(l, ptrs[i]);
12731 }
12732
12733 vim_free(ptrs);
12734 }
12735}
12736
12737 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012738f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012739 typval_T *argvars;
12740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012741{
12742 char_u *str;
12743 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012744 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012745 regmatch_T regmatch;
12746 char_u patbuf[NUMBUFLEN];
12747 char_u *save_cpo;
12748 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 listitem_T *ni;
12750 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012751 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012752 int keepempty = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012753
12754 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12755 save_cpo = p_cpo;
12756 p_cpo = (char_u *)"";
12757
12758 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012759 if (argvars[1].v_type != VAR_UNKNOWN)
12760 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761 pat = get_tv_string_buf(&argvars[1], patbuf);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012762 if (argvars[2].v_type != VAR_UNKNOWN)
12763 keepempty = get_tv_number(&argvars[2]);
12764 }
12765 if (pat == NULL || *pat == NUL)
12766 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767
12768 l = list_alloc();
12769 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012771 rettv->v_type = VAR_LIST;
12772 rettv->vval.v_list = l;
12773 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
Bram Moolenaar0d660222005-01-07 21:51:51 +000012775 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12776 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012779 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012780 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012781 if (*str == NUL)
12782 match = FALSE; /* empty item at the end */
12783 else
12784 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785 if (match)
12786 end = regmatch.startp[0];
12787 else
12788 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012789 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
12790 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791 {
12792 ni = listitem_alloc();
12793 if (ni == NULL)
12794 break;
12795 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012796 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012797 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12798 list_append(l, ni);
12799 }
12800 if (!match)
12801 break;
12802 /* Advance to just after the match. */
12803 if (regmatch.endp[0] > str)
12804 col = 0;
12805 else
12806 {
12807 /* Don't get stuck at the same match. */
12808#ifdef FEAT_MBYTE
12809 col = mb_ptr2len_check(regmatch.endp[0]);
12810#else
12811 col = 1;
12812#endif
12813 }
12814 str = regmatch.endp[0];
12815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816
Bram Moolenaar0d660222005-01-07 21:51:51 +000012817 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819
Bram Moolenaar0d660222005-01-07 21:51:51 +000012820 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821}
12822
12823#ifdef HAVE_STRFTIME
12824/*
12825 * "strftime({format}[, {time}])" function
12826 */
12827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *argvars;
12830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
12832 char_u result_buf[256];
12833 struct tm *curtime;
12834 time_t seconds;
12835 char_u *p;
12836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012840 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 seconds = time(NULL);
12842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 curtime = localtime(&seconds);
12845 /* MSVC returns NULL for an invalid value of seconds. */
12846 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 else
12849 {
12850# ifdef FEAT_MBYTE
12851 vimconv_T conv;
12852 char_u *enc;
12853
12854 conv.vc_type = CONV_NONE;
12855 enc = enc_locale();
12856 convert_setup(&conv, p_enc, enc);
12857 if (conv.vc_type != CONV_NONE)
12858 p = string_convert(&conv, p, NULL);
12859# endif
12860 if (p != NULL)
12861 (void)strftime((char *)result_buf, sizeof(result_buf),
12862 (char *)p, curtime);
12863 else
12864 result_buf[0] = NUL;
12865
12866# ifdef FEAT_MBYTE
12867 if (conv.vc_type != CONV_NONE)
12868 vim_free(p);
12869 convert_setup(&conv, enc, p_enc);
12870 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 else
12873# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875
12876# ifdef FEAT_MBYTE
12877 /* Release conversion descriptors */
12878 convert_setup(&conv, NULL, NULL);
12879 vim_free(enc);
12880# endif
12881 }
12882}
12883#endif
12884
12885/*
12886 * "stridx()" function
12887 */
12888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *argvars;
12891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892{
12893 char_u buf[NUMBUFLEN];
12894 char_u *needle;
12895 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012898 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12902 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 if (argvars[2].v_type != VAR_UNKNOWN)
12905 {
12906 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012907 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012909 if (start_idx >= 0)
12910 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012911 }
12912
12913 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12914 if (pos != NULL)
12915 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916}
12917
12918/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012919 * "string()" function
12920 */
12921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012923 typval_T *argvars;
12924 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012925{
12926 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012927 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012930 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012931 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933}
12934
12935/*
12936 * "strlen()" function
12937 */
12938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012940 typval_T *argvars;
12941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_number = (varnumber_T)(STRLEN(
12944 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945}
12946
12947/*
12948 * "strpart()" function
12949 */
12950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012952 typval_T *argvars;
12953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954{
12955 char_u *p;
12956 int n;
12957 int len;
12958 int slen;
12959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 slen = (int)STRLEN(p);
12962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 else
12967 len = slen - n; /* default len: all bytes that are available. */
12968
12969 /*
12970 * Only return the overlap between the specified part and the actual
12971 * string.
12972 */
12973 if (n < 0)
12974 {
12975 len += n;
12976 n = 0;
12977 }
12978 else if (n > slen)
12979 n = slen;
12980 if (len < 0)
12981 len = 0;
12982 else if (n + len > slen)
12983 len = slen - n;
12984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985 rettv->v_type = VAR_STRING;
12986 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987}
12988
12989/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012990 * "strridx()" function
12991 */
12992 static void
12993f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012994 typval_T *argvars;
12995 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012996{
12997 char_u buf[NUMBUFLEN];
12998 char_u *needle;
12999 char_u *haystack;
13000 char_u *rest;
13001 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013002 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013003
13004 needle = get_tv_string(&argvars[1]);
13005 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013006 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013007 if (argvars[2].v_type != VAR_UNKNOWN)
13008 {
13009 /* Third argument: upper limit for index */
13010 end_idx = get_tv_number(&argvars[2]);
13011 if (end_idx < 0)
13012 {
13013 /* can never find a match */
13014 rettv->vval.v_number = -1;
13015 return;
13016 }
13017 }
13018 else
13019 end_idx = haystack_len;
13020
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013022 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013024 lastmatch = haystack + end_idx;
13025 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013027 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013028 for (rest = haystack; *rest != '\0'; ++rest)
13029 {
13030 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013031 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032 break;
13033 lastmatch = rest;
13034 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013035 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013036
13037 if (lastmatch == NULL)
13038 rettv->vval.v_number = -1;
13039 else
13040 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13041}
13042
13043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 * "strtrans()" function
13045 */
13046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *argvars;
13049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013051 rettv->v_type = VAR_STRING;
13052 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053}
13054
13055/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013056 * "submatch()" function
13057 */
13058 static void
13059f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013060 typval_T *argvars;
13061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062{
13063 rettv->v_type = VAR_STRING;
13064 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
13065}
13066
13067/*
13068 * "substitute()" function
13069 */
13070 static void
13071f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013072 typval_T *argvars;
13073 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013074{
13075 char_u patbuf[NUMBUFLEN];
13076 char_u subbuf[NUMBUFLEN];
13077 char_u flagsbuf[NUMBUFLEN];
13078
13079 rettv->v_type = VAR_STRING;
13080 rettv->vval.v_string = do_string_sub(
13081 get_tv_string(&argvars[0]),
13082 get_tv_string_buf(&argvars[1], patbuf),
13083 get_tv_string_buf(&argvars[2], subbuf),
13084 get_tv_string_buf(&argvars[3], flagsbuf));
13085}
13086
13087/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013088 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 */
13090/*ARGSUSED*/
13091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013093 typval_T *argvars;
13094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095{
13096 int id = 0;
13097#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013098 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 long col;
13100 int trans;
13101
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013102 lnum = get_tv_lnum(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 col = get_tv_number(&argvars[1]) - 1;
13104 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13107 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13108 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109#endif
13110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112}
13113
13114/*
13115 * "synIDattr(id, what [, mode])" function
13116 */
13117/*ARGSUSED*/
13118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122{
13123 char_u *p = NULL;
13124#ifdef FEAT_SYN_HL
13125 int id;
13126 char_u *what;
13127 char_u *mode;
13128 char_u modebuf[NUMBUFLEN];
13129 int modec;
13130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 id = get_tv_number(&argvars[0]);
13132 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136 modec = TOLOWER_ASC(mode[0]);
13137 if (modec != 't' && modec != 'c'
13138#ifdef FEAT_GUI
13139 && modec != 'g'
13140#endif
13141 )
13142 modec = 0; /* replace invalid with current */
13143 }
13144 else
13145 {
13146#ifdef FEAT_GUI
13147 if (gui.in_use)
13148 modec = 'g';
13149 else
13150#endif
13151 if (t_colors > 1)
13152 modec = 'c';
13153 else
13154 modec = 't';
13155 }
13156
13157
13158 switch (TOLOWER_ASC(what[0]))
13159 {
13160 case 'b':
13161 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13162 p = highlight_color(id, what, modec);
13163 else /* bold */
13164 p = highlight_has_attr(id, HL_BOLD, modec);
13165 break;
13166
13167 case 'f': /* fg[#] */
13168 p = highlight_color(id, what, modec);
13169 break;
13170
13171 case 'i':
13172 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13173 p = highlight_has_attr(id, HL_INVERSE, modec);
13174 else /* italic */
13175 p = highlight_has_attr(id, HL_ITALIC, modec);
13176 break;
13177
13178 case 'n': /* name */
13179 p = get_highlight_name(NULL, id - 1);
13180 break;
13181
13182 case 'r': /* reverse */
13183 p = highlight_has_attr(id, HL_INVERSE, modec);
13184 break;
13185
13186 case 's': /* standout */
13187 p = highlight_has_attr(id, HL_STANDOUT, modec);
13188 break;
13189
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013190 case 'u':
13191 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13192 /* underline */
13193 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13194 else
13195 /* undercurl */
13196 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 break;
13198 }
13199
13200 if (p != NULL)
13201 p = vim_strsave(p);
13202#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->v_type = VAR_STRING;
13204 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205}
13206
13207/*
13208 * "synIDtrans(id)" function
13209 */
13210/*ARGSUSED*/
13211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013213 typval_T *argvars;
13214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215{
13216 int id;
13217
13218#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220
13221 if (id > 0)
13222 id = syn_get_final_id(id);
13223 else
13224#endif
13225 id = 0;
13226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228}
13229
13230/*
13231 * "system()" function
13232 */
13233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013235 typval_T *argvars;
13236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013238 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013240 char_u *infile = NULL;
13241 char_u buf[NUMBUFLEN];
13242 int err = FALSE;
13243 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013245 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013246 {
13247 /*
13248 * Write the string to a temp file, to be used for input of the shell
13249 * command.
13250 */
13251 if ((infile = vim_tempname('i')) == NULL)
13252 {
13253 EMSG(_(e_notmp));
13254 return;
13255 }
13256
13257 fd = mch_fopen((char *)infile, WRITEBIN);
13258 if (fd == NULL)
13259 {
13260 EMSG2(_(e_notopen), infile);
13261 goto done;
13262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013264 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13265 err = TRUE;
13266 if (fclose(fd) != 0)
13267 err = TRUE;
13268 if (err)
13269 {
13270 EMSG(_("E677: Error writing temp file"));
13271 goto done;
13272 }
13273 }
13274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013275 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013276
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277#ifdef USE_CR
13278 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013279 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 {
13281 char_u *s;
13282
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013283 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 {
13285 if (*s == CAR)
13286 *s = NL;
13287 }
13288 }
13289#else
13290# ifdef USE_CRNL
13291 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013292 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 {
13294 char_u *s, *d;
13295
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013296 d = res;
13297 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 {
13299 if (s[0] == CAR && s[1] == NL)
13300 ++s;
13301 *d++ = *s;
13302 }
13303 *d = NUL;
13304 }
13305# endif
13306#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013307
13308done:
13309 if (infile != NULL)
13310 {
13311 mch_remove(infile);
13312 vim_free(infile);
13313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314 rettv->v_type = VAR_STRING;
13315 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316}
13317
13318/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013319 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013320 */
13321 static void
13322f_taglist(argvars, rettv)
13323 typval_T *argvars;
13324 typval_T *rettv;
13325{
13326 char_u *tag_pattern;
13327 list_T *l;
13328
13329 tag_pattern = get_tv_string(&argvars[0]);
13330
13331 rettv->vval.v_number = FALSE;
13332
13333 l = list_alloc();
13334 if (l != NULL)
13335 {
13336 if (get_tags(l, tag_pattern) != FAIL)
13337 {
13338 rettv->vval.v_list = l;
13339 rettv->v_type = VAR_LIST;
13340 ++l->lv_refcount;
13341 }
13342 else
13343 list_free(l);
13344 }
13345}
13346
13347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 * "tempname()" function
13349 */
13350/*ARGSUSED*/
13351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 typval_T *argvars;
13354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355{
13356 static int x = 'A';
13357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013358 rettv->v_type = VAR_STRING;
13359 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360
13361 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13362 * names. Skip 'I' and 'O', they are used for shell redirection. */
13363 do
13364 {
13365 if (x == 'Z')
13366 x = '0';
13367 else if (x == '9')
13368 x = 'A';
13369 else
13370 {
13371#ifdef EBCDIC
13372 if (x == 'I')
13373 x = 'J';
13374 else if (x == 'R')
13375 x = 'S';
13376 else
13377#endif
13378 ++x;
13379 }
13380 } while (x == 'I' || x == 'O');
13381}
13382
13383/*
13384 * "tolower(string)" function
13385 */
13386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 typval_T *argvars;
13389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390{
13391 char_u *p;
13392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013393 p = vim_strsave(get_tv_string(&argvars[0]));
13394 rettv->v_type = VAR_STRING;
13395 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396
13397 if (p != NULL)
13398 while (*p != NUL)
13399 {
13400#ifdef FEAT_MBYTE
13401 int l;
13402
13403 if (enc_utf8)
13404 {
13405 int c, lc;
13406
13407 c = utf_ptr2char(p);
13408 lc = utf_tolower(c);
13409 l = utf_ptr2len_check(p);
13410 /* TODO: reallocate string when byte count changes. */
13411 if (utf_char2len(lc) == l)
13412 utf_char2bytes(lc, p);
13413 p += l;
13414 }
13415 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13416 p += l; /* skip multi-byte character */
13417 else
13418#endif
13419 {
13420 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13421 ++p;
13422 }
13423 }
13424}
13425
13426/*
13427 * "toupper(string)" function
13428 */
13429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 typval_T *argvars;
13432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433{
13434 char_u *p;
13435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436 p = vim_strsave(get_tv_string(&argvars[0]));
13437 rettv->v_type = VAR_STRING;
13438 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439
13440 if (p != NULL)
13441 while (*p != NUL)
13442 {
13443#ifdef FEAT_MBYTE
13444 int l;
13445
13446 if (enc_utf8)
13447 {
13448 int c, uc;
13449
13450 c = utf_ptr2char(p);
13451 uc = utf_toupper(c);
13452 l = utf_ptr2len_check(p);
13453 /* TODO: reallocate string when byte count changes. */
13454 if (utf_char2len(uc) == l)
13455 utf_char2bytes(uc, p);
13456 p += l;
13457 }
13458 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13459 p += l; /* skip multi-byte character */
13460 else
13461#endif
13462 {
13463 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13464 p++;
13465 }
13466 }
13467}
13468
13469/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013470 * "tr(string, fromstr, tostr)" function
13471 */
13472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *argvars;
13475 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013476{
13477 char_u *instr;
13478 char_u *fromstr;
13479 char_u *tostr;
13480 char_u *p;
13481#ifdef FEAT_MBYTE
13482 int inlen;
13483 int fromlen;
13484 int tolen;
13485 int idx;
13486 char_u *cpstr;
13487 int cplen;
13488 int first = TRUE;
13489#endif
13490 char_u buf[NUMBUFLEN];
13491 char_u buf2[NUMBUFLEN];
13492 garray_T ga;
13493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 instr = get_tv_string(&argvars[0]);
13495 fromstr = get_tv_string_buf(&argvars[1], buf);
13496 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013497
13498 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 rettv->v_type = VAR_STRING;
13500 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013501 ga_init2(&ga, (int)sizeof(char), 80);
13502
13503#ifdef FEAT_MBYTE
13504 if (!has_mbyte)
13505#endif
13506 /* not multi-byte: fromstr and tostr must be the same length */
13507 if (STRLEN(fromstr) != STRLEN(tostr))
13508 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013509#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013510error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013511#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013512 EMSG2(_(e_invarg2), fromstr);
13513 ga_clear(&ga);
13514 return;
13515 }
13516
13517 /* fromstr and tostr have to contain the same number of chars */
13518 while (*instr != NUL)
13519 {
13520#ifdef FEAT_MBYTE
13521 if (has_mbyte)
13522 {
13523 inlen = mb_ptr2len_check(instr);
13524 cpstr = instr;
13525 cplen = inlen;
13526 idx = 0;
13527 for (p = fromstr; *p != NUL; p += fromlen)
13528 {
13529 fromlen = mb_ptr2len_check(p);
13530 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13531 {
13532 for (p = tostr; *p != NUL; p += tolen)
13533 {
13534 tolen = mb_ptr2len_check(p);
13535 if (idx-- == 0)
13536 {
13537 cplen = tolen;
13538 cpstr = p;
13539 break;
13540 }
13541 }
13542 if (*p == NUL) /* tostr is shorter than fromstr */
13543 goto error;
13544 break;
13545 }
13546 ++idx;
13547 }
13548
13549 if (first && cpstr == instr)
13550 {
13551 /* Check that fromstr and tostr have the same number of
13552 * (multi-byte) characters. Done only once when a character
13553 * of instr doesn't appear in fromstr. */
13554 first = FALSE;
13555 for (p = tostr; *p != NUL; p += tolen)
13556 {
13557 tolen = mb_ptr2len_check(p);
13558 --idx;
13559 }
13560 if (idx != 0)
13561 goto error;
13562 }
13563
13564 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013565 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013566 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013567
13568 instr += inlen;
13569 }
13570 else
13571#endif
13572 {
13573 /* When not using multi-byte chars we can do it faster. */
13574 p = vim_strchr(fromstr, *instr);
13575 if (p != NULL)
13576 ga_append(&ga, tostr[p - fromstr]);
13577 else
13578 ga_append(&ga, *instr);
13579 ++instr;
13580 }
13581 }
13582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013584}
13585
13586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 * "type(expr)" function
13588 */
13589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013591 typval_T *argvars;
13592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013594 int n;
13595
13596 switch (argvars[0].v_type)
13597 {
13598 case VAR_NUMBER: n = 0; break;
13599 case VAR_STRING: n = 1; break;
13600 case VAR_FUNC: n = 2; break;
13601 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013602 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013603 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13604 }
13605 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606}
13607
13608/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013609 * "values(dict)" function
13610 */
13611 static void
13612f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013613 typval_T *argvars;
13614 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013615{
13616 dict_list(argvars, rettv, 1);
13617}
13618
13619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 * "virtcol(string)" function
13621 */
13622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013624 typval_T *argvars;
13625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626{
13627 colnr_T vcol = 0;
13628 pos_T *fp;
13629
13630 fp = var2fpos(&argvars[0], FALSE);
13631 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13632 {
13633 getvvcol(curwin, fp, NULL, NULL, &vcol);
13634 ++vcol;
13635 }
13636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638}
13639
13640/*
13641 * "visualmode()" function
13642 */
13643/*ARGSUSED*/
13644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *argvars;
13647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648{
13649#ifdef FEAT_VISUAL
13650 char_u str[2];
13651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 str[0] = curbuf->b_visual_mode_eval;
13654 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
13657 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013658 if ((argvars[0].v_type == VAR_NUMBER
13659 && argvars[0].vval.v_number != 0)
13660 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 curbuf->b_visual_mode_eval = NUL;
13663#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665#endif
13666}
13667
13668/*
13669 * "winbufnr(nr)" function
13670 */
13671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *argvars;
13674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675{
13676 win_T *wp;
13677
13678 wp = find_win_by_nr(&argvars[0]);
13679 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683}
13684
13685/*
13686 * "wincol()" function
13687 */
13688/*ARGSUSED*/
13689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 typval_T *argvars;
13692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
13694 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696}
13697
13698/*
13699 * "winheight(nr)" function
13700 */
13701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 typval_T *argvars;
13704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705{
13706 win_T *wp;
13707
13708 wp = find_win_by_nr(&argvars[0]);
13709 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713}
13714
13715/*
13716 * "winline()" function
13717 */
13718/*ARGSUSED*/
13719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *argvars;
13722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723{
13724 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726}
13727
13728/*
13729 * "winnr()" function
13730 */
13731/* ARGSUSED */
13732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736{
13737 int nr = 1;
13738#ifdef FEAT_WINDOWS
13739 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013740 win_T *twin = curwin;
13741 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013743 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013746 if (STRCMP(arg, "$") == 0)
13747 twin = lastwin;
13748 else if (STRCMP(arg, "#") == 0)
13749 {
13750 twin = prevwin;
13751 if (prevwin == NULL)
13752 nr = 0;
13753 }
13754 else
13755 {
13756 EMSG2(_(e_invexpr2), arg);
13757 nr = 0;
13758 }
13759 }
13760
13761 if (nr > 0)
13762 for (wp = firstwin; wp != twin; wp = wp->w_next)
13763 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766}
13767
13768/*
13769 * "winrestcmd()" function
13770 */
13771/* ARGSUSED */
13772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013774 typval_T *argvars;
13775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776{
13777#ifdef FEAT_WINDOWS
13778 win_T *wp;
13779 int winnr = 1;
13780 garray_T ga;
13781 char_u buf[50];
13782
13783 ga_init2(&ga, (int)sizeof(char), 70);
13784 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13785 {
13786 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13787 ga_concat(&ga, buf);
13788# ifdef FEAT_VERTSPLIT
13789 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13790 ga_concat(&ga, buf);
13791# endif
13792 ++winnr;
13793 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013794 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801}
13802
13803/*
13804 * "winwidth(nr)" function
13805 */
13806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013808 typval_T *argvars;
13809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810{
13811 win_T *wp;
13812
13813 wp = find_win_by_nr(&argvars[0]);
13814 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013815 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 else
13817#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013820 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821#endif
13822}
13823
13824 static win_T *
13825find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
13828#ifdef FEAT_WINDOWS
13829 win_T *wp;
13830#endif
13831 int nr;
13832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834
13835#ifdef FEAT_WINDOWS
13836 if (nr == 0)
13837 return curwin;
13838
13839 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13840 if (--nr <= 0)
13841 break;
13842 return wp;
13843#else
13844 if (nr == 0 || nr == 1)
13845 return curwin;
13846 return NULL;
13847#endif
13848}
13849
13850/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013851 * "writefile()" function
13852 */
13853 static void
13854f_writefile(argvars, rettv)
13855 typval_T *argvars;
13856 typval_T *rettv;
13857{
13858 int binary = FALSE;
13859 char_u *fname;
13860 FILE *fd;
13861 listitem_T *li;
13862 char_u *s;
13863 int ret = 0;
13864 int c;
13865
13866 if (argvars[0].v_type != VAR_LIST)
13867 {
13868 EMSG2(_(e_listarg), "writefile()");
13869 return;
13870 }
13871 if (argvars[0].vval.v_list == NULL)
13872 return;
13873
13874 if (argvars[2].v_type != VAR_UNKNOWN
13875 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13876 binary = TRUE;
13877
13878 /* Always open the file in binary mode, library functions have a mind of
13879 * their own about CR-LF conversion. */
13880 fname = get_tv_string(&argvars[1]);
13881 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13882 {
13883 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13884 ret = -1;
13885 }
13886 else
13887 {
13888 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13889 li = li->li_next)
13890 {
13891 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13892 {
13893 if (*s == '\n')
13894 c = putc(NUL, fd);
13895 else
13896 c = putc(*s, fd);
13897 if (c == EOF)
13898 {
13899 ret = -1;
13900 break;
13901 }
13902 }
13903 if (!binary || li->li_next != NULL)
13904 if (putc('\n', fd) == EOF)
13905 {
13906 ret = -1;
13907 break;
13908 }
13909 if (ret < 0)
13910 {
13911 EMSG(_(e_write));
13912 break;
13913 }
13914 }
13915 fclose(fd);
13916 }
13917
13918 rettv->vval.v_number = ret;
13919}
13920
13921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 * Translate a String variable into a position.
13923 */
13924 static pos_T *
13925var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013926 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 int lnum; /* TRUE when $ is last line */
13928{
13929 char_u *name;
13930 static pos_T pos;
13931 pos_T *pp;
13932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013933 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 if (name[0] == '.') /* cursor */
13935 return &curwin->w_cursor;
13936 if (name[0] == '\'') /* mark */
13937 {
13938 pp = getmark(name[1], FALSE);
13939 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13940 return NULL;
13941 return pp;
13942 }
13943 if (name[0] == '$') /* last column or line */
13944 {
13945 if (lnum)
13946 {
13947 pos.lnum = curbuf->b_ml.ml_line_count;
13948 pos.col = 0;
13949 }
13950 else
13951 {
13952 pos.lnum = curwin->w_cursor.lnum;
13953 pos.col = (colnr_T)STRLEN(ml_get_curline());
13954 }
13955 return &pos;
13956 }
13957 return NULL;
13958}
13959
13960/*
13961 * Get the length of an environment variable name.
13962 * Advance "arg" to the first character after the name.
13963 * Return 0 for error.
13964 */
13965 static int
13966get_env_len(arg)
13967 char_u **arg;
13968{
13969 char_u *p;
13970 int len;
13971
13972 for (p = *arg; vim_isIDc(*p); ++p)
13973 ;
13974 if (p == *arg) /* no name found */
13975 return 0;
13976
13977 len = (int)(p - *arg);
13978 *arg = p;
13979 return len;
13980}
13981
13982/*
13983 * Get the length of the name of a function or internal variable.
13984 * "arg" is advanced to the first non-white character after the name.
13985 * Return 0 if something is wrong.
13986 */
13987 static int
13988get_id_len(arg)
13989 char_u **arg;
13990{
13991 char_u *p;
13992 int len;
13993
13994 /* Find the end of the name. */
13995 for (p = *arg; eval_isnamec(*p); ++p)
13996 ;
13997 if (p == *arg) /* no name found */
13998 return 0;
13999
14000 len = (int)(p - *arg);
14001 *arg = skipwhite(p);
14002
14003 return len;
14004}
14005
14006/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014007 * Get the length of the name of a variable or function.
14008 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014010 * Return -1 if curly braces expansion failed.
14011 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 * If the name contains 'magic' {}'s, expand them and return the
14013 * expanded name in an allocated string via 'alias' - caller must free.
14014 */
14015 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014016get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 char_u **arg;
14018 char_u **alias;
14019 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014020 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021{
14022 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 char_u *p;
14024 char_u *expr_start;
14025 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026
14027 *alias = NULL; /* default to no alias */
14028
14029 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14030 && (*arg)[2] == (int)KE_SNR)
14031 {
14032 /* hard coded <SNR>, already translated */
14033 *arg += 3;
14034 return get_id_len(arg) + 3;
14035 }
14036 len = eval_fname_script(*arg);
14037 if (len > 0)
14038 {
14039 /* literal "<SID>", "s:" or "<SNR>" */
14040 *arg += len;
14041 }
14042
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014046 p = find_name_end(*arg, &expr_start, &expr_end,
14047 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 if (expr_start != NULL)
14049 {
14050 char_u *temp_string;
14051
14052 if (!evaluate)
14053 {
14054 len += (int)(p - *arg);
14055 *arg = skipwhite(p);
14056 return len;
14057 }
14058
14059 /*
14060 * Include any <SID> etc in the expanded string:
14061 * Thus the -len here.
14062 */
14063 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14064 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014065 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 *alias = temp_string;
14067 *arg = skipwhite(p);
14068 return (int)STRLEN(temp_string);
14069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070
14071 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014072 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 EMSG2(_(e_invexpr2), *arg);
14074
14075 return len;
14076}
14077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078/*
14079 * Find the end of a variable or function name, taking care of magic braces.
14080 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14081 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014082 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 * Return a pointer to just after the name. Equal to "arg" if there is no
14084 * valid name.
14085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014087find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 char_u *arg;
14089 char_u **expr_start;
14090 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014091 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014093 int mb_nest = 0;
14094 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 char_u *p;
14096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 *expr_start = NULL;
14100 *expr_end = NULL;
14101 }
14102
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014103 /* Quick check for valid starting character. */
14104 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14105 return arg;
14106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 for (p = arg; *p != NUL
14108 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014109 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014110 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111 || mb_nest != 0
14112 || br_nest != 0); ++p)
14113 {
14114 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014116 if (*p == '[')
14117 ++br_nest;
14118 else if (*p == ']')
14119 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014121 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 if (*p == '{')
14124 {
14125 mb_nest++;
14126 if (expr_start != NULL && *expr_start == NULL)
14127 *expr_start = p;
14128 }
14129 else if (*p == '}')
14130 {
14131 mb_nest--;
14132 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14133 *expr_end = p;
14134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 }
14137
14138 return p;
14139}
14140
14141/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014142 * Expands out the 'magic' {}'s in a variable/function name.
14143 * Note that this can call itself recursively, to deal with
14144 * constructs like foo{bar}{baz}{bam}
14145 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14146 * "in_start" ^
14147 * "expr_start" ^
14148 * "expr_end" ^
14149 * "in_end" ^
14150 *
14151 * Returns a new allocated string, which the caller must free.
14152 * Returns NULL for failure.
14153 */
14154 static char_u *
14155make_expanded_name(in_start, expr_start, expr_end, in_end)
14156 char_u *in_start;
14157 char_u *expr_start;
14158 char_u *expr_end;
14159 char_u *in_end;
14160{
14161 char_u c1;
14162 char_u *retval = NULL;
14163 char_u *temp_result;
14164 char_u *nextcmd = NULL;
14165
14166 if (expr_end == NULL || in_end == NULL)
14167 return NULL;
14168 *expr_start = NUL;
14169 *expr_end = NUL;
14170 c1 = *in_end;
14171 *in_end = NUL;
14172
14173 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14174 if (temp_result != NULL && nextcmd == NULL)
14175 {
14176 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14177 + (in_end - expr_end) + 1));
14178 if (retval != NULL)
14179 {
14180 STRCPY(retval, in_start);
14181 STRCAT(retval, temp_result);
14182 STRCAT(retval, expr_end + 1);
14183 }
14184 }
14185 vim_free(temp_result);
14186
14187 *in_end = c1; /* put char back for error messages */
14188 *expr_start = '{';
14189 *expr_end = '}';
14190
14191 if (retval != NULL)
14192 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014193 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014194 if (expr_start != NULL)
14195 {
14196 /* Further expansion! */
14197 temp_result = make_expanded_name(retval, expr_start,
14198 expr_end, temp_result);
14199 vim_free(retval);
14200 retval = temp_result;
14201 }
14202 }
14203
14204 return retval;
14205}
14206
14207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014209 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 */
14211 static int
14212eval_isnamec(c)
14213 int c;
14214{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014215 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
14216}
14217
14218/*
14219 * Return TRUE if character "c" can be used as the first character in a
14220 * variable or function name (excluding '{' and '}').
14221 */
14222 static int
14223eval_isnamec1(c)
14224 int c;
14225{
14226 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 * Set number v: variable to "val".
14231 */
14232 void
14233set_vim_var_nr(idx, val)
14234 int idx;
14235 long val;
14236{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014237 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238}
14239
14240/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014241 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 */
14243 long
14244get_vim_var_nr(idx)
14245 int idx;
14246{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014247 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248}
14249
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014250#if defined(FEAT_AUTOCMD) || defined(PROTO)
14251/*
14252 * Get string v: variable value. Uses a static buffer, can only be used once.
14253 */
14254 char_u *
14255get_vim_var_str(idx)
14256 int idx;
14257{
14258 return get_tv_string(&vimvars[idx].vv_tv);
14259}
14260#endif
14261
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262/*
14263 * Set v:count, v:count1 and v:prevcount.
14264 */
14265 void
14266set_vcount(count, count1)
14267 long count;
14268 long count1;
14269{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014270 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14271 vimvars[VV_COUNT].vv_nr = count;
14272 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273}
14274
14275/*
14276 * Set string v: variable to a copy of "val".
14277 */
14278 void
14279set_vim_var_string(idx, val, len)
14280 int idx;
14281 char_u *val;
14282 int len; /* length of "val" to use or -1 (whole string) */
14283{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014284 /* Need to do this (at least) once, since we can't initialize a union.
14285 * Will always be invoked when "v:progname" is set. */
14286 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14287
Bram Moolenaare9a41262005-01-15 22:18:47 +000014288 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014290 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014292 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014294 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295}
14296
14297/*
14298 * Set v:register if needed.
14299 */
14300 void
14301set_reg_var(c)
14302 int c;
14303{
14304 char_u regname;
14305
14306 if (c == 0 || c == ' ')
14307 regname = '"';
14308 else
14309 regname = c;
14310 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014311 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 set_vim_var_string(VV_REG, &regname, 1);
14313}
14314
14315/*
14316 * Get or set v:exception. If "oldval" == NULL, return the current value.
14317 * Otherwise, restore the value to "oldval" and return NULL.
14318 * Must always be called in pairs to save and restore v:exception! Does not
14319 * take care of memory allocations.
14320 */
14321 char_u *
14322v_exception(oldval)
14323 char_u *oldval;
14324{
14325 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014326 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 return NULL;
14330}
14331
14332/*
14333 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14334 * Otherwise, restore the value to "oldval" and return NULL.
14335 * Must always be called in pairs to save and restore v:throwpoint! Does not
14336 * take care of memory allocations.
14337 */
14338 char_u *
14339v_throwpoint(oldval)
14340 char_u *oldval;
14341{
14342 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014343 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaare9a41262005-01-15 22:18:47 +000014345 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 return NULL;
14347}
14348
14349#if defined(FEAT_AUTOCMD) || defined(PROTO)
14350/*
14351 * Set v:cmdarg.
14352 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14353 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14354 * Must always be called in pairs!
14355 */
14356 char_u *
14357set_cmdarg(eap, oldarg)
14358 exarg_T *eap;
14359 char_u *oldarg;
14360{
14361 char_u *oldval;
14362 char_u *newval;
14363 unsigned len;
14364
Bram Moolenaare9a41262005-01-15 22:18:47 +000014365 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014366 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014368 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014369 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014370 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 }
14372
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014373 if (eap->force_bin == FORCE_BIN)
14374 len = 6;
14375 else if (eap->force_bin == FORCE_NOBIN)
14376 len = 8;
14377 else
14378 len = 0;
14379 if (eap->force_ff != 0)
14380 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14381# ifdef FEAT_MBYTE
14382 if (eap->force_enc != 0)
14383 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14384# endif
14385
14386 newval = alloc(len + 1);
14387 if (newval == NULL)
14388 return NULL;
14389
14390 if (eap->force_bin == FORCE_BIN)
14391 sprintf((char *)newval, " ++bin");
14392 else if (eap->force_bin == FORCE_NOBIN)
14393 sprintf((char *)newval, " ++nobin");
14394 else
14395 *newval = NUL;
14396 if (eap->force_ff != 0)
14397 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14398 eap->cmd + eap->force_ff);
14399# ifdef FEAT_MBYTE
14400 if (eap->force_enc != 0)
14401 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14402 eap->cmd + eap->force_enc);
14403# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014404 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014405 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406}
14407#endif
14408
14409/*
14410 * Get the value of internal variable "name".
14411 * Return OK or FAIL.
14412 */
14413 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014414get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 char_u *name;
14416 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014417 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014418 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419{
14420 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014421 typval_T *tv = NULL;
14422 typval_T atv;
14423 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425
14426 /* truncate the name, so that we can use strcmp() */
14427 cc = name[len];
14428 name[len] = NUL;
14429
14430 /*
14431 * Check for "b:changedtick".
14432 */
14433 if (STRCMP(name, "b:changedtick") == 0)
14434 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014435 atv.v_type = VAR_NUMBER;
14436 atv.vval.v_number = curbuf->b_changedtick;
14437 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 }
14439
14440 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 * Check for user-defined variables.
14442 */
14443 else
14444 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014445 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 }
14449
Bram Moolenaare9a41262005-01-15 22:18:47 +000014450 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014452 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 ret = FAIL;
14455 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014457 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458
14459 name[len] = cc;
14460
14461 return ret;
14462}
14463
14464/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014465 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14466 * Also handle function call with Funcref variable: func(expr)
14467 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14468 */
14469 static int
14470handle_subscript(arg, rettv, evaluate, verbose)
14471 char_u **arg;
14472 typval_T *rettv;
14473 int evaluate; /* do more than finding the end */
14474 int verbose; /* give error messages */
14475{
14476 int ret = OK;
14477 dict_T *selfdict = NULL;
14478 char_u *s;
14479 int len;
14480
14481 while (ret == OK
14482 && (**arg == '['
14483 || (**arg == '.' && rettv->v_type == VAR_DICT)
14484 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14485 && !vim_iswhite(*(*arg - 1)))
14486 {
14487 if (**arg == '(')
14488 {
14489 s = rettv->vval.v_string;
14490
14491 /* Invoke the function. Recursive! */
14492 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14493 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14494 &len, evaluate, selfdict);
14495
14496 /* Stop the expression evaluation when immediately aborting on
14497 * error, or when an interrupt occurred or an exception was thrown
14498 * but not caught. */
14499 if (aborting())
14500 {
14501 if (ret == OK)
14502 clear_tv(rettv);
14503 ret = FAIL;
14504 }
14505 dict_unref(selfdict);
14506 selfdict = NULL;
14507 }
14508 else /* **arg == '[' || **arg == '.' */
14509 {
14510 dict_unref(selfdict);
14511 if (rettv->v_type == VAR_DICT)
14512 {
14513 selfdict = rettv->vval.v_dict;
14514 if (selfdict != NULL)
14515 ++selfdict->dv_refcount;
14516 }
14517 else
14518 selfdict = NULL;
14519 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14520 {
14521 clear_tv(rettv);
14522 ret = FAIL;
14523 }
14524 }
14525 }
14526 dict_unref(selfdict);
14527 return ret;
14528}
14529
14530/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014531 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14532 * value).
14533 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014536{
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014538}
14539
14540/*
14541 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 * The string "s" must have been allocated, it is consumed.
14543 * Return NULL for out of memory, the variable otherwise.
14544 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 char_u *s;
14548{
Bram Moolenaar33570922005-01-25 22:26:29 +000014549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014551 rettv = alloc_tv();
14552 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 rettv->v_type = VAR_STRING;
14555 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 }
14557 else
14558 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560}
14561
14562/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014563 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 */
14565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568{
14569 if (varp != NULL)
14570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014571 switch (varp->v_type)
14572 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014573 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014574 func_unref(varp->vval.v_string);
14575 /*FALLTHROUGH*/
14576 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014577 vim_free(varp->vval.v_string);
14578 break;
14579 case VAR_LIST:
14580 list_unref(varp->vval.v_list);
14581 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014582 case VAR_DICT:
14583 dict_unref(varp->vval.v_dict);
14584 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014585 case VAR_NUMBER:
14586 case VAR_UNKNOWN:
14587 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014588 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014589 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014590 break;
14591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 vim_free(varp);
14593 }
14594}
14595
14596/*
14597 * Free the memory for a variable value and set the value to NULL or 0.
14598 */
14599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014600clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602{
14603 if (varp != NULL)
14604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014605 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014608 func_unref(varp->vval.v_string);
14609 /*FALLTHROUGH*/
14610 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014611 vim_free(varp->vval.v_string);
14612 varp->vval.v_string = NULL;
14613 break;
14614 case VAR_LIST:
14615 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014616 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014617 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618 case VAR_DICT:
14619 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014620 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014621 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014622 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014623 varp->vval.v_number = 0;
14624 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014625 case VAR_UNKNOWN:
14626 break;
14627 default:
14628 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014630 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 }
14632}
14633
14634/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 * Set the value of a variable to NULL without freeing items.
14636 */
14637 static void
14638init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014639 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640{
14641 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643}
14644
14645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 * Get the number value of a variable.
14647 * If it is a String variable, uses vim_str2nr().
14648 */
14649 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014651 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014653 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014655 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014657 case VAR_NUMBER:
14658 n = (long)(varp->vval.v_number);
14659 break;
14660 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014661 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014662 break;
14663 case VAR_STRING:
14664 if (varp->vval.v_string != NULL)
14665 vim_str2nr(varp->vval.v_string, NULL, NULL,
14666 TRUE, TRUE, &n, NULL);
14667 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014668 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014669 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014670 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014671 case VAR_DICT:
14672 EMSG(_("E728: Using a Dictionary as a number"));
14673 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014674 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014675 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014678 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
14681/*
14682 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14683 */
14684 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687{
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 linenr_T lnum;
14690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 if (lnum == 0) /* no valid number, try using line() */
14693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014694 rettv.v_type = VAR_NUMBER;
14695 f_line(argvars, &rettv);
14696 lnum = rettv.vval.v_number;
14697 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 }
14699 return lnum;
14700}
14701
14702/*
14703 * Get the string value of a variable.
14704 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014705 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14706 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 * If the String variable has never been set, return an empty string.
14708 * Never returns NULL;
14709 */
14710 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014712 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713{
14714 static char_u mybuf[NUMBUFLEN];
14715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014716 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717}
14718
14719 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014720get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014722 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014724 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014726 case VAR_NUMBER:
14727 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14728 return buf;
14729 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014730 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014731 break;
14732 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014733 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734 break;
14735 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014736 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014737 break;
14738 case VAR_STRING:
14739 if (varp->vval.v_string != NULL)
14740 return varp->vval.v_string;
14741 break;
14742 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014744 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747}
14748
14749/*
14750 * Find variable "name" in the list of variables.
14751 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014752 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014753 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014756 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014757find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014759 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014762 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763
Bram Moolenaara7043832005-01-21 11:56:39 +000014764 ht = find_var_ht(name, &varname);
14765 if (htp != NULL)
14766 *htp = ht;
14767 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014769 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770}
14771
14772/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014774 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014777find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014779 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014780 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014781{
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 hashitem_T *hi;
14783
14784 if (*varname == NUL)
14785 {
14786 /* Must be something like "s:", otherwise "ht" would be NULL. */
14787 switch (varname[-2])
14788 {
14789 case 's': return &SCRIPT_SV(current_SID).sv_var;
14790 case 'g': return &globvars_var;
14791 case 'v': return &vimvars_var;
14792 case 'b': return &curbuf->b_bufvar;
14793 case 'w': return &curwin->w_winvar;
14794 case 'l': return &current_funccal->l_vars_var;
14795 case 'a': return &current_funccal->l_avars_var;
14796 }
14797 return NULL;
14798 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014799
14800 hi = hash_find(ht, varname);
14801 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014802 {
14803 /* For global variables we may try auto-loading the script. If it
14804 * worked find the variable again. */
14805 if (ht == &globvarht && !writing
14806 && script_autoload(varname) && !aborting())
14807 hi = hash_find(ht, varname);
14808 if (HASHITEM_EMPTY(hi))
14809 return NULL;
14810 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014811 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014812}
14813
14814/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014815 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014816 * Set "varname" to the start of name without ':'.
14817 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014818 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014819find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 char_u *name;
14821 char_u **varname;
14822{
14823 if (name[1] != ':')
14824 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014825 /* The name must not start with a colon or #. */
14826 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 return NULL;
14828 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014829
14830 /* "version" is "v:version" in all scopes */
14831 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14832 return &compat_hashtab;
14833
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 return &globvarht; /* global variable */
14836 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 }
14838 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014839 if (*name == 'g') /* global variable */
14840 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014841 /* There must be no ':' or '#' in the rest of the name, unless g: is used
14842 */
14843 if (vim_strchr(name + 2, ':') != NULL
14844 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014845 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014850 if (*name == 'v') /* v: variable */
14851 return &vimvarht;
14852 if (*name == 'a' && current_funccal != NULL) /* function argument */
14853 return &current_funccal->l_avars.dv_hashtab;
14854 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14855 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 if (*name == 's' /* script variable */
14857 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14858 return &SCRIPT_VARS(current_SID);
14859 return NULL;
14860}
14861
14862/*
14863 * Get the string value of a (global/local) variable.
14864 * Returns NULL when it doesn't exist.
14865 */
14866 char_u *
14867get_var_value(name)
14868 char_u *name;
14869{
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871
Bram Moolenaara7043832005-01-21 11:56:39 +000014872 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 if (v == NULL)
14874 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876}
14877
14878/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 * sourcing this script and when executing functions defined in the script.
14881 */
14882 void
14883new_script_vars(id)
14884 scid_T id;
14885{
Bram Moolenaara7043832005-01-21 11:56:39 +000014886 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014887 hashtab_T *ht;
14888 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014889
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14891 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014892 /* Re-allocating ga_data means that an ht_array pointing to
14893 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014894 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014895 for (i = 1; i <= ga_scripts.ga_len; ++i)
14896 {
14897 ht = &SCRIPT_VARS(i);
14898 if (ht->ht_mask == HT_INIT_SIZE - 1)
14899 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014900 sv = &SCRIPT_SV(i);
14901 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014902 }
14903
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 while (ga_scripts.ga_len < id)
14905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014906 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14907 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 }
14910 }
14911}
14912
14913/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014914 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14915 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916 */
14917 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014918init_var_dict(dict, dict_var)
14919 dict_T *dict;
14920 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921{
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 hash_init(&dict->dv_hashtab);
14923 dict->dv_refcount = 99999;
14924 dict_var->di_tv.vval.v_dict = dict;
14925 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014926 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014927 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14928 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929}
14930
14931/*
14932 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014933 * Frees all allocated variables and the value they contain.
14934 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 */
14936 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014937vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014938 hashtab_T *ht;
14939{
14940 vars_clear_ext(ht, TRUE);
14941}
14942
14943/*
14944 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14945 */
14946 static void
14947vars_clear_ext(ht, free_val)
14948 hashtab_T *ht;
14949 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950{
Bram Moolenaara7043832005-01-21 11:56:39 +000014951 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014952 hashitem_T *hi;
14953 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954
Bram Moolenaar33570922005-01-25 22:26:29 +000014955 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014956 todo = ht->ht_used;
14957 for (hi = ht->ht_array; todo > 0; ++hi)
14958 {
14959 if (!HASHITEM_EMPTY(hi))
14960 {
14961 --todo;
14962
Bram Moolenaar33570922005-01-25 22:26:29 +000014963 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014964 * ht_array might change then. hash_clear() takes care of it
14965 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014966 v = HI2DI(hi);
14967 if (free_val)
14968 clear_tv(&v->di_tv);
14969 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14970 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014971 }
14972 }
14973 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974}
14975
Bram Moolenaara7043832005-01-21 11:56:39 +000014976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014977 * Delete a variable from hashtab "ht" at item "hi".
14978 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014981delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014982 hashtab_T *ht;
14983 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984{
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014986
14987 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014988 clear_tv(&di->di_tv);
14989 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990}
14991
14992/*
14993 * List the value of one internal variable.
14994 */
14995 static void
14996list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 char_u *prefix;
14999{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015000 char_u *tofree;
15001 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015002 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015003
Bram Moolenaar33570922005-01-25 22:26:29 +000015004 s = echo_string(&v->di_tv, &tofree, numbuf);
15005 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015006 s == NULL ? (char_u *)"" : s);
15007 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008}
15009
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 static void
15011list_one_var_a(prefix, name, type, string)
15012 char_u *prefix;
15013 char_u *name;
15014 int type;
15015 char_u *string;
15016{
15017 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15018 if (name != NULL) /* "a:" vars don't have a name stored */
15019 msg_puts(name);
15020 msg_putchar(' ');
15021 msg_advance(22);
15022 if (type == VAR_NUMBER)
15023 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015024 else if (type == VAR_FUNC)
15025 msg_putchar('*');
15026 else if (type == VAR_LIST)
15027 {
15028 msg_putchar('[');
15029 if (*string == '[')
15030 ++string;
15031 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015032 else if (type == VAR_DICT)
15033 {
15034 msg_putchar('{');
15035 if (*string == '{')
15036 ++string;
15037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 else
15039 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015040
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015042
15043 if (type == VAR_FUNC)
15044 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045}
15046
15047/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015048 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 * If the variable already exists, the value is updated.
15050 * Otherwise the variable is created.
15051 */
15052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015055 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057{
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015060 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015062 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015063 {
15064 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15065 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15066 ? name[2] : name[0]))
15067 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015068 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015069 return;
15070 }
15071 if (function_exists(name))
15072 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015073 EMSG2(_("705: Variable name conflicts with existing function: %s"),
15074 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015075 return;
15076 }
15077 }
15078
Bram Moolenaara7043832005-01-21 11:56:39 +000015079 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015080 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015081 {
15082 EMSG2(_("E461: Illegal variable name: %s"), name);
15083 return;
15084 }
15085
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015086 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015089 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015090 if (var_check_ro(v->di_flags, name)
15091 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 return;
15093 if (v->di_tv.v_type != tv->v_type
15094 && !((v->di_tv.v_type == VAR_STRING
15095 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 && (tv->v_type == VAR_STRING
15097 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015098 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015099 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015100 return;
15101 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015102
15103 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015104 * Handle setting internal v: variables separately: we don't change
15105 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015106 */
15107 if (ht == &vimvarht)
15108 {
15109 if (v->di_tv.v_type == VAR_STRING)
15110 {
15111 vim_free(v->di_tv.vval.v_string);
15112 if (copy || tv->v_type != VAR_STRING)
15113 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15114 else
15115 {
15116 /* Take over the string to avoid an extra alloc/free. */
15117 v->di_tv.vval.v_string = tv->vval.v_string;
15118 tv->vval.v_string = NULL;
15119 }
15120 }
15121 else if (v->di_tv.v_type != VAR_NUMBER)
15122 EMSG2(_(e_intern2), "set_var()");
15123 else
15124 v->di_tv.vval.v_number = get_tv_number(tv);
15125 return;
15126 }
15127
15128 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 }
15130 else /* add a new variable */
15131 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015132 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15133 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015134 if (v == NULL)
15135 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015139 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 return;
15141 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015142 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015145 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015147 else
15148 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015149 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015150 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153}
15154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015155/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015156 * Return TRUE if di_flags "flags" indicate read-only variable "name".
15157 * Also give an error message.
15158 */
15159 static int
15160var_check_ro(flags, name)
15161 int flags;
15162 char_u *name;
15163{
15164 if (flags & DI_FLAGS_RO)
15165 {
15166 EMSG2(_(e_readonlyvar), name);
15167 return TRUE;
15168 }
15169 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
15170 {
15171 EMSG2(_(e_readonlysbx), name);
15172 return TRUE;
15173 }
15174 return FALSE;
15175}
15176
15177/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015178 * Return TRUE if typeval "tv" is set to be locked (immutable).
15179 * Also give an error message, using "name".
15180 */
15181 static int
15182tv_check_lock(lock, name)
15183 int lock;
15184 char_u *name;
15185{
15186 if (lock & VAR_LOCKED)
15187 {
15188 EMSG2(_("E741: Value is locked: %s"),
15189 name == NULL ? (char_u *)_("Unknown") : name);
15190 return TRUE;
15191 }
15192 if (lock & VAR_FIXED)
15193 {
15194 EMSG2(_("E742: Cannot change value of %s"),
15195 name == NULL ? (char_u *)_("Unknown") : name);
15196 return TRUE;
15197 }
15198 return FALSE;
15199}
15200
15201/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015203 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015204 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015208 typval_T *from;
15209 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015211 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015212 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015213 switch (from->v_type)
15214 {
15215 case VAR_NUMBER:
15216 to->vval.v_number = from->vval.v_number;
15217 break;
15218 case VAR_STRING:
15219 case VAR_FUNC:
15220 if (from->vval.v_string == NULL)
15221 to->vval.v_string = NULL;
15222 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015223 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015224 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015225 if (from->v_type == VAR_FUNC)
15226 func_ref(to->vval.v_string);
15227 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015228 break;
15229 case VAR_LIST:
15230 if (from->vval.v_list == NULL)
15231 to->vval.v_list = NULL;
15232 else
15233 {
15234 to->vval.v_list = from->vval.v_list;
15235 ++to->vval.v_list->lv_refcount;
15236 }
15237 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015238 case VAR_DICT:
15239 if (from->vval.v_dict == NULL)
15240 to->vval.v_dict = NULL;
15241 else
15242 {
15243 to->vval.v_dict = from->vval.v_dict;
15244 ++to->vval.v_dict->dv_refcount;
15245 }
15246 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015247 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015248 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015249 break;
15250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251}
15252
15253/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015254 * Make a copy of an item.
15255 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015256 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15257 * reference to an already copied list/dict can be used.
15258 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015259 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015260 static int
15261item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 typval_T *from;
15263 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015264 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015265 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015266{
15267 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015268 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015269
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015271 {
15272 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015273 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015274 }
15275 ++recurse;
15276
15277 switch (from->v_type)
15278 {
15279 case VAR_NUMBER:
15280 case VAR_STRING:
15281 case VAR_FUNC:
15282 copy_tv(from, to);
15283 break;
15284 case VAR_LIST:
15285 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015286 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015287 if (from->vval.v_list == NULL)
15288 to->vval.v_list = NULL;
15289 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15290 {
15291 /* use the copy made earlier */
15292 to->vval.v_list = from->vval.v_list->lv_copylist;
15293 ++to->vval.v_list->lv_refcount;
15294 }
15295 else
15296 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15297 if (to->vval.v_list == NULL)
15298 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015299 break;
15300 case VAR_DICT:
15301 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015302 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015303 if (from->vval.v_dict == NULL)
15304 to->vval.v_dict = NULL;
15305 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15306 {
15307 /* use the copy made earlier */
15308 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15309 ++to->vval.v_dict->dv_refcount;
15310 }
15311 else
15312 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15313 if (to->vval.v_dict == NULL)
15314 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015315 break;
15316 default:
15317 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015318 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015319 }
15320 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015321 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015322}
15323
15324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 * ":echo expr1 ..." print each argument separated with a space, add a
15326 * newline at the end.
15327 * ":echon expr1 ..." print each argument plain.
15328 */
15329 void
15330ex_echo(eap)
15331 exarg_T *eap;
15332{
15333 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015334 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015335 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 char_u *p;
15337 int needclr = TRUE;
15338 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015339 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340
15341 if (eap->skip)
15342 ++emsg_skip;
15343 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15344 {
15345 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 {
15348 /*
15349 * Report the invalid expression unless the expression evaluation
15350 * has been cancelled due to an aborting error, an interrupt, or an
15351 * exception.
15352 */
15353 if (!aborting())
15354 EMSG2(_(e_invexpr2), p);
15355 break;
15356 }
15357 if (!eap->skip)
15358 {
15359 if (atstart)
15360 {
15361 atstart = FALSE;
15362 /* Call msg_start() after eval1(), evaluating the expression
15363 * may cause a message to appear. */
15364 if (eap->cmdidx == CMD_echo)
15365 msg_start();
15366 }
15367 else if (eap->cmdidx == CMD_echo)
15368 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015369 p = echo_string(&rettv, &tofree, numbuf);
15370 if (p != NULL)
15371 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015373 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015375 if (*p != TAB && needclr)
15376 {
15377 /* remove any text still there from the command */
15378 msg_clr_eos();
15379 needclr = FALSE;
15380 }
15381 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 }
15383 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015384 {
15385#ifdef FEAT_MBYTE
15386 if (has_mbyte)
15387 {
15388 int i = (*mb_ptr2len_check)(p);
15389
15390 (void)msg_outtrans_len_attr(p, i, echo_attr);
15391 p += i - 1;
15392 }
15393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015395 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015398 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 arg = skipwhite(arg);
15402 }
15403 eap->nextcmd = check_nextcmd(arg);
15404
15405 if (eap->skip)
15406 --emsg_skip;
15407 else
15408 {
15409 /* remove text that may still be there from the command */
15410 if (needclr)
15411 msg_clr_eos();
15412 if (eap->cmdidx == CMD_echo)
15413 msg_end();
15414 }
15415}
15416
15417/*
15418 * ":echohl {name}".
15419 */
15420 void
15421ex_echohl(eap)
15422 exarg_T *eap;
15423{
15424 int id;
15425
15426 id = syn_name2id(eap->arg);
15427 if (id == 0)
15428 echo_attr = 0;
15429 else
15430 echo_attr = syn_id2attr(id);
15431}
15432
15433/*
15434 * ":execute expr1 ..." execute the result of an expression.
15435 * ":echomsg expr1 ..." Print a message
15436 * ":echoerr expr1 ..." Print an error
15437 * Each gets spaces around each argument and a newline at the end for
15438 * echo commands
15439 */
15440 void
15441ex_execute(eap)
15442 exarg_T *eap;
15443{
15444 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015445 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 int ret = OK;
15447 char_u *p;
15448 garray_T ga;
15449 int len;
15450 int save_did_emsg;
15451
15452 ga_init2(&ga, 1, 80);
15453
15454 if (eap->skip)
15455 ++emsg_skip;
15456 while (*arg != NUL && *arg != '|' && *arg != '\n')
15457 {
15458 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 {
15461 /*
15462 * Report the invalid expression unless the expression evaluation
15463 * has been cancelled due to an aborting error, an interrupt, or an
15464 * exception.
15465 */
15466 if (!aborting())
15467 EMSG2(_(e_invexpr2), p);
15468 ret = FAIL;
15469 break;
15470 }
15471
15472 if (!eap->skip)
15473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 len = (int)STRLEN(p);
15476 if (ga_grow(&ga, len + 2) == FAIL)
15477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 ret = FAIL;
15480 break;
15481 }
15482 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 ga.ga_len += len;
15486 }
15487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 arg = skipwhite(arg);
15490 }
15491
15492 if (ret != FAIL && ga.ga_data != NULL)
15493 {
15494 if (eap->cmdidx == CMD_echomsg)
15495 MSG_ATTR(ga.ga_data, echo_attr);
15496 else if (eap->cmdidx == CMD_echoerr)
15497 {
15498 /* We don't want to abort following commands, restore did_emsg. */
15499 save_did_emsg = did_emsg;
15500 EMSG((char_u *)ga.ga_data);
15501 if (!force_abort)
15502 did_emsg = save_did_emsg;
15503 }
15504 else if (eap->cmdidx == CMD_execute)
15505 do_cmdline((char_u *)ga.ga_data,
15506 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15507 }
15508
15509 ga_clear(&ga);
15510
15511 if (eap->skip)
15512 --emsg_skip;
15513
15514 eap->nextcmd = check_nextcmd(arg);
15515}
15516
15517/*
15518 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15519 * "arg" points to the "&" or '+' when called, to "option" when returning.
15520 * Returns NULL when no option name found. Otherwise pointer to the char
15521 * after the option name.
15522 */
15523 static char_u *
15524find_option_end(arg, opt_flags)
15525 char_u **arg;
15526 int *opt_flags;
15527{
15528 char_u *p = *arg;
15529
15530 ++p;
15531 if (*p == 'g' && p[1] == ':')
15532 {
15533 *opt_flags = OPT_GLOBAL;
15534 p += 2;
15535 }
15536 else if (*p == 'l' && p[1] == ':')
15537 {
15538 *opt_flags = OPT_LOCAL;
15539 p += 2;
15540 }
15541 else
15542 *opt_flags = 0;
15543
15544 if (!ASCII_ISALPHA(*p))
15545 return NULL;
15546 *arg = p;
15547
15548 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15549 p += 4; /* termcap option */
15550 else
15551 while (ASCII_ISALPHA(*p))
15552 ++p;
15553 return p;
15554}
15555
15556/*
15557 * ":function"
15558 */
15559 void
15560ex_function(eap)
15561 exarg_T *eap;
15562{
15563 char_u *theline;
15564 int j;
15565 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 char_u *name = NULL;
15568 char_u *p;
15569 char_u *arg;
15570 garray_T newargs;
15571 garray_T newlines;
15572 int varargs = FALSE;
15573 int mustend = FALSE;
15574 int flags = 0;
15575 ufunc_T *fp;
15576 int indent;
15577 int nesting;
15578 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015579 dictitem_T *v;
15580 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015581 static int func_nr = 0; /* number for nameless function */
15582 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015583 hashtab_T *ht;
15584 int todo;
15585 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586
15587 /*
15588 * ":function" without argument: list functions.
15589 */
15590 if (ends_excmd(*eap->arg))
15591 {
15592 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015593 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015594 todo = func_hashtab.ht_used;
15595 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015596 {
15597 if (!HASHITEM_EMPTY(hi))
15598 {
15599 --todo;
15600 fp = HI2UF(hi);
15601 if (!isdigit(*fp->uf_name))
15602 list_func_head(fp, FALSE);
15603 }
15604 }
15605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 eap->nextcmd = check_nextcmd(eap->arg);
15607 return;
15608 }
15609
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015610 /*
15611 * Get the function name. There are these situations:
15612 * func normal function name
15613 * "name" == func, "fudi.fd_dict" == NULL
15614 * dict.func new dictionary entry
15615 * "name" == NULL, "fudi.fd_dict" set,
15616 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15617 * dict.func existing dict entry with a Funcref
15618 * "name" == fname, "fudi.fd_dict" set,
15619 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15620 * dict.func existing dict entry that's not a Funcref
15621 * "name" == NULL, "fudi.fd_dict" set,
15622 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015625 name = trans_function_name(&p, eap->skip, 0, &fudi);
15626 paren = (vim_strchr(p, '(') != NULL);
15627 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 {
15629 /*
15630 * Return on an invalid expression in braces, unless the expression
15631 * evaluation has been cancelled due to an aborting error, an
15632 * interrupt, or an exception.
15633 */
15634 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015636 if (!eap->skip && fudi.fd_newkey != NULL)
15637 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015638 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 else
15642 eap->skip = TRUE;
15643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 /* An error in a function call during evaluation of an expression in magic
15645 * braces should not cause the function not to be defined. */
15646 saved_did_emsg = did_emsg;
15647 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648
15649 /*
15650 * ":function func" with only function name: list function.
15651 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015652 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 {
15654 if (!ends_excmd(*skipwhite(p)))
15655 {
15656 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015657 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 }
15659 eap->nextcmd = check_nextcmd(p);
15660 if (eap->nextcmd != NULL)
15661 *p = NUL;
15662 if (!eap->skip && !got_int)
15663 {
15664 fp = find_func(name);
15665 if (fp != NULL)
15666 {
15667 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015668 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 {
15670 msg_putchar('\n');
15671 msg_outnum((long)(j + 1));
15672 if (j < 9)
15673 msg_putchar(' ');
15674 if (j < 99)
15675 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015676 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 out_flush(); /* show a line at a time */
15678 ui_breakcheck();
15679 }
15680 if (!got_int)
15681 {
15682 msg_putchar('\n');
15683 msg_puts((char_u *)" endfunction");
15684 }
15685 }
15686 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015687 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015689 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 }
15691
15692 /*
15693 * ":function name(arg1, arg2)" Define function.
15694 */
15695 p = skipwhite(p);
15696 if (*p != '(')
15697 {
15698 if (!eap->skip)
15699 {
15700 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015701 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 }
15703 /* attempt to continue by skipping some text */
15704 if (vim_strchr(p, '(') != NULL)
15705 p = vim_strchr(p, '(');
15706 }
15707 p = skipwhite(p + 1);
15708
15709 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15710 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15711
15712 /*
15713 * Isolate the arguments: "arg1, arg2, ...)"
15714 */
15715 while (*p != ')')
15716 {
15717 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15718 {
15719 varargs = TRUE;
15720 p += 3;
15721 mustend = TRUE;
15722 }
15723 else
15724 {
15725 arg = p;
15726 while (ASCII_ISALNUM(*p) || *p == '_')
15727 ++p;
15728 if (arg == p || isdigit(*arg)
15729 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15730 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15731 {
15732 if (!eap->skip)
15733 EMSG2(_("E125: Illegal argument: %s"), arg);
15734 break;
15735 }
15736 if (ga_grow(&newargs, 1) == FAIL)
15737 goto erret;
15738 c = *p;
15739 *p = NUL;
15740 arg = vim_strsave(arg);
15741 if (arg == NULL)
15742 goto erret;
15743 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15744 *p = c;
15745 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 if (*p == ',')
15747 ++p;
15748 else
15749 mustend = TRUE;
15750 }
15751 p = skipwhite(p);
15752 if (mustend && *p != ')')
15753 {
15754 if (!eap->skip)
15755 EMSG2(_(e_invarg2), eap->arg);
15756 break;
15757 }
15758 }
15759 ++p; /* skip the ')' */
15760
Bram Moolenaare9a41262005-01-15 22:18:47 +000015761 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 for (;;)
15763 {
15764 p = skipwhite(p);
15765 if (STRNCMP(p, "range", 5) == 0)
15766 {
15767 flags |= FC_RANGE;
15768 p += 5;
15769 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015770 else if (STRNCMP(p, "dict", 4) == 0)
15771 {
15772 flags |= FC_DICT;
15773 p += 4;
15774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 else if (STRNCMP(p, "abort", 5) == 0)
15776 {
15777 flags |= FC_ABORT;
15778 p += 5;
15779 }
15780 else
15781 break;
15782 }
15783
15784 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15785 EMSG(_(e_trailing));
15786
15787 /*
15788 * Read the body of the function, until ":endfunction" is found.
15789 */
15790 if (KeyTyped)
15791 {
15792 /* Check if the function already exists, don't let the user type the
15793 * whole function before telling him it doesn't work! For a script we
15794 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015795 if (!eap->skip && !eap->forceit)
15796 {
15797 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15798 EMSG(_(e_funcdict));
15799 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015800 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802
15803 msg_putchar('\n'); /* don't overwrite the function name */
15804 cmdline_row = msg_row;
15805 }
15806
15807 indent = 2;
15808 nesting = 0;
15809 for (;;)
15810 {
15811 msg_scroll = TRUE;
15812 need_wait_return = FALSE;
15813 if (eap->getline == NULL)
15814 theline = getcmdline(':', 0L, indent);
15815 else
15816 theline = eap->getline(':', eap->cookie, indent);
15817 if (KeyTyped)
15818 lines_left = Rows - 1;
15819 if (theline == NULL)
15820 {
15821 EMSG(_("E126: Missing :endfunction"));
15822 goto erret;
15823 }
15824
15825 if (skip_until != NULL)
15826 {
15827 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15828 * don't check for ":endfunc". */
15829 if (STRCMP(theline, skip_until) == 0)
15830 {
15831 vim_free(skip_until);
15832 skip_until = NULL;
15833 }
15834 }
15835 else
15836 {
15837 /* skip ':' and blanks*/
15838 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15839 ;
15840
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015841 /* Check for "endfunction". */
15842 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 {
15844 vim_free(theline);
15845 break;
15846 }
15847
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015848 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 * at "end". */
15850 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15851 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015852 else if (STRNCMP(p, "if", 2) == 0
15853 || STRNCMP(p, "wh", 2) == 0
15854 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 || STRNCMP(p, "try", 3) == 0)
15856 indent += 2;
15857
15858 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015859 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015861 if (*p == '!')
15862 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 p += eval_fname_script(p);
15864 if (ASCII_ISALPHA(*p))
15865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015866 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 if (*skipwhite(p) == '(')
15868 {
15869 ++nesting;
15870 indent += 2;
15871 }
15872 }
15873 }
15874
15875 /* Check for ":append" or ":insert". */
15876 p = skip_range(p, NULL);
15877 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15878 || (p[0] == 'i'
15879 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15880 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15881 skip_until = vim_strsave((char_u *)".");
15882
15883 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15884 arg = skipwhite(skiptowhite(p));
15885 if (arg[0] == '<' && arg[1] =='<'
15886 && ((p[0] == 'p' && p[1] == 'y'
15887 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15888 || (p[0] == 'p' && p[1] == 'e'
15889 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15890 || (p[0] == 't' && p[1] == 'c'
15891 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15892 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15893 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015894 || (p[0] == 'm' && p[1] == 'z'
15895 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 ))
15897 {
15898 /* ":python <<" continues until a dot, like ":append" */
15899 p = skipwhite(arg + 2);
15900 if (*p == NUL)
15901 skip_until = vim_strsave((char_u *)".");
15902 else
15903 skip_until = vim_strsave(p);
15904 }
15905 }
15906
15907 /* Add the line to the function. */
15908 if (ga_grow(&newlines, 1) == FAIL)
15909 goto erret;
15910 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15911 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 }
15913
15914 /* Don't define the function when skipping commands or when an error was
15915 * detected. */
15916 if (eap->skip || did_emsg)
15917 goto erret;
15918
15919 /*
15920 * If there are no errors, add the function
15921 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015922 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015923 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015924 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015925 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015926 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015927 emsg_funcname("E707: Function name conflicts with variable: %s",
15928 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015929 goto erret;
15930 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015931
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015932 fp = find_func(name);
15933 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015935 if (!eap->forceit)
15936 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015937 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015938 goto erret;
15939 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015940 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015941 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015942 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015943 name);
15944 goto erret;
15945 }
15946 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015947 ga_clear_strings(&(fp->uf_args));
15948 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015949 vim_free(name);
15950 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 }
15953 else
15954 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015955 char numbuf[20];
15956
15957 fp = NULL;
15958 if (fudi.fd_newkey == NULL && !eap->forceit)
15959 {
15960 EMSG(_(e_funcdict));
15961 goto erret;
15962 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015963 if (fudi.fd_di == NULL)
15964 {
15965 /* Can't add a function to a locked dictionary */
15966 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15967 goto erret;
15968 }
15969 /* Can't change an existing function if it is locked */
15970 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15971 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015972
15973 /* Give the function a sequential number. Can only be used with a
15974 * Funcref! */
15975 vim_free(name);
15976 sprintf(numbuf, "%d", ++func_nr);
15977 name = vim_strsave((char_u *)numbuf);
15978 if (name == NULL)
15979 goto erret;
15980 }
15981
15982 if (fp == NULL)
15983 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015984 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015985 {
15986 int slen, plen;
15987 char_u *scriptname;
15988
15989 /* Check that the autoload name matches the script name. */
15990 j = FAIL;
15991 if (sourcing_name != NULL)
15992 {
15993 scriptname = autoload_name(name);
15994 if (scriptname != NULL)
15995 {
15996 p = vim_strchr(scriptname, '/');
15997 plen = STRLEN(p);
15998 slen = STRLEN(sourcing_name);
15999 if (slen > plen && fnamecmp(p,
16000 sourcing_name + slen - plen) == 0)
16001 j = OK;
16002 vim_free(scriptname);
16003 }
16004 }
16005 if (j == FAIL)
16006 {
16007 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16008 goto erret;
16009 }
16010 }
16011
16012 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 if (fp == NULL)
16014 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016015
16016 if (fudi.fd_dict != NULL)
16017 {
16018 if (fudi.fd_di == NULL)
16019 {
16020 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016021 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016022 if (fudi.fd_di == NULL)
16023 {
16024 vim_free(fp);
16025 goto erret;
16026 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016027 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16028 {
16029 vim_free(fudi.fd_di);
16030 goto erret;
16031 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016032 }
16033 else
16034 /* overwrite existing dict entry */
16035 clear_tv(&fudi.fd_di->di_tv);
16036 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016037 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016038 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016039 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016040 }
16041
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016043 STRCPY(fp->uf_name, name);
16044 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016046 fp->uf_args = newargs;
16047 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016048#ifdef FEAT_PROFILE
16049 fp->uf_tml_count = NULL;
16050 fp->uf_tml_total = NULL;
16051 fp->uf_tml_self = NULL;
16052 fp->uf_profiling = FALSE;
16053 if (prof_def_func())
16054 func_do_profile(fp);
16055#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016056 fp->uf_varargs = varargs;
16057 fp->uf_flags = flags;
16058 fp->uf_calls = 0;
16059 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016060 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061
16062erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 ga_clear_strings(&newargs);
16064 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016065ret_free:
16066 vim_free(skip_until);
16067 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070}
16071
16072/*
16073 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016074 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016076 * flags:
16077 * TFN_INT: internal function name OK
16078 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 * Advances "pp" to just after the function name (if no error).
16080 */
16081 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016082trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 char_u **pp;
16084 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016085 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016088 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 char_u *start;
16090 char_u *end;
16091 int lead;
16092 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016094 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016095
16096 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016099
16100 /* Check for hard coded <SNR>: already translated function ID (from a user
16101 * command). */
16102 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16103 && (*pp)[2] == (int)KE_SNR)
16104 {
16105 *pp += 3;
16106 len = get_id_len(pp) + 3;
16107 return vim_strnsave(start, len);
16108 }
16109
16110 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
16111 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000016113 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016115
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016116 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
16117 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016118 if (end == start)
16119 {
16120 if (!skip)
16121 EMSG(_("E129: Function name required"));
16122 goto theend;
16123 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016124 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016125 {
16126 /*
16127 * Report an invalid expression in braces, unless the expression
16128 * evaluation has been cancelled due to an aborting error, an
16129 * interrupt, or an exception.
16130 */
16131 if (!aborting())
16132 {
16133 if (end != NULL)
16134 EMSG2(_(e_invarg2), start);
16135 }
16136 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016137 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016138 goto theend;
16139 }
16140
16141 if (lv.ll_tv != NULL)
16142 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016143 if (fdp != NULL)
16144 {
16145 fdp->fd_dict = lv.ll_dict;
16146 fdp->fd_newkey = lv.ll_newkey;
16147 lv.ll_newkey = NULL;
16148 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016149 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016150 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
16151 {
16152 name = vim_strsave(lv.ll_tv->vval.v_string);
16153 *pp = end;
16154 }
16155 else
16156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016157 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
16158 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016159 EMSG(_(e_funcref));
16160 else
16161 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016162 name = NULL;
16163 }
16164 goto theend;
16165 }
16166
16167 if (lv.ll_name == NULL)
16168 {
16169 /* Error found, but continue after the function name. */
16170 *pp = end;
16171 goto theend;
16172 }
16173
16174 if (lv.ll_exp_name != NULL)
16175 len = STRLEN(lv.ll_exp_name);
16176 else
Bram Moolenaara7043832005-01-21 11:56:39 +000016177 {
16178 if (lead == 2) /* skip over "s:" */
16179 lv.ll_name += 2;
16180 len = (int)(end - lv.ll_name);
16181 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016182
16183 /*
16184 * Copy the function name to allocated memory.
16185 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16186 * Accept <SNR>123_name() outside a script.
16187 */
16188 if (skip)
16189 lead = 0; /* do nothing */
16190 else if (lead > 0)
16191 {
16192 lead = 3;
16193 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16194 {
16195 if (current_SID <= 0)
16196 {
16197 EMSG(_(e_usingsid));
16198 goto theend;
16199 }
16200 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16201 lead += (int)STRLEN(sid_buf);
16202 }
16203 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016204 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016205 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016206 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016207 goto theend;
16208 }
16209 name = alloc((unsigned)(len + lead + 1));
16210 if (name != NULL)
16211 {
16212 if (lead > 0)
16213 {
16214 name[0] = K_SPECIAL;
16215 name[1] = KS_EXTRA;
16216 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016217 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016218 STRCPY(name + 3, sid_buf);
16219 }
16220 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16221 name[len + lead] = NUL;
16222 }
16223 *pp = end;
16224
16225theend:
16226 clear_lval(&lv);
16227 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228}
16229
16230/*
16231 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16232 * Return 2 if "p" starts with "s:".
16233 * Return 0 otherwise.
16234 */
16235 static int
16236eval_fname_script(p)
16237 char_u *p;
16238{
16239 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16240 || STRNICMP(p + 1, "SNR>", 4) == 0))
16241 return 5;
16242 if (p[0] == 's' && p[1] == ':')
16243 return 2;
16244 return 0;
16245}
16246
16247/*
16248 * Return TRUE if "p" starts with "<SID>" or "s:".
16249 * Only works if eval_fname_script() returned non-zero for "p"!
16250 */
16251 static int
16252eval_fname_sid(p)
16253 char_u *p;
16254{
16255 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16256}
16257
16258/*
16259 * List the head of the function: "name(arg1, arg2)".
16260 */
16261 static void
16262list_func_head(fp, indent)
16263 ufunc_T *fp;
16264 int indent;
16265{
16266 int j;
16267
16268 msg_start();
16269 if (indent)
16270 MSG_PUTS(" ");
16271 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016272 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 {
16274 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016275 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 }
16277 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016278 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016280 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 {
16282 if (j)
16283 MSG_PUTS(", ");
16284 msg_puts(FUNCARG(fp, j));
16285 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016286 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 {
16288 if (j)
16289 MSG_PUTS(", ");
16290 MSG_PUTS("...");
16291 }
16292 msg_putchar(')');
16293}
16294
16295/*
16296 * Find a function by name, return pointer to it in ufuncs.
16297 * Return NULL for unknown function.
16298 */
16299 static ufunc_T *
16300find_func(name)
16301 char_u *name;
16302{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016303 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016305 hi = hash_find(&func_hashtab, name);
16306 if (!HASHITEM_EMPTY(hi))
16307 return HI2UF(hi);
16308 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309}
16310
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016311/*
16312 * Return TRUE if a function "name" exists.
16313 */
16314 static int
16315function_exists(name)
16316 char_u *name;
16317{
16318 char_u *p = name;
16319 int n = FALSE;
16320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016321 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016322 if (p != NULL)
16323 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016324 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016325 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016326 else
16327 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016328 vim_free(p);
16329 }
16330 return n;
16331}
16332
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016333/*
16334 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016335 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016336 */
16337 static int
16338builtin_function(name)
16339 char_u *name;
16340{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016341 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
16342 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016343}
16344
Bram Moolenaar05159a02005-02-26 23:04:13 +000016345#if defined(FEAT_PROFILE) || defined(PROTO)
16346/*
16347 * Start profiling function "fp".
16348 */
16349 static void
16350func_do_profile(fp)
16351 ufunc_T *fp;
16352{
16353 fp->uf_tm_count = 0;
16354 profile_zero(&fp->uf_tm_self);
16355 profile_zero(&fp->uf_tm_total);
16356 if (fp->uf_tml_count == NULL)
16357 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16358 (sizeof(int) * fp->uf_lines.ga_len));
16359 if (fp->uf_tml_total == NULL)
16360 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16361 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16362 if (fp->uf_tml_self == NULL)
16363 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16364 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16365 fp->uf_tml_idx = -1;
16366 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16367 || fp->uf_tml_self == NULL)
16368 return; /* out of memory */
16369
16370 fp->uf_profiling = TRUE;
16371}
16372
16373/*
16374 * Dump the profiling results for all functions in file "fd".
16375 */
16376 void
16377func_dump_profile(fd)
16378 FILE *fd;
16379{
16380 hashitem_T *hi;
16381 int todo;
16382 ufunc_T *fp;
16383 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016384 ufunc_T **sorttab;
16385 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016386
16387 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016388 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16389
Bram Moolenaar05159a02005-02-26 23:04:13 +000016390 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16391 {
16392 if (!HASHITEM_EMPTY(hi))
16393 {
16394 --todo;
16395 fp = HI2UF(hi);
16396 if (fp->uf_profiling)
16397 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016398 if (sorttab != NULL)
16399 sorttab[st_len++] = fp;
16400
Bram Moolenaar05159a02005-02-26 23:04:13 +000016401 if (fp->uf_name[0] == K_SPECIAL)
16402 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16403 else
16404 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16405 if (fp->uf_tm_count == 1)
16406 fprintf(fd, "Called 1 time\n");
16407 else
16408 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16409 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16410 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16411 fprintf(fd, "\n");
16412 fprintf(fd, "count total (s) self (s)\n");
16413
16414 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16415 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016416 prof_func_line(fd, fp->uf_tml_count[i],
16417 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016418 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16419 }
16420 fprintf(fd, "\n");
16421 }
16422 }
16423 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016424
16425 if (sorttab != NULL && st_len > 0)
16426 {
16427 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16428 prof_total_cmp);
16429 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16430 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16431 prof_self_cmp);
16432 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16433 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016434}
Bram Moolenaar73830342005-02-28 22:48:19 +000016435
16436 static void
16437prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16438 FILE *fd;
16439 ufunc_T **sorttab;
16440 int st_len;
16441 char *title;
16442 int prefer_self; /* when equal print only self time */
16443{
16444 int i;
16445 ufunc_T *fp;
16446
16447 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16448 fprintf(fd, "count total (s) self (s) function\n");
16449 for (i = 0; i < 20 && i < st_len; ++i)
16450 {
16451 fp = sorttab[i];
16452 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16453 prefer_self);
16454 if (fp->uf_name[0] == K_SPECIAL)
16455 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16456 else
16457 fprintf(fd, " %s()\n", fp->uf_name);
16458 }
16459 fprintf(fd, "\n");
16460}
16461
16462/*
16463 * Print the count and times for one function or function line.
16464 */
16465 static void
16466prof_func_line(fd, count, total, self, prefer_self)
16467 FILE *fd;
16468 int count;
16469 proftime_T *total;
16470 proftime_T *self;
16471 int prefer_self; /* when equal print only self time */
16472{
16473 if (count > 0)
16474 {
16475 fprintf(fd, "%5d ", count);
16476 if (prefer_self && profile_equal(total, self))
16477 fprintf(fd, " ");
16478 else
16479 fprintf(fd, "%s ", profile_msg(total));
16480 if (!prefer_self && profile_equal(total, self))
16481 fprintf(fd, " ");
16482 else
16483 fprintf(fd, "%s ", profile_msg(self));
16484 }
16485 else
16486 fprintf(fd, " ");
16487}
16488
16489/*
16490 * Compare function for total time sorting.
16491 */
16492 static int
16493#ifdef __BORLANDC__
16494_RTLENTRYF
16495#endif
16496prof_total_cmp(s1, s2)
16497 const void *s1;
16498 const void *s2;
16499{
16500 ufunc_T *p1, *p2;
16501
16502 p1 = *(ufunc_T **)s1;
16503 p2 = *(ufunc_T **)s2;
16504 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16505}
16506
16507/*
16508 * Compare function for self time sorting.
16509 */
16510 static int
16511#ifdef __BORLANDC__
16512_RTLENTRYF
16513#endif
16514prof_self_cmp(s1, s2)
16515 const void *s1;
16516 const void *s2;
16517{
16518 ufunc_T *p1, *p2;
16519
16520 p1 = *(ufunc_T **)s1;
16521 p2 = *(ufunc_T **)s2;
16522 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16523}
16524
Bram Moolenaar05159a02005-02-26 23:04:13 +000016525#endif
16526
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016527/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016528 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016529 * Return TRUE if a package was loaded.
16530 */
16531 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016532script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016533 char_u *name;
16534{
16535 char_u *p;
16536 char_u *scriptname;
16537 int ret = FALSE;
16538
16539 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016540 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016541 if (p == NULL || p <= name + 2)
16542 return FALSE;
16543
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016544 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16545 scriptname = autoload_name(name);
16546 if (cmd_runtime(scriptname, FALSE) == OK)
16547 ret = TRUE;
16548
16549 vim_free(scriptname);
16550 return ret;
16551}
16552
16553/*
16554 * Return the autoload script name for a function or variable name.
16555 * Returns NULL when out of memory.
16556 */
16557 static char_u *
16558autoload_name(name)
16559 char_u *name;
16560{
16561 char_u *p;
16562 char_u *scriptname;
16563
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016564 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016565 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16566 if (scriptname == NULL)
16567 return FALSE;
16568 STRCPY(scriptname, "autoload/");
16569 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016570 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016571 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016572 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016573 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016574 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016575}
16576
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16578
16579/*
16580 * Function given to ExpandGeneric() to obtain the list of user defined
16581 * function names.
16582 */
16583 char_u *
16584get_user_func_name(xp, idx)
16585 expand_T *xp;
16586 int idx;
16587{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016588 static long_u done;
16589 static hashitem_T *hi;
16590 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591
16592 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016594 done = 0;
16595 hi = func_hashtab.ht_array;
16596 }
16597 if (done < func_hashtab.ht_used)
16598 {
16599 if (done++ > 0)
16600 ++hi;
16601 while (HASHITEM_EMPTY(hi))
16602 ++hi;
16603 fp = HI2UF(hi);
16604
16605 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16606 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607
16608 cat_func_name(IObuff, fp);
16609 if (xp->xp_context != EXPAND_USER_FUNC)
16610 {
16611 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016612 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613 STRCAT(IObuff, ")");
16614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 return IObuff;
16616 }
16617 return NULL;
16618}
16619
16620#endif /* FEAT_CMDL_COMPL */
16621
16622/*
16623 * Copy the function name of "fp" to buffer "buf".
16624 * "buf" must be able to hold the function name plus three bytes.
16625 * Takes care of script-local function names.
16626 */
16627 static void
16628cat_func_name(buf, fp)
16629 char_u *buf;
16630 ufunc_T *fp;
16631{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016632 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 {
16634 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016635 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 }
16637 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016638 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639}
16640
16641/*
16642 * ":delfunction {name}"
16643 */
16644 void
16645ex_delfunction(eap)
16646 exarg_T *eap;
16647{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016648 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 char_u *p;
16650 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016651 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652
16653 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016654 name = trans_function_name(&p, eap->skip, 0, &fudi);
16655 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016657 {
16658 if (fudi.fd_dict != NULL && !eap->skip)
16659 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 if (!ends_excmd(*skipwhite(p)))
16663 {
16664 vim_free(name);
16665 EMSG(_(e_trailing));
16666 return;
16667 }
16668 eap->nextcmd = check_nextcmd(p);
16669 if (eap->nextcmd != NULL)
16670 *p = NUL;
16671
16672 if (!eap->skip)
16673 fp = find_func(name);
16674 vim_free(name);
16675
16676 if (!eap->skip)
16677 {
16678 if (fp == NULL)
16679 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016680 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 return;
16682 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016683 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 {
16685 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16686 return;
16687 }
16688
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016689 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016691 /* Delete the dict item that refers to the function, it will
16692 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016693 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016695 else
16696 func_free(fp);
16697 }
16698}
16699
16700/*
16701 * Free a function and remove it from the list of functions.
16702 */
16703 static void
16704func_free(fp)
16705 ufunc_T *fp;
16706{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016707 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016708
16709 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016710 ga_clear_strings(&(fp->uf_args));
16711 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016712#ifdef FEAT_PROFILE
16713 vim_free(fp->uf_tml_count);
16714 vim_free(fp->uf_tml_total);
16715 vim_free(fp->uf_tml_self);
16716#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016717
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016718 /* remove the function from the function hashtable */
16719 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16720 if (HASHITEM_EMPTY(hi))
16721 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016722 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016723 hash_remove(&func_hashtab, hi);
16724
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016725 vim_free(fp);
16726}
16727
16728/*
16729 * Unreference a Function: decrement the reference count and free it when it
16730 * becomes zero. Only for numbered functions.
16731 */
16732 static void
16733func_unref(name)
16734 char_u *name;
16735{
16736 ufunc_T *fp;
16737
16738 if (name != NULL && isdigit(*name))
16739 {
16740 fp = find_func(name);
16741 if (fp == NULL)
16742 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016743 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016744 {
16745 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016746 * when "uf_calls" becomes zero. */
16747 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016748 func_free(fp);
16749 }
16750 }
16751}
16752
16753/*
16754 * Count a reference to a Function.
16755 */
16756 static void
16757func_ref(name)
16758 char_u *name;
16759{
16760 ufunc_T *fp;
16761
16762 if (name != NULL && isdigit(*name))
16763 {
16764 fp = find_func(name);
16765 if (fp == NULL)
16766 EMSG2(_(e_intern2), "func_ref()");
16767 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016768 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 }
16770}
16771
16772/*
16773 * Call a user function.
16774 */
16775 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016776call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 ufunc_T *fp; /* pointer to function */
16778 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016779 typval_T *argvars; /* arguments */
16780 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 linenr_T firstline; /* first line of range */
16782 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016783 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784{
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 char_u *save_sourcing_name;
16786 linenr_T save_sourcing_lnum;
16787 scid_T save_current_SID;
16788 funccall_T fc;
16789 funccall_T *save_fcp = current_funccal;
16790 int save_did_emsg;
16791 static int depth = 0;
16792 dictitem_T *v;
16793 int fixvar_idx = 0; /* index in fixvar[] */
16794 int i;
16795 int ai;
16796 char_u numbuf[NUMBUFLEN];
16797 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016798#ifdef FEAT_PROFILE
16799 proftime_T wait_start;
16800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801
16802 /* If depth of calling is getting too high, don't execute the function */
16803 if (depth >= p_mfd)
16804 {
16805 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016806 rettv->v_type = VAR_NUMBER;
16807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 return;
16809 }
16810 ++depth;
16811
16812 line_breakcheck(); /* check for CTRL-C hit */
16813
Bram Moolenaar33570922005-01-25 22:26:29 +000016814 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016816 fc.rettv = rettv;
16817 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 fc.linenr = 0;
16819 fc.returned = FALSE;
16820 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016822 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 fc.dbg_tick = debug_tick;
16824
Bram Moolenaar33570922005-01-25 22:26:29 +000016825 /*
16826 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16827 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16828 * each argument variable and saves a lot of time.
16829 */
16830 /*
16831 * Init l: variables.
16832 */
16833 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016834 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016835 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016836 /* Set l:self to "selfdict". */
16837 v = &fc.fixvar[fixvar_idx++].var;
16838 STRCPY(v->di_key, "self");
16839 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16840 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16841 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016842 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016843 v->di_tv.vval.v_dict = selfdict;
16844 ++selfdict->dv_refcount;
16845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016846
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 /*
16848 * Init a: variables.
16849 * Set a:0 to "argcount".
16850 * Set a:000 to a list with room for the "..." arguments.
16851 */
16852 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16853 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016854 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 v = &fc.fixvar[fixvar_idx++].var;
16856 STRCPY(v->di_key, "000");
16857 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16858 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16859 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016860 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 v->di_tv.vval.v_list = &fc.l_varlist;
16862 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16863 fc.l_varlist.lv_refcount = 99999;
16864
16865 /*
16866 * Set a:firstline to "firstline" and a:lastline to "lastline".
16867 * Set a:name to named arguments.
16868 * Set a:N to the "..." arguments.
16869 */
16870 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16871 (varnumber_T)firstline);
16872 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16873 (varnumber_T)lastline);
16874 for (i = 0; i < argcount; ++i)
16875 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016876 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016877 if (ai < 0)
16878 /* named argument a:name */
16879 name = FUNCARG(fp, i);
16880 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016881 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 /* "..." argument a:1, a:2, etc. */
16883 sprintf((char *)numbuf, "%d", ai + 1);
16884 name = numbuf;
16885 }
16886 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16887 {
16888 v = &fc.fixvar[fixvar_idx++].var;
16889 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16890 }
16891 else
16892 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016893 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16894 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016895 if (v == NULL)
16896 break;
16897 v->di_flags = DI_FLAGS_RO;
16898 }
16899 STRCPY(v->di_key, name);
16900 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16901
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016902 /* Note: the values are copied directly to avoid alloc/free.
16903 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016905 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016906
16907 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16908 {
16909 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16910 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016911 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016912 }
16913 }
16914
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 /* Don't redraw while executing the function. */
16916 ++RedrawingDisabled;
16917 save_sourcing_name = sourcing_name;
16918 save_sourcing_lnum = sourcing_lnum;
16919 sourcing_lnum = 1;
16920 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016921 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 if (sourcing_name != NULL)
16923 {
16924 if (save_sourcing_name != NULL
16925 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16926 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16927 else
16928 STRCPY(sourcing_name, "function ");
16929 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16930
16931 if (p_verbose >= 12)
16932 {
16933 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000016934 verbose_enter_scroll();
16935
Bram Moolenaar555b2802005-05-19 21:08:39 +000016936 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 if (p_verbose >= 14)
16938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016940 char_u numbuf[NUMBUFLEN];
16941 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942
16943 msg_puts((char_u *)"(");
16944 for (i = 0; i < argcount; ++i)
16945 {
16946 if (i > 0)
16947 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016948 if (argvars[i].v_type == VAR_NUMBER)
16949 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 else
16951 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016952 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016953 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016955 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 }
16957 }
16958 msg_puts((char_u *)")");
16959 }
16960 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000016961
16962 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 --no_wait_return;
16964 }
16965 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016966#ifdef FEAT_PROFILE
16967 if (do_profiling)
16968 {
16969 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16970 func_do_profile(fp);
16971 if (fp->uf_profiling
16972 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16973 {
16974 ++fp->uf_tm_count;
16975 profile_start(&fp->uf_tm_start);
16976 profile_zero(&fp->uf_tm_children);
16977 }
16978 script_prof_save(&wait_start);
16979 }
16980#endif
16981
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016983 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 save_did_emsg = did_emsg;
16985 did_emsg = FALSE;
16986
16987 /* call do_cmdline() to execute the lines */
16988 do_cmdline(NULL, get_func_line, (void *)&fc,
16989 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16990
16991 --RedrawingDisabled;
16992
16993 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016994 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996 clear_tv(rettv);
16997 rettv->v_type = VAR_NUMBER;
16998 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 }
17000
Bram Moolenaar05159a02005-02-26 23:04:13 +000017001#ifdef FEAT_PROFILE
17002 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
17003 {
17004 profile_end(&fp->uf_tm_start);
17005 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17006 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17007 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17008 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
17009 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
17010 {
17011 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
17012 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
17013 }
17014 }
17015#endif
17016
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 /* when being verbose, mention the return value */
17018 if (p_verbose >= 12)
17019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017021 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017024 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017026 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17027 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017030 char_u buf[MSG_BUF_LEN];
17031 char_u numbuf[NUMBUFLEN];
17032 char_u *tofree;
17033
Bram Moolenaar555b2802005-05-19 21:08:39 +000017034 /* The value may be very long. Skip the middle part, so that we
17035 * have some idea how it starts and ends. smsg() would always
17036 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017037 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017038 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017039 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017040 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 }
17042 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017043
17044 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 --no_wait_return;
17046 }
17047
17048 vim_free(sourcing_name);
17049 sourcing_name = save_sourcing_name;
17050 sourcing_lnum = save_sourcing_lnum;
17051 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017052#ifdef FEAT_PROFILE
17053 if (do_profiling)
17054 script_prof_restore(&wait_start);
17055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056
17057 if (p_verbose >= 12 && sourcing_name != NULL)
17058 {
17059 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017060 verbose_enter_scroll();
17061
Bram Moolenaar555b2802005-05-19 21:08:39 +000017062 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017064
17065 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 --no_wait_return;
17067 }
17068
17069 did_emsg |= save_did_emsg;
17070 current_funccal = save_fcp;
17071
Bram Moolenaar33570922005-01-25 22:26:29 +000017072 /* The a: variables typevals were not alloced, only free the allocated
17073 * variables. */
17074 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17075
17076 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 --depth;
17078}
17079
17080/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017081 * Add a number variable "name" to dict "dp" with value "nr".
17082 */
17083 static void
17084add_nr_var(dp, v, name, nr)
17085 dict_T *dp;
17086 dictitem_T *v;
17087 char *name;
17088 varnumber_T nr;
17089{
17090 STRCPY(v->di_key, name);
17091 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17092 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
17093 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017094 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 v->di_tv.vval.v_number = nr;
17096}
17097
17098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099 * ":return [expr]"
17100 */
17101 void
17102ex_return(eap)
17103 exarg_T *eap;
17104{
17105 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017106 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 int returning = FALSE;
17108
17109 if (current_funccal == NULL)
17110 {
17111 EMSG(_("E133: :return not inside a function"));
17112 return;
17113 }
17114
17115 if (eap->skip)
17116 ++emsg_skip;
17117
17118 eap->nextcmd = NULL;
17119 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 {
17122 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 }
17127 /* It's safer to return also on error. */
17128 else if (!eap->skip)
17129 {
17130 /*
17131 * Return unless the expression evaluation has been cancelled due to an
17132 * aborting error, an interrupt, or an exception.
17133 */
17134 if (!aborting())
17135 returning = do_return(eap, FALSE, TRUE, NULL);
17136 }
17137
17138 /* When skipping or the return gets pending, advance to the next command
17139 * in this line (!returning). Otherwise, ignore the rest of the line.
17140 * Following lines will be ignored by get_func_line(). */
17141 if (returning)
17142 eap->nextcmd = NULL;
17143 else if (eap->nextcmd == NULL) /* no argument */
17144 eap->nextcmd = check_nextcmd(arg);
17145
17146 if (eap->skip)
17147 --emsg_skip;
17148}
17149
17150/*
17151 * Return from a function. Possibly makes the return pending. Also called
17152 * for a pending return at the ":endtry" or after returning from an extra
17153 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000017154 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017155 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 * FALSE when the return gets pending.
17157 */
17158 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 exarg_T *eap;
17161 int reanimate;
17162 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017163 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164{
17165 int idx;
17166 struct condstack *cstack = eap->cstack;
17167
17168 if (reanimate)
17169 /* Undo the return. */
17170 current_funccal->returned = FALSE;
17171
17172 /*
17173 * Cleanup (and inactivate) conditionals, but stop when a try conditional
17174 * not in its finally clause (which then is to be executed next) is found.
17175 * In this case, make the ":return" pending for execution at the ":endtry".
17176 * Otherwise, return normally.
17177 */
17178 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
17179 if (idx >= 0)
17180 {
17181 cstack->cs_pending[idx] = CSTP_RETURN;
17182
17183 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 /* A pending return again gets pending. "rettv" points to an
17185 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017187 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 else
17189 {
17190 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017191 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 {
17197 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017198 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 else
17201 EMSG(_(e_outofmem));
17202 }
17203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205
17206 if (reanimate)
17207 {
17208 /* The pending return value could be overwritten by a ":return"
17209 * without argument in a finally clause; reset the default
17210 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211 current_funccal->rettv->v_type = VAR_NUMBER;
17212 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 }
17214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017215 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 }
17217 else
17218 {
17219 current_funccal->returned = TRUE;
17220
17221 /* If the return is carried out now, store the return value. For
17222 * a return immediately after reanimation, the value is already
17223 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017224 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 }
17231 }
17232
17233 return idx < 0;
17234}
17235
17236/*
17237 * Free the variable with a pending return value.
17238 */
17239 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017240discard_pending_return(rettv)
17241 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242{
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244}
17245
17246/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 * is an allocated string. Used by report_pending() for verbose messages.
17249 */
17250 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017251get_return_cmd(rettv)
17252 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017254 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017255 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017256 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017258 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017260 if (s == NULL)
17261 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017262
17263 STRCPY(IObuff, ":return ");
17264 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17265 if (STRLEN(s) + 8 >= IOSIZE)
17266 STRCPY(IObuff + IOSIZE - 4, "...");
17267 vim_free(tofree);
17268 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269}
17270
17271/*
17272 * Get next function line.
17273 * Called by do_cmdline() to get the next line.
17274 * Returns allocated string, or NULL for end of function.
17275 */
17276/* ARGSUSED */
17277 char_u *
17278get_func_line(c, cookie, indent)
17279 int c; /* not used */
17280 void *cookie;
17281 int indent; /* not used */
17282{
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017284 ufunc_T *fp = fcp->func;
17285 char_u *retval;
17286 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287
17288 /* If breakpoints have been added/deleted need to check for it. */
17289 if (fcp->dbg_tick != debug_tick)
17290 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017291 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 sourcing_lnum);
17293 fcp->dbg_tick = debug_tick;
17294 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017295#ifdef FEAT_PROFILE
17296 if (do_profiling)
17297 func_line_end(cookie);
17298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299
Bram Moolenaar05159a02005-02-26 23:04:13 +000017300 gap = &fp->uf_lines;
17301 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302 retval = NULL;
17303 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17304 retval = NULL;
17305 else
17306 {
17307 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17308 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017309#ifdef FEAT_PROFILE
17310 if (do_profiling)
17311 func_line_start(cookie);
17312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 }
17314
17315 /* Did we encounter a breakpoint? */
17316 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17317 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017318 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017320 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 sourcing_lnum);
17322 fcp->dbg_tick = debug_tick;
17323 }
17324
17325 return retval;
17326}
17327
Bram Moolenaar05159a02005-02-26 23:04:13 +000017328#if defined(FEAT_PROFILE) || defined(PROTO)
17329/*
17330 * Called when starting to read a function line.
17331 * "sourcing_lnum" must be correct!
17332 * When skipping lines it may not actually be executed, but we won't find out
17333 * until later and we need to store the time now.
17334 */
17335 void
17336func_line_start(cookie)
17337 void *cookie;
17338{
17339 funccall_T *fcp = (funccall_T *)cookie;
17340 ufunc_T *fp = fcp->func;
17341
17342 if (fp->uf_profiling && sourcing_lnum >= 1
17343 && sourcing_lnum <= fp->uf_lines.ga_len)
17344 {
17345 fp->uf_tml_idx = sourcing_lnum - 1;
17346 fp->uf_tml_execed = FALSE;
17347 profile_start(&fp->uf_tml_start);
17348 profile_zero(&fp->uf_tml_children);
17349 profile_get_wait(&fp->uf_tml_wait);
17350 }
17351}
17352
17353/*
17354 * Called when actually executing a function line.
17355 */
17356 void
17357func_line_exec(cookie)
17358 void *cookie;
17359{
17360 funccall_T *fcp = (funccall_T *)cookie;
17361 ufunc_T *fp = fcp->func;
17362
17363 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17364 fp->uf_tml_execed = TRUE;
17365}
17366
17367/*
17368 * Called when done with a function line.
17369 */
17370 void
17371func_line_end(cookie)
17372 void *cookie;
17373{
17374 funccall_T *fcp = (funccall_T *)cookie;
17375 ufunc_T *fp = fcp->func;
17376
17377 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17378 {
17379 if (fp->uf_tml_execed)
17380 {
17381 ++fp->uf_tml_count[fp->uf_tml_idx];
17382 profile_end(&fp->uf_tml_start);
17383 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17384 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17385 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17386 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17387 }
17388 fp->uf_tml_idx = -1;
17389 }
17390}
17391#endif
17392
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393/*
17394 * Return TRUE if the currently active function should be ended, because a
17395 * return was encountered or an error occured. Used inside a ":while".
17396 */
17397 int
17398func_has_ended(cookie)
17399 void *cookie;
17400{
Bram Moolenaar33570922005-01-25 22:26:29 +000017401 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402
17403 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17404 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017405 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 || fcp->returned);
17407}
17408
17409/*
17410 * return TRUE if cookie indicates a function which "abort"s on errors.
17411 */
17412 int
17413func_has_abort(cookie)
17414 void *cookie;
17415{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017416 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417}
17418
17419#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17420typedef enum
17421{
17422 VAR_FLAVOUR_DEFAULT,
17423 VAR_FLAVOUR_SESSION,
17424 VAR_FLAVOUR_VIMINFO
17425} var_flavour_T;
17426
17427static var_flavour_T var_flavour __ARGS((char_u *varname));
17428
17429 static var_flavour_T
17430var_flavour(varname)
17431 char_u *varname;
17432{
17433 char_u *p = varname;
17434
17435 if (ASCII_ISUPPER(*p))
17436 {
17437 while (*(++p))
17438 if (ASCII_ISLOWER(*p))
17439 return VAR_FLAVOUR_SESSION;
17440 return VAR_FLAVOUR_VIMINFO;
17441 }
17442 else
17443 return VAR_FLAVOUR_DEFAULT;
17444}
17445#endif
17446
17447#if defined(FEAT_VIMINFO) || defined(PROTO)
17448/*
17449 * Restore global vars that start with a capital from the viminfo file
17450 */
17451 int
17452read_viminfo_varlist(virp, writing)
17453 vir_T *virp;
17454 int writing;
17455{
17456 char_u *tab;
17457 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459
17460 if (!writing && (find_viminfo_parameter('!') != NULL))
17461 {
17462 tab = vim_strchr(virp->vir_line + 1, '\t');
17463 if (tab != NULL)
17464 {
17465 *tab++ = '\0'; /* isolate the variable name */
17466 if (*tab == 'S') /* string var */
17467 is_string = TRUE;
17468
17469 tab = vim_strchr(tab, '\t');
17470 if (tab != NULL)
17471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 if (is_string)
17473 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017474 tv.v_type = VAR_STRING;
17475 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 }
17478 else
17479 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017480 tv.v_type = VAR_NUMBER;
17481 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017483 set_var(virp->vir_line + 1, &tv, FALSE);
17484 if (is_string)
17485 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 }
17487 }
17488 }
17489
17490 return viminfo_readline(virp);
17491}
17492
17493/*
17494 * Write global vars that start with a capital to the viminfo file
17495 */
17496 void
17497write_viminfo_varlist(fp)
17498 FILE *fp;
17499{
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 hashitem_T *hi;
17501 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017502 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017504 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017506 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
17508 if (find_viminfo_parameter('!') == NULL)
17509 return;
17510
17511 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017512
Bram Moolenaar33570922005-01-25 22:26:29 +000017513 todo = globvarht.ht_used;
17514 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017516 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017518 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017519 this_var = HI2DI(hi);
17520 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017522 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017523 {
17524 case VAR_STRING: s = "STR"; break;
17525 case VAR_NUMBER: s = "NUM"; break;
17526 default: continue;
17527 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017529 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17530 if (p != NULL)
17531 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017532 vim_free(tofree);
17533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 }
17535 }
17536}
17537#endif
17538
17539#if defined(FEAT_SESSION) || defined(PROTO)
17540 int
17541store_session_globals(fd)
17542 FILE *fd;
17543{
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 hashitem_T *hi;
17545 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017546 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 char_u *p, *t;
17548
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 todo = globvarht.ht_used;
17550 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017552 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017554 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 this_var = HI2DI(hi);
17556 if ((this_var->di_tv.v_type == VAR_NUMBER
17557 || this_var->di_tv.v_type == VAR_STRING)
17558 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017559 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017560 /* Escape special characters with a backslash. Turn a LF and
17561 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017562 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017563 (char_u *)"\\\"\n\r");
17564 if (p == NULL) /* out of memory */
17565 break;
17566 for (t = p; *t != NUL; ++t)
17567 if (*t == '\n')
17568 *t = 'n';
17569 else if (*t == '\r')
17570 *t = 'r';
17571 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 this_var->di_key,
17573 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17574 : ' ',
17575 p,
17576 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17577 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017578 || put_eol(fd) == FAIL)
17579 {
17580 vim_free(p);
17581 return FAIL;
17582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 vim_free(p);
17584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 }
17586 }
17587 return OK;
17588}
17589#endif
17590
17591#endif /* FEAT_EVAL */
17592
17593#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17594
17595
17596#ifdef WIN3264
17597/*
17598 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17599 */
17600static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17601static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17602static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17603
17604/*
17605 * Get the short pathname of a file.
17606 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17607 */
17608 static int
17609get_short_pathname(fnamep, bufp, fnamelen)
17610 char_u **fnamep;
17611 char_u **bufp;
17612 int *fnamelen;
17613{
17614 int l,len;
17615 char_u *newbuf;
17616
17617 len = *fnamelen;
17618
17619 l = GetShortPathName(*fnamep, *fnamep, len);
17620 if (l > len - 1)
17621 {
17622 /* If that doesn't work (not enough space), then save the string
17623 * and try again with a new buffer big enough
17624 */
17625 newbuf = vim_strnsave(*fnamep, l);
17626 if (newbuf == NULL)
17627 return 0;
17628
17629 vim_free(*bufp);
17630 *fnamep = *bufp = newbuf;
17631
17632 l = GetShortPathName(*fnamep,*fnamep,l+1);
17633
17634 /* Really should always succeed, as the buffer is big enough */
17635 }
17636
17637 *fnamelen = l;
17638 return 1;
17639}
17640
17641/*
17642 * Create a short path name. Returns the length of the buffer it needs.
17643 * Doesn't copy over the end of the buffer passed in.
17644 */
17645 static int
17646shortpath_for_invalid_fname(fname, bufp, fnamelen)
17647 char_u **fname;
17648 char_u **bufp;
17649 int *fnamelen;
17650{
17651 char_u *s, *p, *pbuf2, *pbuf3;
17652 char_u ch;
17653 int l,len,len2,plen,slen;
17654
17655 /* Make a copy */
17656 len2 = *fnamelen;
17657 pbuf2 = vim_strnsave(*fname, len2);
17658 pbuf3 = NULL;
17659
17660 s = pbuf2 + len2 - 1; /* Find the end */
17661 slen = 1;
17662 plen = len2;
17663
17664 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017665 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 {
17667 --s;
17668 ++slen;
17669 --plen;
17670 }
17671
17672 do
17673 {
17674 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017675 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 {
17677 --s;
17678 ++slen;
17679 --plen;
17680 }
17681 if (s <= pbuf2)
17682 break;
17683
17684 /* Remeber the character that is about to be blatted */
17685 ch = *s;
17686 *s = 0; /* get_short_pathname requires a null-terminated string */
17687
17688 /* Try it in situ */
17689 p = pbuf2;
17690 if (!get_short_pathname(&p, &pbuf3, &plen))
17691 {
17692 vim_free(pbuf2);
17693 return -1;
17694 }
17695 *s = ch; /* Preserve the string */
17696 } while (plen == 0);
17697
17698 if (plen > 0)
17699 {
17700 /* Remeber the length of the new string. */
17701 *fnamelen = len = plen + slen;
17702 vim_free(*bufp);
17703 if (len > len2)
17704 {
17705 /* If there's not enough space in the currently allocated string,
17706 * then copy it to a buffer big enough.
17707 */
17708 *fname= *bufp = vim_strnsave(p, len);
17709 if (*fname == NULL)
17710 return -1;
17711 }
17712 else
17713 {
17714 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17715 *fname = *bufp = pbuf2;
17716 if (p != pbuf2)
17717 strncpy(*fname, p, plen);
17718 pbuf2 = NULL;
17719 }
17720 /* Concat the next bit */
17721 strncpy(*fname + plen, s, slen);
17722 (*fname)[len] = '\0';
17723 }
17724 vim_free(pbuf3);
17725 vim_free(pbuf2);
17726 return 0;
17727}
17728
17729/*
17730 * Get a pathname for a partial path.
17731 */
17732 static int
17733shortpath_for_partial(fnamep, bufp, fnamelen)
17734 char_u **fnamep;
17735 char_u **bufp;
17736 int *fnamelen;
17737{
17738 int sepcount, len, tflen;
17739 char_u *p;
17740 char_u *pbuf, *tfname;
17741 int hasTilde;
17742
17743 /* Count up the path seperators from the RHS.. so we know which part
17744 * of the path to return.
17745 */
17746 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017747 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 if (vim_ispathsep(*p))
17749 ++sepcount;
17750
17751 /* Need full path first (use expand_env() to remove a "~/") */
17752 hasTilde = (**fnamep == '~');
17753 if (hasTilde)
17754 pbuf = tfname = expand_env_save(*fnamep);
17755 else
17756 pbuf = tfname = FullName_save(*fnamep, FALSE);
17757
17758 len = tflen = STRLEN(tfname);
17759
17760 if (!get_short_pathname(&tfname, &pbuf, &len))
17761 return -1;
17762
17763 if (len == 0)
17764 {
17765 /* Don't have a valid filename, so shorten the rest of the
17766 * path if we can. This CAN give us invalid 8.3 filenames, but
17767 * there's not a lot of point in guessing what it might be.
17768 */
17769 len = tflen;
17770 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17771 return -1;
17772 }
17773
17774 /* Count the paths backward to find the beginning of the desired string. */
17775 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017776 {
17777#ifdef FEAT_MBYTE
17778 if (has_mbyte)
17779 p -= mb_head_off(tfname, p);
17780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 if (vim_ispathsep(*p))
17782 {
17783 if (sepcount == 0 || (hasTilde && sepcount == 1))
17784 break;
17785 else
17786 sepcount --;
17787 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 if (hasTilde)
17790 {
17791 --p;
17792 if (p >= tfname)
17793 *p = '~';
17794 else
17795 return -1;
17796 }
17797 else
17798 ++p;
17799
17800 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17801 vim_free(*bufp);
17802 *fnamelen = (int)STRLEN(p);
17803 *bufp = pbuf;
17804 *fnamep = p;
17805
17806 return 0;
17807}
17808#endif /* WIN3264 */
17809
17810/*
17811 * Adjust a filename, according to a string of modifiers.
17812 * *fnamep must be NUL terminated when called. When returning, the length is
17813 * determined by *fnamelen.
17814 * Returns valid flags.
17815 * When there is an error, *fnamep is set to NULL.
17816 */
17817 int
17818modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17819 char_u *src; /* string with modifiers */
17820 int *usedlen; /* characters after src that are used */
17821 char_u **fnamep; /* file name so far */
17822 char_u **bufp; /* buffer for allocated file name or NULL */
17823 int *fnamelen; /* length of fnamep */
17824{
17825 int valid = 0;
17826 char_u *tail;
17827 char_u *s, *p, *pbuf;
17828 char_u dirname[MAXPATHL];
17829 int c;
17830 int has_fullname = 0;
17831#ifdef WIN3264
17832 int has_shortname = 0;
17833#endif
17834
17835repeat:
17836 /* ":p" - full path/file_name */
17837 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17838 {
17839 has_fullname = 1;
17840
17841 valid |= VALID_PATH;
17842 *usedlen += 2;
17843
17844 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17845 if ((*fnamep)[0] == '~'
17846#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17847 && ((*fnamep)[1] == '/'
17848# ifdef BACKSLASH_IN_FILENAME
17849 || (*fnamep)[1] == '\\'
17850# endif
17851 || (*fnamep)[1] == NUL)
17852
17853#endif
17854 )
17855 {
17856 *fnamep = expand_env_save(*fnamep);
17857 vim_free(*bufp); /* free any allocated file name */
17858 *bufp = *fnamep;
17859 if (*fnamep == NULL)
17860 return -1;
17861 }
17862
17863 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017864 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 {
17866 if (vim_ispathsep(*p)
17867 && p[1] == '.'
17868 && (p[2] == NUL
17869 || vim_ispathsep(p[2])
17870 || (p[2] == '.'
17871 && (p[3] == NUL || vim_ispathsep(p[3])))))
17872 break;
17873 }
17874
17875 /* FullName_save() is slow, don't use it when not needed. */
17876 if (*p != NUL || !vim_isAbsName(*fnamep))
17877 {
17878 *fnamep = FullName_save(*fnamep, *p != NUL);
17879 vim_free(*bufp); /* free any allocated file name */
17880 *bufp = *fnamep;
17881 if (*fnamep == NULL)
17882 return -1;
17883 }
17884
17885 /* Append a path separator to a directory. */
17886 if (mch_isdir(*fnamep))
17887 {
17888 /* Make room for one or two extra characters. */
17889 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17890 vim_free(*bufp); /* free any allocated file name */
17891 *bufp = *fnamep;
17892 if (*fnamep == NULL)
17893 return -1;
17894 add_pathsep(*fnamep);
17895 }
17896 }
17897
17898 /* ":." - path relative to the current directory */
17899 /* ":~" - path relative to the home directory */
17900 /* ":8" - shortname path - postponed till after */
17901 while (src[*usedlen] == ':'
17902 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17903 {
17904 *usedlen += 2;
17905 if (c == '8')
17906 {
17907#ifdef WIN3264
17908 has_shortname = 1; /* Postpone this. */
17909#endif
17910 continue;
17911 }
17912 pbuf = NULL;
17913 /* Need full path first (use expand_env() to remove a "~/") */
17914 if (!has_fullname)
17915 {
17916 if (c == '.' && **fnamep == '~')
17917 p = pbuf = expand_env_save(*fnamep);
17918 else
17919 p = pbuf = FullName_save(*fnamep, FALSE);
17920 }
17921 else
17922 p = *fnamep;
17923
17924 has_fullname = 0;
17925
17926 if (p != NULL)
17927 {
17928 if (c == '.')
17929 {
17930 mch_dirname(dirname, MAXPATHL);
17931 s = shorten_fname(p, dirname);
17932 if (s != NULL)
17933 {
17934 *fnamep = s;
17935 if (pbuf != NULL)
17936 {
17937 vim_free(*bufp); /* free any allocated file name */
17938 *bufp = pbuf;
17939 pbuf = NULL;
17940 }
17941 }
17942 }
17943 else
17944 {
17945 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17946 /* Only replace it when it starts with '~' */
17947 if (*dirname == '~')
17948 {
17949 s = vim_strsave(dirname);
17950 if (s != NULL)
17951 {
17952 *fnamep = s;
17953 vim_free(*bufp);
17954 *bufp = s;
17955 }
17956 }
17957 }
17958 vim_free(pbuf);
17959 }
17960 }
17961
17962 tail = gettail(*fnamep);
17963 *fnamelen = (int)STRLEN(*fnamep);
17964
17965 /* ":h" - head, remove "/file_name", can be repeated */
17966 /* Don't remove the first "/" or "c:\" */
17967 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17968 {
17969 valid |= VALID_HEAD;
17970 *usedlen += 2;
17971 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017972 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 --tail;
17974 *fnamelen = (int)(tail - *fnamep);
17975#ifdef VMS
17976 if (*fnamelen > 0)
17977 *fnamelen += 1; /* the path separator is part of the path */
17978#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017979 while (tail > s && !after_pathsep(s, tail))
17980 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 }
17982
17983 /* ":8" - shortname */
17984 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17985 {
17986 *usedlen += 2;
17987#ifdef WIN3264
17988 has_shortname = 1;
17989#endif
17990 }
17991
17992#ifdef WIN3264
17993 /* Check shortname after we have done 'heads' and before we do 'tails'
17994 */
17995 if (has_shortname)
17996 {
17997 pbuf = NULL;
17998 /* Copy the string if it is shortened by :h */
17999 if (*fnamelen < (int)STRLEN(*fnamep))
18000 {
18001 p = vim_strnsave(*fnamep, *fnamelen);
18002 if (p == 0)
18003 return -1;
18004 vim_free(*bufp);
18005 *bufp = *fnamep = p;
18006 }
18007
18008 /* Split into two implementations - makes it easier. First is where
18009 * there isn't a full name already, second is where there is.
18010 */
18011 if (!has_fullname && !vim_isAbsName(*fnamep))
18012 {
18013 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18014 return -1;
18015 }
18016 else
18017 {
18018 int l;
18019
18020 /* Simple case, already have the full-name
18021 * Nearly always shorter, so try first time. */
18022 l = *fnamelen;
18023 if (!get_short_pathname(fnamep, bufp, &l))
18024 return -1;
18025
18026 if (l == 0)
18027 {
18028 /* Couldn't find the filename.. search the paths.
18029 */
18030 l = *fnamelen;
18031 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18032 return -1;
18033 }
18034 *fnamelen = l;
18035 }
18036 }
18037#endif /* WIN3264 */
18038
18039 /* ":t" - tail, just the basename */
18040 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18041 {
18042 *usedlen += 2;
18043 *fnamelen -= (int)(tail - *fnamep);
18044 *fnamep = tail;
18045 }
18046
18047 /* ":e" - extension, can be repeated */
18048 /* ":r" - root, without extension, can be repeated */
18049 while (src[*usedlen] == ':'
18050 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18051 {
18052 /* find a '.' in the tail:
18053 * - for second :e: before the current fname
18054 * - otherwise: The last '.'
18055 */
18056 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18057 s = *fnamep - 2;
18058 else
18059 s = *fnamep + *fnamelen - 1;
18060 for ( ; s > tail; --s)
18061 if (s[0] == '.')
18062 break;
18063 if (src[*usedlen + 1] == 'e') /* :e */
18064 {
18065 if (s > tail)
18066 {
18067 *fnamelen += (int)(*fnamep - (s + 1));
18068 *fnamep = s + 1;
18069#ifdef VMS
18070 /* cut version from the extension */
18071 s = *fnamep + *fnamelen - 1;
18072 for ( ; s > *fnamep; --s)
18073 if (s[0] == ';')
18074 break;
18075 if (s > *fnamep)
18076 *fnamelen = s - *fnamep;
18077#endif
18078 }
18079 else if (*fnamep <= tail)
18080 *fnamelen = 0;
18081 }
18082 else /* :r */
18083 {
18084 if (s > tail) /* remove one extension */
18085 *fnamelen = (int)(s - *fnamep);
18086 }
18087 *usedlen += 2;
18088 }
18089
18090 /* ":s?pat?foo?" - substitute */
18091 /* ":gs?pat?foo?" - global substitute */
18092 if (src[*usedlen] == ':'
18093 && (src[*usedlen + 1] == 's'
18094 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
18095 {
18096 char_u *str;
18097 char_u *pat;
18098 char_u *sub;
18099 int sep;
18100 char_u *flags;
18101 int didit = FALSE;
18102
18103 flags = (char_u *)"";
18104 s = src + *usedlen + 2;
18105 if (src[*usedlen + 1] == 'g')
18106 {
18107 flags = (char_u *)"g";
18108 ++s;
18109 }
18110
18111 sep = *s++;
18112 if (sep)
18113 {
18114 /* find end of pattern */
18115 p = vim_strchr(s, sep);
18116 if (p != NULL)
18117 {
18118 pat = vim_strnsave(s, (int)(p - s));
18119 if (pat != NULL)
18120 {
18121 s = p + 1;
18122 /* find end of substitution */
18123 p = vim_strchr(s, sep);
18124 if (p != NULL)
18125 {
18126 sub = vim_strnsave(s, (int)(p - s));
18127 str = vim_strnsave(*fnamep, *fnamelen);
18128 if (sub != NULL && str != NULL)
18129 {
18130 *usedlen = (int)(p + 1 - src);
18131 s = do_string_sub(str, pat, sub, flags);
18132 if (s != NULL)
18133 {
18134 *fnamep = s;
18135 *fnamelen = (int)STRLEN(s);
18136 vim_free(*bufp);
18137 *bufp = s;
18138 didit = TRUE;
18139 }
18140 }
18141 vim_free(sub);
18142 vim_free(str);
18143 }
18144 vim_free(pat);
18145 }
18146 }
18147 /* after using ":s", repeat all the modifiers */
18148 if (didit)
18149 goto repeat;
18150 }
18151 }
18152
18153 return valid;
18154}
18155
18156/*
18157 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
18158 * "flags" can be "g" to do a global substitute.
18159 * Returns an allocated string, NULL for error.
18160 */
18161 char_u *
18162do_string_sub(str, pat, sub, flags)
18163 char_u *str;
18164 char_u *pat;
18165 char_u *sub;
18166 char_u *flags;
18167{
18168 int sublen;
18169 regmatch_T regmatch;
18170 int i;
18171 int do_all;
18172 char_u *tail;
18173 garray_T ga;
18174 char_u *ret;
18175 char_u *save_cpo;
18176
18177 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
18178 save_cpo = p_cpo;
18179 p_cpo = (char_u *)"";
18180
18181 ga_init2(&ga, 1, 200);
18182
18183 do_all = (flags[0] == 'g');
18184
18185 regmatch.rm_ic = p_ic;
18186 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18187 if (regmatch.regprog != NULL)
18188 {
18189 tail = str;
18190 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18191 {
18192 /*
18193 * Get some space for a temporary buffer to do the substitution
18194 * into. It will contain:
18195 * - The text up to where the match is.
18196 * - The substituted text.
18197 * - The text after the match.
18198 */
18199 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18200 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18201 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18202 {
18203 ga_clear(&ga);
18204 break;
18205 }
18206
18207 /* copy the text up to where the match is */
18208 i = (int)(regmatch.startp[0] - tail);
18209 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18210 /* add the substituted text */
18211 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18212 + ga.ga_len + i, TRUE, TRUE, FALSE);
18213 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 /* avoid getting stuck on a match with an empty string */
18215 if (tail == regmatch.endp[0])
18216 {
18217 if (*tail == NUL)
18218 break;
18219 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18220 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 }
18222 else
18223 {
18224 tail = regmatch.endp[0];
18225 if (*tail == NUL)
18226 break;
18227 }
18228 if (!do_all)
18229 break;
18230 }
18231
18232 if (ga.ga_data != NULL)
18233 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18234
18235 vim_free(regmatch.regprog);
18236 }
18237
18238 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18239 ga_clear(&ga);
18240 p_cpo = save_cpo;
18241
18242 return ret;
18243}
18244
18245#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */