blob: dd135afbaa68794cf3c5309bf0aefdc494e5d53f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaare9a41262005-01-15 22:18:47 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000110
111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
123hashtab_T compat_hashtab;
124
125/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000126 * Array to hold the hashtab with variables local to each sourced script.
127 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000129typedef struct
130{
131 dictitem_T sv_var;
132 dict_T sv_dict;
133} scriptvar_T;
134
135static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
136#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
137#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139static int echo_attr = 0; /* attributes used for ":echo" */
140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000141/* Values for trans_function_name() argument: */
142#define TFN_INT 1 /* internal function name OK */
143#define TFN_QUIET 2 /* no error messages */
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145/*
146 * Structure to hold info for a user function.
147 */
148typedef struct ufunc ufunc_T;
149
150struct ufunc
151{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000152 int uf_varargs; /* variable nr of arguments */
153 int uf_flags;
154 int uf_calls; /* nr of active calls */
155 garray_T uf_args; /* arguments */
156 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000157#ifdef FEAT_PROFILE
158 int uf_profiling; /* TRUE when func is being profiled */
159 /* profiling the function as a whole */
160 int uf_tm_count; /* nr of calls */
161 proftime_T uf_tm_total; /* time spend in function + children */
162 proftime_T uf_tm_self; /* time spend in function itself */
163 proftime_T uf_tm_start; /* time at function call */
164 proftime_T uf_tm_children; /* time spent in children this call */
165 /* profiling the function per line */
166 int *uf_tml_count; /* nr of times line was executed */
167 proftime_T *uf_tml_total; /* time spend in a line + children */
168 proftime_T *uf_tml_self; /* time spend in a line itself */
169 proftime_T uf_tml_start; /* start time for current line */
170 proftime_T uf_tml_children; /* time spent in children for this line */
171 proftime_T uf_tml_wait; /* start wait time for current line */
172 int uf_tml_idx; /* index of line being timed; -1 if none */
173 int uf_tml_execed; /* line being timed was executed */
174#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000175 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000177 int uf_refcount; /* for numbered function: reference count */
178 char_u uf_name[1]; /* name of function (actually longer); can
179 start with <SNR>123_ (<SNR> is K_SPECIAL
180 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181};
182
183/* function flags */
184#define FC_ABORT 1 /* abort function on error */
185#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000186#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 * All user-defined functions are found in this hash table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193/* From user function to hashitem and back. */
194static ufunc_T dumuf;
195#define UF2HIKEY(fp) ((fp)->uf_name)
196#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
197#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
198
199#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
200#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Bram Moolenaar33570922005-01-25 22:26:29 +0000202#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
203#define VAR_SHORT_LEN 20 /* short variable name length */
204#define FIXVAR_CNT 12 /* number of fixed variables */
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000207typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 ufunc_T *func; /* function being called */
210 int linenr; /* next line to be executed */
211 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 struct /* fixed variables for arguments */
213 {
214 dictitem_T var; /* variable (without room for name) */
215 char_u room[VAR_SHORT_LEN]; /* room for the name */
216 } fixvar[FIXVAR_CNT];
217 dict_T l_vars; /* l: local function variables */
218 dictitem_T l_vars_var; /* variable for l: scope */
219 dict_T l_avars; /* a: argument variables */
220 dictitem_T l_avars_var; /* variable for a: scope */
221 list_T l_varlist; /* list for a:000 */
222 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
223 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 linenr_T breakpoint; /* next line with breakpoint or zero */
225 int dbg_tick; /* debug_tick when breakpoint was set */
226 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000227#ifdef FEAT_PROFILE
228 proftime_T prof_child; /* time spent in a child */
229#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000230} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000233 * Info used by a ":for" loop.
234 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000236{
237 int fi_semicolon; /* TRUE if ending in '; var]' */
238 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 listwatch_T fi_lw; /* keep an eye on the item used. */
240 list_T *fi_list; /* list being used */
241} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000242
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000244 * Struct used by trans_function_name()
245 */
246typedef struct
247{
Bram Moolenaar33570922005-01-25 22:26:29 +0000248 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000249 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250 dictitem_T *fd_di; /* Dictionary item used */
251} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000252
Bram Moolenaara7043832005-01-21 11:56:39 +0000253
254/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 * Array to hold the value of v: variables.
256 * The value is in a dictitem, so that it can also be used in the v: scope.
257 * The reason to use this table anyway is for very quick access to the
258 * variables with the VV_ defines.
259 */
260#include "version.h"
261
262/* values for vv_flags: */
263#define VV_COMPAT 1 /* compatible, also used without "v:" */
264#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000265#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266
267#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
268
269static struct vimvar
270{
271 char *vv_name; /* name of variable, without v: */
272 int vv_len; /* length of name */
273 dictitem_T vv_di; /* value and name for key */
274 char vv_filler[16]; /* space for LONGEST name below!!! */
275 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
276} vimvars[VV_LEN] =
277{
278 /*
279 * The order here must match the VV_ defines in vim.h!
280 * Initializing a union does not work, leave tv.vval empty to get zero's.
281 */
282 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
283 {VV_NAME("count1", VAR_NUMBER), VV_RO},
284 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
285 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
286 {VV_NAME("warningmsg", VAR_STRING), 0},
287 {VV_NAME("statusmsg", VAR_STRING), 0},
288 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
289 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
290 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
291 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
292 {VV_NAME("termresponse", VAR_STRING), VV_RO},
293 {VV_NAME("fname", VAR_STRING), VV_RO},
294 {VV_NAME("lang", VAR_STRING), VV_RO},
295 {VV_NAME("lc_time", VAR_STRING), VV_RO},
296 {VV_NAME("ctype", VAR_STRING), VV_RO},
297 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
298 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
299 {VV_NAME("fname_in", VAR_STRING), VV_RO},
300 {VV_NAME("fname_out", VAR_STRING), VV_RO},
301 {VV_NAME("fname_new", VAR_STRING), VV_RO},
302 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
303 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
304 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
305 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
307 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
308 {VV_NAME("progname", VAR_STRING), VV_RO},
309 {VV_NAME("servername", VAR_STRING), VV_RO},
310 {VV_NAME("dying", VAR_NUMBER), VV_RO},
311 {VV_NAME("exception", VAR_STRING), VV_RO},
312 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
313 {VV_NAME("register", VAR_STRING), VV_RO},
314 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
315 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000316 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
317 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000318 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram 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));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000431static void f_errorlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
465static 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));
539static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
544#ifdef HAVE_STRFTIME
545static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
546#endif
547static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000559static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000575static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576
577static win_T *find_win_by_nr __ARGS((typval_T *vp));
578static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
579static int get_env_len __ARGS((char_u **arg));
580static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
583static int eval_isnamec __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000584static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
585static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static typval_T *alloc_tv __ARGS((void));
587static typval_T *alloc_string_tv __ARGS((char_u *string));
588static void free_tv __ARGS((typval_T *varp));
589static void clear_tv __ARGS((typval_T *varp));
590static void init_tv __ARGS((typval_T *varp));
591static long get_tv_number __ARGS((typval_T *varp));
592static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
593static char_u *get_tv_string __ARGS((typval_T *varp));
594static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
595static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000596static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
598static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
599static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
600static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
601static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
602static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
603static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000604static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000606static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
608static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
609static int eval_fname_script __ARGS((char_u *p));
610static int eval_fname_sid __ARGS((char_u *p));
611static void list_func_head __ARGS((ufunc_T *fp, int indent));
612static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
613static ufunc_T *find_func __ARGS((char_u *name));
614static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000615static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000616#ifdef FEAT_PROFILE
617static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000618static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
619static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
620static int
621# ifdef __BORLANDC__
622 _RTLENTRYF
623# endif
624 prof_total_cmp __ARGS((const void *s1, const void *s2));
625static int
626# ifdef __BORLANDC__
627 _RTLENTRYF
628# endif
629 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000630#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631static int script_autoload __ARGS((char_u *name));
632static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void func_free __ARGS((ufunc_T *fp));
634static void func_unref __ARGS((char_u *name));
635static void func_ref __ARGS((char_u *name));
636static 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));
637static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
638
639static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
640
641static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
642static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
643static char_u *skip_var_one __ARGS((char_u *arg));
644static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
645static void list_glob_vars __ARGS((void));
646static void list_buf_vars __ARGS((void));
647static void list_win_vars __ARGS((void));
648static void list_vim_vars __ARGS((void));
649static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
650static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
651static int check_changedtick __ARGS((char_u *arg));
652static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
653static void clear_lval __ARGS((lval_T *lp));
654static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
655static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
656static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
657static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
658static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000659static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000661static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
662static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000663static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664
665/*
666 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000667 */
668 void
669eval_init()
670{
Bram Moolenaar33570922005-01-25 22:26:29 +0000671 int i;
672 struct vimvar *p;
673
674 init_var_dict(&globvardict, &globvars_var);
675 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000676 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000678
679 for (i = 0; i < VV_LEN; ++i)
680 {
681 p = &vimvars[i];
682 STRCPY(p->vv_di.di_key, p->vv_name);
683 if (p->vv_flags & VV_RO)
684 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
685 else if (p->vv_flags & VV_RO_SBX)
686 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
687 else
688 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000689
690 /* add to v: scope dict, unless the value is not always available */
691 if (p->vv_type != VAR_UNKNOWN)
692 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000694 /* add to compat scope dict */
695 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000697}
698
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 * Return the name of the executed function.
701 */
702 char_u *
703func_name(cookie)
704 void *cookie;
705{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000706 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709/*
710 * Return the address holding the next breakpoint line for a funccall cookie.
711 */
712 linenr_T *
713func_breakpoint(cookie)
714 void *cookie;
715{
Bram Moolenaar33570922005-01-25 22:26:29 +0000716 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718
719/*
720 * Return the address holding the debug tick for a funccall cookie.
721 */
722 int *
723func_dbg_tick(cookie)
724 void *cookie;
725{
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727}
728
729/*
730 * Return the nesting level for a funccall cookie.
731 */
732 int
733func_level(cookie)
734 void *cookie;
735{
Bram Moolenaar33570922005-01-25 22:26:29 +0000736 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
739/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000740funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
742/*
743 * Return TRUE when a function was ended by a ":return" command.
744 */
745 int
746current_func_returned()
747{
748 return current_funccal->returned;
749}
750
751
752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * Set an internal variable to a string value. Creates the variable if it does
754 * not already exist.
755 */
756 void
757set_internal_string_var(name, value)
758 char_u *name;
759 char_u *value;
760{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000761 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
764 val = vim_strsave(value);
765 if (val != NULL)
766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000767 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000768 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000770 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000771 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 }
773 }
774}
775
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000776static lval_T *redir_lval = NULL;
777static char_u *redir_endp = NULL;
778static char_u *redir_varname = NULL;
779
780/*
781 * Start recording command output to a variable
782 * Returns OK if successfully completed the setup. FAIL otherwise.
783 */
784 int
785var_redir_start(name, append)
786 char_u *name;
787 int append; /* append to an existing variable */
788{
789 int save_emsg;
790 int err;
791 typval_T tv;
792
793 /* Make sure a valid variable name is specified */
794 if (!eval_isnamec(*name) || VIM_ISDIGIT(*name))
795 {
796 EMSG(_(e_invarg));
797 return FAIL;
798 }
799
800 redir_varname = vim_strsave(name);
801 if (redir_varname == NULL)
802 return FAIL;
803
804 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
805 if (redir_lval == NULL)
806 {
807 var_redir_stop();
808 return FAIL;
809 }
810
811 /* Parse the variable name (can be a dict or list entry). */
812 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE);
813 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
814 {
815 if (redir_endp != NULL && *redir_endp != NUL)
816 /* Trailing characters are present after the variable name */
817 EMSG(_(e_trailing));
818 else
819 EMSG(_(e_invarg));
820 var_redir_stop();
821 return FAIL;
822 }
823
824 /* check if we can write to the variable: set it to or append an empty
825 * string */
826 save_emsg = did_emsg;
827 did_emsg = FALSE;
828 tv.v_type = VAR_STRING;
829 tv.vval.v_string = (char_u *)"";
830 if (append)
831 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
832 else
833 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
834 err = did_emsg;
835 did_emsg += save_emsg;
836 if (err)
837 {
838 var_redir_stop();
839 return FAIL;
840 }
841 if (redir_lval->ll_newkey != NULL)
842 {
843 /* Dictionary item was created, don't do it again. */
844 vim_free(redir_lval->ll_newkey);
845 redir_lval->ll_newkey = NULL;
846 }
847
848 return OK;
849}
850
851/*
852 * Append "value[len]" to the variable set by var_redir_start().
853 */
854 void
855var_redir_str(value, len)
856 char_u *value;
857 int len;
858{
859 char_u *val;
860 typval_T tv;
861 int save_emsg;
862 int err;
863
864 if (redir_lval == NULL)
865 return;
866
867 if (len == -1)
868 /* Append the entire string */
869 val = vim_strsave(value);
870 else
871 /* Append only the specified number of characters */
872 val = vim_strnsave(value, len);
873 if (val == NULL)
874 return;
875
876 tv.v_type = VAR_STRING;
877 tv.vval.v_string = val;
878
879 save_emsg = did_emsg;
880 did_emsg = FALSE;
881 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
882 err = did_emsg;
883 did_emsg += save_emsg;
884 if (err)
885 var_redir_stop();
886
887 vim_free(tv.vval.v_string);
888}
889
890/*
891 * Stop redirecting command output to a variable.
892 */
893 void
894var_redir_stop()
895{
896 if (redir_lval != NULL)
897 {
898 clear_lval(redir_lval);
899 vim_free(redir_lval);
900 redir_lval = NULL;
901 }
902 vim_free(redir_varname);
903 redir_varname = NULL;
904}
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906# if defined(FEAT_MBYTE) || defined(PROTO)
907 int
908eval_charconvert(enc_from, enc_to, fname_from, fname_to)
909 char_u *enc_from;
910 char_u *enc_to;
911 char_u *fname_from;
912 char_u *fname_to;
913{
914 int err = FALSE;
915
916 set_vim_var_string(VV_CC_FROM, enc_from, -1);
917 set_vim_var_string(VV_CC_TO, enc_to, -1);
918 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
919 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
920 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
921 err = TRUE;
922 set_vim_var_string(VV_CC_FROM, NULL, -1);
923 set_vim_var_string(VV_CC_TO, NULL, -1);
924 set_vim_var_string(VV_FNAME_IN, NULL, -1);
925 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
926
927 if (err)
928 return FAIL;
929 return OK;
930}
931# endif
932
933# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
934 int
935eval_printexpr(fname, args)
936 char_u *fname;
937 char_u *args;
938{
939 int err = FALSE;
940
941 set_vim_var_string(VV_FNAME_IN, fname, -1);
942 set_vim_var_string(VV_CMDARG, args, -1);
943 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
944 err = TRUE;
945 set_vim_var_string(VV_FNAME_IN, NULL, -1);
946 set_vim_var_string(VV_CMDARG, NULL, -1);
947
948 if (err)
949 {
950 mch_remove(fname);
951 return FAIL;
952 }
953 return OK;
954}
955# endif
956
957# if defined(FEAT_DIFF) || defined(PROTO)
958 void
959eval_diff(origfile, newfile, outfile)
960 char_u *origfile;
961 char_u *newfile;
962 char_u *outfile;
963{
964 int err = FALSE;
965
966 set_vim_var_string(VV_FNAME_IN, origfile, -1);
967 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
968 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
969 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
970 set_vim_var_string(VV_FNAME_IN, NULL, -1);
971 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
972 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
973}
974
975 void
976eval_patch(origfile, difffile, outfile)
977 char_u *origfile;
978 char_u *difffile;
979 char_u *outfile;
980{
981 int err;
982
983 set_vim_var_string(VV_FNAME_IN, origfile, -1);
984 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
985 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
986 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
987 set_vim_var_string(VV_FNAME_IN, NULL, -1);
988 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
989 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
990}
991# endif
992
993/*
994 * Top level evaluation function, returning a boolean.
995 * Sets "error" to TRUE if there was an error.
996 * Return TRUE or FALSE.
997 */
998 int
999eval_to_bool(arg, error, nextcmd, skip)
1000 char_u *arg;
1001 int *error;
1002 char_u **nextcmd;
1003 int skip; /* only parse, don't execute */
1004{
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 int retval = FALSE;
1007
1008 if (skip)
1009 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 else
1013 {
1014 *error = FALSE;
1015 if (!skip)
1016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 retval = (get_tv_number(&tv) != 0);
1018 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021 if (skip)
1022 --emsg_skip;
1023
1024 return retval;
1025}
1026
1027/*
1028 * Top level evaluation function, returning a string. If "skip" is TRUE,
1029 * only parsing to "nextcmd" is done, without reporting errors. Return
1030 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1031 */
1032 char_u *
1033eval_to_string_skip(arg, nextcmd, skip)
1034 char_u *arg;
1035 char_u **nextcmd;
1036 int skip; /* only parse, don't execute */
1037{
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 char_u *retval;
1040
1041 if (skip)
1042 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 retval = NULL;
1045 else
1046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 retval = vim_strsave(get_tv_string(&tv));
1048 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 if (skip)
1051 --emsg_skip;
1052
1053 return retval;
1054}
1055
1056/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001057 * Skip over an expression at "*pp".
1058 * Return FAIL for an error, OK otherwise.
1059 */
1060 int
1061skip_expr(pp)
1062 char_u **pp;
1063{
Bram Moolenaar33570922005-01-25 22:26:29 +00001064 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001065
1066 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001067 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001068}
1069
1070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * Top level evaluation function, returning a string.
1072 * Return pointer to allocated memory, or NULL for failure.
1073 */
1074 char_u *
1075eval_to_string(arg, nextcmd)
1076 char_u *arg;
1077 char_u **nextcmd;
1078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 char_u *retval;
1081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001082 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 retval = NULL;
1084 else
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 retval = vim_strsave(get_tv_string(&tv));
1087 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089
1090 return retval;
1091}
1092
1093/*
1094 * Call eval_to_string() with "sandbox" set and not using local variables.
1095 */
1096 char_u *
1097eval_to_string_safe(arg, nextcmd)
1098 char_u *arg;
1099 char_u **nextcmd;
1100{
1101 char_u *retval;
1102 void *save_funccalp;
1103
1104 save_funccalp = save_funccal();
1105 ++sandbox;
1106 retval = eval_to_string(arg, nextcmd);
1107 --sandbox;
1108 restore_funccal(save_funccalp);
1109 return retval;
1110}
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112/*
1113 * Top level evaluation function, returning a number.
1114 * Evaluates "expr" silently.
1115 * Returns -1 for an error.
1116 */
1117 int
1118eval_to_number(expr)
1119 char_u *expr;
1120{
Bram Moolenaar33570922005-01-25 22:26:29 +00001121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001123 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 ++emsg_off;
1126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 retval = -1;
1129 else
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 retval = get_tv_number(&rettv);
1132 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
1134 --emsg_off;
1135
1136 return retval;
1137}
1138
1139#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1140/*
1141 * Call some vimL function and return the result as a string
1142 * Uses argv[argc] for the function arguments.
1143 */
1144 char_u *
1145call_vim_function(func, argc, argv, safe)
1146 char_u *func;
1147 int argc;
1148 char_u **argv;
1149 int safe; /* use the sandbox */
1150{
1151 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001152 typval_T rettv;
1153 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 long n;
1155 int len;
1156 int i;
1157 int doesrange;
1158 void *save_funccalp = NULL;
1159
Bram Moolenaar33570922005-01-25 22:26:29 +00001160 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (argvars == NULL)
1162 return NULL;
1163
1164 for (i = 0; i < argc; i++)
1165 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001166 /* Pass a NULL or empty argument as an empty string */
1167 if (argv[i] == NULL || *argv[i] == NUL)
1168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001169 argvars[i].v_type = VAR_STRING;
1170 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001171 continue;
1172 }
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 /* Recognize a number argument, the others must be strings. */
1175 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1176 if (len != 0 && len == (int)STRLEN(argv[i]))
1177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001178 argvars[i].v_type = VAR_NUMBER;
1179 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
1181 else
1182 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001183 argvars[i].v_type = VAR_STRING;
1184 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186 }
1187
1188 if (safe)
1189 {
1190 save_funccalp = save_funccal();
1191 ++sandbox;
1192 }
1193
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001194 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1195 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001197 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 vim_free(argvars);
1202
1203 if (safe)
1204 {
1205 --sandbox;
1206 restore_funccal(save_funccalp);
1207 }
1208 return retval;
1209}
1210#endif
1211
1212/*
1213 * Save the current function call pointer, and set it to NULL.
1214 * Used when executing autocommands and for ":source".
1215 */
1216 void *
1217save_funccal()
1218{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001219 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 current_funccal = NULL;
1222 return (void *)fc;
1223}
1224
1225 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001226restore_funccal(vfc)
1227 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001229 funccall_T *fc = (funccall_T *)vfc;
1230
1231 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232}
1233
Bram Moolenaar05159a02005-02-26 23:04:13 +00001234#if defined(FEAT_PROFILE) || defined(PROTO)
1235/*
1236 * Prepare profiling for entering a child or something else that is not
1237 * counted for the script/function itself.
1238 * Should always be called in pair with prof_child_exit().
1239 */
1240 void
1241prof_child_enter(tm)
1242 proftime_T *tm; /* place to store waittime */
1243{
1244 funccall_T *fc = current_funccal;
1245
1246 if (fc != NULL && fc->func->uf_profiling)
1247 profile_start(&fc->prof_child);
1248 script_prof_save(tm);
1249}
1250
1251/*
1252 * Take care of time spent in a child.
1253 * Should always be called after prof_child_enter().
1254 */
1255 void
1256prof_child_exit(tm)
1257 proftime_T *tm; /* where waittime was stored */
1258{
1259 funccall_T *fc = current_funccal;
1260
1261 if (fc != NULL && fc->func->uf_profiling)
1262 {
1263 profile_end(&fc->prof_child);
1264 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1265 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1266 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1267 }
1268 script_prof_restore(tm);
1269}
1270#endif
1271
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#ifdef FEAT_FOLDING
1274/*
1275 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1276 * it in "*cp". Doesn't give error messages.
1277 */
1278 int
1279eval_foldexpr(arg, cp)
1280 char_u *arg;
1281 int *cp;
1282{
Bram Moolenaar33570922005-01-25 22:26:29 +00001283 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int retval;
1285 char_u *s;
1286
1287 ++emsg_off;
1288 ++sandbox;
1289 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 retval = 0;
1292 else
1293 {
1294 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 if (tv.v_type == VAR_NUMBER)
1296 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001297 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 retval = 0;
1299 else
1300 {
1301 /* If the result is a string, check if there is a non-digit before
1302 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (!VIM_ISDIGIT(*s) && *s != '-')
1305 *cp = *s++;
1306 retval = atol((char *)s);
1307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001308 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 --emsg_off;
1311 --sandbox;
1312
1313 return retval;
1314}
1315#endif
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001318 * ":let" list all variable values
1319 * ":let var1 var2" list variable values
1320 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001321 * ":let var += expr" assignment command.
1322 * ":let var -= expr" assignment command.
1323 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 */
1326 void
1327ex_let(eap)
1328 exarg_T *eap;
1329{
1330 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001331 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 int var_count = 0;
1335 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001338 expr = skip_var_list(arg, &var_count, &semicolon);
1339 if (expr == NULL)
1340 return;
1341 expr = vim_strchr(expr, '=');
1342 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001344 /*
1345 * ":let" without "=": list variables
1346 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001347 if (*arg == '[')
1348 EMSG(_(e_invarg));
1349 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 /* ":let var1 var2" */
1351 arg = list_arg_vars(eap, arg);
1352 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001355 list_glob_vars();
1356 list_buf_vars();
1357 list_win_vars();
1358 list_vim_vars();
1359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 eap->nextcmd = check_nextcmd(arg);
1361 }
1362 else
1363 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 op[0] = '=';
1365 op[1] = NUL;
1366 if (expr > arg)
1367 {
1368 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1369 op[0] = expr[-1]; /* +=, -= or .= */
1370 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001371 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (eap->skip)
1374 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001375 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (eap->skip)
1377 {
1378 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 --emsg_skip;
1381 }
1382 else if (i != FAIL)
1383 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001384 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001385 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389}
1390
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001391/*
1392 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1393 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 * When "nextchars" is not NULL it points to a string with characters that
1395 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1396 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001397 * Returns OK or FAIL;
1398 */
1399 static int
1400ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1401 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 int copy; /* copy values from "tv", don't move */
1404 int semicolon; /* from skip_var_list() */
1405 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001407{
1408 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 listitem_T *item;
1412 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413
1414 if (*arg != '[')
1415 {
1416 /*
1417 * ":let var = expr" or ":for var in list"
1418 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001419 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420 return FAIL;
1421 return OK;
1422 }
1423
1424 /*
1425 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1426 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001427 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001428 {
1429 EMSG(_(e_listreq));
1430 return FAIL;
1431 }
1432
1433 i = list_len(l);
1434 if (semicolon == 0 && var_count < i)
1435 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001436 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 return FAIL;
1438 }
1439 if (var_count - semicolon > i)
1440 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001441 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442 return FAIL;
1443 }
1444
1445 item = l->lv_first;
1446 while (*arg != ']')
1447 {
1448 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001450 item = item->li_next;
1451 if (arg == NULL)
1452 return FAIL;
1453
1454 arg = skipwhite(arg);
1455 if (*arg == ';')
1456 {
1457 /* Put the rest of the list (may be empty) in the var after ';'.
1458 * Create a new list for this. */
1459 l = list_alloc();
1460 if (l == NULL)
1461 return FAIL;
1462 while (item != NULL)
1463 {
1464 list_append_tv(l, &item->li_tv);
1465 item = item->li_next;
1466 }
1467
1468 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001469 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470 ltv.vval.v_list = l;
1471 l->lv_refcount = 1;
1472
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1474 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 clear_tv(&ltv);
1476 if (arg == NULL)
1477 return FAIL;
1478 break;
1479 }
1480 else if (*arg != ',' && *arg != ']')
1481 {
1482 EMSG2(_(e_intern2), "ex_let_vars()");
1483 return FAIL;
1484 }
1485 }
1486
1487 return OK;
1488}
1489
1490/*
1491 * Skip over assignable variable "var" or list of variables "[var, var]".
1492 * Used for ":let varvar = expr" and ":for varvar in expr".
1493 * For "[var, var]" increment "*var_count" for each variable.
1494 * for "[var, var; var]" set "semicolon".
1495 * Return NULL for an error.
1496 */
1497 static char_u *
1498skip_var_list(arg, var_count, semicolon)
1499 char_u *arg;
1500 int *var_count;
1501 int *semicolon;
1502{
1503 char_u *p, *s;
1504
1505 if (*arg == '[')
1506 {
1507 /* "[var, var]": find the matching ']'. */
1508 p = arg;
1509 while (1)
1510 {
1511 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1512 s = skip_var_one(p);
1513 if (s == p)
1514 {
1515 EMSG2(_(e_invarg2), p);
1516 return NULL;
1517 }
1518 ++*var_count;
1519
1520 p = skipwhite(s);
1521 if (*p == ']')
1522 break;
1523 else if (*p == ';')
1524 {
1525 if (*semicolon == 1)
1526 {
1527 EMSG(_("Double ; in list of variables"));
1528 return NULL;
1529 }
1530 *semicolon = 1;
1531 }
1532 else if (*p != ',')
1533 {
1534 EMSG2(_(e_invarg2), p);
1535 return NULL;
1536 }
1537 }
1538 return p + 1;
1539 }
1540 else
1541 return skip_var_one(arg);
1542}
1543
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001544/*
1545 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1546 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 static char_u *
1548skip_var_one(arg)
1549 char_u *arg;
1550{
1551 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1552 ++arg;
1553 return find_name_end(arg, NULL, NULL, TRUE);
1554}
1555
Bram Moolenaara7043832005-01-21 11:56:39 +00001556/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001557 * List variables for hashtab "ht" with prefix "prefix".
1558 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001559 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001560 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001561list_hashtable_vars(ht, prefix, empty)
1562 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001563 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 hashitem_T *hi;
1567 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001568 int todo;
1569
1570 todo = ht->ht_used;
1571 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1572 {
1573 if (!HASHITEM_EMPTY(hi))
1574 {
1575 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 di = HI2DI(hi);
1577 if (empty || di->di_tv.v_type != VAR_STRING
1578 || di->di_tv.vval.v_string != NULL)
1579 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001580 }
1581 }
1582}
1583
1584/*
1585 * List global variables.
1586 */
1587 static void
1588list_glob_vars()
1589{
Bram Moolenaar33570922005-01-25 22:26:29 +00001590 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001591}
1592
1593/*
1594 * List buffer variables.
1595 */
1596 static void
1597list_buf_vars()
1598{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001599 char_u numbuf[NUMBUFLEN];
1600
Bram Moolenaar33570922005-01-25 22:26:29 +00001601 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001602
1603 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1604 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001605}
1606
1607/*
1608 * List window variables.
1609 */
1610 static void
1611list_win_vars()
1612{
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001614}
1615
1616/*
1617 * List Vim variables.
1618 */
1619 static void
1620list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623}
1624
1625/*
1626 * List variables in "arg".
1627 */
1628 static char_u *
1629list_arg_vars(eap, arg)
1630 exarg_T *eap;
1631 char_u *arg;
1632{
1633 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001634 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001636 char_u *name_start;
1637 char_u *arg_subsc;
1638 char_u *tofree;
1639 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001640
1641 while (!ends_excmd(*arg) && !got_int)
1642 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001643 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001645 arg = find_name_end(arg, NULL, NULL, TRUE);
1646 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1647 {
1648 emsg_severe = TRUE;
1649 EMSG(_(e_trailing));
1650 break;
1651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 /* get_name_len() takes care of expanding curly braces */
1656 name_start = name = arg;
1657 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1658 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001660 /* This is mainly to keep test 49 working: when expanding
1661 * curly braces fails overrule the exception error message. */
1662 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001664 emsg_severe = TRUE;
1665 EMSG2(_(e_invarg2), arg);
1666 break;
1667 }
1668 error = TRUE;
1669 }
1670 else
1671 {
1672 if (tofree != NULL)
1673 name = tofree;
1674 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001676 else
1677 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001678 /* handle d.key, l[idx], f(expr) */
1679 arg_subsc = arg;
1680 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001681 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001682 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001683 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001684 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001685 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001686 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001687 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001688 case 'g': list_glob_vars(); break;
1689 case 'b': list_buf_vars(); break;
1690 case 'w': list_win_vars(); break;
1691 case 'v': list_vim_vars(); break;
1692 default:
1693 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001695 }
1696 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001697 {
1698 char_u numbuf[NUMBUFLEN];
1699 char_u *tf;
1700 int c;
1701 char_u *s;
1702
1703 s = echo_string(&tv, &tf, numbuf);
1704 c = *arg;
1705 *arg = NUL;
1706 list_one_var_a((char_u *)"",
1707 arg == arg_subsc ? name : name_start,
1708 tv.v_type, s == NULL ? (char_u *)"" : s);
1709 *arg = c;
1710 vim_free(tf);
1711 }
1712 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001713 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 }
1715 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001716
1717 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001719
1720 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 }
1722
1723 return arg;
1724}
1725
1726/*
1727 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1728 * Returns a pointer to the char just after the var name.
1729 * Returns NULL if there is an error.
1730 */
1731 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001734 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738{
1739 int c1;
1740 char_u *name;
1741 char_u *p;
1742 char_u *arg_end = NULL;
1743 int len;
1744 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746
1747 /*
1748 * ":let $VAR = expr": Set environment variable.
1749 */
1750 if (*arg == '$')
1751 {
1752 /* Find the end of the name. */
1753 ++arg;
1754 name = arg;
1755 len = get_env_len(&arg);
1756 if (len == 0)
1757 EMSG2(_(e_invarg2), name - 1);
1758 else
1759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 if (op != NULL && (*op == '+' || *op == '-'))
1761 EMSG2(_(e_letwrong), op);
1762 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 EMSG(_(e_letunexp));
1765 else
1766 {
1767 c1 = name[len];
1768 name[len] = NUL;
1769 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 if (op != NULL && *op == '.')
1771 {
1772 int mustfree = FALSE;
1773 char_u *s = vim_getenv(name, &mustfree);
1774
1775 if (s != NULL)
1776 {
1777 p = tofree = concat_str(s, p);
1778 if (mustfree)
1779 vim_free(s);
1780 }
1781 }
1782 if (p != NULL)
1783 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 if (STRICMP(name, "HOME") == 0)
1785 init_homedir();
1786 else if (didset_vim && STRICMP(name, "VIM") == 0)
1787 didset_vim = FALSE;
1788 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1789 didset_vimruntime = FALSE;
1790 name[len] = c1;
1791 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001792 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 }
1794 }
1795 }
1796
1797 /*
1798 * ":let &option = expr": Set option value.
1799 * ":let &l:option = expr": Set local option value.
1800 * ":let &g:option = expr": Set global option value.
1801 */
1802 else if (*arg == '&')
1803 {
1804 /* Find the end of the name. */
1805 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 if (p == NULL || (endchars != NULL
1807 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 EMSG(_(e_letunexp));
1809 else
1810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001811 long n;
1812 int opt_type;
1813 long numval;
1814 char_u *stringval = NULL;
1815 char_u *s;
1816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 c1 = *p;
1818 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001819
1820 n = get_tv_number(tv);
1821 s = get_tv_string(tv);
1822 if (op != NULL && *op != '=')
1823 {
1824 opt_type = get_option_value(arg, &numval,
1825 &stringval, opt_flags);
1826 if ((opt_type == 1 && *op == '.')
1827 || (opt_type == 0 && *op != '.'))
1828 EMSG2(_(e_letwrong), op);
1829 else
1830 {
1831 if (opt_type == 1) /* number */
1832 {
1833 if (*op == '+')
1834 n = numval + n;
1835 else
1836 n = numval - n;
1837 }
1838 else if (opt_type == 0 && stringval != NULL) /* string */
1839 {
1840 s = concat_str(stringval, s);
1841 vim_free(stringval);
1842 stringval = s;
1843 }
1844 }
1845 }
1846 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 *p = c1;
1848 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 }
1851 }
1852
1853 /*
1854 * ":let @r = expr": Set register contents.
1855 */
1856 else if (*arg == '@')
1857 {
1858 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 if (op != NULL && (*op == '+' || *op == '-'))
1860 EMSG2(_(e_letwrong), op);
1861 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 EMSG(_(e_letunexp));
1864 else
1865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 char_u *tofree = NULL;
1867 char_u *s;
1868
1869 p = get_tv_string(tv);
1870 if (op != NULL && *op == '.')
1871 {
1872 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1873 if (s != NULL)
1874 {
1875 p = tofree = concat_str(s, p);
1876 vim_free(s);
1877 }
1878 }
1879 if (p != NULL)
1880 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 }
1884 }
1885
1886 /*
1887 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001888 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001890 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001892 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1895 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001897 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1898 EMSG(_(e_letunexp));
1899 else
1900 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 arg_end = p;
1903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001905 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 }
1907
1908 else
1909 EMSG2(_(e_invarg2), arg);
1910
1911 return arg_end;
1912}
1913
1914/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001915 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1916 */
1917 static int
1918check_changedtick(arg)
1919 char_u *arg;
1920{
1921 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1922 {
1923 EMSG2(_(e_readonlyvar), arg);
1924 return TRUE;
1925 }
1926 return FALSE;
1927}
1928
1929/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001930 * Get an lval: variable, Dict item or List item that can be assigned a value
1931 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1932 * "name.key", "name.key[expr]" etc.
1933 * Indexing only works if "name" is an existing List or Dictionary.
1934 * "name" points to the start of the name.
1935 * If "rettv" is not NULL it points to the value to be assigned.
1936 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1937 * wrong; must end in space or cmd separator.
1938 *
1939 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001940 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001941 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 */
1943 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001944get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 typval_T *rettv;
1947 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001948 int unlet;
1949 int skip;
1950 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 char_u *expr_start, *expr_end;
1954 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 dictitem_T *v;
1956 typval_T var1;
1957 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001958 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001959 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001960 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001961 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001962 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001964 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001966
1967 if (skip)
1968 {
1969 /* When skipping just find the end of the name. */
1970 lp->ll_name = name;
1971 return find_name_end(name, NULL, NULL, TRUE);
1972 }
1973
1974 /* Find the end of the name. */
1975 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1976 if (expr_start != NULL)
1977 {
1978 /* Don't expand the name when we already know there is an error. */
1979 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1980 && *p != '[' && *p != '.')
1981 {
1982 EMSG(_(e_trailing));
1983 return NULL;
1984 }
1985
1986 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1987 if (lp->ll_exp_name == NULL)
1988 {
1989 /* Report an invalid expression in braces, unless the
1990 * expression evaluation has been cancelled due to an
1991 * aborting error, an interrupt, or an exception. */
1992 if (!aborting() && !quiet)
1993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001994 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 EMSG2(_(e_invarg2), name);
1996 return NULL;
1997 }
1998 }
1999 lp->ll_name = lp->ll_exp_name;
2000 }
2001 else
2002 lp->ll_name = name;
2003
2004 /* Without [idx] or .key we are done. */
2005 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2006 return p;
2007
2008 cc = *p;
2009 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002010 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002011 if (v == NULL && !quiet)
2012 EMSG2(_(e_undefvar), lp->ll_name);
2013 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 if (v == NULL)
2015 return NULL;
2016
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002017 /*
2018 * Loop until no more [idx] or .key is following.
2019 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002021 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002023 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2024 && !(lp->ll_tv->v_type == VAR_DICT
2025 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002027 if (!quiet)
2028 EMSG(_("E689: Can only index a List or Dictionary"));
2029 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002031 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 if (!quiet)
2034 EMSG(_("E708: [:] must come last"));
2035 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002037
Bram Moolenaar8c711452005-01-14 21:53:12 +00002038 len = -1;
2039 if (*p == '.')
2040 {
2041 key = p + 1;
2042 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2043 ;
2044 if (len == 0)
2045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002046 if (!quiet)
2047 EMSG(_(e_emptykey));
2048 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002049 }
2050 p = key + len;
2051 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002052 else
2053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002054 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002055 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002056 if (*p == ':')
2057 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002058 else
2059 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 empty1 = FALSE;
2061 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002063 }
2064
2065 /* Optionally get the second index [ :expr]. */
2066 if (*p == ':')
2067 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002069 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002070 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002071 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002072 if (!empty1)
2073 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002074 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002075 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002076 if (rettv != NULL && (rettv->v_type != VAR_LIST
2077 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002078 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002079 if (!quiet)
2080 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002081 if (!empty1)
2082 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002083 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002084 }
2085 p = skipwhite(p + 1);
2086 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002087 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002088 else
2089 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002090 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002091 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2092 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002093 if (!empty1)
2094 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002095 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002096 }
2097 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002098 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002100 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002101 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002102
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002105 if (!quiet)
2106 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 if (!empty1)
2108 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002109 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002110 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002111 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002112 }
2113
2114 /* Skip to past ']'. */
2115 ++p;
2116 }
2117
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002119 {
2120 if (len == -1)
2121 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002123 key = get_tv_string(&var1);
2124 if (*key == NUL)
2125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002126 if (!quiet)
2127 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002128 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002130 }
2131 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002132 lp->ll_list = NULL;
2133 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002134 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002137 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002139 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002140 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002141 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002142 if (len == -1)
2143 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002145 }
2146 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002147 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002148 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002149 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002150 if (len == -1)
2151 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002152 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002153 p = NULL;
2154 break;
2155 }
2156 if (len == -1)
2157 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002159 }
2160 else
2161 {
2162 /*
2163 * Get the number and item for the only or first index of the List.
2164 */
2165 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002166 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002167 else
2168 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002170 clear_tv(&var1);
2171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 lp->ll_list = lp->ll_tv->vval.v_list;
2174 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2175 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002176 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 if (!quiet)
2178 EMSGN(_(e_listidx), lp->ll_n1);
2179 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002180 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002182 }
2183
2184 /*
2185 * May need to find the item or absolute index for the second
2186 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 * When no index given: "lp->ll_empty2" is TRUE.
2188 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002189 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002195 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002197 if (ni == NULL)
2198 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199 if (!quiet)
2200 EMSGN(_(e_listidx), lp->ll_n2);
2201 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002202 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002203 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 }
2205
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2207 if (lp->ll_n1 < 0)
2208 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2209 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002210 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002211 if (!quiet)
2212 EMSGN(_(e_listidx), lp->ll_n2);
2213 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002214 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002215 }
2216
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 }
2220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 return p;
2222}
2223
2224/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002225 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 */
2227 static void
2228clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002229 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230{
2231 vim_free(lp->ll_exp_name);
2232 vim_free(lp->ll_newkey);
2233}
2234
2235/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002236 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002238 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 */
2240 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002242 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002244 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247{
2248 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 listitem_T *ni;
2250 listitem_T *ri;
2251 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252
2253 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 cc = *endp;
2258 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002259 if (op != NULL && *op != '=')
2260 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002261 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002262
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002263 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2265 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 {
2267 if (tv_op(&tv, rettv, op) == OK)
2268 set_var(lp->ll_name, &tv, FALSE);
2269 clear_tv(&tv);
2270 }
2271 }
2272 else
2273 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002277 else if (tv_check_lock(lp->ll_newkey == NULL
2278 ? lp->ll_tv->v_lock
2279 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2280 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 else if (lp->ll_range)
2282 {
2283 /*
2284 * Assign the List values to the list items.
2285 */
2286 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002287 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 if (op != NULL && *op != '=')
2289 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2290 else
2291 {
2292 clear_tv(&lp->ll_li->li_tv);
2293 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 ri = ri->li_next;
2296 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2297 break;
2298 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 /* Need to add an empty item. */
2301 ni = listitem_alloc();
2302 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 ri = NULL;
2305 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002306 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002308 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 ni->li_tv.vval.v_number = 0;
2310 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002311 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 lp->ll_li = lp->ll_li->li_next;
2313 ++lp->ll_n1;
2314 }
2315 if (ri != NULL)
2316 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002317 else if (lp->ll_empty2
2318 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 : lp->ll_n1 != lp->ll_n2)
2320 EMSG(_("E711: List value has not enough items"));
2321 }
2322 else
2323 {
2324 /*
2325 * Assign to a List or Dictionary item.
2326 */
2327 if (lp->ll_newkey != NULL)
2328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 if (op != NULL && *op != '=')
2330 {
2331 EMSG2(_(e_letwrong), op);
2332 return;
2333 }
2334
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002336 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (di == NULL)
2338 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002339 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2340 {
2341 vim_free(di);
2342 return;
2343 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 else if (op != NULL && *op != '=')
2347 {
2348 tv_op(lp->ll_tv, rettv, op);
2349 return;
2350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 /*
2355 * Assign the value to the variable or list item.
2356 */
2357 if (copy)
2358 copy_tv(rettv, lp->ll_tv);
2359 else
2360 {
2361 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002362 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
2365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366}
2367
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2370 * Returns OK or FAIL.
2371 */
2372 static int
2373tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002374 typval_T *tv1;
2375 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 char_u *op;
2377{
2378 long n;
2379 char_u numbuf[NUMBUFLEN];
2380 char_u *s;
2381
2382 /* Can't do anything with a Funcref or a Dict on the right. */
2383 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2384 {
2385 switch (tv1->v_type)
2386 {
2387 case VAR_DICT:
2388 case VAR_FUNC:
2389 break;
2390
2391 case VAR_LIST:
2392 if (*op != '+' || tv2->v_type != VAR_LIST)
2393 break;
2394 /* List += List */
2395 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2396 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2397 return OK;
2398
2399 case VAR_NUMBER:
2400 case VAR_STRING:
2401 if (tv2->v_type == VAR_LIST)
2402 break;
2403 if (*op == '+' || *op == '-')
2404 {
2405 /* nr += nr or nr -= nr*/
2406 n = get_tv_number(tv1);
2407 if (*op == '+')
2408 n += get_tv_number(tv2);
2409 else
2410 n -= get_tv_number(tv2);
2411 clear_tv(tv1);
2412 tv1->v_type = VAR_NUMBER;
2413 tv1->vval.v_number = n;
2414 }
2415 else
2416 {
2417 /* str .= str */
2418 s = get_tv_string(tv1);
2419 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2420 clear_tv(tv1);
2421 tv1->v_type = VAR_STRING;
2422 tv1->vval.v_string = s;
2423 }
2424 return OK;
2425 }
2426 }
2427
2428 EMSG2(_(e_letwrong), op);
2429 return FAIL;
2430}
2431
2432/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 * Add a watcher to a list.
2434 */
2435 static void
2436list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002437 list_T *l;
2438 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439{
2440 lw->lw_next = l->lv_watch;
2441 l->lv_watch = lw;
2442}
2443
2444/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002445 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002446 * No warning when it isn't found...
2447 */
2448 static void
2449list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002450 list_T *l;
2451 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002452{
Bram Moolenaar33570922005-01-25 22:26:29 +00002453 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454
2455 lwp = &l->lv_watch;
2456 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2457 {
2458 if (lw == lwrem)
2459 {
2460 *lwp = lw->lw_next;
2461 break;
2462 }
2463 lwp = &lw->lw_next;
2464 }
2465}
2466
2467/*
2468 * Just before removing an item from a list: advance watchers to the next
2469 * item.
2470 */
2471 static void
2472list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 list_T *l;
2474 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002475{
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477
2478 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2479 if (lw->lw_item == item)
2480 lw->lw_item = item->li_next;
2481}
2482
2483/*
2484 * Evaluate the expression used in a ":for var in expr" command.
2485 * "arg" points to "var".
2486 * Set "*errp" to TRUE for an error, FALSE otherwise;
2487 * Return a pointer that holds the info. Null when there is an error.
2488 */
2489 void *
2490eval_for_line(arg, errp, nextcmdp, skip)
2491 char_u *arg;
2492 int *errp;
2493 char_u **nextcmdp;
2494 int skip;
2495{
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002497 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002498 typval_T tv;
2499 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002500
2501 *errp = TRUE; /* default: there is an error */
2502
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002504 if (fi == NULL)
2505 return NULL;
2506
2507 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2508 if (expr == NULL)
2509 return fi;
2510
2511 expr = skipwhite(expr);
2512 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2513 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002514 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002515 return fi;
2516 }
2517
2518 if (skip)
2519 ++emsg_skip;
2520 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2521 {
2522 *errp = FALSE;
2523 if (!skip)
2524 {
2525 l = tv.vval.v_list;
2526 if (tv.v_type != VAR_LIST || l == NULL)
2527 EMSG(_(e_listreq));
2528 else
2529 {
2530 fi->fi_list = l;
2531 list_add_watch(l, &fi->fi_lw);
2532 fi->fi_lw.lw_item = l->lv_first;
2533 }
2534 }
2535 }
2536 if (skip)
2537 --emsg_skip;
2538
2539 return fi;
2540}
2541
2542/*
2543 * Use the first item in a ":for" list. Advance to the next.
2544 * Assign the values to the variable (list). "arg" points to the first one.
2545 * Return TRUE when a valid item was found, FALSE when at end of list or
2546 * something wrong.
2547 */
2548 int
2549next_for_item(fi_void, arg)
2550 void *fi_void;
2551 char_u *arg;
2552{
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002554 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002556
2557 item = fi->fi_lw.lw_item;
2558 if (item == NULL)
2559 result = FALSE;
2560 else
2561 {
2562 fi->fi_lw.lw_item = item->li_next;
2563 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2564 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2565 }
2566 return result;
2567}
2568
2569/*
2570 * Free the structure used to store info used by ":for".
2571 */
2572 void
2573free_for_info(fi_void)
2574 void *fi_void;
2575{
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002577
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002578 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002579 list_rem_watch(fi->fi_list, &fi->fi_lw);
2580 vim_free(fi);
2581}
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2584
2585 void
2586set_context_for_expression(xp, arg, cmdidx)
2587 expand_T *xp;
2588 char_u *arg;
2589 cmdidx_T cmdidx;
2590{
2591 int got_eq = FALSE;
2592 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002593 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002595 if (cmdidx == CMD_let)
2596 {
2597 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002598 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002599 {
2600 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002601 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002602 {
2603 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002605 if (vim_iswhite(*p))
2606 break;
2607 }
2608 return;
2609 }
2610 }
2611 else
2612 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2613 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 while ((xp->xp_pattern = vim_strpbrk(arg,
2615 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2616 {
2617 c = *xp->xp_pattern;
2618 if (c == '&')
2619 {
2620 c = xp->xp_pattern[1];
2621 if (c == '&')
2622 {
2623 ++xp->xp_pattern;
2624 xp->xp_context = cmdidx != CMD_let || got_eq
2625 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2626 }
2627 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002630 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2631 xp->xp_pattern += 2;
2632
2633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635 else if (c == '$')
2636 {
2637 /* environment variable */
2638 xp->xp_context = EXPAND_ENV_VARS;
2639 }
2640 else if (c == '=')
2641 {
2642 got_eq = TRUE;
2643 xp->xp_context = EXPAND_EXPRESSION;
2644 }
2645 else if (c == '<'
2646 && xp->xp_context == EXPAND_FUNCTIONS
2647 && vim_strchr(xp->xp_pattern, '(') == NULL)
2648 {
2649 /* Function name can start with "<SNR>" */
2650 break;
2651 }
2652 else if (cmdidx != CMD_let || got_eq)
2653 {
2654 if (c == '"') /* string */
2655 {
2656 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2657 if (c == '\\' && xp->xp_pattern[1] != NUL)
2658 ++xp->xp_pattern;
2659 xp->xp_context = EXPAND_NOTHING;
2660 }
2661 else if (c == '\'') /* literal string */
2662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2665 /* skip */ ;
2666 xp->xp_context = EXPAND_NOTHING;
2667 }
2668 else if (c == '|')
2669 {
2670 if (xp->xp_pattern[1] == '|')
2671 {
2672 ++xp->xp_pattern;
2673 xp->xp_context = EXPAND_EXPRESSION;
2674 }
2675 else
2676 xp->xp_context = EXPAND_COMMANDS;
2677 }
2678 else
2679 xp->xp_context = EXPAND_EXPRESSION;
2680 }
2681 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002682 /* Doesn't look like something valid, expand as an expression
2683 * anyway. */
2684 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 arg = xp->xp_pattern;
2686 if (*arg != NUL)
2687 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2688 /* skip */ ;
2689 }
2690 xp->xp_pattern = arg;
2691}
2692
2693#endif /* FEAT_CMDL_COMPL */
2694
2695/*
2696 * ":1,25call func(arg1, arg2)" function call.
2697 */
2698 void
2699ex_call(eap)
2700 exarg_T *eap;
2701{
2702 char_u *arg = eap->arg;
2703 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002707 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 linenr_T lnum;
2709 int doesrange;
2710 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002711 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002713 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2714 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 if (tofree == NULL)
2716 return;
2717
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002718 /* Increase refcount on dictionary, it could get deleted when evaluating
2719 * the arguments. */
2720 if (fudi.fd_dict != NULL)
2721 ++fudi.fd_dict->dv_refcount;
2722
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2724 len = STRLEN(tofree);
2725 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaar532c7802005-01-27 14:44:31 +00002727 /* Skip white space to allow ":call func ()". Not good, but required for
2728 * backward compatibility. */
2729 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002730 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 if (*startarg != '(')
2733 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002734 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 goto end;
2736 }
2737
2738 /*
2739 * When skipping, evaluate the function once, to find the end of the
2740 * arguments.
2741 * When the function takes a range, this is discovered after the first
2742 * call, and the loop is broken.
2743 */
2744 if (eap->skip)
2745 {
2746 ++emsg_skip;
2747 lnum = eap->line2; /* do it once, also with an invalid range */
2748 }
2749 else
2750 lnum = eap->line1;
2751 for ( ; lnum <= eap->line2; ++lnum)
2752 {
2753 if (!eap->skip && eap->addr_count > 0)
2754 {
2755 curwin->w_cursor.lnum = lnum;
2756 curwin->w_cursor.col = 0;
2757 }
2758 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002760 eap->line1, eap->line2, &doesrange,
2761 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 failed = TRUE;
2764 break;
2765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002766 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (doesrange || eap->skip)
2768 break;
2769 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002770 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002771 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002772 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if (aborting())
2774 break;
2775 }
2776 if (eap->skip)
2777 --emsg_skip;
2778
2779 if (!failed)
2780 {
2781 /* Check for trailing illegal characters and a following command. */
2782 if (!ends_excmd(*arg))
2783 {
2784 emsg_severe = TRUE;
2785 EMSG(_(e_trailing));
2786 }
2787 else
2788 eap->nextcmd = check_nextcmd(arg);
2789 }
2790
2791end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002792 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002793 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
2796/*
2797 * ":unlet[!] var1 ... " command.
2798 */
2799 void
2800ex_unlet(eap)
2801 exarg_T *eap;
2802{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002803 ex_unletlock(eap, eap->arg, 0);
2804}
2805
2806/*
2807 * ":lockvar" and ":unlockvar" commands
2808 */
2809 void
2810ex_lockvar(eap)
2811 exarg_T *eap;
2812{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002814 int deep = 2;
2815
2816 if (eap->forceit)
2817 deep = -1;
2818 else if (vim_isdigit(*arg))
2819 {
2820 deep = getdigits(&arg);
2821 arg = skipwhite(arg);
2822 }
2823
2824 ex_unletlock(eap, arg, deep);
2825}
2826
2827/*
2828 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2829 */
2830 static void
2831ex_unletlock(eap, argstart, deep)
2832 exarg_T *eap;
2833 char_u *argstart;
2834 int deep;
2835{
2836 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002839 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 do
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 /* Parse the name and find the end. */
2844 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2845 if (lv.ll_name == NULL)
2846 error = TRUE; /* error but continue parsing */
2847 if (name_end == NULL || (!vim_iswhite(*name_end)
2848 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (name_end != NULL)
2851 {
2852 emsg_severe = TRUE;
2853 EMSG(_(e_trailing));
2854 }
2855 if (!(eap->skip || error))
2856 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 break;
2858 }
2859
2860 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002861 {
2862 if (eap->cmdidx == CMD_unlet)
2863 {
2864 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2865 error = TRUE;
2866 }
2867 else
2868 {
2869 if (do_lock_var(&lv, name_end, deep,
2870 eap->cmdidx == CMD_lockvar) == FAIL)
2871 error = TRUE;
2872 }
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (!eap->skip)
2876 clear_lval(&lv);
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 arg = skipwhite(name_end);
2879 } while (!ends_excmd(*arg));
2880
2881 eap->nextcmd = check_nextcmd(arg);
2882}
2883
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002886 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 int forceit;
2889{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 int ret = OK;
2891 int cc;
2892
2893 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 cc = *name_end;
2896 *name_end = NUL;
2897
2898 /* Normal name or expanded name. */
2899 if (check_changedtick(lp->ll_name))
2900 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002905 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2906 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 else if (lp->ll_range)
2908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910
2911 /* Delete a range of List items. */
2912 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2913 {
2914 li = lp->ll_li->li_next;
2915 listitem_remove(lp->ll_list, lp->ll_li);
2916 lp->ll_li = li;
2917 ++lp->ll_n1;
2918 }
2919 }
2920 else
2921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 /* unlet a List item. */
2924 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002927 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 }
2929
2930 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931}
2932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933/*
2934 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 */
2937 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002938do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 hashtab_T *ht;
2943 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002944 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 ht = find_var_ht(name, &varname);
2947 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002949 hi = hash_find(ht, varname);
2950 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002951 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 if (var_check_ro(HI2DI(hi)->di_flags, name))
2953 return FAIL;
2954 delete_var(ht, hi);
2955 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002958 if (forceit)
2959 return OK;
2960 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 return FAIL;
2962}
2963
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964/*
2965 * Lock or unlock variable indicated by "lp".
2966 * "deep" is the levels to go (-1 for unlimited);
2967 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2968 */
2969 static int
2970do_lock_var(lp, name_end, deep, lock)
2971 lval_T *lp;
2972 char_u *name_end;
2973 int deep;
2974 int lock;
2975{
2976 int ret = OK;
2977 int cc;
2978 dictitem_T *di;
2979
2980 if (deep == 0) /* nothing to do */
2981 return OK;
2982
2983 if (lp->ll_tv == NULL)
2984 {
2985 cc = *name_end;
2986 *name_end = NUL;
2987
2988 /* Normal name or expanded name. */
2989 if (check_changedtick(lp->ll_name))
2990 ret = FAIL;
2991 else
2992 {
2993 di = find_var(lp->ll_name, NULL);
2994 if (di == NULL)
2995 ret = FAIL;
2996 else
2997 {
2998 if (lock)
2999 di->di_flags |= DI_FLAGS_LOCK;
3000 else
3001 di->di_flags &= ~DI_FLAGS_LOCK;
3002 item_lock(&di->di_tv, deep, lock);
3003 }
3004 }
3005 *name_end = cc;
3006 }
3007 else if (lp->ll_range)
3008 {
3009 listitem_T *li = lp->ll_li;
3010
3011 /* (un)lock a range of List items. */
3012 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3013 {
3014 item_lock(&li->li_tv, deep, lock);
3015 li = li->li_next;
3016 ++lp->ll_n1;
3017 }
3018 }
3019 else if (lp->ll_list != NULL)
3020 /* (un)lock a List item. */
3021 item_lock(&lp->ll_li->li_tv, deep, lock);
3022 else
3023 /* un(lock) a Dictionary item. */
3024 item_lock(&lp->ll_di->di_tv, deep, lock);
3025
3026 return ret;
3027}
3028
3029/*
3030 * Lock or unlock an item. "deep" is nr of levels to go.
3031 */
3032 static void
3033item_lock(tv, deep, lock)
3034 typval_T *tv;
3035 int deep;
3036 int lock;
3037{
3038 static int recurse = 0;
3039 list_T *l;
3040 listitem_T *li;
3041 dict_T *d;
3042 hashitem_T *hi;
3043 int todo;
3044
3045 if (recurse >= DICT_MAXNEST)
3046 {
3047 EMSG(_("E743: variable nested too deep for (un)lock"));
3048 return;
3049 }
3050 if (deep == 0)
3051 return;
3052 ++recurse;
3053
3054 /* lock/unlock the item itself */
3055 if (lock)
3056 tv->v_lock |= VAR_LOCKED;
3057 else
3058 tv->v_lock &= ~VAR_LOCKED;
3059
3060 switch (tv->v_type)
3061 {
3062 case VAR_LIST:
3063 if ((l = tv->vval.v_list) != NULL)
3064 {
3065 if (lock)
3066 l->lv_lock |= VAR_LOCKED;
3067 else
3068 l->lv_lock &= ~VAR_LOCKED;
3069 if (deep < 0 || deep > 1)
3070 /* recursive: lock/unlock the items the List contains */
3071 for (li = l->lv_first; li != NULL; li = li->li_next)
3072 item_lock(&li->li_tv, deep - 1, lock);
3073 }
3074 break;
3075 case VAR_DICT:
3076 if ((d = tv->vval.v_dict) != NULL)
3077 {
3078 if (lock)
3079 d->dv_lock |= VAR_LOCKED;
3080 else
3081 d->dv_lock &= ~VAR_LOCKED;
3082 if (deep < 0 || deep > 1)
3083 {
3084 /* recursive: lock/unlock the items the List contains */
3085 todo = d->dv_hashtab.ht_used;
3086 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3087 {
3088 if (!HASHITEM_EMPTY(hi))
3089 {
3090 --todo;
3091 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3092 }
3093 }
3094 }
3095 }
3096 }
3097 --recurse;
3098}
3099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3101/*
3102 * Delete all "menutrans_" variables.
3103 */
3104 void
3105del_menutrans_vars()
3106{
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003108 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 hash_lock(&globvarht);
3111 todo = globvarht.ht_used;
3112 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003113 {
3114 if (!HASHITEM_EMPTY(hi))
3115 {
3116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3118 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003119 }
3120 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
3123#endif
3124
3125#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3126
3127/*
3128 * Local string buffer for the next two functions to store a variable name
3129 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3130 * get_user_var_name().
3131 */
3132
3133static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3134
3135static char_u *varnamebuf = NULL;
3136static int varnamebuflen = 0;
3137
3138/*
3139 * Function to concatenate a prefix and a variable name.
3140 */
3141 static char_u *
3142cat_prefix_varname(prefix, name)
3143 int prefix;
3144 char_u *name;
3145{
3146 int len;
3147
3148 len = (int)STRLEN(name) + 3;
3149 if (len > varnamebuflen)
3150 {
3151 vim_free(varnamebuf);
3152 len += 10; /* some additional space */
3153 varnamebuf = alloc(len);
3154 if (varnamebuf == NULL)
3155 {
3156 varnamebuflen = 0;
3157 return NULL;
3158 }
3159 varnamebuflen = len;
3160 }
3161 *varnamebuf = prefix;
3162 varnamebuf[1] = ':';
3163 STRCPY(varnamebuf + 2, name);
3164 return varnamebuf;
3165}
3166
3167/*
3168 * Function given to ExpandGeneric() to obtain the list of user defined
3169 * (global/buffer/window/built-in) variable names.
3170 */
3171/*ARGSUSED*/
3172 char_u *
3173get_user_var_name(xp, idx)
3174 expand_T *xp;
3175 int idx;
3176{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003177 static long_u gdone;
3178 static long_u bdone;
3179 static long_u wdone;
3180 static int vidx;
3181 static hashitem_T *hi;
3182 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003185 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003186
3187 /* Global variables */
3188 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003190 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003192 else
3193 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003194 while (HASHITEM_EMPTY(hi))
3195 ++hi;
3196 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3197 return cat_prefix_varname('g', hi->hi_key);
3198 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003200
3201 /* b: variables */
3202 ht = &curbuf->b_vars.dv_hashtab;
3203 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003205 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003206 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003207 else
3208 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003209 while (HASHITEM_EMPTY(hi))
3210 ++hi;
3211 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003215 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 return (char_u *)"b:changedtick";
3217 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003218
3219 /* w: variables */
3220 ht = &curwin->w_vars.dv_hashtab;
3221 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003223 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003225 else
3226 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003227 while (HASHITEM_EMPTY(hi))
3228 ++hi;
3229 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003231
3232 /* v: variables */
3233 if (vidx < VV_LEN)
3234 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 vim_free(varnamebuf);
3237 varnamebuf = NULL;
3238 varnamebuflen = 0;
3239 return NULL;
3240}
3241
3242#endif /* FEAT_CMDL_COMPL */
3243
3244/*
3245 * types for expressions.
3246 */
3247typedef enum
3248{
3249 TYPE_UNKNOWN = 0
3250 , TYPE_EQUAL /* == */
3251 , TYPE_NEQUAL /* != */
3252 , TYPE_GREATER /* > */
3253 , TYPE_GEQUAL /* >= */
3254 , TYPE_SMALLER /* < */
3255 , TYPE_SEQUAL /* <= */
3256 , TYPE_MATCH /* =~ */
3257 , TYPE_NOMATCH /* !~ */
3258} exptype_T;
3259
3260/*
3261 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003262 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3264 */
3265
3266/*
3267 * Handle zero level expression.
3268 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 * Return OK or FAIL.
3271 */
3272 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 char_u **nextcmd;
3277 int evaluate;
3278{
3279 int ret;
3280 char_u *p;
3281
3282 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (ret == FAIL || !ends_excmd(*p))
3285 {
3286 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 /*
3289 * Report the invalid expression unless the expression evaluation has
3290 * been cancelled due to an aborting error, an interrupt, or an
3291 * exception.
3292 */
3293 if (!aborting())
3294 EMSG2(_(e_invexpr2), arg);
3295 ret = FAIL;
3296 }
3297 if (nextcmd != NULL)
3298 *nextcmd = check_nextcmd(p);
3299
3300 return ret;
3301}
3302
3303/*
3304 * Handle top level expression:
3305 * expr1 ? expr0 : expr0
3306 *
3307 * "arg" must point to the first non-white of the expression.
3308 * "arg" is advanced to the next non-white after the recognized expression.
3309 *
3310 * Return OK or FAIL.
3311 */
3312 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003313eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 int evaluate;
3317{
3318 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 /*
3322 * Get the first variable.
3323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return FAIL;
3326
3327 if ((*arg)[0] == '?')
3328 {
3329 result = FALSE;
3330 if (evaluate)
3331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336
3337 /*
3338 * Get the second variable.
3339 */
3340 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return FAIL;
3343
3344 /*
3345 * Check for the ":".
3346 */
3347 if ((*arg)[0] != ':')
3348 {
3349 EMSG(_("E109: Missing ':' after '?'"));
3350 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003351 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 return FAIL;
3353 }
3354
3355 /*
3356 * Get the third variable.
3357 */
3358 *arg = skipwhite(*arg + 1);
3359 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3360 {
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 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368
3369 return OK;
3370}
3371
3372/*
3373 * Handle first level expression:
3374 * expr2 || expr2 || expr2 logical OR
3375 *
3376 * "arg" must point to the first non-white of the expression.
3377 * "arg" is advanced to the next non-white after the recognized expression.
3378 *
3379 * Return OK or FAIL.
3380 */
3381 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 int evaluate;
3386{
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 long result;
3389 int first;
3390
3391 /*
3392 * Get the first variable.
3393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return FAIL;
3396
3397 /*
3398 * Repeat until there is no following "||".
3399 */
3400 first = TRUE;
3401 result = FALSE;
3402 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3403 {
3404 if (evaluate && first)
3405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 first = FALSE;
3410 }
3411
3412 /*
3413 * Get the second variable.
3414 */
3415 *arg = skipwhite(*arg + 2);
3416 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3417 return FAIL;
3418
3419 /*
3420 * Compute the result.
3421 */
3422 if (evaluate && !result)
3423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003424 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 if (evaluate)
3429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 rettv->v_type = VAR_NUMBER;
3431 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433 }
3434
3435 return OK;
3436}
3437
3438/*
3439 * Handle second level expression:
3440 * expr3 && expr3 && expr3 logical AND
3441 *
3442 * "arg" must point to the first non-white of the expression.
3443 * "arg" is advanced to the next non-white after the recognized expression.
3444 *
3445 * Return OK or FAIL.
3446 */
3447 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003448eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int evaluate;
3452{
Bram Moolenaar33570922005-01-25 22:26:29 +00003453 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 long result;
3455 int first;
3456
3457 /*
3458 * Get the first variable.
3459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003460 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return FAIL;
3462
3463 /*
3464 * Repeat until there is no following "&&".
3465 */
3466 first = TRUE;
3467 result = TRUE;
3468 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3469 {
3470 if (evaluate && first)
3471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003472 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003474 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 first = FALSE;
3476 }
3477
3478 /*
3479 * Get the second variable.
3480 */
3481 *arg = skipwhite(*arg + 2);
3482 if (eval4(arg, &var2, evaluate && result) == FAIL)
3483 return FAIL;
3484
3485 /*
3486 * Compute the result.
3487 */
3488 if (evaluate && result)
3489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 if (evaluate)
3495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 rettv->v_type = VAR_NUMBER;
3497 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 }
3500
3501 return OK;
3502}
3503
3504/*
3505 * Handle third level expression:
3506 * var1 == var2
3507 * var1 =~ var2
3508 * var1 != var2
3509 * var1 !~ var2
3510 * var1 > var2
3511 * var1 >= var2
3512 * var1 < var2
3513 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003514 * var1 is var2
3515 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 *
3517 * "arg" must point to the first non-white of the expression.
3518 * "arg" is advanced to the next non-white after the recognized expression.
3519 *
3520 * Return OK or FAIL.
3521 */
3522 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 int evaluate;
3527{
Bram Moolenaar33570922005-01-25 22:26:29 +00003528 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 char_u *p;
3530 int i;
3531 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003532 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int len = 2;
3534 long n1, n2;
3535 char_u *s1, *s2;
3536 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3537 regmatch_T regmatch;
3538 int ic;
3539 char_u *save_cpo;
3540
3541 /*
3542 * Get the first variable.
3543 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return FAIL;
3546
3547 p = *arg;
3548 switch (p[0])
3549 {
3550 case '=': if (p[1] == '=')
3551 type = TYPE_EQUAL;
3552 else if (p[1] == '~')
3553 type = TYPE_MATCH;
3554 break;
3555 case '!': if (p[1] == '=')
3556 type = TYPE_NEQUAL;
3557 else if (p[1] == '~')
3558 type = TYPE_NOMATCH;
3559 break;
3560 case '>': if (p[1] != '=')
3561 {
3562 type = TYPE_GREATER;
3563 len = 1;
3564 }
3565 else
3566 type = TYPE_GEQUAL;
3567 break;
3568 case '<': if (p[1] != '=')
3569 {
3570 type = TYPE_SMALLER;
3571 len = 1;
3572 }
3573 else
3574 type = TYPE_SEQUAL;
3575 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003576 case 'i': if (p[1] == 's')
3577 {
3578 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3579 len = 5;
3580 if (!vim_isIDc(p[len]))
3581 {
3582 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3583 type_is = TRUE;
3584 }
3585 }
3586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588
3589 /*
3590 * If there is a comparitive operator, use it.
3591 */
3592 if (type != TYPE_UNKNOWN)
3593 {
3594 /* extra question mark appended: ignore case */
3595 if (p[len] == '?')
3596 {
3597 ic = TRUE;
3598 ++len;
3599 }
3600 /* extra '#' appended: match case */
3601 else if (p[len] == '#')
3602 {
3603 ic = FALSE;
3604 ++len;
3605 }
3606 /* nothing appened: use 'ignorecase' */
3607 else
3608 ic = p_ic;
3609
3610 /*
3611 * Get the second variable.
3612 */
3613 *arg = skipwhite(p + len);
3614 if (eval5(arg, &var2, evaluate) == FAIL)
3615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return FAIL;
3618 }
3619
3620 if (evaluate)
3621 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003622 if (type_is && rettv->v_type != var2.v_type)
3623 {
3624 /* For "is" a different type always means FALSE, for "notis"
3625 * it means TRUE. */
3626 n1 = (type == TYPE_NEQUAL);
3627 }
3628 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3629 {
3630 if (type_is)
3631 {
3632 n1 = (rettv->v_type == var2.v_type
3633 && rettv->vval.v_list == var2.vval.v_list);
3634 if (type == TYPE_NEQUAL)
3635 n1 = !n1;
3636 }
3637 else if (rettv->v_type != var2.v_type
3638 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3639 {
3640 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003641 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003642 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003643 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003644 clear_tv(rettv);
3645 clear_tv(&var2);
3646 return FAIL;
3647 }
3648 else
3649 {
3650 /* Compare two Lists for being equal or unequal. */
3651 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3652 if (type == TYPE_NEQUAL)
3653 n1 = !n1;
3654 }
3655 }
3656
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003657 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3658 {
3659 if (type_is)
3660 {
3661 n1 = (rettv->v_type == var2.v_type
3662 && rettv->vval.v_dict == var2.vval.v_dict);
3663 if (type == TYPE_NEQUAL)
3664 n1 = !n1;
3665 }
3666 else if (rettv->v_type != var2.v_type
3667 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3668 {
3669 if (rettv->v_type != var2.v_type)
3670 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3671 else
3672 EMSG(_("E736: Invalid operation for Dictionary"));
3673 clear_tv(rettv);
3674 clear_tv(&var2);
3675 return FAIL;
3676 }
3677 else
3678 {
3679 /* Compare two Dictionaries for being equal or unequal. */
3680 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3681 if (type == TYPE_NEQUAL)
3682 n1 = !n1;
3683 }
3684 }
3685
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003686 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3687 {
3688 if (rettv->v_type != var2.v_type
3689 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3690 {
3691 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003692 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003693 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003694 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003695 clear_tv(rettv);
3696 clear_tv(&var2);
3697 return FAIL;
3698 }
3699 else
3700 {
3701 /* Compare two Funcrefs for being equal or unequal. */
3702 if (rettv->vval.v_string == NULL
3703 || var2.vval.v_string == NULL)
3704 n1 = FALSE;
3705 else
3706 n1 = STRCMP(rettv->vval.v_string,
3707 var2.vval.v_string) == 0;
3708 if (type == TYPE_NEQUAL)
3709 n1 = !n1;
3710 }
3711 }
3712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 /*
3714 * If one of the two variables is a number, compare as a number.
3715 * When using "=~" or "!~", always compare as string.
3716 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003717 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 n1 = get_tv_number(rettv);
3721 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 switch (type)
3723 {
3724 case TYPE_EQUAL: n1 = (n1 == n2); break;
3725 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3726 case TYPE_GREATER: n1 = (n1 > n2); break;
3727 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3728 case TYPE_SMALLER: n1 = (n1 < n2); break;
3729 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3730 case TYPE_UNKNOWN:
3731 case TYPE_MATCH:
3732 case TYPE_NOMATCH: break; /* avoid gcc warning */
3733 }
3734 }
3735 else
3736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 s1 = get_tv_string_buf(rettv, buf1);
3738 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3740 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3741 else
3742 i = 0;
3743 n1 = FALSE;
3744 switch (type)
3745 {
3746 case TYPE_EQUAL: n1 = (i == 0); break;
3747 case TYPE_NEQUAL: n1 = (i != 0); break;
3748 case TYPE_GREATER: n1 = (i > 0); break;
3749 case TYPE_GEQUAL: n1 = (i >= 0); break;
3750 case TYPE_SMALLER: n1 = (i < 0); break;
3751 case TYPE_SEQUAL: n1 = (i <= 0); break;
3752
3753 case TYPE_MATCH:
3754 case TYPE_NOMATCH:
3755 /* avoid 'l' flag in 'cpoptions' */
3756 save_cpo = p_cpo;
3757 p_cpo = (char_u *)"";
3758 regmatch.regprog = vim_regcomp(s2,
3759 RE_MAGIC + RE_STRING);
3760 regmatch.rm_ic = ic;
3761 if (regmatch.regprog != NULL)
3762 {
3763 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3764 vim_free(regmatch.regprog);
3765 if (type == TYPE_NOMATCH)
3766 n1 = !n1;
3767 }
3768 p_cpo = save_cpo;
3769 break;
3770
3771 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3772 }
3773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774 clear_tv(rettv);
3775 clear_tv(&var2);
3776 rettv->v_type = VAR_NUMBER;
3777 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 }
3780
3781 return OK;
3782}
3783
3784/*
3785 * Handle fourth level expression:
3786 * + number addition
3787 * - number subtraction
3788 * . string concatenation
3789 *
3790 * "arg" must point to the first non-white of the expression.
3791 * "arg" is advanced to the next non-white after the recognized expression.
3792 *
3793 * Return OK or FAIL.
3794 */
3795 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 int evaluate;
3800{
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 typval_T var2;
3802 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 int op;
3804 long n1, n2;
3805 char_u *s1, *s2;
3806 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3807 char_u *p;
3808
3809 /*
3810 * Get the first variable.
3811 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 return FAIL;
3814
3815 /*
3816 * Repeat computing, until no '+', '-' or '.' is following.
3817 */
3818 for (;;)
3819 {
3820 op = **arg;
3821 if (op != '+' && op != '-' && op != '.')
3822 break;
3823
3824 /*
3825 * Get the second variable.
3826 */
3827 *arg = skipwhite(*arg + 1);
3828 if (eval6(arg, &var2, evaluate) == FAIL)
3829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return FAIL;
3832 }
3833
3834 if (evaluate)
3835 {
3836 /*
3837 * Compute the result.
3838 */
3839 if (op == '.')
3840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 s1 = get_tv_string_buf(rettv, buf1);
3842 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003843 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003844 clear_tv(rettv);
3845 rettv->v_type = VAR_STRING;
3846 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003848 else if (op == '+' && rettv->v_type == VAR_LIST
3849 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003850 {
3851 /* concatenate Lists */
3852 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3853 &var3) == FAIL)
3854 {
3855 clear_tv(rettv);
3856 clear_tv(&var2);
3857 return FAIL;
3858 }
3859 clear_tv(rettv);
3860 *rettv = var3;
3861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 else
3863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 n1 = get_tv_number(rettv);
3865 n2 = get_tv_number(&var2);
3866 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (op == '+')
3868 n1 = n1 + n2;
3869 else
3870 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871 rettv->v_type = VAR_NUMBER;
3872 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003874 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 }
3877 return OK;
3878}
3879
3880/*
3881 * Handle fifth level expression:
3882 * * number multiplication
3883 * / number division
3884 * % number modulo
3885 *
3886 * "arg" must point to the first non-white of the expression.
3887 * "arg" is advanced to the next non-white after the recognized expression.
3888 *
3889 * Return OK or FAIL.
3890 */
3891 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003892eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 int evaluate;
3896{
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 int op;
3899 long n1, n2;
3900
3901 /*
3902 * Get the first variable.
3903 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003904 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 return FAIL;
3906
3907 /*
3908 * Repeat computing, until no '*', '/' or '%' is following.
3909 */
3910 for (;;)
3911 {
3912 op = **arg;
3913 if (op != '*' && op != '/' && op != '%')
3914 break;
3915
3916 if (evaluate)
3917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 n1 = get_tv_number(rettv);
3919 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 else
3922 n1 = 0;
3923
3924 /*
3925 * Get the second variable.
3926 */
3927 *arg = skipwhite(*arg + 1);
3928 if (eval7(arg, &var2, evaluate) == FAIL)
3929 return FAIL;
3930
3931 if (evaluate)
3932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 n2 = get_tv_number(&var2);
3934 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 /*
3937 * Compute the result.
3938 */
3939 if (op == '*')
3940 n1 = n1 * n2;
3941 else if (op == '/')
3942 {
3943 if (n2 == 0) /* give an error message? */
3944 n1 = 0x7fffffffL;
3945 else
3946 n1 = n1 / n2;
3947 }
3948 else
3949 {
3950 if (n2 == 0) /* give an error message? */
3951 n1 = 0;
3952 else
3953 n1 = n1 % n2;
3954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 rettv->v_type = VAR_NUMBER;
3956 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958 }
3959
3960 return OK;
3961}
3962
3963/*
3964 * Handle sixth level expression:
3965 * number number constant
3966 * "string" string contstant
3967 * 'string' literal string contstant
3968 * &option-name option value
3969 * @r register contents
3970 * identifier variable value
3971 * function() function call
3972 * $VAR environment variable
3973 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003974 * [expr, expr] List
3975 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 *
3977 * Also handle:
3978 * ! in front logical NOT
3979 * - in front unary minus
3980 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003981 * trailing [] subscript in String or List
3982 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *
3984 * "arg" must point to the first non-white of the expression.
3985 * "arg" is advanced to the next non-white after the recognized expression.
3986 *
3987 * Return OK or FAIL.
3988 */
3989 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 int evaluate;
3994{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 long n;
3996 int len;
3997 char_u *s;
3998 int val;
3999 char_u *start_leader, *end_leader;
4000 int ret = OK;
4001 char_u *alias;
4002
4003 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004005 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 /*
4010 * Skip '!' and '-' characters. They are handled later.
4011 */
4012 start_leader = *arg;
4013 while (**arg == '!' || **arg == '-' || **arg == '+')
4014 *arg = skipwhite(*arg + 1);
4015 end_leader = *arg;
4016
4017 switch (**arg)
4018 {
4019 /*
4020 * Number constant.
4021 */
4022 case '0':
4023 case '1':
4024 case '2':
4025 case '3':
4026 case '4':
4027 case '5':
4028 case '6':
4029 case '7':
4030 case '8':
4031 case '9':
4032 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4033 *arg += len;
4034 if (evaluate)
4035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 rettv->v_type = VAR_NUMBER;
4037 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 break;
4040
4041 /*
4042 * String constant: "string".
4043 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 break;
4046
4047 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004048 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004050 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004051 break;
4052
4053 /*
4054 * List: [expr, expr]
4055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 break;
4058
4059 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 * Dictionary: {key: val, key: val}
4061 */
4062 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4063 break;
4064
4065 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004066 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004068 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
4070
4071 /*
4072 * Environment variable: $VAR.
4073 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 break;
4076
4077 /*
4078 * Register contents: @r.
4079 */
4080 case '@': ++*arg;
4081 if (evaluate)
4082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 rettv->v_type = VAR_STRING;
4084 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 if (**arg != NUL)
4087 ++*arg;
4088 break;
4089
4090 /*
4091 * nested expression: (expression).
4092 */
4093 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (**arg == ')')
4096 ++*arg;
4097 else if (ret == OK)
4098 {
4099 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 ret = FAIL;
4102 }
4103 break;
4104
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 break;
4107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004108
4109 if (ret == NOTDONE)
4110 {
4111 /*
4112 * Must be a variable or function name.
4113 * Can also be a curly-braces kind of name: {expr}.
4114 */
4115 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004116 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 if (alias != NULL)
4118 s = alias;
4119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004120 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 ret = FAIL;
4122 else
4123 {
4124 if (**arg == '(') /* recursive! */
4125 {
4126 /* If "s" is the name of a variable of type VAR_FUNC
4127 * use its contents. */
4128 s = deref_func_name(s, &len);
4129
4130 /* Invoke the function. */
4131 ret = get_func_tv(s, len, rettv, arg,
4132 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004133 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004134 /* Stop the expression evaluation when immediately
4135 * aborting on error, or when an interrupt occurred or
4136 * an exception was thrown but not caught. */
4137 if (aborting())
4138 {
4139 if (ret == OK)
4140 clear_tv(rettv);
4141 ret = FAIL;
4142 }
4143 }
4144 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004145 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004146 else
4147 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004148 }
4149
4150 if (alias != NULL)
4151 vim_free(alias);
4152 }
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *arg = skipwhite(*arg);
4155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004156 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4157 * expr(expr). */
4158 if (ret == OK)
4159 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 /*
4162 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4163 */
4164 if (ret == OK && evaluate && end_leader > start_leader)
4165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 while (end_leader > start_leader)
4168 {
4169 --end_leader;
4170 if (*end_leader == '!')
4171 val = !val;
4172 else if (*end_leader == '-')
4173 val = -val;
4174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(rettv);
4176 rettv->v_type = VAR_NUMBER;
4177 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179
4180 return ret;
4181}
4182
4183/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004184 * Evaluate an "[expr]" or "[expr:expr]" index.
4185 * "*arg" points to the '['.
4186 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4187 */
4188 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004189eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004190 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004191 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004192 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004193 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004194{
4195 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004196 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004197 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004198 long len = -1;
4199 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004200 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004201 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004205 if (verbose)
4206 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004207 return FAIL;
4208 }
4209
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004212 /*
4213 * dict.name
4214 */
4215 key = *arg + 1;
4216 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4217 ;
4218 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004219 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004220 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 }
4222 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004224 /*
4225 * something[idx]
4226 *
4227 * Get the (first) variable from inside the [].
4228 */
4229 *arg = skipwhite(*arg + 1);
4230 if (**arg == ':')
4231 empty1 = TRUE;
4232 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4233 return FAIL;
4234
4235 /*
4236 * Get the second variable from inside the [:].
4237 */
4238 if (**arg == ':')
4239 {
4240 range = TRUE;
4241 *arg = skipwhite(*arg + 1);
4242 if (**arg == ']')
4243 empty2 = TRUE;
4244 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4245 {
4246 clear_tv(&var1);
4247 return FAIL;
4248 }
4249 }
4250
4251 /* Check for the ']'. */
4252 if (**arg != ']')
4253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004254 if (verbose)
4255 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004256 clear_tv(&var1);
4257 if (range)
4258 clear_tv(&var2);
4259 return FAIL;
4260 }
4261 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004262 }
4263
4264 if (evaluate)
4265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004266 n1 = 0;
4267 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 n1 = get_tv_number(&var1);
4270 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271 }
4272 if (range)
4273 {
4274 if (empty2)
4275 n2 = -1;
4276 else
4277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 n2 = get_tv_number(&var2);
4279 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 }
4281 }
4282
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {
4285 case VAR_NUMBER:
4286 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004288 len = (long)STRLEN(s);
4289 if (range)
4290 {
4291 /* The resulting variable is a substring. If the indexes
4292 * are out of range the result is empty. */
4293 if (n1 < 0)
4294 {
4295 n1 = len + n1;
4296 if (n1 < 0)
4297 n1 = 0;
4298 }
4299 if (n2 < 0)
4300 n2 = len + n2;
4301 else if (n2 >= len)
4302 n2 = len;
4303 if (n1 >= len || n2 < 0 || n1 > n2)
4304 s = NULL;
4305 else
4306 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4307 }
4308 else
4309 {
4310 /* The resulting variable is a string of a single
4311 * character. If the index is too big or negative the
4312 * result is empty. */
4313 if (n1 >= len || n1 < 0)
4314 s = NULL;
4315 else
4316 s = vim_strnsave(s + n1, 1);
4317 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
4319 rettv->v_type = VAR_STRING;
4320 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004321 break;
4322
4323 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004325 if (n1 < 0)
4326 n1 = len + n1;
4327 if (!empty1 && (n1 < 0 || n1 >= len))
4328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004329 if (verbose)
4330 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004331 return FAIL;
4332 }
4333 if (range)
4334 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004335 list_T *l;
4336 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004337
4338 if (n2 < 0)
4339 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004340 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004342 if (verbose)
4343 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004344 return FAIL;
4345 }
4346 l = list_alloc();
4347 if (l == NULL)
4348 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004350 n1 <= n2; ++n1)
4351 {
4352 if (list_append_tv(l, &item->li_tv) == FAIL)
4353 {
4354 list_free(l);
4355 return FAIL;
4356 }
4357 item = item->li_next;
4358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(rettv);
4360 rettv->v_type = VAR_LIST;
4361 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004362 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004363 }
4364 else
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004367 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 clear_tv(rettv);
4369 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004370 }
4371 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004372
4373 case VAR_DICT:
4374 if (range)
4375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004376 if (verbose)
4377 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004378 if (len == -1)
4379 clear_tv(&var1);
4380 return FAIL;
4381 }
4382 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384
4385 if (len == -1)
4386 {
4387 key = get_tv_string(&var1);
4388 if (*key == NUL)
4389 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004390 if (verbose)
4391 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004392 clear_tv(&var1);
4393 return FAIL;
4394 }
4395 }
4396
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004397 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004398
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004399 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004400 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004401 if (len == -1)
4402 clear_tv(&var1);
4403 if (item == NULL)
4404 return FAIL;
4405
4406 copy_tv(&item->di_tv, &var1);
4407 clear_tv(rettv);
4408 *rettv = var1;
4409 }
4410 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004411 }
4412 }
4413
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414 return OK;
4415}
4416
4417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 * Get an option value.
4419 * "arg" points to the '&' or '+' before the option name.
4420 * "arg" is advanced to character after the option name.
4421 * Return OK or FAIL.
4422 */
4423 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004426 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 int evaluate;
4428{
4429 char_u *option_end;
4430 long numval;
4431 char_u *stringval;
4432 int opt_type;
4433 int c;
4434 int working = (**arg == '+'); /* has("+option") */
4435 int ret = OK;
4436 int opt_flags;
4437
4438 /*
4439 * Isolate the option name and find its value.
4440 */
4441 option_end = find_option_end(arg, &opt_flags);
4442 if (option_end == NULL)
4443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 EMSG2(_("E112: Option name missing: %s"), *arg);
4446 return FAIL;
4447 }
4448
4449 if (!evaluate)
4450 {
4451 *arg = option_end;
4452 return OK;
4453 }
4454
4455 c = *option_end;
4456 *option_end = NUL;
4457 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 if (opt_type == -3) /* invalid name */
4461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 EMSG2(_("E113: Unknown option: %s"), *arg);
4464 ret = FAIL;
4465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004466 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 {
4468 if (opt_type == -2) /* hidden string option */
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_STRING;
4471 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 else if (opt_type == -1) /* hidden number option */
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 rettv->v_type = VAR_NUMBER;
4476 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else if (opt_type == 1) /* number option */
4479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 rettv->v_type = VAR_NUMBER;
4481 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 else /* string option */
4484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 rettv->v_type = VAR_STRING;
4486 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 }
4489 else if (working && (opt_type == -2 || opt_type == -1))
4490 ret = FAIL;
4491
4492 *option_end = c; /* put back for error messages */
4493 *arg = option_end;
4494
4495 return ret;
4496}
4497
4498/*
4499 * Allocate a variable for a string constant.
4500 * Return OK or FAIL.
4501 */
4502 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 int evaluate;
4507{
4508 char_u *p;
4509 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 int extra = 0;
4511
4512 /*
4513 * Find the end of the string, skipping backslashed characters.
4514 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004515 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 if (*p == '\\' && p[1] != NUL)
4518 {
4519 ++p;
4520 /* A "\<x>" form occupies at least 4 characters, and produces up
4521 * to 6 characters: reserve space for 2 extra */
4522 if (*p == '<')
4523 extra += 2;
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
4527 if (*p != '"')
4528 {
4529 EMSG2(_("E114: Missing quote: %s"), *arg);
4530 return FAIL;
4531 }
4532
4533 /* If only parsing, set *arg and return here */
4534 if (!evaluate)
4535 {
4536 *arg = p + 1;
4537 return OK;
4538 }
4539
4540 /*
4541 * Copy the string into allocated memory, handling backslashed
4542 * characters.
4543 */
4544 name = alloc((unsigned)(p - *arg + extra));
4545 if (name == NULL)
4546 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 rettv->v_type = VAR_STRING;
4548 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 if (*p == '\\')
4553 {
4554 switch (*++p)
4555 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 case 'b': *name++ = BS; ++p; break;
4557 case 'e': *name++ = ESC; ++p; break;
4558 case 'f': *name++ = FF; ++p; break;
4559 case 'n': *name++ = NL; ++p; break;
4560 case 'r': *name++ = CAR; ++p; break;
4561 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 case 'X': /* hex: "\x1", "\x12" */
4564 case 'x':
4565 case 'u': /* Unicode: "\u0023" */
4566 case 'U':
4567 if (vim_isxdigit(p[1]))
4568 {
4569 int n, nr;
4570 int c = toupper(*p);
4571
4572 if (c == 'X')
4573 n = 2;
4574 else
4575 n = 4;
4576 nr = 0;
4577 while (--n >= 0 && vim_isxdigit(p[1]))
4578 {
4579 ++p;
4580 nr = (nr << 4) + hex2nr(*p);
4581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583#ifdef FEAT_MBYTE
4584 /* For "\u" store the number according to
4585 * 'encoding'. */
4586 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 else
4589#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004590 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593
4594 /* octal: "\1", "\12", "\123" */
4595 case '0':
4596 case '1':
4597 case '2':
4598 case '3':
4599 case '4':
4600 case '5':
4601 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 case '7': *name = *p++ - '0';
4603 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 *name = (*name << 3) + *p++ - '0';
4606 if (*p >= '0' && *p <= '7')
4607 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 break;
4611
4612 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (extra != 0)
4615 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618 }
4619 /* FALLTHROUGH */
4620
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623 }
4624 }
4625 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 *arg = p + 1;
4631
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 return OK;
4633}
4634
4635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 * Return OK or FAIL.
4638 */
4639 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 int evaluate;
4644{
4645 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004646 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004647 int reduce = 0;
4648
4649 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004650 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004651 */
4652 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004654 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004655 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004657 break;
4658 ++reduce;
4659 ++p;
4660 }
4661 }
4662
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004666 return FAIL;
4667 }
4668
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004670 if (!evaluate)
4671 {
4672 *arg = p + 1;
4673 return OK;
4674 }
4675
4676 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004678 */
4679 str = alloc((unsigned)((p - *arg) - reduce));
4680 if (str == NULL)
4681 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 rettv->v_type = VAR_STRING;
4683 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004684
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004690 break;
4691 ++p;
4692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004695 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004696 *arg = p + 1;
4697
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004698 return OK;
4699}
4700
4701/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 * Allocate a variable for a List and fill it from "*arg".
4703 * Return OK or FAIL.
4704 */
4705 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004706get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 int evaluate;
4710{
Bram Moolenaar33570922005-01-25 22:26:29 +00004711 list_T *l = NULL;
4712 typval_T tv;
4713 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714
4715 if (evaluate)
4716 {
4717 l = list_alloc();
4718 if (l == NULL)
4719 return FAIL;
4720 }
4721
4722 *arg = skipwhite(*arg + 1);
4723 while (**arg != ']' && **arg != NUL)
4724 {
4725 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4726 goto failret;
4727 if (evaluate)
4728 {
4729 item = listitem_alloc();
4730 if (item != NULL)
4731 {
4732 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004733 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 list_append(l, item);
4735 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004736 else
4737 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 }
4739
4740 if (**arg == ']')
4741 break;
4742 if (**arg != ',')
4743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 goto failret;
4746 }
4747 *arg = skipwhite(*arg + 1);
4748 }
4749
4750 if (**arg != ']')
4751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753failret:
4754 if (evaluate)
4755 list_free(l);
4756 return FAIL;
4757 }
4758
4759 *arg = skipwhite(*arg + 1);
4760 if (evaluate)
4761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 rettv->v_type = VAR_LIST;
4763 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 ++l->lv_refcount;
4765 }
4766
4767 return OK;
4768}
4769
4770/*
4771 * Allocate an empty header for a list.
4772 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004773 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774list_alloc()
4775{
Bram Moolenaar33570922005-01-25 22:26:29 +00004776 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777}
4778
4779/*
4780 * Unreference a list: decrement the reference count and free it when it
4781 * becomes zero.
4782 */
4783 static void
4784list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786{
4787 if (l != NULL && --l->lv_refcount <= 0)
4788 list_free(l);
4789}
4790
4791/*
4792 * Free a list, including all items it points to.
4793 * Ignores the reference count.
4794 */
4795 static void
4796list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004797 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798{
Bram Moolenaar33570922005-01-25 22:26:29 +00004799 listitem_T *item;
4800 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801
4802 for (item = l->lv_first; item != NULL; item = next)
4803 {
4804 next = item->li_next;
4805 listitem_free(item);
4806 }
4807 vim_free(l);
4808}
4809
4810/*
4811 * Allocate a list item.
4812 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004813 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814listitem_alloc()
4815{
Bram Moolenaar33570922005-01-25 22:26:29 +00004816 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817}
4818
4819/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004820 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 */
4822 static void
4823listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004824 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 vim_free(item);
4828}
4829
4830/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004831 * Remove a list item from a List and free it. Also clears the value.
4832 */
4833 static void
4834listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004835 list_T *l;
4836 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004837{
4838 list_remove(l, item, item);
4839 listitem_free(item);
4840}
4841
4842/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 * Get the number of items in a list.
4844 */
4845 static long
4846list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004847 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 if (l == NULL)
4850 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004851 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852}
4853
4854/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004855 * Return TRUE when two lists have exactly the same values.
4856 */
4857 static int
4858list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004859 list_T *l1;
4860 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004861 int ic; /* ignore case for strings */
4862{
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004864
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004865 if (list_len(l1) != list_len(l2))
4866 return FALSE;
4867
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004868 for (item1 = l1->lv_first, item2 = l2->lv_first;
4869 item1 != NULL && item2 != NULL;
4870 item1 = item1->li_next, item2 = item2->li_next)
4871 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4872 return FALSE;
4873 return item1 == NULL && item2 == NULL;
4874}
4875
4876/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004877 * Return TRUE when two dictionaries have exactly the same key/values.
4878 */
4879 static int
4880dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004881 dict_T *d1;
4882 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004883 int ic; /* ignore case for strings */
4884{
Bram Moolenaar33570922005-01-25 22:26:29 +00004885 hashitem_T *hi;
4886 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004887 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004888
4889 if (dict_len(d1) != dict_len(d2))
4890 return FALSE;
4891
Bram Moolenaar33570922005-01-25 22:26:29 +00004892 todo = d1->dv_hashtab.ht_used;
4893 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004894 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004895 if (!HASHITEM_EMPTY(hi))
4896 {
4897 item2 = dict_find(d2, hi->hi_key, -1);
4898 if (item2 == NULL)
4899 return FALSE;
4900 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4901 return FALSE;
4902 --todo;
4903 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004904 }
4905 return TRUE;
4906}
4907
4908/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004909 * Return TRUE if "tv1" and "tv2" have the same value.
4910 * Compares the items just like "==" would compare them.
4911 */
4912 static int
4913tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004914 typval_T *tv1;
4915 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004916 int ic; /* ignore case */
4917{
4918 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4919
4920 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4921 {
4922 /* recursive! */
4923 if (tv1->v_type != tv2->v_type
4924 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4925 return FALSE;
4926 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004927 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4928 {
4929 /* recursive! */
4930 if (tv1->v_type != tv2->v_type
4931 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4932 return FALSE;
4933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004934 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4935 {
4936 if (tv1->v_type != tv2->v_type
4937 || tv1->vval.v_string == NULL
4938 || tv2->vval.v_string == NULL
4939 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4940 return FALSE;
4941 }
4942 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4943 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004944 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4945 * Don't consider "4x" to be equal to 4. */
4946 if ((tv1->v_type == VAR_STRING
4947 && !string_isa_number(tv1->vval.v_string))
4948 || (tv2->v_type == VAR_STRING
4949 && !string_isa_number(tv2->vval.v_string)))
4950 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004951 if (get_tv_number(tv1) != get_tv_number(tv2))
4952 return FALSE;
4953 }
4954 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4955 get_tv_string_buf(tv2, buf2)) != 0)
4956 return FALSE;
4957 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4958 get_tv_string_buf(tv2, buf2)) != 0)
4959 return FALSE;
4960 return TRUE;
4961}
4962
4963/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004964 * Return TRUE if "tv" is a number without other non-white characters.
4965 */
4966 static int
4967string_isa_number(s)
4968 char_u *s;
4969{
4970 int len;
4971
4972 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4973 return len > 0 && *skipwhite(s + len) == NUL;
4974}
4975
4976/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 * Locate item with index "n" in list "l" and return it.
4978 * A negative index is counted from the end; -1 is the last item.
4979 * Returns NULL when "n" is out of range.
4980 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 long n;
4985{
Bram Moolenaar33570922005-01-25 22:26:29 +00004986 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 long idx;
4988
4989 if (l == NULL)
4990 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004991
4992 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004994 n = l->lv_len + n;
4995
4996 /* Check for index out of range. */
4997 if (n < 0 || n >= l->lv_len)
4998 return NULL;
4999
5000 /* When there is a cached index may start search from there. */
5001 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005003 if (n < l->lv_idx / 2)
5004 {
5005 /* closest to the start of the list */
5006 item = l->lv_first;
5007 idx = 0;
5008 }
5009 else if (n > (l->lv_idx + l->lv_len) / 2)
5010 {
5011 /* closest to the end of the list */
5012 item = l->lv_last;
5013 idx = l->lv_len - 1;
5014 }
5015 else
5016 {
5017 /* closest to the cached index */
5018 item = l->lv_idx_item;
5019 idx = l->lv_idx;
5020 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005021 }
5022 else
5023 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005024 if (n < l->lv_len / 2)
5025 {
5026 /* closest to the start of the list */
5027 item = l->lv_first;
5028 idx = 0;
5029 }
5030 else
5031 {
5032 /* closest to the end of the list */
5033 item = l->lv_last;
5034 idx = l->lv_len - 1;
5035 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005037
5038 while (n > idx)
5039 {
5040 /* search forward */
5041 item = item->li_next;
5042 ++idx;
5043 }
5044 while (n < idx)
5045 {
5046 /* search backward */
5047 item = item->li_prev;
5048 --idx;
5049 }
5050
5051 /* cache the used index */
5052 l->lv_idx = idx;
5053 l->lv_idx_item = item;
5054
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 return item;
5056}
5057
5058/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005059 * Locate "item" list "l" and return its index.
5060 * Returns -1 when "item" is not in the list.
5061 */
5062 static long
5063list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005064 list_T *l;
5065 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005066{
5067 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005068 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005069
5070 if (l == NULL)
5071 return -1;
5072 idx = 0;
5073 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5074 ++idx;
5075 if (li == NULL)
5076 return -1;
5077 return idx;;
5078}
5079
5080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 * Append item "item" to the end of list "l".
5082 */
5083 static void
5084list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005085 list_T *l;
5086 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087{
5088 if (l->lv_last == NULL)
5089 {
5090 /* empty list */
5091 l->lv_first = item;
5092 l->lv_last = item;
5093 item->li_prev = NULL;
5094 }
5095 else
5096 {
5097 l->lv_last->li_next = item;
5098 item->li_prev = l->lv_last;
5099 l->lv_last = item;
5100 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005101 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102 item->li_next = NULL;
5103}
5104
5105/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005106 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005107 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 */
5109 static int
5110list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005111 list_T *l;
5112 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005114 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115
Bram Moolenaar05159a02005-02-26 23:04:13 +00005116 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005118 copy_tv(tv, &li->li_tv);
5119 list_append(l, li);
5120 return OK;
5121}
5122
5123/*
5124 * Add a dictionary to a list. Used by errorlist().
5125 * Return FAIL when out of memory.
5126 */
5127 int
5128list_append_dict(list, dict)
5129 list_T *list;
5130 dict_T *dict;
5131{
5132 listitem_T *li = listitem_alloc();
5133
5134 if (li == NULL)
5135 return FAIL;
5136 li->li_tv.v_type = VAR_DICT;
5137 li->li_tv.v_lock = 0;
5138 li->li_tv.vval.v_dict = dict;
5139 list_append(list, li);
5140 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 return OK;
5142}
5143
5144/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005146 * If "item" is NULL append at the end.
5147 * Return FAIL when out of memory.
5148 */
5149 static int
5150list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005151 list_T *l;
5152 typval_T *tv;
5153 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005154{
Bram Moolenaar33570922005-01-25 22:26:29 +00005155 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005156
5157 if (ni == NULL)
5158 return FAIL;
5159 copy_tv(tv, &ni->li_tv);
5160 if (item == NULL)
5161 /* Append new item at end of list. */
5162 list_append(l, ni);
5163 else
5164 {
5165 /* Insert new item before existing item. */
5166 ni->li_prev = item->li_prev;
5167 ni->li_next = item;
5168 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005169 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005170 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005171 ++l->lv_idx;
5172 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005173 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005174 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005175 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005176 l->lv_idx_item = NULL;
5177 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005178 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005179 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005180 }
5181 return OK;
5182}
5183
5184/*
5185 * Extend "l1" with "l2".
5186 * If "bef" is NULL append at the end, otherwise insert before this item.
5187 * Returns FAIL when out of memory.
5188 */
5189 static int
5190list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005191 list_T *l1;
5192 list_T *l2;
5193 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005194{
Bram Moolenaar33570922005-01-25 22:26:29 +00005195 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005196
5197 for (item = l2->lv_first; item != NULL; item = item->li_next)
5198 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5199 return FAIL;
5200 return OK;
5201}
5202
5203/*
5204 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5205 * Return FAIL when out of memory.
5206 */
5207 static int
5208list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005209 list_T *l1;
5210 list_T *l2;
5211 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005212{
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005214
5215 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005216 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005217 if (l == NULL)
5218 return FAIL;
5219 tv->v_type = VAR_LIST;
5220 tv->vval.v_list = l;
5221
5222 /* append all items from the second list */
5223 return list_extend(l, l2, NULL);
5224}
5225
5226/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005227 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005229 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 * Returns NULL when out of memory.
5231 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005232 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005233list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005234 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005236 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237{
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 list_T *copy;
5239 listitem_T *item;
5240 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241
5242 if (orig == NULL)
5243 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244
5245 copy = list_alloc();
5246 if (copy != NULL)
5247 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005248 if (copyID != 0)
5249 {
5250 /* Do this before adding the items, because one of the items may
5251 * refer back to this list. */
5252 orig->lv_copyID = copyID;
5253 orig->lv_copylist = copy;
5254 }
5255 for (item = orig->lv_first; item != NULL && !got_int;
5256 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 {
5258 ni = listitem_alloc();
5259 if (ni == NULL)
5260 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005261 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005262 {
5263 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5264 {
5265 vim_free(ni);
5266 break;
5267 }
5268 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 list_append(copy, ni);
5272 }
5273 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005274 if (item != NULL)
5275 {
5276 list_unref(copy);
5277 copy = NULL;
5278 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 }
5280
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 return copy;
5282}
5283
5284/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005285 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005286 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005288 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005289list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 list_T *l;
5291 listitem_T *item;
5292 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005293{
Bram Moolenaar33570922005-01-25 22:26:29 +00005294 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005296 /* notify watchers */
5297 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005299 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 list_fix_watch(l, ip);
5301 if (ip == item2)
5302 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005304
5305 if (item2->li_next == NULL)
5306 l->lv_last = item->li_prev;
5307 else
5308 item2->li_next->li_prev = item->li_prev;
5309 if (item->li_prev == NULL)
5310 l->lv_first = item2->li_next;
5311 else
5312 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005313 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314}
5315
5316/*
5317 * Return an allocated string with the string representation of a list.
5318 * May return NULL.
5319 */
5320 static char_u *
5321list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005322 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323{
5324 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325
5326 if (tv->vval.v_list == NULL)
5327 return NULL;
5328 ga_init2(&ga, (int)sizeof(char), 80);
5329 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005330 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5331 {
5332 vim_free(ga.ga_data);
5333 return NULL;
5334 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 ga_append(&ga, ']');
5336 ga_append(&ga, NUL);
5337 return (char_u *)ga.ga_data;
5338}
5339
5340/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005341 * Join list "l" into a string in "*gap", using separator "sep".
5342 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005343 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005344 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005345 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005346list_join(gap, l, sep, echo)
5347 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005349 char_u *sep;
5350 int echo;
5351{
5352 int first = TRUE;
5353 char_u *tofree;
5354 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005356 char_u *s;
5357
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005358 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005359 {
5360 if (first)
5361 first = FALSE;
5362 else
5363 ga_concat(gap, sep);
5364
5365 if (echo)
5366 s = echo_string(&item->li_tv, &tofree, numbuf);
5367 else
5368 s = tv2string(&item->li_tv, &tofree, numbuf);
5369 if (s != NULL)
5370 ga_concat(gap, s);
5371 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005372 if (s == NULL)
5373 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005374 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005375 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005376}
5377
5378/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 * Allocate an empty header for a dictionary.
5380 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005381 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382dict_alloc()
5383{
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005385
Bram Moolenaar33570922005-01-25 22:26:29 +00005386 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005387 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005388 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005390 d->dv_lock = 0;
5391 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005392 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005393 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005394 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395}
5396
5397/*
5398 * Unreference a Dictionary: decrement the reference count and free it when it
5399 * becomes zero.
5400 */
5401 static void
5402dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005403 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404{
5405 if (d != NULL && --d->dv_refcount <= 0)
5406 dict_free(d);
5407}
5408
5409/*
5410 * Free a Dictionary, including all items it contains.
5411 * Ignores the reference count.
5412 */
5413 static void
5414dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005417 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005420 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 * hashtab. Must not try to resize the hashtab! */
5422 todo = d->dv_hashtab.ht_used;
5423 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005425 if (!HASHITEM_EMPTY(hi))
5426 {
5427 dictitem_free(HI2DI(hi));
5428 --todo;
5429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 vim_free(d);
5433}
5434
5435/*
5436 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005437 * The "key" is copied to the new item.
5438 * Note that the value of the item "di_tv" still needs to be initialized!
5439 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005440 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005442dictitem_alloc(key)
5443 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444{
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005446
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005448 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005450 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005451 di->di_flags = 0;
5452 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005453 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454}
5455
5456/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005457 * Make a copy of a Dictionary item.
5458 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005460dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005461 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005462{
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005464
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005466 if (di != NULL)
5467 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005468 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005469 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005470 copy_tv(&org->di_tv, &di->di_tv);
5471 }
5472 return di;
5473}
5474
5475/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005476 * Remove item "item" from Dictionary "dict" and free it.
5477 */
5478 static void
5479dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 dict_T *dict;
5481 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005482{
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005484
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005486 if (HASHITEM_EMPTY(hi))
5487 EMSG2(_(e_intern2), "dictitem_remove()");
5488 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005489 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005490 dictitem_free(item);
5491}
5492
5493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 * Free a dict item. Also clears the value.
5495 */
5496 static void
5497dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500 clear_tv(&item->di_tv);
5501 vim_free(item);
5502}
5503
5504/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005505 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5506 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005507 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005508 * Returns NULL when out of memory.
5509 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005511dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005513 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005514 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005515{
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 dict_T *copy;
5517 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005518 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520
5521 if (orig == NULL)
5522 return NULL;
5523
5524 copy = dict_alloc();
5525 if (copy != NULL)
5526 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005527 if (copyID != 0)
5528 {
5529 orig->dv_copyID = copyID;
5530 orig->dv_copydict = copy;
5531 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005533 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005534 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005535 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005536 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005537 --todo;
5538
5539 di = dictitem_alloc(hi->hi_key);
5540 if (di == NULL)
5541 break;
5542 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005543 {
5544 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5545 copyID) == FAIL)
5546 {
5547 vim_free(di);
5548 break;
5549 }
5550 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 else
5552 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5553 if (dict_add(copy, di) == FAIL)
5554 {
5555 dictitem_free(di);
5556 break;
5557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005560
Bram Moolenaare9a41262005-01-15 22:18:47 +00005561 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005562 if (todo > 0)
5563 {
5564 dict_unref(copy);
5565 copy = NULL;
5566 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005567 }
5568
5569 return copy;
5570}
5571
5572/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005574 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005576 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 dict_T *d;
5579 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580{
Bram Moolenaar33570922005-01-25 22:26:29 +00005581 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582}
5583
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00005585 * Add a number or string entry to dictionary "d".
5586 * When "str" is NULL use number "nr", otherwise use "str".
5587 * Returns FAIL when out of memory and when key already exists.
5588 */
5589 int
5590dict_add_nr_str(d, key, nr, str)
5591 dict_T *d;
5592 char *key;
5593 long nr;
5594 char_u *str;
5595{
5596 dictitem_T *item;
5597
5598 item = dictitem_alloc((char_u *)key);
5599 if (item == NULL)
5600 return FAIL;
5601 item->di_tv.v_lock = 0;
5602 if (str == NULL)
5603 {
5604 item->di_tv.v_type = VAR_NUMBER;
5605 item->di_tv.vval.v_number = nr;
5606 }
5607 else
5608 {
5609 item->di_tv.v_type = VAR_STRING;
5610 item->di_tv.vval.v_string = vim_strsave(str);
5611 }
5612 if (dict_add(d, item) == FAIL)
5613 {
5614 dictitem_free(item);
5615 return FAIL;
5616 }
5617 return OK;
5618}
5619
5620/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005621 * Get the number of items in a Dictionary.
5622 */
5623 static long
5624dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005626{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005627 if (d == NULL)
5628 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005630}
5631
5632/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 * Find item "key[len]" in Dictionary "d".
5634 * If "len" is negative use strlen(key).
5635 * Returns NULL when not found.
5636 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005637 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005638dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 char_u *key;
5641 int len;
5642{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005643#define AKEYLEN 200
5644 char_u buf[AKEYLEN];
5645 char_u *akey;
5646 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005649 if (len < 0)
5650 akey = key;
5651 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005653 tofree = akey = vim_strnsave(key, len);
5654 if (akey == NULL)
5655 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005656 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 else
5658 {
5659 /* Avoid a malloc/free by using buf[]. */
5660 STRNCPY(buf, key, len);
5661 buf[len] = NUL;
5662 akey = buf;
5663 }
5664
Bram Moolenaar33570922005-01-25 22:26:29 +00005665 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005666 vim_free(tofree);
5667 if (HASHITEM_EMPTY(hi))
5668 return NULL;
5669 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670}
5671
5672/*
5673 * Return an allocated string with the string representation of a Dictionary.
5674 * May return NULL.
5675 */
5676 static char_u *
5677dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005678 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679{
5680 garray_T ga;
5681 int first = TRUE;
5682 char_u *tofree;
5683 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005684 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005686 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005687 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005689 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 return NULL;
5691 ga_init2(&ga, (int)sizeof(char), 80);
5692 ga_append(&ga, '{');
5693
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005695 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005697 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005699 --todo;
5700
5701 if (first)
5702 first = FALSE;
5703 else
5704 ga_concat(&ga, (char_u *)", ");
5705
5706 tofree = string_quote(hi->hi_key, FALSE);
5707 if (tofree != NULL)
5708 {
5709 ga_concat(&ga, tofree);
5710 vim_free(tofree);
5711 }
5712 ga_concat(&ga, (char_u *)": ");
5713 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5714 if (s != NULL)
5715 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005717 if (s == NULL)
5718 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005721 if (todo > 0)
5722 {
5723 vim_free(ga.ga_data);
5724 return NULL;
5725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726
5727 ga_append(&ga, '}');
5728 ga_append(&ga, NUL);
5729 return (char_u *)ga.ga_data;
5730}
5731
5732/*
5733 * Allocate a variable for a Dictionary and fill it from "*arg".
5734 * Return OK or FAIL. Returns NOTDONE for {expr}.
5735 */
5736 static int
5737get_dict_tv(arg, rettv, evaluate)
5738 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 int evaluate;
5741{
Bram Moolenaar33570922005-01-25 22:26:29 +00005742 dict_T *d = NULL;
5743 typval_T tvkey;
5744 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005746 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005748 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749
5750 /*
5751 * First check if it's not a curly-braces thing: {expr}.
5752 * Must do this without evaluating, otherwise a function may be called
5753 * twice. Unfortunately this means we need to call eval1() twice for the
5754 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005755 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005757 if (*start != '}')
5758 {
5759 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5760 return FAIL;
5761 if (*start == '}')
5762 return NOTDONE;
5763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764
5765 if (evaluate)
5766 {
5767 d = dict_alloc();
5768 if (d == NULL)
5769 return FAIL;
5770 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005771 tvkey.v_type = VAR_UNKNOWN;
5772 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773
5774 *arg = skipwhite(*arg + 1);
5775 while (**arg != '}' && **arg != NUL)
5776 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005777 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 goto failret;
5779 if (**arg != ':')
5780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005781 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005782 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 goto failret;
5784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005785 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 if (*key == NUL)
5787 {
5788 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005789 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 goto failret;
5791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792
5793 *arg = skipwhite(*arg + 1);
5794 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005796 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 goto failret;
5798 }
5799 if (evaluate)
5800 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005801 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 if (item != NULL)
5803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005804 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005805 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 clear_tv(&tv);
5807 goto failret;
5808 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005809 item = dictitem_alloc(key);
5810 clear_tv(&tvkey);
5811 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005814 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005815 if (dict_add(d, item) == FAIL)
5816 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 }
5818 }
5819
5820 if (**arg == '}')
5821 break;
5822 if (**arg != ',')
5823 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005824 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 goto failret;
5826 }
5827 *arg = skipwhite(*arg + 1);
5828 }
5829
5830 if (**arg != '}')
5831 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005832 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833failret:
5834 if (evaluate)
5835 dict_free(d);
5836 return FAIL;
5837 }
5838
5839 *arg = skipwhite(*arg + 1);
5840 if (evaluate)
5841 {
5842 rettv->v_type = VAR_DICT;
5843 rettv->vval.v_dict = d;
5844 ++d->dv_refcount;
5845 }
5846
5847 return OK;
5848}
5849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 * Return a string with the string representation of a variable.
5852 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005853 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 * May return NULL;
5856 */
5857 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863 static int recurse = 0;
5864 char_u *r = NULL;
5865
Bram Moolenaar33570922005-01-25 22:26:29 +00005866 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005867 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005868 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005869 *tofree = NULL;
5870 return NULL;
5871 }
5872 ++recurse;
5873
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 switch (tv->v_type)
5875 {
5876 case VAR_FUNC:
5877 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005878 r = tv->vval.v_string;
5879 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 case VAR_LIST:
5881 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005882 r = *tofree;
5883 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 case VAR_DICT:
5885 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005886 r = *tofree;
5887 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005888 case VAR_STRING:
5889 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890 *tofree = NULL;
5891 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005893 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005895 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897
5898 --recurse;
5899 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900}
5901
5902/*
5903 * Return a string with the string representation of a variable.
5904 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5905 * "numbuf" is used for a number.
5906 * Puts quotes around strings, so that they can be parsed back by eval().
5907 * May return NULL;
5908 */
5909 static char_u *
5910tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 char_u **tofree;
5913 char_u *numbuf;
5914{
5915 switch (tv->v_type)
5916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 case VAR_FUNC:
5918 *tofree = string_quote(tv->vval.v_string, TRUE);
5919 return *tofree;
5920 case VAR_STRING:
5921 *tofree = string_quote(tv->vval.v_string, FALSE);
5922 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005923 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005926 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005928 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005930 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931}
5932
5933/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 * Return string "str" in ' quotes, doubling ' characters.
5935 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 */
5938 static char_u *
5939string_quote(str, function)
5940 char_u *str;
5941 int function;
5942{
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 char_u *p, *r, *s;
5945
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 len = (function ? 13 : 3);
5947 if (str != NULL)
5948 {
5949 len += STRLEN(str);
5950 for (p = str; *p != NUL; mb_ptr_adv(p))
5951 if (*p == '\'')
5952 ++len;
5953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 s = r = alloc(len);
5955 if (r != NULL)
5956 {
5957 if (function)
5958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960 r += 10;
5961 }
5962 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005963 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 if (str != NULL)
5965 for (p = str; *p != NUL; )
5966 {
5967 if (*p == '\'')
5968 *r++ = '\'';
5969 MB_COPY_CHAR(p, r);
5970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 if (function)
5973 *r++ = ')';
5974 *r++ = NUL;
5975 }
5976 return s;
5977}
5978
5979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 * Get the value of an environment variable.
5981 * "arg" is pointing to the '$'. It is advanced to after the name.
5982 * If the environment variable was not set, silently assume it is empty.
5983 * Always return OK.
5984 */
5985 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005986get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 int evaluate;
5990{
5991 char_u *string = NULL;
5992 int len;
5993 int cc;
5994 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005995 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
5997 ++*arg;
5998 name = *arg;
5999 len = get_env_len(arg);
6000 if (evaluate)
6001 {
6002 if (len != 0)
6003 {
6004 cc = name[len];
6005 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006006 /* first try vim_getenv(), fast for normal environment vars */
6007 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006009 {
6010 if (!mustfree)
6011 string = vim_strsave(string);
6012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 else
6014 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006015 if (mustfree)
6016 vim_free(string);
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 /* next try expanding things like $VIM and ${HOME} */
6019 string = expand_env_save(name - 1);
6020 if (string != NULL && *string == '$')
6021 {
6022 vim_free(string);
6023 string = NULL;
6024 }
6025 }
6026 name[len] = cc;
6027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006028 rettv->v_type = VAR_STRING;
6029 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
6031
6032 return OK;
6033}
6034
6035/*
6036 * Array with names and number of arguments of all internal functions
6037 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6038 */
6039static struct fst
6040{
6041 char *f_name; /* function name */
6042 char f_min_argc; /* minimal number of arguments */
6043 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046} functions[] =
6047{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006048 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 {"append", 2, 2, f_append},
6050 {"argc", 0, 0, f_argc},
6051 {"argidx", 0, 0, f_argidx},
6052 {"argv", 1, 1, f_argv},
6053 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006054 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {"bufexists", 1, 1, f_bufexists},
6056 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6057 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6058 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6059 {"buflisted", 1, 1, f_buflisted},
6060 {"bufloaded", 1, 1, f_bufloaded},
6061 {"bufname", 1, 1, f_bufname},
6062 {"bufnr", 1, 1, f_bufnr},
6063 {"bufwinnr", 1, 1, f_bufwinnr},
6064 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006065 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006066 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 {"char2nr", 1, 1, f_char2nr},
6068 {"cindent", 1, 1, f_cindent},
6069 {"col", 1, 1, f_col},
6070 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {"cscope_connection",0,3, f_cscope_connection},
6074 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006075 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {"delete", 1, 1, f_delete},
6077 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006078 {"diff_filler", 1, 1, f_diff_filler},
6079 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006080 {"empty", 1, 1, f_empty},
Bram Moolenaar05159a02005-02-26 23:04:13 +00006081 {"errorlist", 0, 0, f_errorlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006083 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {"eventhandler", 0, 0, f_eventhandler},
6085 {"executable", 1, 1, f_executable},
6086 {"exists", 1, 1, f_exists},
6087 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6090 {"filereadable", 1, 1, f_filereadable},
6091 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006092 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006093 {"finddir", 1, 3, f_finddir},
6094 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {"fnamemodify", 2, 2, f_fnamemodify},
6096 {"foldclosed", 1, 1, f_foldclosed},
6097 {"foldclosedend", 1, 1, f_foldclosedend},
6098 {"foldlevel", 1, 1, f_foldlevel},
6099 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006100 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006103 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {"getbufvar", 2, 2, f_getbufvar},
6105 {"getchar", 0, 1, f_getchar},
6106 {"getcharmod", 0, 0, f_getcharmod},
6107 {"getcmdline", 0, 0, f_getcmdline},
6108 {"getcmdpos", 0, 0, f_getcmdpos},
6109 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006110 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006111 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {"getfsize", 1, 1, f_getfsize},
6113 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006114 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006115 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 {"getreg", 0, 1, f_getreg},
6117 {"getregtype", 0, 1, f_getregtype},
6118 {"getwinposx", 0, 0, f_getwinposx},
6119 {"getwinposy", 0, 0, f_getwinposy},
6120 {"getwinvar", 2, 2, f_getwinvar},
6121 {"glob", 1, 1, f_glob},
6122 {"globpath", 2, 2, f_globpath},
6123 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006124 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {"hasmapto", 1, 2, f_hasmapto},
6126 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6127 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6128 {"histadd", 2, 2, f_histadd},
6129 {"histdel", 1, 2, f_histdel},
6130 {"histget", 1, 2, f_histget},
6131 {"histnr", 1, 1, f_histnr},
6132 {"hlID", 1, 1, f_hlID},
6133 {"hlexists", 1, 1, f_hlexists},
6134 {"hostname", 0, 0, f_hostname},
6135 {"iconv", 3, 3, f_iconv},
6136 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006137 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {"input", 1, 2, f_input},
6139 {"inputdialog", 1, 3, f_inputdialog},
6140 {"inputrestore", 0, 0, f_inputrestore},
6141 {"inputsave", 0, 0, f_inputsave},
6142 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006145 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006147 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {"libcall", 3, 3, f_libcall},
6152 {"libcallnr", 3, 3, f_libcallnr},
6153 {"line", 1, 1, f_line},
6154 {"line2byte", 1, 1, f_line2byte},
6155 {"lispindent", 1, 1, f_lispindent},
6156 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006157 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {"maparg", 1, 2, f_maparg},
6159 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006160 {"match", 2, 4, f_match},
6161 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006162 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006163 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006164 {"max", 1, 1, f_max},
6165 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006166#ifdef vim_mkdir
6167 {"mkdir", 1, 3, f_mkdir},
6168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {"mode", 0, 0, f_mode},
6170 {"nextnonblank", 1, 1, f_nextnonblank},
6171 {"nr2char", 1, 1, f_nr2char},
6172 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006174 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {"remote_expr", 2, 3, f_remote_expr},
6176 {"remote_foreground", 1, 1, f_remote_foreground},
6177 {"remote_peek", 1, 2, f_remote_peek},
6178 {"remote_read", 1, 1, f_remote_read},
6179 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006182 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006184 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {"search", 1, 2, f_search},
6186 {"searchpair", 3, 5, f_searchpair},
6187 {"server2client", 2, 2, f_server2client},
6188 {"serverlist", 0, 0, f_serverlist},
6189 {"setbufvar", 3, 3, f_setbufvar},
6190 {"setcmdpos", 1, 1, f_setcmdpos},
6191 {"setline", 2, 2, f_setline},
6192 {"setreg", 2, 3, f_setreg},
6193 {"setwinvar", 3, 3, f_setwinvar},
6194 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006195 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006196 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197#ifdef HAVE_STRFTIME
6198 {"strftime", 1, 2, f_strftime},
6199#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {"strlen", 1, 1, f_strlen},
6203 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006204 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {"strtrans", 1, 1, f_strtrans},
6206 {"submatch", 1, 1, f_submatch},
6207 {"substitute", 4, 4, f_substitute},
6208 {"synID", 3, 3, f_synID},
6209 {"synIDattr", 2, 3, f_synIDattr},
6210 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006211 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006212 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {"tempname", 0, 0, f_tempname},
6214 {"tolower", 1, 1, f_tolower},
6215 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006216 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006218 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {"virtcol", 1, 1, f_virtcol},
6220 {"visualmode", 0, 1, f_visualmode},
6221 {"winbufnr", 1, 1, f_winbufnr},
6222 {"wincol", 0, 0, f_wincol},
6223 {"winheight", 1, 1, f_winheight},
6224 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006225 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {"winrestcmd", 0, 0, f_winrestcmd},
6227 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006228 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229};
6230
6231#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6232
6233/*
6234 * Function given to ExpandGeneric() to obtain the list of internal
6235 * or user defined function names.
6236 */
6237 char_u *
6238get_function_name(xp, idx)
6239 expand_T *xp;
6240 int idx;
6241{
6242 static int intidx = -1;
6243 char_u *name;
6244
6245 if (idx == 0)
6246 intidx = -1;
6247 if (intidx < 0)
6248 {
6249 name = get_user_func_name(xp, idx);
6250 if (name != NULL)
6251 return name;
6252 }
6253 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6254 {
6255 STRCPY(IObuff, functions[intidx].f_name);
6256 STRCAT(IObuff, "(");
6257 if (functions[intidx].f_max_argc == 0)
6258 STRCAT(IObuff, ")");
6259 return IObuff;
6260 }
6261
6262 return NULL;
6263}
6264
6265/*
6266 * Function given to ExpandGeneric() to obtain the list of internal or
6267 * user defined variable or function names.
6268 */
6269/*ARGSUSED*/
6270 char_u *
6271get_expr_name(xp, idx)
6272 expand_T *xp;
6273 int idx;
6274{
6275 static int intidx = -1;
6276 char_u *name;
6277
6278 if (idx == 0)
6279 intidx = -1;
6280 if (intidx < 0)
6281 {
6282 name = get_function_name(xp, idx);
6283 if (name != NULL)
6284 return name;
6285 }
6286 return get_user_var_name(xp, ++intidx);
6287}
6288
6289#endif /* FEAT_CMDL_COMPL */
6290
6291/*
6292 * Find internal function in table above.
6293 * Return index, or -1 if not found
6294 */
6295 static int
6296find_internal_func(name)
6297 char_u *name; /* name of the function */
6298{
6299 int first = 0;
6300 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6301 int cmp;
6302 int x;
6303
6304 /*
6305 * Find the function name in the table. Binary search.
6306 */
6307 while (first <= last)
6308 {
6309 x = first + ((unsigned)(last - first) >> 1);
6310 cmp = STRCMP(name, functions[x].f_name);
6311 if (cmp < 0)
6312 last = x - 1;
6313 else if (cmp > 0)
6314 first = x + 1;
6315 else
6316 return x;
6317 }
6318 return -1;
6319}
6320
6321/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6323 * name it contains, otherwise return "name".
6324 */
6325 static char_u *
6326deref_func_name(name, lenp)
6327 char_u *name;
6328 int *lenp;
6329{
Bram Moolenaar33570922005-01-25 22:26:29 +00006330 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 int cc;
6332
6333 cc = name[*lenp];
6334 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006335 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 {
6341 *lenp = 0;
6342 return (char_u *)""; /* just in case */
6343 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 *lenp = STRLEN(v->di_tv.vval.v_string);
6345 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 }
6347
6348 return name;
6349}
6350
6351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 * Allocate a variable for the result of a function.
6353 * Return OK or FAIL.
6354 */
6355 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6357 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 char_u *name; /* name of the function */
6359 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 char_u **arg; /* argument, pointing to the '(' */
6362 linenr_T firstline; /* first line of range */
6363 linenr_T lastline; /* last line of range */
6364 int *doesrange; /* return: function handled range */
6365 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 char_u *argp;
6369 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 int argcount = 0; /* number of arguments found */
6372
6373 /*
6374 * Get the arguments.
6375 */
6376 argp = *arg;
6377 while (argcount < MAX_FUNC_ARGS)
6378 {
6379 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6380 if (*argp == ')' || *argp == ',' || *argp == NUL)
6381 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6383 {
6384 ret = FAIL;
6385 break;
6386 }
6387 ++argcount;
6388 if (*argp != ',')
6389 break;
6390 }
6391 if (*argp == ')')
6392 ++argp;
6393 else
6394 ret = FAIL;
6395
6396 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006397 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 {
6401 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006402 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006408 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410 *arg = skipwhite(argp);
6411 return ret;
6412}
6413
6414
6415/*
6416 * Call a function with its resolved parameters
6417 * Return OK or FAIL.
6418 */
6419 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006421 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 char_u *name; /* name of the function */
6423 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 linenr_T firstline; /* first line of range */
6428 linenr_T lastline; /* last line of range */
6429 int *doesrange; /* return: function handled range */
6430 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006431 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434#define ERROR_UNKNOWN 0
6435#define ERROR_TOOMANY 1
6436#define ERROR_TOOFEW 2
6437#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438#define ERROR_DICT 4
6439#define ERROR_NONE 5
6440#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 int error = ERROR_NONE;
6442 int i;
6443 int llen;
6444 ufunc_T *fp;
6445 int cc;
6446#define FLEN_FIXED 40
6447 char_u fname_buf[FLEN_FIXED + 1];
6448 char_u *fname;
6449
6450 /*
6451 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6452 * Change <SNR>123_name() to K_SNR 123_name().
6453 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6454 */
6455 cc = name[len];
6456 name[len] = NUL;
6457 llen = eval_fname_script(name);
6458 if (llen > 0)
6459 {
6460 fname_buf[0] = K_SPECIAL;
6461 fname_buf[1] = KS_EXTRA;
6462 fname_buf[2] = (int)KE_SNR;
6463 i = 3;
6464 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6465 {
6466 if (current_SID <= 0)
6467 error = ERROR_SCRIPT;
6468 else
6469 {
6470 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6471 i = (int)STRLEN(fname_buf);
6472 }
6473 }
6474 if (i + STRLEN(name + llen) < FLEN_FIXED)
6475 {
6476 STRCPY(fname_buf + i, name + llen);
6477 fname = fname_buf;
6478 }
6479 else
6480 {
6481 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6482 if (fname == NULL)
6483 error = ERROR_OTHER;
6484 else
6485 {
6486 mch_memmove(fname, fname_buf, (size_t)i);
6487 STRCPY(fname + i, name + llen);
6488 }
6489 }
6490 }
6491 else
6492 fname = name;
6493
6494 *doesrange = FALSE;
6495
6496
6497 /* execute the function if no errors detected and executing */
6498 if (evaluate && error == ERROR_NONE)
6499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006500 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 error = ERROR_UNKNOWN;
6502
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006503 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
6505 /*
6506 * User defined function.
6507 */
6508 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006511 /* Trigger FuncUndefined event, may load the function. */
6512 if (fp == NULL
6513 && apply_autocmds(EVENT_FUNCUNDEFINED,
6514 fname, fname, TRUE, NULL)
6515 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006517 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 fp = find_func(fname);
6519 }
6520#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006521 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006522 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006523 {
6524 /* loaded a package, search for the function again */
6525 fp = find_func(fname);
6526 }
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (fp != NULL)
6529 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006530 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006532 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006534 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006536 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 else
6539 {
6540 /*
6541 * Call the user function.
6542 * Save and restore search patterns, script variables and
6543 * redo buffer.
6544 */
6545 save_search_patterns();
6546 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006547 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006550 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
6551 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
6552 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006553 /* Function was unreferenced while being used, free it
6554 * now. */
6555 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 restoreRedobuff();
6557 restore_search_patterns();
6558 error = ERROR_NONE;
6559 }
6560 }
6561 }
6562 else
6563 {
6564 /*
6565 * Find the function name in the table, call its implementation.
6566 */
6567 i = find_internal_func(fname);
6568 if (i >= 0)
6569 {
6570 if (argcount < functions[i].f_min_argc)
6571 error = ERROR_TOOFEW;
6572 else if (argcount > functions[i].f_max_argc)
6573 error = ERROR_TOOMANY;
6574 else
6575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006577 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 error = ERROR_NONE;
6579 }
6580 }
6581 }
6582 /*
6583 * The function call (or "FuncUndefined" autocommand sequence) might
6584 * have been aborted by an error, an interrupt, or an explicitly thrown
6585 * exception that has not been caught so far. This situation can be
6586 * tested for by calling aborting(). For an error in an internal
6587 * function or for the "E132" error in call_user_func(), however, the
6588 * throw point at which the "force_abort" flag (temporarily reset by
6589 * emsg()) is normally updated has not been reached yet. We need to
6590 * update that flag first to make aborting() reliable.
6591 */
6592 update_force_abort();
6593 }
6594 if (error == ERROR_NONE)
6595 ret = OK;
6596
6597 /*
6598 * Report an error unless the argument evaluation or function call has been
6599 * cancelled due to an aborting error, an interrupt, or an exception.
6600 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 if (!aborting())
6602 {
6603 switch (error)
6604 {
6605 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 break;
6608 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006609 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610 break;
6611 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006612 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 name);
6614 break;
6615 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 name);
6618 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006619 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006621 name);
6622 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 }
6624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626 name[len] = cc;
6627 if (fname != name && fname != fname_buf)
6628 vim_free(fname);
6629
6630 return ret;
6631}
6632
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633/*
6634 * Give an error message with a function name. Handle <SNR> things.
6635 */
6636 static void
6637emsg_funcname(msg, name)
6638 char *msg;
6639 char_u *name;
6640{
6641 char_u *p;
6642
6643 if (*name == K_SPECIAL)
6644 p = concat_str((char_u *)"<SNR>", name + 3);
6645 else
6646 p = name;
6647 EMSG2(_(msg), p);
6648 if (p != name)
6649 vim_free(p);
6650}
6651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652/*********************************************
6653 * Implementation of the built-in functions
6654 */
6655
6656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006657 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 */
6659 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006660f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006661 typval_T *argvars;
6662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006666 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006669 if ((l = argvars[0].vval.v_list) != NULL
6670 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6671 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673 }
6674 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006675 EMSG(_(e_listreq));
6676}
6677
6678/*
6679 * "append(lnum, string/list)" function
6680 */
6681 static void
6682f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 typval_T *argvars;
6684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006685{
6686 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *l = NULL;
6688 listitem_T *li = NULL;
6689 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006690 long added = 0;
6691
6692 rettv->vval.v_number = 1; /* Default: Failed */
6693 lnum = get_tv_lnum(argvars);
6694 if (lnum >= 0
6695 && lnum <= curbuf->b_ml.ml_line_count
6696 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006698 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006700 l = argvars[1].vval.v_list;
6701 if (l == NULL)
6702 return;
6703 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006705 for (;;)
6706 {
6707 if (l == NULL)
6708 tv = &argvars[1]; /* append a string */
6709 else if (li == NULL)
6710 break; /* end of list */
6711 else
6712 tv = &li->li_tv; /* append item from list */
6713 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6714 ++added;
6715 if (l == NULL)
6716 break;
6717 li = li->li_next;
6718 }
6719
6720 appended_lines_mark(lnum, added);
6721 if (curwin->w_cursor.lnum > lnum)
6722 curwin->w_cursor.lnum += added;
6723 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725}
6726
6727/*
6728 * "argc()" function
6729 */
6730/* ARGSUSED */
6731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006733 typval_T *argvars;
6734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737}
6738
6739/*
6740 * "argidx()" function
6741 */
6742/* ARGSUSED */
6743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006745 typval_T *argvars;
6746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006748 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
6750
6751/*
6752 * "argv(nr)" function
6753 */
6754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006755f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 typval_T *argvars;
6757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
6759 int idx;
6760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006761 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006763 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006765 rettv->vval.v_string = NULL;
6766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767}
6768
6769/*
6770 * "browse(save, title, initdir, default)" function
6771 */
6772/* ARGSUSED */
6773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006775 typval_T *argvars;
6776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778#ifdef FEAT_BROWSE
6779 int save;
6780 char_u *title;
6781 char_u *initdir;
6782 char_u *defname;
6783 char_u buf[NUMBUFLEN];
6784 char_u buf2[NUMBUFLEN];
6785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006786 save = get_tv_number(&argvars[0]);
6787 title = get_tv_string(&argvars[1]);
6788 initdir = get_tv_string_buf(&argvars[2], buf);
6789 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006792 do_browse(save ? BROWSE_SAVE : 0,
6793 title, defname, NULL, initdir, NULL, curbuf);
6794#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006797 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006798}
6799
6800/*
6801 * "browsedir(title, initdir)" function
6802 */
6803/* ARGSUSED */
6804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 typval_T *argvars;
6807 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006808{
6809#ifdef FEAT_BROWSE
6810 char_u *title;
6811 char_u *initdir;
6812 char_u buf[NUMBUFLEN];
6813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 title = get_tv_string(&argvars[0]);
6815 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006818 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006822 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823}
6824
Bram Moolenaar33570922005-01-25 22:26:29 +00006825static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006826
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827/*
6828 * Find a buffer by number or exact name.
6829 */
6830 static buf_T *
6831find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 if (avar->v_type == VAR_NUMBER)
6837 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006838 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006841 if (buf == NULL)
6842 {
6843 /* No full path name match, try a match with a URL or a "nofile"
6844 * buffer, these don't use the full path. */
6845 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6846 if (buf->b_fname != NULL
6847 && (path_with_url(buf->b_fname)
6848#ifdef FEAT_QUICKFIX
6849 || bt_nofile(buf)
6850#endif
6851 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006852 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006853 break;
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
6856 return buf;
6857}
6858
6859/*
6860 * "bufexists(expr)" function
6861 */
6862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006863f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 typval_T *argvars;
6865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006867 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868}
6869
6870/*
6871 * "buflisted(expr)" function
6872 */
6873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006874f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006875 typval_T *argvars;
6876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 buf_T *buf;
6879
6880 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882}
6883
6884/*
6885 * "bufloaded(expr)" function
6886 */
6887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006888f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006889 typval_T *argvars;
6890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 buf_T *buf;
6893
6894 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896}
6897
Bram Moolenaar33570922005-01-25 22:26:29 +00006898static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006899
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900/*
6901 * Get buffer by number or pattern.
6902 */
6903 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006904get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006907 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 int save_magic;
6909 char_u *save_cpo;
6910 buf_T *buf;
6911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006912 if (tv->v_type == VAR_NUMBER)
6913 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006914 if (tv->v_type != VAR_STRING)
6915 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (name == NULL || *name == NUL)
6917 return curbuf;
6918 if (name[0] == '$' && name[1] == NUL)
6919 return lastbuf;
6920
6921 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6922 save_magic = p_magic;
6923 p_magic = TRUE;
6924 save_cpo = p_cpo;
6925 p_cpo = (char_u *)"";
6926
6927 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6928 TRUE, FALSE));
6929
6930 p_magic = save_magic;
6931 p_cpo = save_cpo;
6932
6933 /* If not found, try expanding the name, like done for bufexists(). */
6934 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937 return buf;
6938}
6939
6940/*
6941 * "bufname(expr)" function
6942 */
6943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006944f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 typval_T *argvars;
6946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 buf_T *buf;
6949
6950 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006951 buf = get_buf_tv(&argvars[0]);
6952 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006954 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 --emsg_off;
6958}
6959
6960/*
6961 * "bufnr(expr)" function
6962 */
6963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006964f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006965 typval_T *argvars;
6966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 buf_T *buf;
6969
6970 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006973 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006975 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 --emsg_off;
6977}
6978
6979/*
6980 * "bufwinnr(nr)" function
6981 */
6982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006983f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 typval_T *argvars;
6985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987#ifdef FEAT_WINDOWS
6988 win_T *wp;
6989 int winnr = 0;
6990#endif
6991 buf_T *buf;
6992
6993 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995#ifdef FEAT_WINDOWS
6996 for (wp = firstwin; wp; wp = wp->w_next)
6997 {
6998 ++winnr;
6999 if (wp->w_buffer == buf)
7000 break;
7001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007004 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005#endif
7006 --emsg_off;
7007}
7008
7009/*
7010 * "byte2line(byte)" function
7011 */
7012/*ARGSUSED*/
7013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 typval_T *argvars;
7016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
7018#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007019 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020#else
7021 long boff = 0;
7022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007023 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007027 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 (linenr_T)0, &boff);
7029#endif
7030}
7031
7032/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007033 * "byteidx()" function
7034 */
7035/*ARGSUSED*/
7036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007037f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 typval_T *argvars;
7039 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007040{
7041#ifdef FEAT_MBYTE
7042 char_u *t;
7043#endif
7044 char_u *str;
7045 long idx;
7046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007047 str = get_tv_string(&argvars[0]);
7048 idx = get_tv_number(&argvars[1]);
7049 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007050 if (idx < 0)
7051 return;
7052
7053#ifdef FEAT_MBYTE
7054 t = str;
7055 for ( ; idx > 0; idx--)
7056 {
7057 if (*t == NUL) /* EOL reached */
7058 return;
7059 t += mb_ptr2len_check(t);
7060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007061 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007062#else
7063 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007064 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007065#endif
7066}
7067
7068/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007069 * "call(func, arglist)" function
7070 */
7071 static void
7072f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 typval_T *argvars;
7074 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007075{
7076 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007078 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007080 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007082
7083 rettv->vval.v_number = 0;
7084 if (argvars[1].v_type != VAR_LIST)
7085 {
7086 EMSG(_(e_listreq));
7087 return;
7088 }
7089 if (argvars[1].vval.v_list == NULL)
7090 return;
7091
7092 if (argvars[0].v_type == VAR_FUNC)
7093 func = argvars[0].vval.v_string;
7094 else
7095 func = get_tv_string(&argvars[0]);
7096
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097 if (argvars[2].v_type != VAR_UNKNOWN)
7098 {
7099 if (argvars[2].v_type != VAR_DICT)
7100 {
7101 EMSG(_(e_dictreq));
7102 return;
7103 }
7104 selfdict = argvars[2].vval.v_dict;
7105 }
7106
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007107 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7108 item = item->li_next)
7109 {
7110 if (argc == MAX_FUNC_ARGS)
7111 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007112 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007113 break;
7114 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007115 /* Make a copy of each argument. This is needed to be able to set
7116 * v_lock to VAR_FIXED in the copy without changing the original list.
7117 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007118 copy_tv(&item->li_tv, &argv[argc++]);
7119 }
7120
7121 if (item == NULL)
7122 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7124 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007125
7126 /* Free the arguments. */
7127 while (argc > 0)
7128 clear_tv(&argv[--argc]);
7129}
7130
7131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 * "char2nr(string)" function
7133 */
7134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007135f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 typval_T *argvars;
7137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139#ifdef FEAT_MBYTE
7140 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007141 rettv->vval.v_number =
7142 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 else
7144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007145 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146}
7147
7148/*
7149 * "cindent(lnum)" function
7150 */
7151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007152f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 typval_T *argvars;
7154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156#ifdef FEAT_CINDENT
7157 pos_T pos;
7158 linenr_T lnum;
7159
7160 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007161 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7163 {
7164 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007165 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 curwin->w_cursor = pos;
7167 }
7168 else
7169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171}
7172
7173/*
7174 * "col(string)" function
7175 */
7176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007177f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 typval_T *argvars;
7179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 colnr_T col = 0;
7182 pos_T *fp;
7183
7184 fp = var2fpos(&argvars[0], FALSE);
7185 if (fp != NULL)
7186 {
7187 if (fp->col == MAXCOL)
7188 {
7189 /* '> can be MAXCOL, get the length of the line then */
7190 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7191 col = STRLEN(ml_get(fp->lnum)) + 1;
7192 else
7193 col = MAXCOL;
7194 }
7195 else
7196 {
7197 col = fp->col + 1;
7198#ifdef FEAT_VIRTUALEDIT
7199 /* col(".") when the cursor is on the NUL at the end of the line
7200 * because of "coladd" can be seen as an extra column. */
7201 if (virtual_active() && fp == &curwin->w_cursor)
7202 {
7203 char_u *p = ml_get_cursor();
7204
7205 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7206 curwin->w_virtcol - curwin->w_cursor.coladd))
7207 {
7208# ifdef FEAT_MBYTE
7209 int l;
7210
7211 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7212 col += l;
7213# else
7214 if (*p != NUL && p[1] == NUL)
7215 ++col;
7216# endif
7217 }
7218 }
7219#endif
7220 }
7221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007222 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223}
7224
7225/*
7226 * "confirm(message, buttons[, default [, type]])" function
7227 */
7228/*ARGSUSED*/
7229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007230f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007231 typval_T *argvars;
7232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
7234#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7235 char_u *message;
7236 char_u *buttons = NULL;
7237 char_u buf[NUMBUFLEN];
7238 char_u buf2[NUMBUFLEN];
7239 int def = 1;
7240 int type = VIM_GENERIC;
7241 int c;
7242
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007247 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007250 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007252 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007253 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 switch (TOUPPER_ASC(c))
7255 {
7256 case 'E': type = VIM_ERROR; break;
7257 case 'Q': type = VIM_QUESTION; break;
7258 case 'I': type = VIM_INFO; break;
7259 case 'W': type = VIM_WARNING; break;
7260 case 'G': type = VIM_GENERIC; break;
7261 }
7262 }
7263 }
7264 }
7265
7266 if (buttons == NULL || *buttons == NUL)
7267 buttons = (char_u *)_("&Ok");
7268
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 def, NULL);
7271#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007272 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273#endif
7274}
7275
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007276/*
7277 * "copy()" function
7278 */
7279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007280f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 typval_T *argvars;
7282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007283{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
7287/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007288 * "count()" function
7289 */
7290 static void
7291f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 typval_T *argvars;
7293 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007294{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007295 long n = 0;
7296 int ic = FALSE;
7297
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007299 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 listitem_T *li;
7301 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007303
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 if ((l = argvars[0].vval.v_list) != NULL)
7305 {
7306 li = l->lv_first;
7307 if (argvars[2].v_type != VAR_UNKNOWN)
7308 {
7309 ic = get_tv_number(&argvars[2]);
7310 if (argvars[3].v_type != VAR_UNKNOWN)
7311 {
7312 idx = get_tv_number(&argvars[3]);
7313 li = list_find(l, idx);
7314 if (li == NULL)
7315 EMSGN(_(e_listidx), idx);
7316 }
7317 }
7318
7319 for ( ; li != NULL; li = li->li_next)
7320 if (tv_equal(&li->li_tv, &argvars[1], ic))
7321 ++n;
7322 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 else if (argvars[0].v_type == VAR_DICT)
7325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 int todo;
7327 dict_T *d;
7328 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 if ((d = argvars[0].vval.v_dict) != NULL)
7331 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 if (argvars[2].v_type != VAR_UNKNOWN)
7333 {
7334 ic = get_tv_number(&argvars[2]);
7335 if (argvars[3].v_type != VAR_UNKNOWN)
7336 EMSG(_(e_invarg));
7337 }
7338
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 todo = d->dv_hashtab.ht_used;
7340 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 {
7342 if (!HASHITEM_EMPTY(hi))
7343 {
7344 --todo;
7345 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7346 ++n;
7347 }
7348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 }
7350 }
7351 else
7352 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007353 rettv->vval.v_number = n;
7354}
7355
7356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7358 *
7359 * Checks the existence of a cscope connection.
7360 */
7361/*ARGSUSED*/
7362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007363f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 typval_T *argvars;
7365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367#ifdef FEAT_CSCOPE
7368 int num = 0;
7369 char_u *dbpath = NULL;
7370 char_u *prepend = NULL;
7371 char_u buf[NUMBUFLEN];
7372
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373 if (argvars[0].v_type != VAR_UNKNOWN
7374 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 num = (int)get_tv_number(&argvars[0]);
7377 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007378 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007379 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007382 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007384 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385#endif
7386}
7387
7388/*
7389 * "cursor(lnum, col)" function
7390 *
7391 * Moves the cursor to the specified line and column
7392 */
7393/*ARGSUSED*/
7394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007395f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 typval_T *argvars;
7397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399 long line, col;
7400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007401 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (line > 0)
7403 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007404 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if (col > 0)
7406 curwin->w_cursor.col = col - 1;
7407#ifdef FEAT_VIRTUALEDIT
7408 curwin->w_cursor.coladd = 0;
7409#endif
7410
7411 /* Make sure the cursor is in a valid position. */
7412 check_cursor();
7413#ifdef FEAT_MBYTE
7414 /* Correct cursor for multi-byte character. */
7415 if (has_mbyte)
7416 mb_adjust_cursor();
7417#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007418
7419 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420}
7421
7422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007423 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 */
7425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 typval_T *argvars;
7428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007430 static int copyID = 0;
7431 int noref = 0;
7432
7433 if (argvars[1].v_type != VAR_UNKNOWN)
7434 noref = get_tv_number(&argvars[1]);
7435 if (noref < 0 || noref > 1)
7436 EMSG(_(e_invarg));
7437 else
7438 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
7441/*
7442 * "delete()" function
7443 */
7444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 typval_T *argvars;
7447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453}
7454
7455/*
7456 * "did_filetype()" function
7457 */
7458/*ARGSUSED*/
7459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 typval_T *argvars;
7462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
7464#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007465 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007467 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468#endif
7469}
7470
7471/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007472 * "diff_filler()" function
7473 */
7474/*ARGSUSED*/
7475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 typval_T *argvars;
7478 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007479{
7480#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007481 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007482#endif
7483}
7484
7485/*
7486 * "diff_hlID()" function
7487 */
7488/*ARGSUSED*/
7489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007490f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 typval_T *argvars;
7492 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007493{
7494#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007495 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007496 static linenr_T prev_lnum = 0;
7497 static int changedtick = 0;
7498 static int fnum = 0;
7499 static int change_start = 0;
7500 static int change_end = 0;
7501 static enum hlf_value hlID = 0;
7502 int filler_lines;
7503 int col;
7504
7505 if (lnum != prev_lnum
7506 || changedtick != curbuf->b_changedtick
7507 || fnum != curbuf->b_fnum)
7508 {
7509 /* New line, buffer, change: need to get the values. */
7510 filler_lines = diff_check(curwin, lnum);
7511 if (filler_lines < 0)
7512 {
7513 if (filler_lines == -1)
7514 {
7515 change_start = MAXCOL;
7516 change_end = -1;
7517 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7518 hlID = HLF_ADD; /* added line */
7519 else
7520 hlID = HLF_CHD; /* changed line */
7521 }
7522 else
7523 hlID = HLF_ADD; /* added line */
7524 }
7525 else
7526 hlID = (enum hlf_value)0;
7527 prev_lnum = lnum;
7528 changedtick = curbuf->b_changedtick;
7529 fnum = curbuf->b_fnum;
7530 }
7531
7532 if (hlID == HLF_CHD || hlID == HLF_TXD)
7533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007535 if (col >= change_start && col <= change_end)
7536 hlID = HLF_TXD; /* changed text */
7537 else
7538 hlID = HLF_CHD; /* changed line */
7539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007540 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007541#endif
7542}
7543
7544/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007545 * "empty({expr})" function
7546 */
7547 static void
7548f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 typval_T *argvars;
7550 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007551{
7552 int n;
7553
7554 switch (argvars[0].v_type)
7555 {
7556 case VAR_STRING:
7557 case VAR_FUNC:
7558 n = argvars[0].vval.v_string == NULL
7559 || *argvars[0].vval.v_string == NUL;
7560 break;
7561 case VAR_NUMBER:
7562 n = argvars[0].vval.v_number == 0;
7563 break;
7564 case VAR_LIST:
7565 n = argvars[0].vval.v_list == NULL
7566 || argvars[0].vval.v_list->lv_first == NULL;
7567 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007568 case VAR_DICT:
7569 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007572 default:
7573 EMSG2(_(e_intern2), "f_empty()");
7574 n = 0;
7575 }
7576
7577 rettv->vval.v_number = n;
7578}
7579
7580/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007581 * "errorlist()" function
7582 */
7583/*ARGSUSED*/
7584 static void
7585f_errorlist(argvars, rettv)
7586 typval_T *argvars;
7587 typval_T *rettv;
7588{
7589#ifdef FEAT_QUICKFIX
7590 list_T *l;
7591#endif
7592
7593 rettv->vval.v_number = FALSE;
7594#ifdef FEAT_QUICKFIX
7595 l = list_alloc();
7596 if (l != NULL)
7597 {
7598 if (get_errorlist(l) != FAIL)
7599 {
7600 rettv->vval.v_list = l;
7601 rettv->v_type = VAR_LIST;
7602 ++l->lv_refcount;
7603 }
7604 else
7605 list_free(l);
7606 }
7607#endif
7608}
7609
7610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 * "escape({string}, {chars})" function
7612 */
7613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007614f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 typval_T *argvars;
7616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 char_u buf[NUMBUFLEN];
7619
Bram Moolenaar758711c2005-02-02 23:11:38 +00007620 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7621 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624
7625/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007626 * "eval()" function
7627 */
7628/*ARGSUSED*/
7629 static void
7630f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007633{
7634 char_u *s;
7635
7636 s = get_tv_string(&argvars[0]);
7637 s = skipwhite(s);
7638
7639 if (eval1(&s, rettv, TRUE) == FAIL)
7640 rettv->vval.v_number = 0;
7641 else if (*s != NUL)
7642 EMSG(_(e_trailing));
7643}
7644
7645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 * "eventhandler()" function
7647 */
7648/*ARGSUSED*/
7649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 typval_T *argvars;
7652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655}
7656
7657/*
7658 * "executable()" function
7659 */
7660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 typval_T *argvars;
7663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668/*
7669 * "exists()" function
7670 */
7671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *argvars;
7674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 char_u *p;
7677 char_u *name;
7678 int n = FALSE;
7679 int len = 0;
7680
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (*p == '$') /* environment variable */
7683 {
7684 /* first try "normal" environment variables (fast) */
7685 if (mch_getenv(p + 1) != NULL)
7686 n = TRUE;
7687 else
7688 {
7689 /* try expanding things like $VIM and ${HOME} */
7690 p = expand_env_save(p);
7691 if (p != NULL && *p != '$')
7692 n = TRUE;
7693 vim_free(p);
7694 }
7695 }
7696 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 else if (*p == '*') /* internal or user defined function */
7699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 else if (*p == ':')
7703 {
7704 n = cmd_exists(p + 1);
7705 }
7706 else if (*p == '#')
7707 {
7708#ifdef FEAT_AUTOCMD
7709 name = p + 1;
7710 p = vim_strchr(name, '#');
7711 if (p != NULL)
7712 n = au_exists(name, p, p + 1);
7713 else
7714 n = au_exists(name, name + STRLEN(name), NULL);
7715#endif
7716 }
7717 else /* internal variable */
7718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007719 char_u *tofree;
7720 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007722 /* get_name_len() takes care of expanding curly braces */
7723 name = p;
7724 len = get_name_len(&p, &tofree, TRUE, FALSE);
7725 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007727 if (tofree != NULL)
7728 name = tofree;
7729 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7730 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007732 /* handle d.key, l[idx], f(expr) */
7733 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7734 if (n)
7735 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
7737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007739 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 }
7741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
7746 * "expand()" function
7747 */
7748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007749f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 typval_T *argvars;
7751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 char_u *s;
7754 int len;
7755 char_u *errormsg;
7756 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7757 expand_T xpc;
7758
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759 rettv->v_type = VAR_STRING;
7760 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (*s == '%' || *s == '#' || *s == '<')
7762 {
7763 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 --emsg_off;
7766 }
7767 else
7768 {
7769 /* When the optional second argument is non-zero, don't remove matches
7770 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 flags |= WILD_KEEP_ALL;
7773 ExpandInit(&xpc);
7774 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 ExpandCleanup(&xpc);
7777 }
7778}
7779
7780/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007781 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007782 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007783 */
7784 static void
7785f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T *argvars;
7787 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007788{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007789 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 list_T *l1, *l2;
7793 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007794 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795
Bram Moolenaare9a41262005-01-15 22:18:47 +00007796 l1 = argvars[0].vval.v_list;
7797 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007798 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7799 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 {
7801 if (argvars[2].v_type != VAR_UNKNOWN)
7802 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007803 before = get_tv_number(&argvars[2]);
7804 if (before == l1->lv_len)
7805 item = NULL;
7806 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007808 item = list_find(l1, before);
7809 if (item == NULL)
7810 {
7811 EMSGN(_(e_listidx), before);
7812 return;
7813 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 }
7815 }
7816 else
7817 item = NULL;
7818 list_extend(l1, l2, item);
7819
7820 ++l1->lv_refcount;
7821 copy_tv(&argvars[0], rettv);
7822 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007823 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7825 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007826 dict_T *d1, *d2;
7827 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 char_u *action;
7829 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007830 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007831 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832
7833 d1 = argvars[0].vval.v_dict;
7834 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007835 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7836 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 {
7838 /* Check the third argument. */
7839 if (argvars[2].v_type != VAR_UNKNOWN)
7840 {
7841 static char *(av[]) = {"keep", "force", "error"};
7842
7843 action = get_tv_string(&argvars[2]);
7844 for (i = 0; i < 3; ++i)
7845 if (STRCMP(action, av[i]) == 0)
7846 break;
7847 if (i == 3)
7848 {
7849 EMSGN(_(e_invarg2), action);
7850 return;
7851 }
7852 }
7853 else
7854 action = (char_u *)"force";
7855
7856 /* Go over all entries in the second dict and add them to the
7857 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 todo = d2->dv_hashtab.ht_used;
7859 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007861 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863 --todo;
7864 di1 = dict_find(d1, hi2->hi_key, -1);
7865 if (di1 == NULL)
7866 {
7867 di1 = dictitem_copy(HI2DI(hi2));
7868 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7869 dictitem_free(di1);
7870 }
7871 else if (*action == 'e')
7872 {
7873 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7874 break;
7875 }
7876 else if (*action == 'f')
7877 {
7878 clear_tv(&di1->di_tv);
7879 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881 }
7882 }
7883
7884 ++d1->dv_refcount;
7885 copy_tv(&argvars[0], rettv);
7886 }
7887 }
7888 else
7889 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890}
7891
7892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 * "filereadable()" function
7894 */
7895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 typval_T *argvars;
7898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 FILE *fd;
7901 char_u *p;
7902 int n;
7903
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007904 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7906 {
7907 n = TRUE;
7908 fclose(fd);
7909 }
7910 else
7911 n = FALSE;
7912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914}
7915
7916/*
7917 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7918 * rights to write into.
7919 */
7920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 typval_T *argvars;
7923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 char_u *p;
7926 int retval = 0;
7927#if defined(UNIX) || defined(VMS)
7928 int perm = 0;
7929#endif
7930
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007931 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932#if defined(UNIX) || defined(VMS)
7933 perm = mch_getperm(p);
7934#endif
7935#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7936 if (
7937# ifdef WIN3264
7938 mch_writable(p) &&
7939# else
7940# if defined(UNIX) || defined(VMS)
7941 (perm & 0222) &&
7942# endif
7943# endif
7944 mch_access((char *)p, W_OK) == 0
7945 )
7946#endif
7947 {
7948 ++retval;
7949 if (mch_isdir(p))
7950 ++retval;
7951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953}
7954
Bram Moolenaar33570922005-01-25 22:26:29 +00007955static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007956
7957 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007958findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 typval_T *argvars;
7960 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007961 int dir;
7962{
7963#ifdef FEAT_SEARCHPATH
7964 char_u *fname;
7965 char_u *fresult = NULL;
7966 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7967 char_u *p;
7968 char_u pathbuf[NUMBUFLEN];
7969 int count = 1;
7970 int first = TRUE;
7971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007973
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007974 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007977 if (*p != NUL)
7978 path = p;
7979
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007980 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007982 }
7983
7984 do
7985 {
7986 vim_free(fresult);
7987 fresult = find_file_in_path_option(first ? fname : NULL,
7988 first ? (int)STRLEN(fname) : 0,
7989 0, first, path, dir, NULL);
7990 first = FALSE;
7991 } while (--count > 0 && fresult != NULL);
7992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007994#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007996#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007998}
7999
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008000static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
8001static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00008002static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8003static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004
8005/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008006 * Prepare v: variable "idx" to be used.
8007 * Save the current typeval in "save_tv".
8008 * When not used yet add the variable to the v: hashtable.
8009 */
8010 static void
8011prepare_vimvar(idx, save_tv)
8012 int idx;
8013 typval_T *save_tv;
8014{
8015 *save_tv = vimvars[idx].vv_tv;
8016 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8017 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8018}
8019
8020/*
8021 * Restore v: variable "idx" to typeval "save_tv".
8022 * When no longer defined, remove the variable from the v: hashtable.
8023 */
8024 static void
8025restore_vimvar(idx, save_tv)
8026 int idx;
8027 typval_T *save_tv;
8028{
8029 hashitem_T *hi;
8030
8031 clear_tv(&vimvars[idx].vv_tv);
8032 vimvars[idx].vv_tv = *save_tv;
8033 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8034 {
8035 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8036 if (HASHITEM_EMPTY(hi))
8037 EMSG2(_(e_intern2), "restore_vimvar()");
8038 else
8039 hash_remove(&vimvarht, hi);
8040 }
8041}
8042
8043/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008044 * Implementation of map() and filter().
8045 */
8046 static void
8047filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 typval_T *argvars;
8049 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008050 int map;
8051{
8052 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 listitem_T *li, *nli;
8055 list_T *l = NULL;
8056 dictitem_T *di;
8057 hashtab_T *ht;
8058 hashitem_T *hi;
8059 dict_T *d = NULL;
8060 typval_T save_val;
8061 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008063 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008064 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8065
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008066
8067 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008069 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008070 if ((l = argvars[0].vval.v_list) == NULL
8071 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008072 return;
8073 }
8074 else if (argvars[0].v_type == VAR_DICT)
8075 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008076 if ((d = argvars[0].vval.v_dict) == NULL
8077 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 return;
8079 }
8080 else
8081 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008082 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008083 return;
8084 }
8085
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008086 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008087 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088
8089 if (argvars[0].v_type == VAR_DICT)
8090 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008091 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00008092 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008093
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00008095 hash_lock(ht);
8096 todo = ht->ht_used;
8097 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008099 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008101 --todo;
8102 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008103 if (tv_check_lock(di->di_tv.v_lock, msg))
8104 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008106 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8107 break;
8108 if (!map && rem)
8109 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008111 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008112 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008113 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008115 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008116 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008117 else
8118 {
8119 for (li = l->lv_first; li != NULL; li = nli)
8120 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008121 if (tv_check_lock(li->li_tv.v_lock, msg))
8122 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 nli = li->li_next;
8124 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
8125 break;
8126 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00008127 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008128 }
8129 }
8130
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008131 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008132
8133 copy_tv(&argvars[0], rettv);
8134}
8135
8136 static int
8137filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 char_u *expr;
8140 int map;
8141 int *remp;
8142{
Bram Moolenaar33570922005-01-25 22:26:29 +00008143 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008144 char_u *s;
8145
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008147 s = expr;
8148 if (eval1(&s, &rettv, TRUE) == FAIL)
8149 return FAIL;
8150 if (*s != NUL) /* check for trailing chars after expr */
8151 {
8152 EMSG2(_(e_invexpr2), s);
8153 return FAIL;
8154 }
8155 if (map)
8156 {
8157 /* map(): replace the list item value */
8158 clear_tv(tv);
8159 *tv = rettv;
8160 }
8161 else
8162 {
8163 /* filter(): when expr is zero remove the item */
8164 *remp = (get_tv_number(&rettv) == 0);
8165 clear_tv(&rettv);
8166 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008169}
8170
8171/*
8172 * "filter()" function
8173 */
8174 static void
8175f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 typval_T *argvars;
8177 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008178{
8179 filter_map(argvars, rettv, FALSE);
8180}
8181
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008182/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008183 * "finddir({fname}[, {path}[, {count}]])" function
8184 */
8185 static void
8186f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 typval_T *argvars;
8188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008189{
8190 findfilendir(argvars, rettv, TRUE);
8191}
8192
8193/*
8194 * "findfile({fname}[, {path}[, {count}]])" function
8195 */
8196 static void
8197f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 typval_T *argvars;
8199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008200{
8201 findfilendir(argvars, rettv, FALSE);
8202}
8203
8204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 * "fnamemodify({fname}, {mods})" function
8206 */
8207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008209 typval_T *argvars;
8210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211{
8212 char_u *fname;
8213 char_u *mods;
8214 int usedlen = 0;
8215 int len;
8216 char_u *fbuf = NULL;
8217 char_u buf[NUMBUFLEN];
8218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 fname = get_tv_string(&argvars[0]);
8220 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 len = (int)STRLEN(fname);
8222
8223 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 vim_free(fbuf);
8231}
8232
Bram Moolenaar33570922005-01-25 22:26:29 +00008233static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
8235/*
8236 * "foldclosed()" function
8237 */
8238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 typval_T *argvars;
8241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 int end;
8243{
8244#ifdef FEAT_FOLDING
8245 linenr_T lnum;
8246 linenr_T first, last;
8247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8250 {
8251 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8252 {
8253 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 return;
8258 }
8259 }
8260#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262}
8263
8264/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008265 * "foldclosed()" function
8266 */
8267 static void
8268f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 typval_T *argvars;
8270 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008271{
8272 foldclosed_both(argvars, rettv, FALSE);
8273}
8274
8275/*
8276 * "foldclosedend()" function
8277 */
8278 static void
8279f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 typval_T *argvars;
8281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008282{
8283 foldclosed_both(argvars, rettv, TRUE);
8284}
8285
8286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * "foldlevel()" function
8288 */
8289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008290f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 typval_T *argvars;
8292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
8294#ifdef FEAT_FOLDING
8295 linenr_T lnum;
8296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008299 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 else
8301#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303}
8304
8305/*
8306 * "foldtext()" function
8307 */
8308/*ARGSUSED*/
8309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 typval_T *argvars;
8312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314#ifdef FEAT_FOLDING
8315 linenr_T lnum;
8316 char_u *s;
8317 char_u *r;
8318 int len;
8319 char *txt;
8320#endif
8321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->v_type = VAR_STRING;
8323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008325 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8326 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8327 <= curbuf->b_ml.ml_line_count
8328 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
8330 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8332 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 if (!linewhite(lnum))
8335 break;
8336 ++lnum;
8337 }
8338
8339 /* Find interesting text in this line. */
8340 s = skipwhite(ml_get(lnum));
8341 /* skip C comment-start */
8342 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008345 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008346 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008347 {
8348 s = skipwhite(ml_get(lnum + 1));
8349 if (*s == '*')
8350 s = skipwhite(s + 1);
8351 }
8352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 txt = _("+-%s%3ld lines: ");
8354 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008355 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 + 20 /* for %3ld */
8357 + STRLEN(s))); /* concatenated */
8358 if (r != NULL)
8359 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008360 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8361 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8362 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 len = (int)STRLEN(r);
8364 STRCAT(r, s);
8365 /* remove 'foldmarker' and 'commentstring' */
8366 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 }
8369 }
8370#endif
8371}
8372
8373/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008374 * "foldtextresult(lnum)" function
8375 */
8376/*ARGSUSED*/
8377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008378f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 typval_T *argvars;
8380 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008381{
8382#ifdef FEAT_FOLDING
8383 linenr_T lnum;
8384 char_u *text;
8385 char_u buf[51];
8386 foldinfo_T foldinfo;
8387 int fold_count;
8388#endif
8389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 rettv->v_type = VAR_STRING;
8391 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008392#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008394 fold_count = foldedCount(curwin, lnum, &foldinfo);
8395 if (fold_count > 0)
8396 {
8397 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8398 &foldinfo, buf);
8399 if (text == buf)
8400 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008402 }
8403#endif
8404}
8405
8406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 * "foreground()" function
8408 */
8409/*ARGSUSED*/
8410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#ifdef FEAT_GUI
8417 if (gui.in_use)
8418 gui_mch_set_foreground();
8419#else
8420# ifdef WIN32
8421 win32_set_foreground();
8422# endif
8423#endif
8424}
8425
8426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427 * "function()" function
8428 */
8429/*ARGSUSED*/
8430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008431f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008432 typval_T *argvars;
8433 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434{
8435 char_u *s;
8436
Bram Moolenaara7043832005-01-21 11:56:39 +00008437 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008439 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440 EMSG2(_(e_invarg2), s);
8441 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008442 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 else
8444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008445 rettv->vval.v_string = vim_strsave(s);
8446 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 }
8448}
8449
8450/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008451 * "get()" function
8452 */
8453 static void
8454f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008455 typval_T *argvars;
8456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008457{
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 listitem_T *li;
8459 list_T *l;
8460 dictitem_T *di;
8461 dict_T *d;
8462 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008463
Bram Moolenaare9a41262005-01-15 22:18:47 +00008464 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008465 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008466 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008467 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008468 li = list_find(l, get_tv_number(&argvars[1]));
8469 if (li != NULL)
8470 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008471 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 else if (argvars[0].v_type == VAR_DICT)
8474 {
8475 if ((d = argvars[0].vval.v_dict) != NULL)
8476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008477 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008478 if (di != NULL)
8479 tv = &di->di_tv;
8480 }
8481 }
8482 else
8483 EMSG2(_(e_listdictarg), "get()");
8484
8485 if (tv == NULL)
8486 {
8487 if (argvars[2].v_type == VAR_UNKNOWN)
8488 rettv->vval.v_number = 0;
8489 else
8490 copy_tv(&argvars[2], rettv);
8491 }
8492 else
8493 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008494}
8495
8496/*
8497 * "getbufvar()" function
8498 */
8499 static void
8500f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T *argvars;
8502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503{
8504 buf_T *buf;
8505 buf_T *save_curbuf;
8506 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008508
8509 ++emsg_off;
8510 buf = get_buf_tv(&argvars[0]);
8511 varname = get_tv_string(&argvars[1]);
8512
8513 rettv->v_type = VAR_STRING;
8514 rettv->vval.v_string = NULL;
8515
8516 if (buf != NULL && varname != NULL)
8517 {
8518 if (*varname == '&') /* buffer-local-option */
8519 {
8520 /* set curbuf to be our buf, temporarily */
8521 save_curbuf = curbuf;
8522 curbuf = buf;
8523
8524 get_option_tv(&varname, rettv, TRUE);
8525
8526 /* restore previous notion of curbuf */
8527 curbuf = save_curbuf;
8528 }
8529 else
8530 {
8531 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008533 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008535 }
8536 }
8537
8538 --emsg_off;
8539}
8540
8541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 * "getchar()" function
8543 */
8544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 typval_T *argvars;
8547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 varnumber_T n;
8550
8551 ++no_mapping;
8552 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 /* getchar(): blocking wait. */
8555 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008556 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 /* getchar(1): only check if char avail */
8558 n = vpeekc();
8559 else if (vpeekc() == NUL)
8560 /* getchar(0) and no char avail: return zero */
8561 n = 0;
8562 else
8563 /* getchar(0) and char avail: return char */
8564 n = safe_vgetc();
8565 --no_mapping;
8566 --allow_keys;
8567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 if (IS_SPECIAL(n) || mod_mask != 0)
8570 {
8571 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8572 int i = 0;
8573
8574 /* Turn a special key into three bytes, plus modifier. */
8575 if (mod_mask != 0)
8576 {
8577 temp[i++] = K_SPECIAL;
8578 temp[i++] = KS_MODIFIER;
8579 temp[i++] = mod_mask;
8580 }
8581 if (IS_SPECIAL(n))
8582 {
8583 temp[i++] = K_SPECIAL;
8584 temp[i++] = K_SECOND(n);
8585 temp[i++] = K_THIRD(n);
8586 }
8587#ifdef FEAT_MBYTE
8588 else if (has_mbyte)
8589 i += (*mb_char2bytes)(n, temp + i);
8590#endif
8591 else
8592 temp[i++] = n;
8593 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->v_type = VAR_STRING;
8595 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597}
8598
8599/*
8600 * "getcharmod()" function
8601 */
8602/*ARGSUSED*/
8603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 typval_T *argvars;
8606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
8612 * "getcmdline()" function
8613 */
8614/*ARGSUSED*/
8615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008617 typval_T *argvars;
8618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008620 rettv->v_type = VAR_STRING;
8621 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622}
8623
8624/*
8625 * "getcmdpos()" function
8626 */
8627/*ARGSUSED*/
8628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *argvars;
8631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008633 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634}
8635
8636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 * "getcwd()" function
8638 */
8639/*ARGSUSED*/
8640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 typval_T *argvars;
8643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645 char_u cwd[MAXPATHL];
8646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 else
8651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655#endif
8656 }
8657}
8658
8659/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008660 * "getfontname()" function
8661 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008662/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->v_type = VAR_STRING;
8669 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008670#ifdef FEAT_GUI
8671 if (gui.in_use)
8672 {
8673 GuiFont font;
8674 char_u *name = NULL;
8675
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008676 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008677 {
8678 /* Get the "Normal" font. Either the name saved by
8679 * hl_set_font_name() or from the font ID. */
8680 font = gui.norm_font;
8681 name = hl_get_font_name();
8682 }
8683 else
8684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008686 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8687 return;
8688 font = gui_mch_get_font(name, FALSE);
8689 if (font == NOFONT)
8690 return; /* Invalid font name, return empty string. */
8691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008693 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008694 gui_mch_free_font(font);
8695 }
8696#endif
8697}
8698
8699/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008700 * "getfperm({fname})" function
8701 */
8702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 typval_T *argvars;
8705 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008706{
8707 char_u *fname;
8708 struct stat st;
8709 char_u *perm = NULL;
8710 char_u flags[] = "rwx";
8711 int i;
8712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008714
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008716 if (mch_stat((char *)fname, &st) >= 0)
8717 {
8718 perm = vim_strsave((char_u *)"---------");
8719 if (perm != NULL)
8720 {
8721 for (i = 0; i < 9; i++)
8722 {
8723 if (st.st_mode & (1 << (8 - i)))
8724 perm[i] = flags[i % 3];
8725 }
8726 }
8727 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008729}
8730
8731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 * "getfsize({fname})" function
8733 */
8734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 char_u *fname;
8740 struct stat st;
8741
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
8746 if (mch_stat((char *)fname, &st) >= 0)
8747 {
8748 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 }
8753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * "getftime({fname})" function
8759 */
8760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 char_u *fname;
8766 struct stat st;
8767
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
8770 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774}
8775
8776/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008777 * "getftype({fname})" function
8778 */
8779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *argvars;
8782 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008783{
8784 char_u *fname;
8785 struct stat st;
8786 char_u *type = NULL;
8787 char *t;
8788
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008792 if (mch_lstat((char *)fname, &st) >= 0)
8793 {
8794#ifdef S_ISREG
8795 if (S_ISREG(st.st_mode))
8796 t = "file";
8797 else if (S_ISDIR(st.st_mode))
8798 t = "dir";
8799# ifdef S_ISLNK
8800 else if (S_ISLNK(st.st_mode))
8801 t = "link";
8802# endif
8803# ifdef S_ISBLK
8804 else if (S_ISBLK(st.st_mode))
8805 t = "bdev";
8806# endif
8807# ifdef S_ISCHR
8808 else if (S_ISCHR(st.st_mode))
8809 t = "cdev";
8810# endif
8811# ifdef S_ISFIFO
8812 else if (S_ISFIFO(st.st_mode))
8813 t = "fifo";
8814# endif
8815# ifdef S_ISSOCK
8816 else if (S_ISSOCK(st.st_mode))
8817 t = "fifo";
8818# endif
8819 else
8820 t = "other";
8821#else
8822# ifdef S_IFMT
8823 switch (st.st_mode & S_IFMT)
8824 {
8825 case S_IFREG: t = "file"; break;
8826 case S_IFDIR: t = "dir"; break;
8827# ifdef S_IFLNK
8828 case S_IFLNK: t = "link"; break;
8829# endif
8830# ifdef S_IFBLK
8831 case S_IFBLK: t = "bdev"; break;
8832# endif
8833# ifdef S_IFCHR
8834 case S_IFCHR: t = "cdev"; break;
8835# endif
8836# ifdef S_IFIFO
8837 case S_IFIFO: t = "fifo"; break;
8838# endif
8839# ifdef S_IFSOCK
8840 case S_IFSOCK: t = "socket"; break;
8841# endif
8842 default: t = "other";
8843 }
8844# else
8845 if (mch_isdir(fname))
8846 t = "dir";
8847 else
8848 t = "file";
8849# endif
8850#endif
8851 type = vim_strsave((char_u *)t);
8852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008854}
8855
8856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008857 * "getline(lnum)" function
8858 */
8859 static void
8860f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 typval_T *argvars;
8862 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008863{
8864 linenr_T lnum;
8865 linenr_T end;
8866 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 list_T *l;
8868 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008869
8870 lnum = get_tv_lnum(argvars);
8871
8872 if (argvars[1].v_type == VAR_UNKNOWN)
8873 {
8874 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8875 p = ml_get(lnum);
8876 else
8877 p = (char_u *)"";
8878
8879 rettv->v_type = VAR_STRING;
8880 rettv->vval.v_string = vim_strsave(p);
8881 }
8882 else
8883 {
8884 end = get_tv_lnum(&argvars[1]);
8885 if (end < lnum)
8886 {
8887 EMSG(_(e_invrange));
8888 rettv->vval.v_number = 0;
8889 }
8890 else
8891 {
8892 l = list_alloc();
8893 if (l != NULL)
8894 {
8895 if (lnum < 1)
8896 lnum = 1;
8897 if (end > curbuf->b_ml.ml_line_count)
8898 end = curbuf->b_ml.ml_line_count;
8899 while (lnum <= end)
8900 {
8901 li = listitem_alloc();
8902 if (li == NULL)
8903 break;
8904 list_append(l, li);
8905 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008906 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008907 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8908 }
8909 rettv->vval.v_list = l;
8910 rettv->v_type = VAR_LIST;
8911 ++l->lv_refcount;
8912 }
8913 }
8914 }
8915}
8916
8917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 * "getreg()" function
8919 */
8920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 typval_T *argvars;
8923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 char_u *strregname;
8926 int regname;
8927
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008928 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008931 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 regname = (strregname == NULL ? '"' : *strregname);
8933 if (regname == 0)
8934 regname = '"';
8935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->v_type = VAR_STRING;
8937 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939
8940/*
8941 * "getregtype()" function
8942 */
8943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *argvars;
8946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948 char_u *strregname;
8949 int regname;
8950 char_u buf[NUMBUFLEN + 2];
8951 long reglen = 0;
8952
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008953 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 else
8956 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
8959 regname = (strregname == NULL ? '"' : *strregname);
8960 if (regname == 0)
8961 regname = '"';
8962
8963 buf[0] = NUL;
8964 buf[1] = NUL;
8965 switch (get_reg_type(regname, &reglen))
8966 {
8967 case MLINE: buf[0] = 'V'; break;
8968 case MCHAR: buf[0] = 'v'; break;
8969#ifdef FEAT_VISUAL
8970 case MBLOCK:
8971 buf[0] = Ctrl_V;
8972 sprintf((char *)buf + 1, "%ld", reglen + 1);
8973 break;
8974#endif
8975 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->v_type = VAR_STRING;
8977 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 * "getwinposx()" function
8982 */
8983/*ARGSUSED*/
8984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *argvars;
8987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990#ifdef FEAT_GUI
8991 if (gui.in_use)
8992 {
8993 int x, y;
8994
8995 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 }
8998#endif
8999}
9000
9001/*
9002 * "getwinposy()" function
9003 */
9004/*ARGSUSED*/
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011#ifdef FEAT_GUI
9012 if (gui.in_use)
9013 {
9014 int x, y;
9015
9016 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 }
9019#endif
9020}
9021
9022/*
9023 * "getwinvar()" function
9024 */
9025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030 win_T *win, *oldcurwin;
9031 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
9034 ++emsg_off;
9035 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038 rettv->v_type = VAR_STRING;
9039 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 if (win != NULL && varname != NULL)
9042 {
9043 if (*varname == '&') /* window-local-option */
9044 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009045 /* Set curwin to be our win, temporarily. Also set curbuf, so
9046 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 oldcurwin = curwin;
9048 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009049 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052
9053 /* restore previous notion of curwin */
9054 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009055 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 }
9057 else
9058 {
9059 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009060 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 }
9064 }
9065
9066 --emsg_off;
9067}
9068
9069/*
9070 * "glob()" function
9071 */
9072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *argvars;
9075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 expand_T xpc;
9078
9079 ExpandInit(&xpc);
9080 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->v_type = VAR_STRING;
9082 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9084 ExpandCleanup(&xpc);
9085}
9086
9087/*
9088 * "globpath()" function
9089 */
9090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 typval_T *argvars;
9093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
9095 char_u buf1[NUMBUFLEN];
9096
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 rettv->v_type = VAR_STRING;
9098 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9099 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102/*
9103 * "has()" function
9104 */
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110 int i;
9111 char_u *name;
9112 int n = FALSE;
9113 static char *(has_list[]) =
9114 {
9115#ifdef AMIGA
9116 "amiga",
9117# ifdef FEAT_ARP
9118 "arp",
9119# endif
9120#endif
9121#ifdef __BEOS__
9122 "beos",
9123#endif
9124#ifdef MSDOS
9125# ifdef DJGPP
9126 "dos32",
9127# else
9128 "dos16",
9129# endif
9130#endif
9131#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9132 "mac",
9133#endif
9134#if defined(MACOS_X_UNIX)
9135 "macunix",
9136#endif
9137#ifdef OS2
9138 "os2",
9139#endif
9140#ifdef __QNX__
9141 "qnx",
9142#endif
9143#ifdef RISCOS
9144 "riscos",
9145#endif
9146#ifdef UNIX
9147 "unix",
9148#endif
9149#ifdef VMS
9150 "vms",
9151#endif
9152#ifdef WIN16
9153 "win16",
9154#endif
9155#ifdef WIN32
9156 "win32",
9157#endif
9158#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9159 "win32unix",
9160#endif
9161#ifdef WIN64
9162 "win64",
9163#endif
9164#ifdef EBCDIC
9165 "ebcdic",
9166#endif
9167#ifndef CASE_INSENSITIVE_FILENAME
9168 "fname_case",
9169#endif
9170#ifdef FEAT_ARABIC
9171 "arabic",
9172#endif
9173#ifdef FEAT_AUTOCMD
9174 "autocmd",
9175#endif
9176#ifdef FEAT_BEVAL
9177 "balloon_eval",
9178#endif
9179#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9180 "builtin_terms",
9181# ifdef ALL_BUILTIN_TCAPS
9182 "all_builtin_terms",
9183# endif
9184#endif
9185#ifdef FEAT_BYTEOFF
9186 "byte_offset",
9187#endif
9188#ifdef FEAT_CINDENT
9189 "cindent",
9190#endif
9191#ifdef FEAT_CLIENTSERVER
9192 "clientserver",
9193#endif
9194#ifdef FEAT_CLIPBOARD
9195 "clipboard",
9196#endif
9197#ifdef FEAT_CMDL_COMPL
9198 "cmdline_compl",
9199#endif
9200#ifdef FEAT_CMDHIST
9201 "cmdline_hist",
9202#endif
9203#ifdef FEAT_COMMENTS
9204 "comments",
9205#endif
9206#ifdef FEAT_CRYPT
9207 "cryptv",
9208#endif
9209#ifdef FEAT_CSCOPE
9210 "cscope",
9211#endif
9212#ifdef DEBUG
9213 "debug",
9214#endif
9215#ifdef FEAT_CON_DIALOG
9216 "dialog_con",
9217#endif
9218#ifdef FEAT_GUI_DIALOG
9219 "dialog_gui",
9220#endif
9221#ifdef FEAT_DIFF
9222 "diff",
9223#endif
9224#ifdef FEAT_DIGRAPHS
9225 "digraphs",
9226#endif
9227#ifdef FEAT_DND
9228 "dnd",
9229#endif
9230#ifdef FEAT_EMACS_TAGS
9231 "emacs_tags",
9232#endif
9233 "eval", /* always present, of course! */
9234#ifdef FEAT_EX_EXTRA
9235 "ex_extra",
9236#endif
9237#ifdef FEAT_SEARCH_EXTRA
9238 "extra_search",
9239#endif
9240#ifdef FEAT_FKMAP
9241 "farsi",
9242#endif
9243#ifdef FEAT_SEARCHPATH
9244 "file_in_path",
9245#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009246#if defined(UNIX) && !defined(USE_SYSTEM)
9247 "filterpipe",
9248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249#ifdef FEAT_FIND_ID
9250 "find_in_path",
9251#endif
9252#ifdef FEAT_FOLDING
9253 "folding",
9254#endif
9255#ifdef FEAT_FOOTER
9256 "footer",
9257#endif
9258#if !defined(USE_SYSTEM) && defined(UNIX)
9259 "fork",
9260#endif
9261#ifdef FEAT_GETTEXT
9262 "gettext",
9263#endif
9264#ifdef FEAT_GUI
9265 "gui",
9266#endif
9267#ifdef FEAT_GUI_ATHENA
9268# ifdef FEAT_GUI_NEXTAW
9269 "gui_neXtaw",
9270# else
9271 "gui_athena",
9272# endif
9273#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009274#ifdef FEAT_GUI_KDE
9275 "gui_kde",
9276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277#ifdef FEAT_GUI_GTK
9278 "gui_gtk",
9279# ifdef HAVE_GTK2
9280 "gui_gtk2",
9281# endif
9282#endif
9283#ifdef FEAT_GUI_MAC
9284 "gui_mac",
9285#endif
9286#ifdef FEAT_GUI_MOTIF
9287 "gui_motif",
9288#endif
9289#ifdef FEAT_GUI_PHOTON
9290 "gui_photon",
9291#endif
9292#ifdef FEAT_GUI_W16
9293 "gui_win16",
9294#endif
9295#ifdef FEAT_GUI_W32
9296 "gui_win32",
9297#endif
9298#ifdef FEAT_HANGULIN
9299 "hangul_input",
9300#endif
9301#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9302 "iconv",
9303#endif
9304#ifdef FEAT_INS_EXPAND
9305 "insert_expand",
9306#endif
9307#ifdef FEAT_JUMPLIST
9308 "jumplist",
9309#endif
9310#ifdef FEAT_KEYMAP
9311 "keymap",
9312#endif
9313#ifdef FEAT_LANGMAP
9314 "langmap",
9315#endif
9316#ifdef FEAT_LIBCALL
9317 "libcall",
9318#endif
9319#ifdef FEAT_LINEBREAK
9320 "linebreak",
9321#endif
9322#ifdef FEAT_LISP
9323 "lispindent",
9324#endif
9325#ifdef FEAT_LISTCMDS
9326 "listcmds",
9327#endif
9328#ifdef FEAT_LOCALMAP
9329 "localmap",
9330#endif
9331#ifdef FEAT_MENU
9332 "menu",
9333#endif
9334#ifdef FEAT_SESSION
9335 "mksession",
9336#endif
9337#ifdef FEAT_MODIFY_FNAME
9338 "modify_fname",
9339#endif
9340#ifdef FEAT_MOUSE
9341 "mouse",
9342#endif
9343#ifdef FEAT_MOUSESHAPE
9344 "mouseshape",
9345#endif
9346#if defined(UNIX) || defined(VMS)
9347# ifdef FEAT_MOUSE_DEC
9348 "mouse_dec",
9349# endif
9350# ifdef FEAT_MOUSE_GPM
9351 "mouse_gpm",
9352# endif
9353# ifdef FEAT_MOUSE_JSB
9354 "mouse_jsbterm",
9355# endif
9356# ifdef FEAT_MOUSE_NET
9357 "mouse_netterm",
9358# endif
9359# ifdef FEAT_MOUSE_PTERM
9360 "mouse_pterm",
9361# endif
9362# ifdef FEAT_MOUSE_XTERM
9363 "mouse_xterm",
9364# endif
9365#endif
9366#ifdef FEAT_MBYTE
9367 "multi_byte",
9368#endif
9369#ifdef FEAT_MBYTE_IME
9370 "multi_byte_ime",
9371#endif
9372#ifdef FEAT_MULTI_LANG
9373 "multi_lang",
9374#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009375#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009376#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009377 "mzscheme",
9378#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#ifdef FEAT_OLE
9381 "ole",
9382#endif
9383#ifdef FEAT_OSFILETYPE
9384 "osfiletype",
9385#endif
9386#ifdef FEAT_PATH_EXTRA
9387 "path_extra",
9388#endif
9389#ifdef FEAT_PERL
9390#ifndef DYNAMIC_PERL
9391 "perl",
9392#endif
9393#endif
9394#ifdef FEAT_PYTHON
9395#ifndef DYNAMIC_PYTHON
9396 "python",
9397#endif
9398#endif
9399#ifdef FEAT_POSTSCRIPT
9400 "postscript",
9401#endif
9402#ifdef FEAT_PRINTER
9403 "printer",
9404#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009405#ifdef FEAT_PROFILE
9406 "profile",
9407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408#ifdef FEAT_QUICKFIX
9409 "quickfix",
9410#endif
9411#ifdef FEAT_RIGHTLEFT
9412 "rightleft",
9413#endif
9414#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9415 "ruby",
9416#endif
9417#ifdef FEAT_SCROLLBIND
9418 "scrollbind",
9419#endif
9420#ifdef FEAT_CMDL_INFO
9421 "showcmd",
9422 "cmdline_info",
9423#endif
9424#ifdef FEAT_SIGNS
9425 "signs",
9426#endif
9427#ifdef FEAT_SMARTINDENT
9428 "smartindent",
9429#endif
9430#ifdef FEAT_SNIFF
9431 "sniff",
9432#endif
9433#ifdef FEAT_STL_OPT
9434 "statusline",
9435#endif
9436#ifdef FEAT_SUN_WORKSHOP
9437 "sun_workshop",
9438#endif
9439#ifdef FEAT_NETBEANS_INTG
9440 "netbeans_intg",
9441#endif
9442#ifdef FEAT_SYN_HL
9443 "syntax",
9444#endif
9445#if defined(USE_SYSTEM) || !defined(UNIX)
9446 "system",
9447#endif
9448#ifdef FEAT_TAG_BINS
9449 "tag_binary",
9450#endif
9451#ifdef FEAT_TAG_OLDSTATIC
9452 "tag_old_static",
9453#endif
9454#ifdef FEAT_TAG_ANYWHITE
9455 "tag_any_white",
9456#endif
9457#ifdef FEAT_TCL
9458# ifndef DYNAMIC_TCL
9459 "tcl",
9460# endif
9461#endif
9462#ifdef TERMINFO
9463 "terminfo",
9464#endif
9465#ifdef FEAT_TERMRESPONSE
9466 "termresponse",
9467#endif
9468#ifdef FEAT_TEXTOBJ
9469 "textobjects",
9470#endif
9471#ifdef HAVE_TGETENT
9472 "tgetent",
9473#endif
9474#ifdef FEAT_TITLE
9475 "title",
9476#endif
9477#ifdef FEAT_TOOLBAR
9478 "toolbar",
9479#endif
9480#ifdef FEAT_USR_CMDS
9481 "user-commands", /* was accidentally included in 5.4 */
9482 "user_commands",
9483#endif
9484#ifdef FEAT_VIMINFO
9485 "viminfo",
9486#endif
9487#ifdef FEAT_VERTSPLIT
9488 "vertsplit",
9489#endif
9490#ifdef FEAT_VIRTUALEDIT
9491 "virtualedit",
9492#endif
9493#ifdef FEAT_VISUAL
9494 "visual",
9495#endif
9496#ifdef FEAT_VISUALEXTRA
9497 "visualextra",
9498#endif
9499#ifdef FEAT_VREPLACE
9500 "vreplace",
9501#endif
9502#ifdef FEAT_WILDIGN
9503 "wildignore",
9504#endif
9505#ifdef FEAT_WILDMENU
9506 "wildmenu",
9507#endif
9508#ifdef FEAT_WINDOWS
9509 "windows",
9510#endif
9511#ifdef FEAT_WAK
9512 "winaltkeys",
9513#endif
9514#ifdef FEAT_WRITEBACKUP
9515 "writebackup",
9516#endif
9517#ifdef FEAT_XIM
9518 "xim",
9519#endif
9520#ifdef FEAT_XFONTSET
9521 "xfontset",
9522#endif
9523#ifdef USE_XSMP
9524 "xsmp",
9525#endif
9526#ifdef USE_XSMP_INTERACT
9527 "xsmp_interact",
9528#endif
9529#ifdef FEAT_XCLIPBOARD
9530 "xterm_clipboard",
9531#endif
9532#ifdef FEAT_XTERM_SAVE
9533 "xterm_save",
9534#endif
9535#if defined(UNIX) && defined(FEAT_X11)
9536 "X11",
9537#endif
9538 NULL
9539 };
9540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 for (i = 0; has_list[i] != NULL; ++i)
9543 if (STRICMP(name, has_list[i]) == 0)
9544 {
9545 n = TRUE;
9546 break;
9547 }
9548
9549 if (n == FALSE)
9550 {
9551 if (STRNICMP(name, "patch", 5) == 0)
9552 n = has_patch(atoi((char *)name + 5));
9553 else if (STRICMP(name, "vim_starting") == 0)
9554 n = (starting != 0);
9555#ifdef DYNAMIC_TCL
9556 else if (STRICMP(name, "tcl") == 0)
9557 n = tcl_enabled(FALSE);
9558#endif
9559#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9560 else if (STRICMP(name, "iconv") == 0)
9561 n = iconv_enabled(FALSE);
9562#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009563#ifdef DYNAMIC_MZSCHEME
9564 else if (STRICMP(name, "mzscheme") == 0)
9565 n = mzscheme_enabled(FALSE);
9566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567#ifdef DYNAMIC_RUBY
9568 else if (STRICMP(name, "ruby") == 0)
9569 n = ruby_enabled(FALSE);
9570#endif
9571#ifdef DYNAMIC_PYTHON
9572 else if (STRICMP(name, "python") == 0)
9573 n = python_enabled(FALSE);
9574#endif
9575#ifdef DYNAMIC_PERL
9576 else if (STRICMP(name, "perl") == 0)
9577 n = perl_enabled(FALSE);
9578#endif
9579#ifdef FEAT_GUI
9580 else if (STRICMP(name, "gui_running") == 0)
9581 n = (gui.in_use || gui.starting);
9582# ifdef FEAT_GUI_W32
9583 else if (STRICMP(name, "gui_win32s") == 0)
9584 n = gui_is_win32s();
9585# endif
9586# ifdef FEAT_BROWSE
9587 else if (STRICMP(name, "browse") == 0)
9588 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9589# endif
9590#endif
9591#ifdef FEAT_SYN_HL
9592 else if (STRICMP(name, "syntax_items") == 0)
9593 n = syntax_present(curbuf);
9594#endif
9595#if defined(WIN3264)
9596 else if (STRICMP(name, "win95") == 0)
9597 n = mch_windows95();
9598#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009599#ifdef FEAT_NETBEANS_INTG
9600 else if (STRICMP(name, "netbeans_enabled") == 0)
9601 n = usingNetbeans;
9602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 }
9604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606}
9607
9608/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 * "has_key()" function
9610 */
9611 static void
9612f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615{
9616 rettv->vval.v_number = 0;
9617 if (argvars[0].v_type != VAR_DICT)
9618 {
9619 EMSG(_(e_dictreq));
9620 return;
9621 }
9622 if (argvars[0].vval.v_dict == NULL)
9623 return;
9624
9625 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009626 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009627}
9628
9629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 * "hasmapto()" function
9631 */
9632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 typval_T *argvars;
9635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 char_u *name;
9638 char_u *mode;
9639 char_u buf[NUMBUFLEN];
9640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009642 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 mode = (char_u *)"nvo";
9644 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646
9647 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
9653/*
9654 * "histadd()" function
9655 */
9656/*ARGSUSED*/
9657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 typval_T *argvars;
9660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662#ifdef FEAT_CMDHIST
9663 int histype;
9664 char_u *str;
9665 char_u buf[NUMBUFLEN];
9666#endif
9667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 if (check_restricted() || check_secure())
9670 return;
9671#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (histype >= 0)
9674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 if (*str != NUL)
9677 {
9678 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 return;
9681 }
9682 }
9683#endif
9684}
9685
9686/*
9687 * "histdel()" function
9688 */
9689/*ARGSUSED*/
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695#ifdef FEAT_CMDHIST
9696 int n;
9697 char_u buf[NUMBUFLEN];
9698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9705 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 else
9707 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9709 get_tv_string_buf(&argvars[1], buf));
9710 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713#endif
9714}
9715
9716/*
9717 * "histget()" function
9718 */
9719/*ARGSUSED*/
9720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725#ifdef FEAT_CMDHIST
9726 int type;
9727 int idx;
9728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009730 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 idx = get_history_idx(type);
9732 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 idx = (int)get_tv_number(&argvars[1]);
9734 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739}
9740
9741/*
9742 * "histnr()" function
9743 */
9744/*ARGSUSED*/
9745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009747 typval_T *argvars;
9748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 int i;
9751
9752#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 if (i >= HIST_CMD && i < HIST_COUNT)
9755 i = get_history_idx(i);
9756 else
9757#endif
9758 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760}
9761
9762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 * "highlightID(name)" function
9764 */
9765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009767 typval_T *argvars;
9768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009774 * "highlight_exists()" function
9775 */
9776 static void
9777f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009778 typval_T *argvars;
9779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009780{
9781 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9782}
9783
9784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 * "hostname()" function
9786 */
9787/*ARGSUSED*/
9788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009790 typval_T *argvars;
9791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 char_u hostname[256];
9794
9795 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->v_type = VAR_STRING;
9797 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * iconv() function
9802 */
9803/*ARGSUSED*/
9804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809#ifdef FEAT_MBYTE
9810 char_u buf1[NUMBUFLEN];
9811 char_u buf2[NUMBUFLEN];
9812 char_u *from, *to, *str;
9813 vimconv_T vimconv;
9814#endif
9815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->v_type = VAR_STRING;
9817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818
9819#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 str = get_tv_string(&argvars[0]);
9821 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9822 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 vimconv.vc_type = CONV_NONE;
9824 convert_setup(&vimconv, from, to);
9825
9826 /* If the encodings are equal, no conversion needed. */
9827 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831
9832 convert_setup(&vimconv, NULL, NULL);
9833 vim_free(from);
9834 vim_free(to);
9835#endif
9836}
9837
9838/*
9839 * "indent()" function
9840 */
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 linenr_T lnum;
9847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853}
9854
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009855/*
9856 * "index()" function
9857 */
9858 static void
9859f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 typval_T *argvars;
9861 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009862{
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 list_T *l;
9864 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009865 long idx = 0;
9866 int ic = FALSE;
9867
9868 rettv->vval.v_number = -1;
9869 if (argvars[0].v_type != VAR_LIST)
9870 {
9871 EMSG(_(e_listreq));
9872 return;
9873 }
9874 l = argvars[0].vval.v_list;
9875 if (l != NULL)
9876 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009877 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009879 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009880 /* Start at specified item. Use the cached index that list_find()
9881 * sets, so that a negative number also works. */
9882 item = list_find(l, get_tv_number(&argvars[2]));
9883 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009884 if (argvars[3].v_type != VAR_UNKNOWN)
9885 ic = get_tv_number(&argvars[3]);
9886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887
Bram Moolenaar758711c2005-02-02 23:11:38 +00009888 for ( ; item != NULL; item = item->li_next, ++idx)
9889 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009890 {
9891 rettv->vval.v_number = idx;
9892 break;
9893 }
9894 }
9895}
9896
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897static int inputsecret_flag = 0;
9898
9899/*
9900 * "input()" function
9901 * Also handles inputsecret() when inputsecret is set.
9902 */
9903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 typval_T *argvars;
9906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 char_u *p = NULL;
9910 int c;
9911 char_u buf[NUMBUFLEN];
9912 int cmd_silent_save = cmd_silent;
9913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915
9916#ifdef NO_CONSOLE_INPUT
9917 /* While starting up, there is no place to enter text. */
9918 if (no_console_input())
9919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 return;
9922 }
9923#endif
9924
9925 cmd_silent = FALSE; /* Want to see the prompt. */
9926 if (prompt != NULL)
9927 {
9928 /* Only the part of the message after the last NL is considered as
9929 * prompt for the command line */
9930 p = vim_strrchr(prompt, '\n');
9931 if (p == NULL)
9932 p = prompt;
9933 else
9934 {
9935 ++p;
9936 c = *p;
9937 *p = NUL;
9938 msg_start();
9939 msg_clr_eos();
9940 msg_puts_attr(prompt, echo_attr);
9941 msg_didout = FALSE;
9942 msg_starthere();
9943 *p = c;
9944 }
9945 cmdline_row = msg_row;
9946 }
9947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009948 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9953
9954 /* since the user typed this, no need to wait for return */
9955 need_wait_return = FALSE;
9956 msg_didout = FALSE;
9957 cmd_silent = cmd_silent_save;
9958}
9959
9960/*
9961 * "inputdialog()" function
9962 */
9963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *argvars;
9966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968#if defined(FEAT_GUI_TEXTDIALOG)
9969 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9970 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9971 {
9972 char_u *message;
9973 char_u buf[NUMBUFLEN];
9974
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 IObuff[IOSIZE - 1] = NUL;
9980 }
9981 else
9982 IObuff[0] = NUL;
9983 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9984 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 else
9987 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009988 if (argvars[1].v_type != VAR_UNKNOWN
9989 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->vval.v_string = vim_strsave(
9991 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 }
9997 else
9998#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000}
10001
10002static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10003
10004/*
10005 * "inputrestore()" function
10006 */
10007/*ARGSUSED*/
10008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010010 typval_T *argvars;
10011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
10013 if (ga_userinput.ga_len > 0)
10014 {
10015 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10017 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020 else if (p_verbose > 1)
10021 {
10022 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025}
10026
10027/*
10028 * "inputsave()" function
10029 */
10030/*ARGSUSED*/
10031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
10036 /* Add an entry to the stack of typehead storage. */
10037 if (ga_grow(&ga_userinput, 1) == OK)
10038 {
10039 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10040 + ga_userinput.ga_len);
10041 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046}
10047
10048/*
10049 * "inputsecret()" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
10056 ++cmdline_star;
10057 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 --cmdline_star;
10060 --inputsecret_flag;
10061}
10062
10063/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010064 * "insert()" function
10065 */
10066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 typval_T *argvars;
10069 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010070{
10071 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 listitem_T *item;
10073 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010074
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010075 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010076 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010077 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010078 else if ((l = argvars[0].vval.v_list) != NULL
10079 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010080 {
10081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010083
Bram Moolenaar758711c2005-02-02 23:11:38 +000010084 if (before == l->lv_len)
10085 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010086 else
10087 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010088 item = list_find(l, before);
10089 if (item == NULL)
10090 {
10091 EMSGN(_(e_listidx), before);
10092 l = NULL;
10093 }
10094 }
10095 if (l != NULL)
10096 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010097 list_insert_tv(l, &argvars[1], item);
10098 ++l->lv_refcount;
10099 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010100 }
10101 }
10102}
10103
10104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 * "isdirectory()" function
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113}
10114
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010115/*
10116 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10117 * it refers to a List or Dictionary that is locked.
10118 */
10119 static int
10120tv_islocked(tv)
10121 typval_T *tv;
10122{
10123 return (tv->v_lock & VAR_LOCKED)
10124 || (tv->v_type == VAR_LIST
10125 && tv->vval.v_list != NULL
10126 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10127 || (tv->v_type == VAR_DICT
10128 && tv->vval.v_dict != NULL
10129 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10130}
10131
10132/*
10133 * "islocked()" function
10134 */
10135 static void
10136f_islocked(argvars, rettv)
10137 typval_T *argvars;
10138 typval_T *rettv;
10139{
10140 lval_T lv;
10141 char_u *end;
10142 dictitem_T *di;
10143
10144 rettv->vval.v_number = -1;
10145 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
10146 if (end != NULL && lv.ll_name != NULL)
10147 {
10148 if (*end != NUL)
10149 EMSG(_(e_trailing));
10150 else
10151 {
10152 if (lv.ll_tv == NULL)
10153 {
10154 if (check_changedtick(lv.ll_name))
10155 rettv->vval.v_number = 1; /* always locked */
10156 else
10157 {
10158 di = find_var(lv.ll_name, NULL);
10159 if (di != NULL)
10160 {
10161 /* Consider a variable locked when:
10162 * 1. the variable itself is locked
10163 * 2. the value of the variable is locked.
10164 * 3. the List or Dict value is locked.
10165 */
10166 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10167 || tv_islocked(&di->di_tv));
10168 }
10169 }
10170 }
10171 else if (lv.ll_range)
10172 EMSG(_("E745: Range not allowed"));
10173 else if (lv.ll_newkey != NULL)
10174 EMSG2(_(e_dictkey), lv.ll_newkey);
10175 else if (lv.ll_list != NULL)
10176 /* List item. */
10177 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10178 else
10179 /* Dictionary item. */
10180 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10181 }
10182 }
10183
10184 clear_lval(&lv);
10185}
10186
Bram Moolenaar33570922005-01-25 22:26:29 +000010187static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010188
10189/*
10190 * Turn a dict into a list:
10191 * "what" == 0: list of keys
10192 * "what" == 1: list of values
10193 * "what" == 2: list of items
10194 */
10195 static void
10196dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010197 typval_T *argvars;
10198 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010199 int what;
10200{
Bram Moolenaar33570922005-01-25 22:26:29 +000010201 list_T *l;
10202 list_T *l2;
10203 dictitem_T *di;
10204 hashitem_T *hi;
10205 listitem_T *li;
10206 listitem_T *li2;
10207 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010208 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010209
10210 rettv->vval.v_number = 0;
10211 if (argvars[0].v_type != VAR_DICT)
10212 {
10213 EMSG(_(e_dictreq));
10214 return;
10215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010216 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010217 return;
10218
10219 l = list_alloc();
10220 if (l == NULL)
10221 return;
10222 rettv->v_type = VAR_LIST;
10223 rettv->vval.v_list = l;
10224 ++l->lv_refcount;
10225
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 todo = d->dv_hashtab.ht_used;
10227 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010228 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010229 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010230 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010231 --todo;
10232 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010233
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010234 li = listitem_alloc();
10235 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010236 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010237 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010238
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010239 if (what == 0)
10240 {
10241 /* keys() */
10242 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010243 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10245 }
10246 else if (what == 1)
10247 {
10248 /* values() */
10249 copy_tv(&di->di_tv, &li->li_tv);
10250 }
10251 else
10252 {
10253 /* items() */
10254 l2 = list_alloc();
10255 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010256 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010257 li->li_tv.vval.v_list = l2;
10258 if (l2 == NULL)
10259 break;
10260 ++l2->lv_refcount;
10261
10262 li2 = listitem_alloc();
10263 if (li2 == NULL)
10264 break;
10265 list_append(l2, li2);
10266 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010267 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010268 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10269
10270 li2 = listitem_alloc();
10271 if (li2 == NULL)
10272 break;
10273 list_append(l2, li2);
10274 copy_tv(&di->di_tv, &li2->li_tv);
10275 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010276 }
10277 }
10278}
10279
10280/*
10281 * "items(dict)" function
10282 */
10283 static void
10284f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010285 typval_T *argvars;
10286 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010287{
10288 dict_list(argvars, rettv, 2);
10289}
10290
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010292 * "join()" function
10293 */
10294 static void
10295f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010296 typval_T *argvars;
10297 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010298{
10299 garray_T ga;
10300 char_u *sep;
10301
10302 rettv->vval.v_number = 0;
10303 if (argvars[0].v_type != VAR_LIST)
10304 {
10305 EMSG(_(e_listreq));
10306 return;
10307 }
10308 if (argvars[0].vval.v_list == NULL)
10309 return;
10310 if (argvars[1].v_type == VAR_UNKNOWN)
10311 sep = (char_u *)" ";
10312 else
10313 sep = get_tv_string(&argvars[1]);
10314
10315 ga_init2(&ga, (int)sizeof(char), 80);
10316 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10317 ga_append(&ga, NUL);
10318
10319 rettv->v_type = VAR_STRING;
10320 rettv->vval.v_string = (char_u *)ga.ga_data;
10321}
10322
10323/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010324 * "keys()" function
10325 */
10326 static void
10327f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010328 typval_T *argvars;
10329 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010330{
10331 dict_list(argvars, rettv, 0);
10332}
10333
10334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 * "last_buffer_nr()" function.
10336 */
10337/*ARGSUSED*/
10338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010340 typval_T *argvars;
10341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
10343 int n = 0;
10344 buf_T *buf;
10345
10346 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10347 if (n < buf->b_fnum)
10348 n = buf->b_fnum;
10349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010351}
10352
10353/*
10354 * "len()" function
10355 */
10356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010360{
10361 switch (argvars[0].v_type)
10362 {
10363 case VAR_STRING:
10364 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365 rettv->vval.v_number = (varnumber_T)STRLEN(
10366 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010367 break;
10368 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010369 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010370 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 case VAR_DICT:
10372 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10373 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010374 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010375 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010376 break;
10377 }
10378}
10379
Bram Moolenaar33570922005-01-25 22:26:29 +000010380static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010381
10382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 typval_T *argvars;
10385 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010386 int type;
10387{
10388#ifdef FEAT_LIBCALL
10389 char_u *string_in;
10390 char_u **string_result;
10391 int nr_result;
10392#endif
10393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010395 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010396 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010399
10400 if (check_restricted() || check_secure())
10401 return;
10402
10403#ifdef FEAT_LIBCALL
10404 /* The first two args must be strings, otherwise its meaningless */
10405 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10406 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010407 string_in = NULL;
10408 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010409 string_in = argvars[2].vval.v_string;
10410 if (type == VAR_NUMBER)
10411 string_result = NULL;
10412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414 if (mch_libcall(argvars[0].vval.v_string,
10415 argvars[1].vval.v_string,
10416 string_in,
10417 argvars[2].vval.v_number,
10418 string_result,
10419 &nr_result) == OK
10420 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010421 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010422 }
10423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424}
10425
10426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010427 * "libcall()" function
10428 */
10429 static void
10430f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010431 typval_T *argvars;
10432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010433{
10434 libcall_common(argvars, rettv, VAR_STRING);
10435}
10436
10437/*
10438 * "libcallnr()" function
10439 */
10440 static void
10441f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 typval_T *argvars;
10443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010444{
10445 libcall_common(argvars, rettv, VAR_NUMBER);
10446}
10447
10448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 * "line(string)" function
10450 */
10451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010453 typval_T *argvars;
10454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
10456 linenr_T lnum = 0;
10457 pos_T *fp;
10458
10459 fp = var2fpos(&argvars[0], TRUE);
10460 if (fp != NULL)
10461 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463}
10464
10465/*
10466 * "line2byte(lnum)" function
10467 */
10468/*ARGSUSED*/
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
10474#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476#else
10477 linenr_T lnum;
10478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10484 if (rettv->vval.v_number >= 0)
10485 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486#endif
10487}
10488
10489/*
10490 * "lispindent(lnum)" function
10491 */
10492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 typval_T *argvars;
10495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497#ifdef FEAT_LISP
10498 pos_T pos;
10499 linenr_T lnum;
10500
10501 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10504 {
10505 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 curwin->w_cursor = pos;
10508 }
10509 else
10510#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512}
10513
10514/*
10515 * "localtime()" function
10516 */
10517/*ARGSUSED*/
10518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010520 typval_T *argvars;
10521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524}
10525
Bram Moolenaar33570922005-01-25 22:26:29 +000010526static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527
10528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 int exact;
10533{
10534 char_u *keys;
10535 char_u *which;
10536 char_u buf[NUMBUFLEN];
10537 char_u *keys_buf = NULL;
10538 char_u *rhs;
10539 int mode;
10540 garray_T ga;
10541
10542 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 rettv->v_type = VAR_STRING;
10544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 if (*keys == NUL)
10548 return;
10549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 else
10553 which = (char_u *)"";
10554 mode = get_map_mode(&which, 0);
10555
10556 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10557 rhs = check_map(keys, mode, exact);
10558 vim_free(keys_buf);
10559 if (rhs != NULL)
10560 {
10561 ga_init(&ga);
10562 ga.ga_itemsize = 1;
10563 ga.ga_growsize = 40;
10564
10565 while (*rhs != NUL)
10566 ga_concat(&ga, str2special(&rhs, FALSE));
10567
10568 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571}
10572
10573/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010574 * "map()" function
10575 */
10576 static void
10577f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 typval_T *argvars;
10579 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580{
10581 filter_map(argvars, rettv, TRUE);
10582}
10583
10584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010585 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 */
10587 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010588f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 typval_T *argvars;
10590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010592 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593}
10594
10595/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010596 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 */
10598 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010599f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 typval_T *argvars;
10601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010603 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604}
10605
Bram Moolenaar33570922005-01-25 22:26:29 +000010606static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
10608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 typval_T *argvars;
10611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 int type;
10613{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010614 char_u *str = NULL;
10615 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 char_u *pat;
10617 regmatch_T regmatch;
10618 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 char_u *save_cpo;
10621 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010622 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010623 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010624 list_T *l = NULL;
10625 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010626 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010627 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628
10629 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10630 save_cpo = p_cpo;
10631 p_cpo = (char_u *)"";
10632
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010633 rettv->vval.v_number = -1;
10634 if (type == 3)
10635 {
10636 /* return empty list when there are no matches */
10637 if ((rettv->vval.v_list = list_alloc()) == NULL)
10638 goto theend;
10639 rettv->v_type = VAR_LIST;
10640 ++rettv->vval.v_list->lv_refcount;
10641 }
10642 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010644 rettv->v_type = VAR_STRING;
10645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010648 if (argvars[0].v_type == VAR_LIST)
10649 {
10650 if ((l = argvars[0].vval.v_list) == NULL)
10651 goto theend;
10652 li = l->lv_first;
10653 }
10654 else
10655 expr = str = get_tv_string(&argvars[0]);
10656
10657 pat = get_tv_string_buf(&argvars[1], patbuf);
10658
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010662 if (l != NULL)
10663 {
10664 li = list_find(l, start);
10665 if (li == NULL)
10666 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010667 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010668 }
10669 else
10670 {
10671 if (start < 0)
10672 start = 0;
10673 if (start > (long)STRLEN(str))
10674 goto theend;
10675 str += start;
10676 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010678 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 }
10681
10682 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10683 if (regmatch.regprog != NULL)
10684 {
10685 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010686
10687 while (1)
10688 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010689 if (l != NULL)
10690 {
10691 if (li == NULL)
10692 {
10693 match = FALSE;
10694 break;
10695 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010696 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010697 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010698 if (str == NULL)
10699 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010700 }
10701
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010702 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010703
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010704 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010705 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010706 if (l == NULL && !match)
10707 break;
10708
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010709 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010710 if (l != NULL)
10711 {
10712 li = li->li_next;
10713 ++idx;
10714 }
10715 else
10716 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010717#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010718 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010719#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010720 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010721#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010722 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010723 }
10724
10725 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010727 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010729 int i;
10730
10731 /* return list with matched string and submatches */
10732 for (i = 0; i < NSUBEXP; ++i)
10733 {
10734 if (regmatch.endp[i] == NULL)
10735 break;
10736 li = listitem_alloc();
10737 if (li == NULL)
10738 break;
10739 li->li_tv.v_type = VAR_STRING;
10740 li->li_tv.v_lock = 0;
10741 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10742 (int)(regmatch.endp[i] - regmatch.startp[i]));
10743 list_append(rettv->vval.v_list, li);
10744 }
10745 }
10746 else if (type == 2)
10747 {
10748 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010749 if (l != NULL)
10750 copy_tv(&li->li_tv, rettv);
10751 else
10752 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010754 }
10755 else if (l != NULL)
10756 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 else
10758 {
10759 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 (varnumber_T)(regmatch.startp[0] - str);
10762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 }
10767 }
10768 vim_free(regmatch.regprog);
10769 }
10770
10771theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010772 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 p_cpo = save_cpo;
10774}
10775
10776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777 * "match()" function
10778 */
10779 static void
10780f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 typval_T *argvars;
10782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783{
10784 find_some_match(argvars, rettv, 1);
10785}
10786
10787/*
10788 * "matchend()" function
10789 */
10790 static void
10791f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010792 typval_T *argvars;
10793 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010794{
10795 find_some_match(argvars, rettv, 0);
10796}
10797
10798/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010799 * "matchlist()" function
10800 */
10801 static void
10802f_matchlist(argvars, rettv)
10803 typval_T *argvars;
10804 typval_T *rettv;
10805{
10806 find_some_match(argvars, rettv, 3);
10807}
10808
10809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010810 * "matchstr()" function
10811 */
10812 static void
10813f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 typval_T *argvars;
10815 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010816{
10817 find_some_match(argvars, rettv, 2);
10818}
10819
Bram Moolenaar33570922005-01-25 22:26:29 +000010820static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010821
10822 static void
10823max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 typval_T *argvars;
10825 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010826 int domax;
10827{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010828 long n = 0;
10829 long i;
10830
10831 if (argvars[0].v_type == VAR_LIST)
10832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 list_T *l;
10834 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010836 l = argvars[0].vval.v_list;
10837 if (l != NULL)
10838 {
10839 li = l->lv_first;
10840 if (li != NULL)
10841 {
10842 n = get_tv_number(&li->li_tv);
10843 while (1)
10844 {
10845 li = li->li_next;
10846 if (li == NULL)
10847 break;
10848 i = get_tv_number(&li->li_tv);
10849 if (domax ? i > n : i < n)
10850 n = i;
10851 }
10852 }
10853 }
10854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 else if (argvars[0].v_type == VAR_DICT)
10856 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010858 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010860 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861
10862 d = argvars[0].vval.v_dict;
10863 if (d != NULL)
10864 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010865 todo = d->dv_hashtab.ht_used;
10866 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010868 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010870 --todo;
10871 i = get_tv_number(&HI2DI(hi)->di_tv);
10872 if (first)
10873 {
10874 n = i;
10875 first = FALSE;
10876 }
10877 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878 n = i;
10879 }
10880 }
10881 }
10882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010883 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010884 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010885 rettv->vval.v_number = n;
10886}
10887
10888/*
10889 * "max()" function
10890 */
10891 static void
10892f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010895{
10896 max_min(argvars, rettv, TRUE);
10897}
10898
10899/*
10900 * "min()" function
10901 */
10902 static void
10903f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 typval_T *argvars;
10905 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010906{
10907 max_min(argvars, rettv, FALSE);
10908}
10909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010910static int mkdir_recurse __ARGS((char_u *dir, int prot));
10911
10912/*
10913 * Create the directory in which "dir" is located, and higher levels when
10914 * needed.
10915 */
10916 static int
10917mkdir_recurse(dir, prot)
10918 char_u *dir;
10919 int prot;
10920{
10921 char_u *p;
10922 char_u *updir;
10923 int r = FAIL;
10924
10925 /* Get end of directory name in "dir".
10926 * We're done when it's "/" or "c:/". */
10927 p = gettail_sep(dir);
10928 if (p <= get_past_head(dir))
10929 return OK;
10930
10931 /* If the directory exists we're done. Otherwise: create it.*/
10932 updir = vim_strnsave(dir, (int)(p - dir));
10933 if (updir == NULL)
10934 return FAIL;
10935 if (mch_isdir(updir))
10936 r = OK;
10937 else if (mkdir_recurse(updir, prot) == OK)
10938 r = vim_mkdir_emsg(updir, prot);
10939 vim_free(updir);
10940 return r;
10941}
10942
10943#ifdef vim_mkdir
10944/*
10945 * "mkdir()" function
10946 */
10947 static void
10948f_mkdir(argvars, rettv)
10949 typval_T *argvars;
10950 typval_T *rettv;
10951{
10952 char_u *dir;
10953 char_u buf[NUMBUFLEN];
10954 int prot = 0755;
10955
10956 rettv->vval.v_number = FAIL;
10957 if (check_restricted() || check_secure())
10958 return;
10959
10960 dir = get_tv_string_buf(&argvars[0], buf);
10961 if (argvars[1].v_type != VAR_UNKNOWN)
10962 {
10963 if (argvars[2].v_type != VAR_UNKNOWN)
10964 prot = get_tv_number(&argvars[2]);
10965 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
10966 mkdir_recurse(dir, prot);
10967 }
10968 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
10969}
10970#endif
10971
Bram Moolenaar0d660222005-01-07 21:51:51 +000010972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 * "mode()" function
10974 */
10975/*ARGSUSED*/
10976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 typval_T *argvars;
10979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980{
10981 char_u buf[2];
10982
10983#ifdef FEAT_VISUAL
10984 if (VIsual_active)
10985 {
10986 if (VIsual_select)
10987 buf[0] = VIsual_mode + 's' - 'v';
10988 else
10989 buf[0] = VIsual_mode;
10990 }
10991 else
10992#endif
10993 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10994 buf[0] = 'r';
10995 else if (State & INSERT)
10996 {
10997 if (State & REPLACE_FLAG)
10998 buf[0] = 'R';
10999 else
11000 buf[0] = 'i';
11001 }
11002 else if (State & CMDLINE)
11003 buf[0] = 'c';
11004 else
11005 buf[0] = 'n';
11006
11007 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 rettv->vval.v_string = vim_strsave(buf);
11009 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013 * "nextnonblank()" function
11014 */
11015 static void
11016f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011017 typval_T *argvars;
11018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011019{
11020 linenr_T lnum;
11021
11022 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11023 {
11024 if (lnum > curbuf->b_ml.ml_line_count)
11025 {
11026 lnum = 0;
11027 break;
11028 }
11029 if (*skipwhite(ml_get(lnum)) != NUL)
11030 break;
11031 }
11032 rettv->vval.v_number = lnum;
11033}
11034
11035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 * "nr2char()" function
11037 */
11038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011040 typval_T *argvars;
11041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043 char_u buf[NUMBUFLEN];
11044
11045#ifdef FEAT_MBYTE
11046 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 else
11049#endif
11050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 buf[1] = NUL;
11053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->v_type = VAR_STRING;
11055 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011056}
11057
11058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011059 * "prevnonblank()" function
11060 */
11061 static void
11062f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 typval_T *argvars;
11064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011065{
11066 linenr_T lnum;
11067
11068 lnum = get_tv_lnum(argvars);
11069 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11070 lnum = 0;
11071 else
11072 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11073 --lnum;
11074 rettv->vval.v_number = lnum;
11075}
11076
Bram Moolenaar8c711452005-01-14 21:53:12 +000011077/*
11078 * "range()" function
11079 */
11080 static void
11081f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 typval_T *argvars;
11083 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011084{
11085 long start;
11086 long end;
11087 long stride = 1;
11088 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 list_T *l;
11090 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011091
11092 start = get_tv_number(&argvars[0]);
11093 if (argvars[1].v_type == VAR_UNKNOWN)
11094 {
11095 end = start - 1;
11096 start = 0;
11097 }
11098 else
11099 {
11100 end = get_tv_number(&argvars[1]);
11101 if (argvars[2].v_type != VAR_UNKNOWN)
11102 stride = get_tv_number(&argvars[2]);
11103 }
11104
11105 rettv->vval.v_number = 0;
11106 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011107 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011108 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011109 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011110 else
11111 {
11112 l = list_alloc();
11113 if (l != NULL)
11114 {
11115 rettv->v_type = VAR_LIST;
11116 rettv->vval.v_list = l;
11117 ++l->lv_refcount;
11118
11119 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11120 {
11121 li = listitem_alloc();
11122 if (li == NULL)
11123 break;
11124 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011125 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011126 li->li_tv.vval.v_number = i;
11127 list_append(l, li);
11128 }
11129 }
11130 }
11131}
11132
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011133/*
11134 * "readfile()" function
11135 */
11136 static void
11137f_readfile(argvars, rettv)
11138 typval_T *argvars;
11139 typval_T *rettv;
11140{
11141 int binary = FALSE;
11142 char_u *fname;
11143 FILE *fd;
11144 list_T *l;
11145 listitem_T *li;
11146#define FREAD_SIZE 200 /* optimized for text lines */
11147 char_u buf[FREAD_SIZE];
11148 int readlen; /* size of last fread() */
11149 int buflen; /* nr of valid chars in buf[] */
11150 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11151 int tolist; /* first byte in buf[] still to be put in list */
11152 int chop; /* how many CR to chop off */
11153 char_u *prev = NULL; /* previously read bytes, if any */
11154 int prevlen = 0; /* length of "prev" if not NULL */
11155 char_u *s;
11156 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011157 long maxline = MAXLNUM;
11158 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011160 if (argvars[1].v_type != VAR_UNKNOWN)
11161 {
11162 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11163 binary = TRUE;
11164 if (argvars[2].v_type != VAR_UNKNOWN)
11165 maxline = get_tv_number(&argvars[2]);
11166 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011167
11168 l = list_alloc();
11169 if (l == NULL)
11170 return;
11171 rettv->v_type = VAR_LIST;
11172 rettv->vval.v_list = l;
11173 l->lv_refcount = 1;
11174
11175 /* Always open the file in binary mode, library functions have a mind of
11176 * their own about CR-LF conversion. */
11177 fname = get_tv_string(&argvars[0]);
11178 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11179 {
11180 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11181 return;
11182 }
11183
11184 filtd = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011185 while (cnt < maxline)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011186 {
11187 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11188 buflen = filtd + readlen;
11189 tolist = 0;
11190 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11191 {
11192 if (buf[filtd] == '\n' || readlen <= 0)
11193 {
11194 /* Only when in binary mode add an empty list item when the
11195 * last line ends in a '\n'. */
11196 if (!binary && readlen == 0 && filtd == 0)
11197 break;
11198
11199 /* Found end-of-line or end-of-file: add a text line to the
11200 * list. */
11201 chop = 0;
11202 if (!binary)
11203 while (filtd - chop - 1 >= tolist
11204 && buf[filtd - chop - 1] == '\r')
11205 ++chop;
11206 len = filtd - tolist - chop;
11207 if (prev == NULL)
11208 s = vim_strnsave(buf + tolist, len);
11209 else
11210 {
11211 s = alloc((unsigned)(prevlen + len + 1));
11212 if (s != NULL)
11213 {
11214 mch_memmove(s, prev, prevlen);
11215 vim_free(prev);
11216 prev = NULL;
11217 mch_memmove(s + prevlen, buf + tolist, len);
11218 s[prevlen + len] = NUL;
11219 }
11220 }
11221 tolist = filtd + 1;
11222
11223 li = listitem_alloc();
11224 if (li == NULL)
11225 {
11226 vim_free(s);
11227 break;
11228 }
11229 li->li_tv.v_type = VAR_STRING;
11230 li->li_tv.v_lock = 0;
11231 li->li_tv.vval.v_string = s;
11232 list_append(l, li);
11233
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011234 if (++cnt >= maxline)
11235 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011236 if (readlen <= 0)
11237 break;
11238 }
11239 else if (buf[filtd] == NUL)
11240 buf[filtd] = '\n';
11241 }
11242 if (readlen <= 0)
11243 break;
11244
11245 if (tolist == 0)
11246 {
11247 /* "buf" is full, need to move text to an allocated buffer */
11248 if (prev == NULL)
11249 {
11250 prev = vim_strnsave(buf, buflen);
11251 prevlen = buflen;
11252 }
11253 else
11254 {
11255 s = alloc((unsigned)(prevlen + buflen));
11256 if (s != NULL)
11257 {
11258 mch_memmove(s, prev, prevlen);
11259 mch_memmove(s + prevlen, buf, buflen);
11260 vim_free(prev);
11261 prev = s;
11262 prevlen += buflen;
11263 }
11264 }
11265 filtd = 0;
11266 }
11267 else
11268 {
11269 mch_memmove(buf, buf + tolist, buflen - tolist);
11270 filtd -= tolist;
11271 }
11272 }
11273
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011274 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011275 fclose(fd);
11276}
11277
11278
Bram Moolenaar0d660222005-01-07 21:51:51 +000011279#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11280static void make_connection __ARGS((void));
11281static int check_connection __ARGS((void));
11282
11283 static void
11284make_connection()
11285{
11286 if (X_DISPLAY == NULL
11287# ifdef FEAT_GUI
11288 && !gui.in_use
11289# endif
11290 )
11291 {
11292 x_force_connect = TRUE;
11293 setup_term_clip();
11294 x_force_connect = FALSE;
11295 }
11296}
11297
11298 static int
11299check_connection()
11300{
11301 make_connection();
11302 if (X_DISPLAY == NULL)
11303 {
11304 EMSG(_("E240: No connection to Vim server"));
11305 return FAIL;
11306 }
11307 return OK;
11308}
11309#endif
11310
11311#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011312static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011313
11314 static void
11315remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 typval_T *argvars;
11317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011318 int expr;
11319{
11320 char_u *server_name;
11321 char_u *keys;
11322 char_u *r = NULL;
11323 char_u buf[NUMBUFLEN];
11324# ifdef WIN32
11325 HWND w;
11326# else
11327 Window w;
11328# endif
11329
11330 if (check_restricted() || check_secure())
11331 return;
11332
11333# ifdef FEAT_X11
11334 if (check_connection() == FAIL)
11335 return;
11336# endif
11337
11338 server_name = get_tv_string(&argvars[0]);
11339 keys = get_tv_string_buf(&argvars[1], buf);
11340# ifdef WIN32
11341 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11342# else
11343 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11344 < 0)
11345# endif
11346 {
11347 if (r != NULL)
11348 EMSG(r); /* sending worked but evaluation failed */
11349 else
11350 EMSG2(_("E241: Unable to send to %s"), server_name);
11351 return;
11352 }
11353
11354 rettv->vval.v_string = r;
11355
11356 if (argvars[2].v_type != VAR_UNKNOWN)
11357 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011358 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011359 char_u str[30];
11360
11361 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011362 v.di_tv.v_type = VAR_STRING;
11363 v.di_tv.vval.v_string = vim_strsave(str);
11364 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11365 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366 }
11367}
11368#endif
11369
11370/*
11371 * "remote_expr()" function
11372 */
11373/*ARGSUSED*/
11374 static void
11375f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *argvars;
11377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011378{
11379 rettv->v_type = VAR_STRING;
11380 rettv->vval.v_string = NULL;
11381#ifdef FEAT_CLIENTSERVER
11382 remote_common(argvars, rettv, TRUE);
11383#endif
11384}
11385
11386/*
11387 * "remote_foreground()" function
11388 */
11389/*ARGSUSED*/
11390 static void
11391f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011392 typval_T *argvars;
11393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011394{
11395 rettv->vval.v_number = 0;
11396#ifdef FEAT_CLIENTSERVER
11397# ifdef WIN32
11398 /* On Win32 it's done in this application. */
11399 serverForeground(get_tv_string(&argvars[0]));
11400# else
11401 /* Send a foreground() expression to the server. */
11402 argvars[1].v_type = VAR_STRING;
11403 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11404 argvars[2].v_type = VAR_UNKNOWN;
11405 remote_common(argvars, rettv, TRUE);
11406 vim_free(argvars[1].vval.v_string);
11407# endif
11408#endif
11409}
11410
11411/*ARGSUSED*/
11412 static void
11413f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *argvars;
11415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011416{
11417#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011418 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011419 char_u *s = NULL;
11420# ifdef WIN32
11421 int n = 0;
11422# endif
11423
11424 if (check_restricted() || check_secure())
11425 {
11426 rettv->vval.v_number = -1;
11427 return;
11428 }
11429# ifdef WIN32
11430 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11431 if (n == 0)
11432 rettv->vval.v_number = -1;
11433 else
11434 {
11435 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11436 rettv->vval.v_number = (s != NULL);
11437 }
11438# else
11439 rettv->vval.v_number = 0;
11440 if (check_connection() == FAIL)
11441 return;
11442
11443 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11444 serverStrToWin(get_tv_string(&argvars[0])), &s);
11445# endif
11446
11447 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11448 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 v.di_tv.v_type = VAR_STRING;
11450 v.di_tv.vval.v_string = vim_strsave(s);
11451 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11452 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011453 }
11454#else
11455 rettv->vval.v_number = -1;
11456#endif
11457}
11458
11459/*ARGSUSED*/
11460 static void
11461f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *argvars;
11463 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011464{
11465 char_u *r = NULL;
11466
11467#ifdef FEAT_CLIENTSERVER
11468 if (!check_restricted() && !check_secure())
11469 {
11470# ifdef WIN32
11471 /* The server's HWND is encoded in the 'id' parameter */
11472 int n = 0;
11473
11474 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11475 if (n != 0)
11476 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11477 if (r == NULL)
11478# else
11479 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11480 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11481# endif
11482 EMSG(_("E277: Unable to read a server reply"));
11483 }
11484#endif
11485 rettv->v_type = VAR_STRING;
11486 rettv->vval.v_string = r;
11487}
11488
11489/*
11490 * "remote_send()" function
11491 */
11492/*ARGSUSED*/
11493 static void
11494f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *argvars;
11496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497{
11498 rettv->v_type = VAR_STRING;
11499 rettv->vval.v_string = NULL;
11500#ifdef FEAT_CLIENTSERVER
11501 remote_common(argvars, rettv, FALSE);
11502#endif
11503}
11504
11505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011506 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011507 */
11508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011510 typval_T *argvars;
11511 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011512{
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 list_T *l;
11514 listitem_T *item, *item2;
11515 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011516 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011517 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011518 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011519 dict_T *d;
11520 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011521
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011522 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011523 if (argvars[0].v_type == VAR_DICT)
11524 {
11525 if (argvars[2].v_type != VAR_UNKNOWN)
11526 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011527 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011528 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011529 {
11530 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011531 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011532 if (di == NULL)
11533 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011534 else
11535 {
11536 *rettv = di->di_tv;
11537 init_tv(&di->di_tv);
11538 dictitem_remove(d, di);
11539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011540 }
11541 }
11542 else if (argvars[0].v_type != VAR_LIST)
11543 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011544 else if ((l = argvars[0].vval.v_list) != NULL
11545 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011548 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011549 if (item == NULL)
11550 EMSGN(_(e_listidx), idx);
11551 else
11552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011553 if (argvars[2].v_type == VAR_UNKNOWN)
11554 {
11555 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011556 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011557 *rettv = item->li_tv;
11558 vim_free(item);
11559 }
11560 else
11561 {
11562 /* Remove range of items, return list with values. */
11563 end = get_tv_number(&argvars[2]);
11564 item2 = list_find(l, end);
11565 if (item2 == NULL)
11566 EMSGN(_(e_listidx), end);
11567 else
11568 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011569 int cnt = 0;
11570
11571 for (li = item; li != NULL; li = li->li_next)
11572 {
11573 ++cnt;
11574 if (li == item2)
11575 break;
11576 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011577 if (li == NULL) /* didn't find "item2" after "item" */
11578 EMSG(_(e_invrange));
11579 else
11580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011581 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011582 l = list_alloc();
11583 if (l != NULL)
11584 {
11585 rettv->v_type = VAR_LIST;
11586 rettv->vval.v_list = l;
11587 l->lv_first = item;
11588 l->lv_last = item2;
11589 l->lv_refcount = 1;
11590 item->li_prev = NULL;
11591 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011592 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011593 }
11594 }
11595 }
11596 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011597 }
11598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599}
11600
11601/*
11602 * "rename({from}, {to})" function
11603 */
11604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
11609 char_u buf[NUMBUFLEN];
11610
11611 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11615 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616}
11617
11618/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011619 * "repeat()" function
11620 */
11621/*ARGSUSED*/
11622 static void
11623f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011626{
11627 char_u *p;
11628 int n;
11629 int slen;
11630 int len;
11631 char_u *r;
11632 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011634
11635 n = get_tv_number(&argvars[1]);
11636 if (argvars[0].v_type == VAR_LIST)
11637 {
11638 l = list_alloc();
11639 if (l != NULL && argvars[0].vval.v_list != NULL)
11640 {
11641 l->lv_refcount = 1;
11642 while (n-- > 0)
11643 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11644 break;
11645 }
11646 rettv->v_type = VAR_LIST;
11647 rettv->vval.v_list = l;
11648 }
11649 else
11650 {
11651 p = get_tv_string(&argvars[0]);
11652 rettv->v_type = VAR_STRING;
11653 rettv->vval.v_string = NULL;
11654
11655 slen = (int)STRLEN(p);
11656 len = slen * n;
11657 if (len <= 0)
11658 return;
11659
11660 r = alloc(len + 1);
11661 if (r != NULL)
11662 {
11663 for (i = 0; i < n; i++)
11664 mch_memmove(r + i * slen, p, (size_t)slen);
11665 r[len] = NUL;
11666 }
11667
11668 rettv->vval.v_string = r;
11669 }
11670}
11671
11672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 * "resolve()" function
11674 */
11675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011677 typval_T *argvars;
11678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
11680 char_u *p;
11681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683#ifdef FEAT_SHORTCUT
11684 {
11685 char_u *v = NULL;
11686
11687 v = mch_resolve_shortcut(p);
11688 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 }
11693#else
11694# ifdef HAVE_READLINK
11695 {
11696 char_u buf[MAXPATHL + 1];
11697 char_u *cpy;
11698 int len;
11699 char_u *remain = NULL;
11700 char_u *q;
11701 int is_relative_to_current = FALSE;
11702 int has_trailing_pathsep = FALSE;
11703 int limit = 100;
11704
11705 p = vim_strsave(p);
11706
11707 if (p[0] == '.' && (vim_ispathsep(p[1])
11708 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11709 is_relative_to_current = TRUE;
11710
11711 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011712 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 has_trailing_pathsep = TRUE;
11714
11715 q = getnextcomp(p);
11716 if (*q != NUL)
11717 {
11718 /* Separate the first path component in "p", and keep the
11719 * remainder (beginning with the path separator). */
11720 remain = vim_strsave(q - 1);
11721 q[-1] = NUL;
11722 }
11723
11724 for (;;)
11725 {
11726 for (;;)
11727 {
11728 len = readlink((char *)p, (char *)buf, MAXPATHL);
11729 if (len <= 0)
11730 break;
11731 buf[len] = NUL;
11732
11733 if (limit-- == 0)
11734 {
11735 vim_free(p);
11736 vim_free(remain);
11737 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 goto fail;
11740 }
11741
11742 /* Ensure that the result will have a trailing path separator
11743 * if the argument has one. */
11744 if (remain == NULL && has_trailing_pathsep)
11745 add_pathsep(buf);
11746
11747 /* Separate the first path component in the link value and
11748 * concatenate the remainders. */
11749 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11750 if (*q != NUL)
11751 {
11752 if (remain == NULL)
11753 remain = vim_strsave(q - 1);
11754 else
11755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011756 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 if (cpy != NULL)
11758 {
11759 STRCAT(cpy, remain);
11760 vim_free(remain);
11761 remain = cpy;
11762 }
11763 }
11764 q[-1] = NUL;
11765 }
11766
11767 q = gettail(p);
11768 if (q > p && *q == NUL)
11769 {
11770 /* Ignore trailing path separator. */
11771 q[-1] = NUL;
11772 q = gettail(p);
11773 }
11774 if (q > p && !mch_isFullName(buf))
11775 {
11776 /* symlink is relative to directory of argument */
11777 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11778 if (cpy != NULL)
11779 {
11780 STRCPY(cpy, p);
11781 STRCPY(gettail(cpy), buf);
11782 vim_free(p);
11783 p = cpy;
11784 }
11785 }
11786 else
11787 {
11788 vim_free(p);
11789 p = vim_strsave(buf);
11790 }
11791 }
11792
11793 if (remain == NULL)
11794 break;
11795
11796 /* Append the first path component of "remain" to "p". */
11797 q = getnextcomp(remain + 1);
11798 len = q - remain - (*q != NUL);
11799 cpy = vim_strnsave(p, STRLEN(p) + len);
11800 if (cpy != NULL)
11801 {
11802 STRNCAT(cpy, remain, len);
11803 vim_free(p);
11804 p = cpy;
11805 }
11806 /* Shorten "remain". */
11807 if (*q != NUL)
11808 STRCPY(remain, q - 1);
11809 else
11810 {
11811 vim_free(remain);
11812 remain = NULL;
11813 }
11814 }
11815
11816 /* If the result is a relative path name, make it explicitly relative to
11817 * the current directory if and only if the argument had this form. */
11818 if (!vim_ispathsep(*p))
11819 {
11820 if (is_relative_to_current
11821 && *p != NUL
11822 && !(p[0] == '.'
11823 && (p[1] == NUL
11824 || vim_ispathsep(p[1])
11825 || (p[1] == '.'
11826 && (p[2] == NUL
11827 || vim_ispathsep(p[2]))))))
11828 {
11829 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011830 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 if (cpy != NULL)
11832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 vim_free(p);
11834 p = cpy;
11835 }
11836 }
11837 else if (!is_relative_to_current)
11838 {
11839 /* Strip leading "./". */
11840 q = p;
11841 while (q[0] == '.' && vim_ispathsep(q[1]))
11842 q += 2;
11843 if (q > p)
11844 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11845 }
11846 }
11847
11848 /* Ensure that the result will have no trailing path separator
11849 * if the argument had none. But keep "/" or "//". */
11850 if (!has_trailing_pathsep)
11851 {
11852 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011853 if (after_pathsep(p, q))
11854 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 }
11856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 }
11859# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861# endif
11862#endif
11863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865
11866#ifdef HAVE_READLINK
11867fail:
11868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870}
11871
11872/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 */
11875 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011876f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 typval_T *argvars;
11878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879{
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 list_T *l;
11881 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883 rettv->vval.v_number = 0;
11884 if (argvars[0].v_type != VAR_LIST)
11885 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011886 else if ((l = argvars[0].vval.v_list) != NULL
11887 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 {
11889 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011890 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011891 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 while (li != NULL)
11893 {
11894 ni = li->li_prev;
11895 list_append(l, li);
11896 li = ni;
11897 }
11898 rettv->vval.v_list = l;
11899 rettv->v_type = VAR_LIST;
11900 ++l->lv_refcount;
11901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902}
11903
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011904#define SP_NOMOVE 1 /* don't move cursor */
11905#define SP_REPEAT 2 /* repeat to find outer pair */
11906#define SP_RETCOUNT 4 /* return matchcount */
11907
Bram Moolenaar33570922005-01-25 22:26:29 +000011908static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011909
11910/*
11911 * Get flags for a search function.
11912 * Possibly sets "p_ws".
11913 * Returns BACKWARD, FORWARD or zero (for an error).
11914 */
11915 static int
11916get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011918 int *flagsp;
11919{
11920 int dir = FORWARD;
11921 char_u *flags;
11922 char_u nbuf[NUMBUFLEN];
11923 int mask;
11924
11925 if (varp->v_type != VAR_UNKNOWN)
11926 {
11927 flags = get_tv_string_buf(varp, nbuf);
11928 while (*flags != NUL)
11929 {
11930 switch (*flags)
11931 {
11932 case 'b': dir = BACKWARD; break;
11933 case 'w': p_ws = TRUE; break;
11934 case 'W': p_ws = FALSE; break;
11935 default: mask = 0;
11936 if (flagsp != NULL)
11937 switch (*flags)
11938 {
11939 case 'n': mask = SP_NOMOVE; break;
11940 case 'r': mask = SP_REPEAT; break;
11941 case 'm': mask = SP_RETCOUNT; break;
11942 }
11943 if (mask == 0)
11944 {
11945 EMSG2(_(e_invarg2), flags);
11946 dir = 0;
11947 }
11948 else
11949 *flagsp |= mask;
11950 }
11951 if (dir == 0)
11952 break;
11953 ++flags;
11954 }
11955 }
11956 return dir;
11957}
11958
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959/*
11960 * "search()" function
11961 */
11962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011964 typval_T *argvars;
11965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967 char_u *pat;
11968 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011969 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 int save_p_ws = p_ws;
11971 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011972 int flags = 0;
11973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011977 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11978 if (dir == 0)
11979 goto theend;
11980 if ((flags & ~SP_NOMOVE) != 0)
11981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011983 goto theend;
11984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011986 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11988 SEARCH_KEEP, RE_SEARCH) != FAIL)
11989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 curwin->w_cursor = pos;
11992 /* "/$" will put the cursor after the end of the line, may need to
11993 * correct that here */
11994 check_cursor();
11995 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011996
11997 /* If 'n' flag is used: restore cursor position. */
11998 if (flags & SP_NOMOVE)
11999 curwin->w_cursor = save_cursor;
12000theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 p_ws = save_p_ws;
12002}
12003
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004/*
12005 * "searchpair()" function
12006 */
12007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 typval_T *argvars;
12010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
12012 char_u *spat, *mpat, *epat;
12013 char_u *skip;
12014 char_u *pat, *pat2, *pat3;
12015 pos_T pos;
12016 pos_T firstpos;
12017 pos_T save_cursor;
12018 pos_T save_pos;
12019 int save_p_ws = p_ws;
12020 char_u *save_cpo;
12021 int dir;
12022 int flags = 0;
12023 char_u nbuf1[NUMBUFLEN];
12024 char_u nbuf2[NUMBUFLEN];
12025 char_u nbuf3[NUMBUFLEN];
12026 int n;
12027 int r;
12028 int nest = 1;
12029 int err;
12030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
12033 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12034 save_cpo = p_cpo;
12035 p_cpo = (char_u *)"";
12036
12037 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 spat = get_tv_string(&argvars[0]);
12039 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12040 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041
12042 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12043 * start/middle/end (pat3, for the top pair). */
12044 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12045 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12046 if (pat2 == NULL || pat3 == NULL)
12047 goto theend;
12048 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12049 if (*mpat == NUL)
12050 STRCPY(pat3, pat2);
12051 else
12052 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12053 spat, epat, mpat);
12054
12055 /* Handle the optional fourth argument: flags */
12056 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012057 if (dir == 0)
12058 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
12060 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012061 if (argvars[3].v_type == VAR_UNKNOWN
12062 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 skip = (char_u *)"";
12064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066
12067 save_cursor = curwin->w_cursor;
12068 pos = curwin->w_cursor;
12069 firstpos.lnum = 0;
12070 pat = pat3;
12071 for (;;)
12072 {
12073 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12074 SEARCH_KEEP, RE_SEARCH);
12075 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12076 /* didn't find it or found the first match again: FAIL */
12077 break;
12078
12079 if (firstpos.lnum == 0)
12080 firstpos = pos;
12081
12082 /* If the skip pattern matches, ignore this match. */
12083 if (*skip != NUL)
12084 {
12085 save_pos = curwin->w_cursor;
12086 curwin->w_cursor = pos;
12087 r = eval_to_bool(skip, &err, NULL, FALSE);
12088 curwin->w_cursor = save_pos;
12089 if (err)
12090 {
12091 /* Evaluating {skip} caused an error, break here. */
12092 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 break;
12095 }
12096 if (r)
12097 continue;
12098 }
12099
12100 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12101 {
12102 /* Found end when searching backwards or start when searching
12103 * forward: nested pair. */
12104 ++nest;
12105 pat = pat2; /* nested, don't search for middle */
12106 }
12107 else
12108 {
12109 /* Found end when searching forward or start when searching
12110 * backward: end of (nested) pair; or found middle in outer pair. */
12111 if (--nest == 1)
12112 pat = pat3; /* outer level, search for middle */
12113 }
12114
12115 if (nest == 0)
12116 {
12117 /* Found the match: return matchcount or line number. */
12118 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 curwin->w_cursor = pos;
12123 if (!(flags & SP_REPEAT))
12124 break;
12125 nest = 1; /* search for next unmatched */
12126 }
12127 }
12128
12129 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 curwin->w_cursor = save_cursor;
12132
12133theend:
12134 vim_free(pat2);
12135 vim_free(pat3);
12136 p_ws = save_p_ws;
12137 p_cpo = save_cpo;
12138}
12139
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140/*ARGSUSED*/
12141 static void
12142f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146#ifdef FEAT_CLIENTSERVER
12147 char_u buf[NUMBUFLEN];
12148 char_u *server = get_tv_string(&argvars[0]);
12149 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151 rettv->vval.v_number = -1;
12152 if (check_restricted() || check_secure())
12153 return;
12154# ifdef FEAT_X11
12155 if (check_connection() == FAIL)
12156 return;
12157# endif
12158
12159 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 EMSG(_("E258: Unable to send to client"));
12162 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 rettv->vval.v_number = 0;
12165#else
12166 rettv->vval.v_number = -1;
12167#endif
12168}
12169
12170/*ARGSUSED*/
12171 static void
12172f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 typval_T *argvars;
12174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175{
12176 char_u *r = NULL;
12177
12178#ifdef FEAT_CLIENTSERVER
12179# ifdef WIN32
12180 r = serverGetVimNames();
12181# else
12182 make_connection();
12183 if (X_DISPLAY != NULL)
12184 r = serverGetVimNames(X_DISPLAY);
12185# endif
12186#endif
12187 rettv->v_type = VAR_STRING;
12188 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190
12191/*
12192 * "setbufvar()" function
12193 */
12194/*ARGSUSED*/
12195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012197 typval_T *argvars;
12198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199{
12200 buf_T *buf;
12201#ifdef FEAT_AUTOCMD
12202 aco_save_T aco;
12203#else
12204 buf_T *save_curbuf;
12205#endif
12206 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 char_u nbuf[NUMBUFLEN];
12209
12210 if (check_restricted() || check_secure())
12211 return;
12212 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 buf = get_buf_tv(&argvars[0]);
12214 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 varp = &argvars[2];
12216
12217 if (buf != NULL && varname != NULL && varp != NULL)
12218 {
12219 /* set curbuf to be our buf, temporarily */
12220#ifdef FEAT_AUTOCMD
12221 aucmd_prepbuf(&aco, buf);
12222#else
12223 save_curbuf = curbuf;
12224 curbuf = buf;
12225#endif
12226
12227 if (*varname == '&')
12228 {
12229 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 set_option_value(varname, get_tv_number(varp),
12231 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 }
12233 else
12234 {
12235 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12236 if (bufvarname != NULL)
12237 {
12238 STRCPY(bufvarname, "b:");
12239 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012240 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 vim_free(bufvarname);
12242 }
12243 }
12244
12245 /* reset notion of buffer */
12246#ifdef FEAT_AUTOCMD
12247 aucmd_restbuf(&aco);
12248#else
12249 curbuf = save_curbuf;
12250#endif
12251 }
12252 --emsg_off;
12253}
12254
12255/*
12256 * "setcmdpos()" function
12257 */
12258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012260 typval_T *argvars;
12261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263 rettv->vval.v_number = set_cmdline_pos(
12264 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265}
12266
12267/*
12268 * "setline()" function
12269 */
12270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012271f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012272 typval_T *argvars;
12273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274{
12275 linenr_T lnum;
12276 char_u *line;
12277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 lnum = get_tv_lnum(argvars);
12279 line = get_tv_string(&argvars[1]);
12280 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281
12282 if (lnum >= 1
12283 && lnum <= curbuf->b_ml.ml_line_count
12284 && u_savesub(lnum) == OK
12285 && ml_replace(lnum, line, TRUE) == OK)
12286 {
12287 changed_bytes(lnum, 0);
12288 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 }
12291}
12292
12293/*
12294 * "setreg()" function
12295 */
12296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012298 typval_T *argvars;
12299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300{
12301 int regname;
12302 char_u *strregname;
12303 char_u *stropt;
12304 int append;
12305 char_u yank_type;
12306 long block_len;
12307
12308 block_len = -1;
12309 yank_type = MAUTO;
12310 append = FALSE;
12311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 strregname = get_tv_string(argvars);
12313 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314
12315 regname = (strregname == NULL ? '"' : *strregname);
12316 if (regname == 0 || regname == '@')
12317 regname = '"';
12318 else if (regname == '=')
12319 return;
12320
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 switch (*stropt)
12325 {
12326 case 'a': case 'A': /* append */
12327 append = TRUE;
12328 break;
12329 case 'v': case 'c': /* character-wise selection */
12330 yank_type = MCHAR;
12331 break;
12332 case 'V': case 'l': /* line-wise selection */
12333 yank_type = MLINE;
12334 break;
12335#ifdef FEAT_VISUAL
12336 case 'b': case Ctrl_V: /* block-wise selection */
12337 yank_type = MBLOCK;
12338 if (VIM_ISDIGIT(stropt[1]))
12339 {
12340 ++stropt;
12341 block_len = getdigits(&stropt) - 1;
12342 --stropt;
12343 }
12344 break;
12345#endif
12346 }
12347 }
12348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012351 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352}
12353
12354
12355/*
12356 * "setwinvar(expr)" function
12357 */
12358/*ARGSUSED*/
12359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012361 typval_T *argvars;
12362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363{
12364 win_T *win;
12365#ifdef FEAT_WINDOWS
12366 win_T *save_curwin;
12367#endif
12368 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 char_u nbuf[NUMBUFLEN];
12371
12372 if (check_restricted() || check_secure())
12373 return;
12374 ++emsg_off;
12375 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 varp = &argvars[2];
12378
12379 if (win != NULL && varname != NULL && varp != NULL)
12380 {
12381#ifdef FEAT_WINDOWS
12382 /* set curwin to be our win, temporarily */
12383 save_curwin = curwin;
12384 curwin = win;
12385 curbuf = curwin->w_buffer;
12386#endif
12387
12388 if (*varname == '&')
12389 {
12390 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 set_option_value(varname, get_tv_number(varp),
12392 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 }
12394 else
12395 {
12396 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12397 if (winvarname != NULL)
12398 {
12399 STRCPY(winvarname, "w:");
12400 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012401 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 vim_free(winvarname);
12403 }
12404 }
12405
12406#ifdef FEAT_WINDOWS
12407 /* Restore current window, if it's still valid (autocomands can make
12408 * it invalid). */
12409 if (win_valid(save_curwin))
12410 {
12411 curwin = save_curwin;
12412 curbuf = curwin->w_buffer;
12413 }
12414#endif
12415 }
12416 --emsg_off;
12417}
12418
12419/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012420 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 */
12422 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012423f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012424 typval_T *argvars;
12425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012427 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429 p = get_tv_string(&argvars[0]);
12430 rettv->vval.v_string = vim_strsave(p);
12431 simplify_filename(rettv->vval.v_string); /* simplify in place */
12432 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433}
12434
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435static int
12436#ifdef __BORLANDC__
12437 _RTLENTRYF
12438#endif
12439 item_compare __ARGS((const void *s1, const void *s2));
12440static int
12441#ifdef __BORLANDC__
12442 _RTLENTRYF
12443#endif
12444 item_compare2 __ARGS((const void *s1, const void *s2));
12445
12446static int item_compare_ic;
12447static char_u *item_compare_func;
12448#define ITEM_COMPARE_FAIL 999
12449
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012451 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012453 static int
12454#ifdef __BORLANDC__
12455_RTLENTRYF
12456#endif
12457item_compare(s1, s2)
12458 const void *s1;
12459 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012461 char_u *p1, *p2;
12462 char_u *tofree1, *tofree2;
12463 int res;
12464 char_u numbuf1[NUMBUFLEN];
12465 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12468 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012469 if (item_compare_ic)
12470 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012472 res = STRCMP(p1, p2);
12473 vim_free(tofree1);
12474 vim_free(tofree2);
12475 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476}
12477
12478 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479#ifdef __BORLANDC__
12480_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482item_compare2(s1, s2)
12483 const void *s1;
12484 const void *s2;
12485{
12486 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012487 typval_T rettv;
12488 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012491 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12492 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012493 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12494 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495
12496 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12497 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012498 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499 clear_tv(&argv[0]);
12500 clear_tv(&argv[1]);
12501
12502 if (res == FAIL)
12503 res = ITEM_COMPARE_FAIL;
12504 else
12505 res = get_tv_number(&rettv);
12506 clear_tv(&rettv);
12507 return res;
12508}
12509
12510/*
12511 * "sort({list})" function
12512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012514f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012515 typval_T *argvars;
12516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517{
Bram Moolenaar33570922005-01-25 22:26:29 +000012518 list_T *l;
12519 listitem_T *li;
12520 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521 long len;
12522 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 rettv->vval.v_number = 0;
12525 if (argvars[0].v_type != VAR_LIST)
12526 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 else
12528 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012530 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531 return;
12532 rettv->vval.v_list = l;
12533 rettv->v_type = VAR_LIST;
12534 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
Bram Moolenaar0d660222005-01-07 21:51:51 +000012536 len = list_len(l);
12537 if (len <= 1)
12538 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 item_compare_ic = FALSE;
12541 item_compare_func = NULL;
12542 if (argvars[1].v_type != VAR_UNKNOWN)
12543 {
12544 if (argvars[1].v_type == VAR_FUNC)
12545 item_compare_func = argvars[0].vval.v_string;
12546 else
12547 {
12548 i = get_tv_number(&argvars[1]);
12549 if (i == 1)
12550 item_compare_ic = TRUE;
12551 else
12552 item_compare_func = get_tv_string(&argvars[1]);
12553 }
12554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012557 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558 if (ptrs == NULL)
12559 return;
12560 i = 0;
12561 for (li = l->lv_first; li != NULL; li = li->li_next)
12562 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564 /* test the compare function */
12565 if (item_compare_func != NULL
12566 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12567 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012568 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 {
12571 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012572 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 item_compare_func == NULL ? item_compare : item_compare2);
12574
12575 /* Clear the List and append the items in the sorted order. */
12576 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012577 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578 for (i = 0; i < len; ++i)
12579 list_append(l, ptrs[i]);
12580 }
12581
12582 vim_free(ptrs);
12583 }
12584}
12585
12586 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012587f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012588 typval_T *argvars;
12589 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590{
12591 char_u *str;
12592 char_u *end;
12593 char_u *pat;
12594 regmatch_T regmatch;
12595 char_u patbuf[NUMBUFLEN];
12596 char_u *save_cpo;
12597 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012598 listitem_T *ni;
12599 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 colnr_T col = 0;
12601
12602 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12603 save_cpo = p_cpo;
12604 p_cpo = (char_u *)"";
12605
12606 str = get_tv_string(&argvars[0]);
12607 if (argvars[1].v_type == VAR_UNKNOWN)
12608 pat = (char_u *)"[\\x01- ]\\+";
12609 else
12610 pat = get_tv_string_buf(&argvars[1], patbuf);
12611
12612 l = list_alloc();
12613 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615 rettv->v_type = VAR_LIST;
12616 rettv->vval.v_list = l;
12617 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12620 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012622 regmatch.rm_ic = FALSE;
12623 while (*str != NUL)
12624 {
12625 match = vim_regexec_nl(&regmatch, str, col);
12626 if (match)
12627 end = regmatch.startp[0];
12628 else
12629 end = str + STRLEN(str);
12630 if (end > str)
12631 {
12632 ni = listitem_alloc();
12633 if (ni == NULL)
12634 break;
12635 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012636 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012637 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12638 list_append(l, ni);
12639 }
12640 if (!match)
12641 break;
12642 /* Advance to just after the match. */
12643 if (regmatch.endp[0] > str)
12644 col = 0;
12645 else
12646 {
12647 /* Don't get stuck at the same match. */
12648#ifdef FEAT_MBYTE
12649 col = mb_ptr2len_check(regmatch.endp[0]);
12650#else
12651 col = 1;
12652#endif
12653 }
12654 str = regmatch.endp[0];
12655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659
Bram Moolenaar0d660222005-01-07 21:51:51 +000012660 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661}
12662
12663#ifdef HAVE_STRFTIME
12664/*
12665 * "strftime({format}[, {time}])" function
12666 */
12667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *argvars;
12670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671{
12672 char_u result_buf[256];
12673 struct tm *curtime;
12674 time_t seconds;
12675 char_u *p;
12676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012680 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 seconds = time(NULL);
12682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012683 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 curtime = localtime(&seconds);
12685 /* MSVC returns NULL for an invalid value of seconds. */
12686 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 else
12689 {
12690# ifdef FEAT_MBYTE
12691 vimconv_T conv;
12692 char_u *enc;
12693
12694 conv.vc_type = CONV_NONE;
12695 enc = enc_locale();
12696 convert_setup(&conv, p_enc, enc);
12697 if (conv.vc_type != CONV_NONE)
12698 p = string_convert(&conv, p, NULL);
12699# endif
12700 if (p != NULL)
12701 (void)strftime((char *)result_buf, sizeof(result_buf),
12702 (char *)p, curtime);
12703 else
12704 result_buf[0] = NUL;
12705
12706# ifdef FEAT_MBYTE
12707 if (conv.vc_type != CONV_NONE)
12708 vim_free(p);
12709 convert_setup(&conv, enc, p_enc);
12710 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 else
12713# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715
12716# ifdef FEAT_MBYTE
12717 /* Release conversion descriptors */
12718 convert_setup(&conv, NULL, NULL);
12719 vim_free(enc);
12720# endif
12721 }
12722}
12723#endif
12724
12725/*
12726 * "stridx()" function
12727 */
12728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012730 typval_T *argvars;
12731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732{
12733 char_u buf[NUMBUFLEN];
12734 char_u *needle;
12735 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012736 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012738 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743
Bram Moolenaar33570922005-01-25 22:26:29 +000012744 if (argvars[2].v_type != VAR_UNKNOWN)
12745 {
12746 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012747 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012748 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012749 if (start_idx >= 0)
12750 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 }
12752
12753 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12754 if (pos != NULL)
12755 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756}
12757
12758/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012759 * "string()" function
12760 */
12761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012765{
12766 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012767 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012770 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012771 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773}
12774
12775/*
12776 * "strlen()" function
12777 */
12778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012780 typval_T *argvars;
12781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783 rettv->vval.v_number = (varnumber_T)(STRLEN(
12784 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785}
12786
12787/*
12788 * "strpart()" function
12789 */
12790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794{
12795 char_u *p;
12796 int n;
12797 int len;
12798 int slen;
12799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 slen = (int)STRLEN(p);
12802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 else
12807 len = slen - n; /* default len: all bytes that are available. */
12808
12809 /*
12810 * Only return the overlap between the specified part and the actual
12811 * string.
12812 */
12813 if (n < 0)
12814 {
12815 len += n;
12816 n = 0;
12817 }
12818 else if (n > slen)
12819 n = slen;
12820 if (len < 0)
12821 len = 0;
12822 else if (n + len > slen)
12823 len = slen - n;
12824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 rettv->v_type = VAR_STRING;
12826 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827}
12828
12829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012830 * "strridx()" function
12831 */
12832 static void
12833f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 typval_T *argvars;
12835 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012836{
12837 char_u buf[NUMBUFLEN];
12838 char_u *needle;
12839 char_u *haystack;
12840 char_u *rest;
12841 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012842 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012843
12844 needle = get_tv_string(&argvars[1]);
12845 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012846 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012847 if (argvars[2].v_type != VAR_UNKNOWN)
12848 {
12849 /* Third argument: upper limit for index */
12850 end_idx = get_tv_number(&argvars[2]);
12851 if (end_idx < 0)
12852 {
12853 /* can never find a match */
12854 rettv->vval.v_number = -1;
12855 return;
12856 }
12857 }
12858 else
12859 end_idx = haystack_len;
12860
Bram Moolenaar0d660222005-01-07 21:51:51 +000012861 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012862 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012863 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012864 lastmatch = haystack + end_idx;
12865 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012867 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868 for (rest = haystack; *rest != '\0'; ++rest)
12869 {
12870 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012871 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012872 break;
12873 lastmatch = rest;
12874 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012875 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012876
12877 if (lastmatch == NULL)
12878 rettv->vval.v_number = -1;
12879 else
12880 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12881}
12882
12883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 * "strtrans()" function
12885 */
12886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012888 typval_T *argvars;
12889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 rettv->v_type = VAR_STRING;
12892 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893}
12894
12895/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012896 * "submatch()" function
12897 */
12898 static void
12899f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012900 typval_T *argvars;
12901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012902{
12903 rettv->v_type = VAR_STRING;
12904 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12905}
12906
12907/*
12908 * "substitute()" function
12909 */
12910 static void
12911f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 typval_T *argvars;
12913 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914{
12915 char_u patbuf[NUMBUFLEN];
12916 char_u subbuf[NUMBUFLEN];
12917 char_u flagsbuf[NUMBUFLEN];
12918
12919 rettv->v_type = VAR_STRING;
12920 rettv->vval.v_string = do_string_sub(
12921 get_tv_string(&argvars[0]),
12922 get_tv_string_buf(&argvars[1], patbuf),
12923 get_tv_string_buf(&argvars[2], subbuf),
12924 get_tv_string_buf(&argvars[3], flagsbuf));
12925}
12926
12927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 * "synID(line, col, trans)" function
12929 */
12930/*ARGSUSED*/
12931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012933 typval_T *argvars;
12934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935{
12936 int id = 0;
12937#ifdef FEAT_SYN_HL
12938 long line;
12939 long col;
12940 int trans;
12941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 line = get_tv_lnum(argvars);
12943 col = get_tv_number(&argvars[1]) - 1;
12944 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
12946 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12947 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12948 id = syn_get_id(line, col, trans);
12949#endif
12950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952}
12953
12954/*
12955 * "synIDattr(id, what [, mode])" function
12956 */
12957/*ARGSUSED*/
12958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 typval_T *argvars;
12961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962{
12963 char_u *p = NULL;
12964#ifdef FEAT_SYN_HL
12965 int id;
12966 char_u *what;
12967 char_u *mode;
12968 char_u modebuf[NUMBUFLEN];
12969 int modec;
12970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 id = get_tv_number(&argvars[0]);
12972 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012973 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 modec = TOLOWER_ASC(mode[0]);
12977 if (modec != 't' && modec != 'c'
12978#ifdef FEAT_GUI
12979 && modec != 'g'
12980#endif
12981 )
12982 modec = 0; /* replace invalid with current */
12983 }
12984 else
12985 {
12986#ifdef FEAT_GUI
12987 if (gui.in_use)
12988 modec = 'g';
12989 else
12990#endif
12991 if (t_colors > 1)
12992 modec = 'c';
12993 else
12994 modec = 't';
12995 }
12996
12997
12998 switch (TOLOWER_ASC(what[0]))
12999 {
13000 case 'b':
13001 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13002 p = highlight_color(id, what, modec);
13003 else /* bold */
13004 p = highlight_has_attr(id, HL_BOLD, modec);
13005 break;
13006
13007 case 'f': /* fg[#] */
13008 p = highlight_color(id, what, modec);
13009 break;
13010
13011 case 'i':
13012 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13013 p = highlight_has_attr(id, HL_INVERSE, modec);
13014 else /* italic */
13015 p = highlight_has_attr(id, HL_ITALIC, modec);
13016 break;
13017
13018 case 'n': /* name */
13019 p = get_highlight_name(NULL, id - 1);
13020 break;
13021
13022 case 'r': /* reverse */
13023 p = highlight_has_attr(id, HL_INVERSE, modec);
13024 break;
13025
13026 case 's': /* standout */
13027 p = highlight_has_attr(id, HL_STANDOUT, modec);
13028 break;
13029
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013030 case 'u':
13031 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13032 /* underline */
13033 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13034 else
13035 /* undercurl */
13036 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 break;
13038 }
13039
13040 if (p != NULL)
13041 p = vim_strsave(p);
13042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->v_type = VAR_STRING;
13044 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045}
13046
13047/*
13048 * "synIDtrans(id)" function
13049 */
13050/*ARGSUSED*/
13051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013053 typval_T *argvars;
13054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055{
13056 int id;
13057
13058#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060
13061 if (id > 0)
13062 id = syn_get_final_id(id);
13063 else
13064#endif
13065 id = 0;
13066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013067 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068}
13069
13070/*
13071 * "system()" function
13072 */
13073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *argvars;
13076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013078 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013080 char_u *infile = NULL;
13081 char_u buf[NUMBUFLEN];
13082 int err = FALSE;
13083 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013085 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013086 {
13087 /*
13088 * Write the string to a temp file, to be used for input of the shell
13089 * command.
13090 */
13091 if ((infile = vim_tempname('i')) == NULL)
13092 {
13093 EMSG(_(e_notmp));
13094 return;
13095 }
13096
13097 fd = mch_fopen((char *)infile, WRITEBIN);
13098 if (fd == NULL)
13099 {
13100 EMSG2(_(e_notopen), infile);
13101 goto done;
13102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013104 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13105 err = TRUE;
13106 if (fclose(fd) != 0)
13107 err = TRUE;
13108 if (err)
13109 {
13110 EMSG(_("E677: Error writing temp file"));
13111 goto done;
13112 }
13113 }
13114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013116
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117#ifdef USE_CR
13118 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013119 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 {
13121 char_u *s;
13122
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013123 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 {
13125 if (*s == CAR)
13126 *s = NL;
13127 }
13128 }
13129#else
13130# ifdef USE_CRNL
13131 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013132 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 {
13134 char_u *s, *d;
13135
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013136 d = res;
13137 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 {
13139 if (s[0] == CAR && s[1] == NL)
13140 ++s;
13141 *d++ = *s;
13142 }
13143 *d = NUL;
13144 }
13145# endif
13146#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013147
13148done:
13149 if (infile != NULL)
13150 {
13151 mch_remove(infile);
13152 vim_free(infile);
13153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154 rettv->v_type = VAR_STRING;
13155 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156}
13157
13158/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013159 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013160 */
13161 static void
13162f_taglist(argvars, rettv)
13163 typval_T *argvars;
13164 typval_T *rettv;
13165{
13166 char_u *tag_pattern;
13167 list_T *l;
13168
13169 tag_pattern = get_tv_string(&argvars[0]);
13170
13171 rettv->vval.v_number = FALSE;
13172
13173 l = list_alloc();
13174 if (l != NULL)
13175 {
13176 if (get_tags(l, tag_pattern) != FAIL)
13177 {
13178 rettv->vval.v_list = l;
13179 rettv->v_type = VAR_LIST;
13180 ++l->lv_refcount;
13181 }
13182 else
13183 list_free(l);
13184 }
13185}
13186
13187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 * "tempname()" function
13189 */
13190/*ARGSUSED*/
13191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013193 typval_T *argvars;
13194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195{
13196 static int x = 'A';
13197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 rettv->v_type = VAR_STRING;
13199 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200
13201 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13202 * names. Skip 'I' and 'O', they are used for shell redirection. */
13203 do
13204 {
13205 if (x == 'Z')
13206 x = '0';
13207 else if (x == '9')
13208 x = 'A';
13209 else
13210 {
13211#ifdef EBCDIC
13212 if (x == 'I')
13213 x = 'J';
13214 else if (x == 'R')
13215 x = 'S';
13216 else
13217#endif
13218 ++x;
13219 }
13220 } while (x == 'I' || x == 'O');
13221}
13222
13223/*
13224 * "tolower(string)" function
13225 */
13226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *argvars;
13229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230{
13231 char_u *p;
13232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 p = vim_strsave(get_tv_string(&argvars[0]));
13234 rettv->v_type = VAR_STRING;
13235 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236
13237 if (p != NULL)
13238 while (*p != NUL)
13239 {
13240#ifdef FEAT_MBYTE
13241 int l;
13242
13243 if (enc_utf8)
13244 {
13245 int c, lc;
13246
13247 c = utf_ptr2char(p);
13248 lc = utf_tolower(c);
13249 l = utf_ptr2len_check(p);
13250 /* TODO: reallocate string when byte count changes. */
13251 if (utf_char2len(lc) == l)
13252 utf_char2bytes(lc, p);
13253 p += l;
13254 }
13255 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13256 p += l; /* skip multi-byte character */
13257 else
13258#endif
13259 {
13260 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13261 ++p;
13262 }
13263 }
13264}
13265
13266/*
13267 * "toupper(string)" function
13268 */
13269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 typval_T *argvars;
13272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273{
13274 char_u *p;
13275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 p = vim_strsave(get_tv_string(&argvars[0]));
13277 rettv->v_type = VAR_STRING;
13278 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
13280 if (p != NULL)
13281 while (*p != NUL)
13282 {
13283#ifdef FEAT_MBYTE
13284 int l;
13285
13286 if (enc_utf8)
13287 {
13288 int c, uc;
13289
13290 c = utf_ptr2char(p);
13291 uc = utf_toupper(c);
13292 l = utf_ptr2len_check(p);
13293 /* TODO: reallocate string when byte count changes. */
13294 if (utf_char2len(uc) == l)
13295 utf_char2bytes(uc, p);
13296 p += l;
13297 }
13298 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13299 p += l; /* skip multi-byte character */
13300 else
13301#endif
13302 {
13303 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13304 p++;
13305 }
13306 }
13307}
13308
13309/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013310 * "tr(string, fromstr, tostr)" function
13311 */
13312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013314 typval_T *argvars;
13315 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013316{
13317 char_u *instr;
13318 char_u *fromstr;
13319 char_u *tostr;
13320 char_u *p;
13321#ifdef FEAT_MBYTE
13322 int inlen;
13323 int fromlen;
13324 int tolen;
13325 int idx;
13326 char_u *cpstr;
13327 int cplen;
13328 int first = TRUE;
13329#endif
13330 char_u buf[NUMBUFLEN];
13331 char_u buf2[NUMBUFLEN];
13332 garray_T ga;
13333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 instr = get_tv_string(&argvars[0]);
13335 fromstr = get_tv_string_buf(&argvars[1], buf);
13336 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013337
13338 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 rettv->v_type = VAR_STRING;
13340 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013341 ga_init2(&ga, (int)sizeof(char), 80);
13342
13343#ifdef FEAT_MBYTE
13344 if (!has_mbyte)
13345#endif
13346 /* not multi-byte: fromstr and tostr must be the same length */
13347 if (STRLEN(fromstr) != STRLEN(tostr))
13348 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013349#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013350error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013351#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013352 EMSG2(_(e_invarg2), fromstr);
13353 ga_clear(&ga);
13354 return;
13355 }
13356
13357 /* fromstr and tostr have to contain the same number of chars */
13358 while (*instr != NUL)
13359 {
13360#ifdef FEAT_MBYTE
13361 if (has_mbyte)
13362 {
13363 inlen = mb_ptr2len_check(instr);
13364 cpstr = instr;
13365 cplen = inlen;
13366 idx = 0;
13367 for (p = fromstr; *p != NUL; p += fromlen)
13368 {
13369 fromlen = mb_ptr2len_check(p);
13370 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13371 {
13372 for (p = tostr; *p != NUL; p += tolen)
13373 {
13374 tolen = mb_ptr2len_check(p);
13375 if (idx-- == 0)
13376 {
13377 cplen = tolen;
13378 cpstr = p;
13379 break;
13380 }
13381 }
13382 if (*p == NUL) /* tostr is shorter than fromstr */
13383 goto error;
13384 break;
13385 }
13386 ++idx;
13387 }
13388
13389 if (first && cpstr == instr)
13390 {
13391 /* Check that fromstr and tostr have the same number of
13392 * (multi-byte) characters. Done only once when a character
13393 * of instr doesn't appear in fromstr. */
13394 first = FALSE;
13395 for (p = tostr; *p != NUL; p += tolen)
13396 {
13397 tolen = mb_ptr2len_check(p);
13398 --idx;
13399 }
13400 if (idx != 0)
13401 goto error;
13402 }
13403
13404 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013405 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013406 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013407
13408 instr += inlen;
13409 }
13410 else
13411#endif
13412 {
13413 /* When not using multi-byte chars we can do it faster. */
13414 p = vim_strchr(fromstr, *instr);
13415 if (p != NULL)
13416 ga_append(&ga, tostr[p - fromstr]);
13417 else
13418 ga_append(&ga, *instr);
13419 ++instr;
13420 }
13421 }
13422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013423 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013424}
13425
13426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 * "type(expr)" function
13428 */
13429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430f_type(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{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013434 int n;
13435
13436 switch (argvars[0].v_type)
13437 {
13438 case VAR_NUMBER: n = 0; break;
13439 case VAR_STRING: n = 1; break;
13440 case VAR_FUNC: n = 2; break;
13441 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013442 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013443 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13444 }
13445 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446}
13447
13448/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013449 * "values(dict)" function
13450 */
13451 static void
13452f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013455{
13456 dict_list(argvars, rettv, 1);
13457}
13458
13459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 * "virtcol(string)" function
13461 */
13462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 typval_T *argvars;
13465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466{
13467 colnr_T vcol = 0;
13468 pos_T *fp;
13469
13470 fp = var2fpos(&argvars[0], FALSE);
13471 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13472 {
13473 getvvcol(curwin, fp, NULL, NULL, &vcol);
13474 ++vcol;
13475 }
13476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478}
13479
13480/*
13481 * "visualmode()" function
13482 */
13483/*ARGSUSED*/
13484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
13487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488{
13489#ifdef FEAT_VISUAL
13490 char_u str[2];
13491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 str[0] = curbuf->b_visual_mode_eval;
13494 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013495 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496
13497 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013498 if ((argvars[0].v_type == VAR_NUMBER
13499 && argvars[0].vval.v_number != 0)
13500 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 curbuf->b_visual_mode_eval = NUL;
13503#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505#endif
13506}
13507
13508/*
13509 * "winbufnr(nr)" function
13510 */
13511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013513 typval_T *argvars;
13514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515{
13516 win_T *wp;
13517
13518 wp = find_win_by_nr(&argvars[0]);
13519 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523}
13524
13525/*
13526 * "wincol()" function
13527 */
13528/*ARGSUSED*/
13529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013531 typval_T *argvars;
13532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533{
13534 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536}
13537
13538/*
13539 * "winheight(nr)" function
13540 */
13541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 typval_T *argvars;
13544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545{
13546 win_T *wp;
13547
13548 wp = find_win_by_nr(&argvars[0]);
13549 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553}
13554
13555/*
13556 * "winline()" function
13557 */
13558/*ARGSUSED*/
13559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 typval_T *argvars;
13562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563{
13564 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566}
13567
13568/*
13569 * "winnr()" function
13570 */
13571/* ARGSUSED */
13572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013574 typval_T *argvars;
13575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576{
13577 int nr = 1;
13578#ifdef FEAT_WINDOWS
13579 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013580 win_T *twin = curwin;
13581 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013586 if (STRCMP(arg, "$") == 0)
13587 twin = lastwin;
13588 else if (STRCMP(arg, "#") == 0)
13589 {
13590 twin = prevwin;
13591 if (prevwin == NULL)
13592 nr = 0;
13593 }
13594 else
13595 {
13596 EMSG2(_(e_invexpr2), arg);
13597 nr = 0;
13598 }
13599 }
13600
13601 if (nr > 0)
13602 for (wp = firstwin; wp != twin; wp = wp->w_next)
13603 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606}
13607
13608/*
13609 * "winrestcmd()" function
13610 */
13611/* ARGSUSED */
13612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013614 typval_T *argvars;
13615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
13617#ifdef FEAT_WINDOWS
13618 win_T *wp;
13619 int winnr = 1;
13620 garray_T ga;
13621 char_u buf[50];
13622
13623 ga_init2(&ga, (int)sizeof(char), 70);
13624 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13625 {
13626 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13627 ga_concat(&ga, buf);
13628# ifdef FEAT_VERTSPLIT
13629 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13630 ga_concat(&ga, buf);
13631# endif
13632 ++winnr;
13633 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013634 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641}
13642
13643/*
13644 * "winwidth(nr)" function
13645 */
13646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013648 typval_T *argvars;
13649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650{
13651 win_T *wp;
13652
13653 wp = find_win_by_nr(&argvars[0]);
13654 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 else
13657#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661#endif
13662}
13663
13664 static win_T *
13665find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667{
13668#ifdef FEAT_WINDOWS
13669 win_T *wp;
13670#endif
13671 int nr;
13672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674
13675#ifdef FEAT_WINDOWS
13676 if (nr == 0)
13677 return curwin;
13678
13679 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13680 if (--nr <= 0)
13681 break;
13682 return wp;
13683#else
13684 if (nr == 0 || nr == 1)
13685 return curwin;
13686 return NULL;
13687#endif
13688}
13689
13690/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013691 * "writefile()" function
13692 */
13693 static void
13694f_writefile(argvars, rettv)
13695 typval_T *argvars;
13696 typval_T *rettv;
13697{
13698 int binary = FALSE;
13699 char_u *fname;
13700 FILE *fd;
13701 listitem_T *li;
13702 char_u *s;
13703 int ret = 0;
13704 int c;
13705
13706 if (argvars[0].v_type != VAR_LIST)
13707 {
13708 EMSG2(_(e_listarg), "writefile()");
13709 return;
13710 }
13711 if (argvars[0].vval.v_list == NULL)
13712 return;
13713
13714 if (argvars[2].v_type != VAR_UNKNOWN
13715 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13716 binary = TRUE;
13717
13718 /* Always open the file in binary mode, library functions have a mind of
13719 * their own about CR-LF conversion. */
13720 fname = get_tv_string(&argvars[1]);
13721 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13722 {
13723 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13724 ret = -1;
13725 }
13726 else
13727 {
13728 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13729 li = li->li_next)
13730 {
13731 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13732 {
13733 if (*s == '\n')
13734 c = putc(NUL, fd);
13735 else
13736 c = putc(*s, fd);
13737 if (c == EOF)
13738 {
13739 ret = -1;
13740 break;
13741 }
13742 }
13743 if (!binary || li->li_next != NULL)
13744 if (putc('\n', fd) == EOF)
13745 {
13746 ret = -1;
13747 break;
13748 }
13749 if (ret < 0)
13750 {
13751 EMSG(_(e_write));
13752 break;
13753 }
13754 }
13755 fclose(fd);
13756 }
13757
13758 rettv->vval.v_number = ret;
13759}
13760
13761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 * Translate a String variable into a position.
13763 */
13764 static pos_T *
13765var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 int lnum; /* TRUE when $ is last line */
13768{
13769 char_u *name;
13770 static pos_T pos;
13771 pos_T *pp;
13772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 if (name[0] == '.') /* cursor */
13775 return &curwin->w_cursor;
13776 if (name[0] == '\'') /* mark */
13777 {
13778 pp = getmark(name[1], FALSE);
13779 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13780 return NULL;
13781 return pp;
13782 }
13783 if (name[0] == '$') /* last column or line */
13784 {
13785 if (lnum)
13786 {
13787 pos.lnum = curbuf->b_ml.ml_line_count;
13788 pos.col = 0;
13789 }
13790 else
13791 {
13792 pos.lnum = curwin->w_cursor.lnum;
13793 pos.col = (colnr_T)STRLEN(ml_get_curline());
13794 }
13795 return &pos;
13796 }
13797 return NULL;
13798}
13799
13800/*
13801 * Get the length of an environment variable name.
13802 * Advance "arg" to the first character after the name.
13803 * Return 0 for error.
13804 */
13805 static int
13806get_env_len(arg)
13807 char_u **arg;
13808{
13809 char_u *p;
13810 int len;
13811
13812 for (p = *arg; vim_isIDc(*p); ++p)
13813 ;
13814 if (p == *arg) /* no name found */
13815 return 0;
13816
13817 len = (int)(p - *arg);
13818 *arg = p;
13819 return len;
13820}
13821
13822/*
13823 * Get the length of the name of a function or internal variable.
13824 * "arg" is advanced to the first non-white character after the name.
13825 * Return 0 if something is wrong.
13826 */
13827 static int
13828get_id_len(arg)
13829 char_u **arg;
13830{
13831 char_u *p;
13832 int len;
13833
13834 /* Find the end of the name. */
13835 for (p = *arg; eval_isnamec(*p); ++p)
13836 ;
13837 if (p == *arg) /* no name found */
13838 return 0;
13839
13840 len = (int)(p - *arg);
13841 *arg = skipwhite(p);
13842
13843 return len;
13844}
13845
13846/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013847 * Get the length of the name of a variable or function.
13848 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013850 * Return -1 if curly braces expansion failed.
13851 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 * If the name contains 'magic' {}'s, expand them and return the
13853 * expanded name in an allocated string via 'alias' - caller must free.
13854 */
13855 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013856get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 char_u **arg;
13858 char_u **alias;
13859 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013860 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861{
13862 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 char_u *p;
13864 char_u *expr_start;
13865 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866
13867 *alias = NULL; /* default to no alias */
13868
13869 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13870 && (*arg)[2] == (int)KE_SNR)
13871 {
13872 /* hard coded <SNR>, already translated */
13873 *arg += 3;
13874 return get_id_len(arg) + 3;
13875 }
13876 len = eval_fname_script(*arg);
13877 if (len > 0)
13878 {
13879 /* literal "<SID>", "s:" or "<SNR>" */
13880 *arg += len;
13881 }
13882
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013884 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 if (expr_start != NULL)
13888 {
13889 char_u *temp_string;
13890
13891 if (!evaluate)
13892 {
13893 len += (int)(p - *arg);
13894 *arg = skipwhite(p);
13895 return len;
13896 }
13897
13898 /*
13899 * Include any <SID> etc in the expanded string:
13900 * Thus the -len here.
13901 */
13902 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13903 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013904 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 *alias = temp_string;
13906 *arg = skipwhite(p);
13907 return (int)STRLEN(temp_string);
13908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909
13910 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013911 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 EMSG2(_(e_invexpr2), *arg);
13913
13914 return len;
13915}
13916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917/*
13918 * Find the end of a variable or function name, taking care of magic braces.
13919 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13920 * start and end of the first magic braces item.
13921 * Return a pointer to just after the name. Equal to "arg" if there is no
13922 * valid name.
13923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926 char_u *arg;
13927 char_u **expr_start;
13928 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013929 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931 int mb_nest = 0;
13932 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 char_u *p;
13934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013935 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 *expr_start = NULL;
13938 *expr_end = NULL;
13939 }
13940
13941 for (p = arg; *p != NUL
13942 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013943 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013944 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 || mb_nest != 0
13946 || br_nest != 0); ++p)
13947 {
13948 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013950 if (*p == '[')
13951 ++br_nest;
13952 else if (*p == ']')
13953 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 if (*p == '{')
13958 {
13959 mb_nest++;
13960 if (expr_start != NULL && *expr_start == NULL)
13961 *expr_start = p;
13962 }
13963 else if (*p == '}')
13964 {
13965 mb_nest--;
13966 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13967 *expr_end = p;
13968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 }
13971
13972 return p;
13973}
13974
13975/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013976 * Expands out the 'magic' {}'s in a variable/function name.
13977 * Note that this can call itself recursively, to deal with
13978 * constructs like foo{bar}{baz}{bam}
13979 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13980 * "in_start" ^
13981 * "expr_start" ^
13982 * "expr_end" ^
13983 * "in_end" ^
13984 *
13985 * Returns a new allocated string, which the caller must free.
13986 * Returns NULL for failure.
13987 */
13988 static char_u *
13989make_expanded_name(in_start, expr_start, expr_end, in_end)
13990 char_u *in_start;
13991 char_u *expr_start;
13992 char_u *expr_end;
13993 char_u *in_end;
13994{
13995 char_u c1;
13996 char_u *retval = NULL;
13997 char_u *temp_result;
13998 char_u *nextcmd = NULL;
13999
14000 if (expr_end == NULL || in_end == NULL)
14001 return NULL;
14002 *expr_start = NUL;
14003 *expr_end = NUL;
14004 c1 = *in_end;
14005 *in_end = NUL;
14006
14007 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14008 if (temp_result != NULL && nextcmd == NULL)
14009 {
14010 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14011 + (in_end - expr_end) + 1));
14012 if (retval != NULL)
14013 {
14014 STRCPY(retval, in_start);
14015 STRCAT(retval, temp_result);
14016 STRCAT(retval, expr_end + 1);
14017 }
14018 }
14019 vim_free(temp_result);
14020
14021 *in_end = c1; /* put char back for error messages */
14022 *expr_start = '{';
14023 *expr_end = '}';
14024
14025 if (retval != NULL)
14026 {
14027 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
14028 if (expr_start != NULL)
14029 {
14030 /* Further expansion! */
14031 temp_result = make_expanded_name(retval, expr_start,
14032 expr_end, temp_result);
14033 vim_free(retval);
14034 retval = temp_result;
14035 }
14036 }
14037
14038 return retval;
14039}
14040
14041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014043 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 */
14045 static int
14046eval_isnamec(c)
14047 int c;
14048{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014049 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050}
14051
14052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 * Set number v: variable to "val".
14054 */
14055 void
14056set_vim_var_nr(idx, val)
14057 int idx;
14058 long val;
14059{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014060 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061}
14062
14063/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014064 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 */
14066 long
14067get_vim_var_nr(idx)
14068 int idx;
14069{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014070 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071}
14072
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014073#if defined(FEAT_AUTOCMD) || defined(PROTO)
14074/*
14075 * Get string v: variable value. Uses a static buffer, can only be used once.
14076 */
14077 char_u *
14078get_vim_var_str(idx)
14079 int idx;
14080{
14081 return get_tv_string(&vimvars[idx].vv_tv);
14082}
14083#endif
14084
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085/*
14086 * Set v:count, v:count1 and v:prevcount.
14087 */
14088 void
14089set_vcount(count, count1)
14090 long count;
14091 long count1;
14092{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014093 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14094 vimvars[VV_COUNT].vv_nr = count;
14095 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096}
14097
14098/*
14099 * Set string v: variable to a copy of "val".
14100 */
14101 void
14102set_vim_var_string(idx, val, len)
14103 int idx;
14104 char_u *val;
14105 int len; /* length of "val" to use or -1 (whole string) */
14106{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014107 /* Need to do this (at least) once, since we can't initialize a union.
14108 * Will always be invoked when "v:progname" is set. */
14109 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14110
Bram Moolenaare9a41262005-01-15 22:18:47 +000014111 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014113 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014115 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014117 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118}
14119
14120/*
14121 * Set v:register if needed.
14122 */
14123 void
14124set_reg_var(c)
14125 int c;
14126{
14127 char_u regname;
14128
14129 if (c == 0 || c == ' ')
14130 regname = '"';
14131 else
14132 regname = c;
14133 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014134 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 set_vim_var_string(VV_REG, &regname, 1);
14136}
14137
14138/*
14139 * Get or set v:exception. If "oldval" == NULL, return the current value.
14140 * Otherwise, restore the value to "oldval" and return NULL.
14141 * Must always be called in pairs to save and restore v:exception! Does not
14142 * take care of memory allocations.
14143 */
14144 char_u *
14145v_exception(oldval)
14146 char_u *oldval;
14147{
14148 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014149 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150
Bram Moolenaare9a41262005-01-15 22:18:47 +000014151 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 return NULL;
14153}
14154
14155/*
14156 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14157 * Otherwise, restore the value to "oldval" and return NULL.
14158 * Must always be called in pairs to save and restore v:throwpoint! Does not
14159 * take care of memory allocations.
14160 */
14161 char_u *
14162v_throwpoint(oldval)
14163 char_u *oldval;
14164{
14165 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014166 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167
Bram Moolenaare9a41262005-01-15 22:18:47 +000014168 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 return NULL;
14170}
14171
14172#if defined(FEAT_AUTOCMD) || defined(PROTO)
14173/*
14174 * Set v:cmdarg.
14175 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14176 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14177 * Must always be called in pairs!
14178 */
14179 char_u *
14180set_cmdarg(eap, oldarg)
14181 exarg_T *eap;
14182 char_u *oldarg;
14183{
14184 char_u *oldval;
14185 char_u *newval;
14186 unsigned len;
14187
Bram Moolenaare9a41262005-01-15 22:18:47 +000014188 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014189 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014191 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014192 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014193 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 }
14195
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014196 if (eap->force_bin == FORCE_BIN)
14197 len = 6;
14198 else if (eap->force_bin == FORCE_NOBIN)
14199 len = 8;
14200 else
14201 len = 0;
14202 if (eap->force_ff != 0)
14203 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14204# ifdef FEAT_MBYTE
14205 if (eap->force_enc != 0)
14206 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14207# endif
14208
14209 newval = alloc(len + 1);
14210 if (newval == NULL)
14211 return NULL;
14212
14213 if (eap->force_bin == FORCE_BIN)
14214 sprintf((char *)newval, " ++bin");
14215 else if (eap->force_bin == FORCE_NOBIN)
14216 sprintf((char *)newval, " ++nobin");
14217 else
14218 *newval = NUL;
14219 if (eap->force_ff != 0)
14220 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14221 eap->cmd + eap->force_ff);
14222# ifdef FEAT_MBYTE
14223 if (eap->force_enc != 0)
14224 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14225 eap->cmd + eap->force_enc);
14226# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014227 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014228 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229}
14230#endif
14231
14232/*
14233 * Get the value of internal variable "name".
14234 * Return OK or FAIL.
14235 */
14236 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014237get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 char_u *name;
14239 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014241 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242{
14243 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014244 typval_T *tv = NULL;
14245 typval_T atv;
14246 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248
14249 /* truncate the name, so that we can use strcmp() */
14250 cc = name[len];
14251 name[len] = NUL;
14252
14253 /*
14254 * Check for "b:changedtick".
14255 */
14256 if (STRCMP(name, "b:changedtick") == 0)
14257 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014258 atv.v_type = VAR_NUMBER;
14259 atv.vval.v_number = curbuf->b_changedtick;
14260 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 }
14262
14263 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 * Check for user-defined variables.
14265 */
14266 else
14267 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014268 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014270 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 }
14272
Bram Moolenaare9a41262005-01-15 22:18:47 +000014273 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014275 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 ret = FAIL;
14278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014280 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281
14282 name[len] = cc;
14283
14284 return ret;
14285}
14286
14287/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014288 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14289 * Also handle function call with Funcref variable: func(expr)
14290 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14291 */
14292 static int
14293handle_subscript(arg, rettv, evaluate, verbose)
14294 char_u **arg;
14295 typval_T *rettv;
14296 int evaluate; /* do more than finding the end */
14297 int verbose; /* give error messages */
14298{
14299 int ret = OK;
14300 dict_T *selfdict = NULL;
14301 char_u *s;
14302 int len;
14303
14304 while (ret == OK
14305 && (**arg == '['
14306 || (**arg == '.' && rettv->v_type == VAR_DICT)
14307 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14308 && !vim_iswhite(*(*arg - 1)))
14309 {
14310 if (**arg == '(')
14311 {
14312 s = rettv->vval.v_string;
14313
14314 /* Invoke the function. Recursive! */
14315 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14316 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14317 &len, evaluate, selfdict);
14318
14319 /* Stop the expression evaluation when immediately aborting on
14320 * error, or when an interrupt occurred or an exception was thrown
14321 * but not caught. */
14322 if (aborting())
14323 {
14324 if (ret == OK)
14325 clear_tv(rettv);
14326 ret = FAIL;
14327 }
14328 dict_unref(selfdict);
14329 selfdict = NULL;
14330 }
14331 else /* **arg == '[' || **arg == '.' */
14332 {
14333 dict_unref(selfdict);
14334 if (rettv->v_type == VAR_DICT)
14335 {
14336 selfdict = rettv->vval.v_dict;
14337 if (selfdict != NULL)
14338 ++selfdict->dv_refcount;
14339 }
14340 else
14341 selfdict = NULL;
14342 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14343 {
14344 clear_tv(rettv);
14345 ret = FAIL;
14346 }
14347 }
14348 }
14349 dict_unref(selfdict);
14350 return ret;
14351}
14352
14353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014354 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14355 * value).
14356 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014357 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014359{
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014361}
14362
14363/*
14364 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 * The string "s" must have been allocated, it is consumed.
14366 * Return NULL for out of memory, the variable otherwise.
14367 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014368 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 char_u *s;
14371{
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv = alloc_tv();
14375 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 rettv->v_type = VAR_STRING;
14378 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 }
14380 else
14381 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383}
14384
14385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014386 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 */
14388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014390 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391{
14392 if (varp != NULL)
14393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014394 switch (varp->v_type)
14395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014396 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014397 func_unref(varp->vval.v_string);
14398 /*FALLTHROUGH*/
14399 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014400 vim_free(varp->vval.v_string);
14401 break;
14402 case VAR_LIST:
14403 list_unref(varp->vval.v_list);
14404 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014405 case VAR_DICT:
14406 dict_unref(varp->vval.v_dict);
14407 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014408 case VAR_NUMBER:
14409 case VAR_UNKNOWN:
14410 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014411 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014412 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014413 break;
14414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 vim_free(varp);
14416 }
14417}
14418
14419/*
14420 * Free the memory for a variable value and set the value to NULL or 0.
14421 */
14422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014424 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425{
14426 if (varp != NULL)
14427 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014428 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014430 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014431 func_unref(varp->vval.v_string);
14432 /*FALLTHROUGH*/
14433 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014434 vim_free(varp->vval.v_string);
14435 varp->vval.v_string = NULL;
14436 break;
14437 case VAR_LIST:
14438 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014439 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014440 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014441 case VAR_DICT:
14442 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014443 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014444 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014446 varp->vval.v_number = 0;
14447 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 case VAR_UNKNOWN:
14449 break;
14450 default:
14451 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014453 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 }
14455}
14456
14457/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 * Set the value of a variable to NULL without freeing items.
14459 */
14460 static void
14461init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014462 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463{
14464 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014465 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466}
14467
14468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 * Get the number value of a variable.
14470 * If it is a String variable, uses vim_str2nr().
14471 */
14472 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014474 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014476 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014478 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014480 case VAR_NUMBER:
14481 n = (long)(varp->vval.v_number);
14482 break;
14483 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014484 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014485 break;
14486 case VAR_STRING:
14487 if (varp->vval.v_string != NULL)
14488 vim_str2nr(varp->vval.v_string, NULL, NULL,
14489 TRUE, TRUE, &n, NULL);
14490 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014491 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014492 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014493 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014494 case VAR_DICT:
14495 EMSG(_("E728: Using a Dictionary as a number"));
14496 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014497 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014498 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014501 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502}
14503
14504/*
14505 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14506 */
14507 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510{
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 linenr_T lnum;
14513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 if (lnum == 0) /* no valid number, try using line() */
14516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv.v_type = VAR_NUMBER;
14518 f_line(argvars, &rettv);
14519 lnum = rettv.vval.v_number;
14520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 }
14522 return lnum;
14523}
14524
14525/*
14526 * Get the string value of a variable.
14527 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014528 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14529 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 * If the String variable has never been set, return an empty string.
14531 * Never returns NULL;
14532 */
14533 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536{
14537 static char_u mybuf[NUMBUFLEN];
14538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540}
14541
14542 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014545 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014547 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014549 case VAR_NUMBER:
14550 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14551 return buf;
14552 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014553 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014554 break;
14555 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014556 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014557 break;
14558 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014559 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014560 break;
14561 case VAR_STRING:
14562 if (varp->vval.v_string != NULL)
14563 return varp->vval.v_string;
14564 break;
14565 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014569 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570}
14571
14572/*
14573 * Find variable "name" in the list of variables.
14574 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014575 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014576 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014580find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014585 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586
Bram Moolenaara7043832005-01-21 11:56:39 +000014587 ht = find_var_ht(name, &varname);
14588 if (htp != NULL)
14589 *htp = ht;
14590 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014592 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593}
14594
14595/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014597 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014600find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014602 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014603 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014604{
Bram Moolenaar33570922005-01-25 22:26:29 +000014605 hashitem_T *hi;
14606
14607 if (*varname == NUL)
14608 {
14609 /* Must be something like "s:", otherwise "ht" would be NULL. */
14610 switch (varname[-2])
14611 {
14612 case 's': return &SCRIPT_SV(current_SID).sv_var;
14613 case 'g': return &globvars_var;
14614 case 'v': return &vimvars_var;
14615 case 'b': return &curbuf->b_bufvar;
14616 case 'w': return &curwin->w_winvar;
14617 case 'l': return &current_funccal->l_vars_var;
14618 case 'a': return &current_funccal->l_avars_var;
14619 }
14620 return NULL;
14621 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014622
14623 hi = hash_find(ht, varname);
14624 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014625 {
14626 /* For global variables we may try auto-loading the script. If it
14627 * worked find the variable again. */
14628 if (ht == &globvarht && !writing
14629 && script_autoload(varname) && !aborting())
14630 hi = hash_find(ht, varname);
14631 if (HASHITEM_EMPTY(hi))
14632 return NULL;
14633 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014635}
14636
14637/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014638 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014639 * Set "varname" to the start of name without ':'.
14640 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014641 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014642find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643 char_u *name;
14644 char_u **varname;
14645{
14646 if (name[1] != ':')
14647 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014648 /* The name must not start with a colon. */
14649 if (name[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 return NULL;
14651 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014652
14653 /* "version" is "v:version" in all scopes */
14654 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14655 return &compat_hashtab;
14656
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 return &globvarht; /* global variable */
14659 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 }
14661 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014662 if (*name == 'g') /* global variable */
14663 return &globvarht;
14664 /* There must be no ':' in the rest of the name, unless g: is used */
14665 if (vim_strchr(name + 2, ':') != NULL)
14666 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014671 if (*name == 'v') /* v: variable */
14672 return &vimvarht;
14673 if (*name == 'a' && current_funccal != NULL) /* function argument */
14674 return &current_funccal->l_avars.dv_hashtab;
14675 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14676 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 if (*name == 's' /* script variable */
14678 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14679 return &SCRIPT_VARS(current_SID);
14680 return NULL;
14681}
14682
14683/*
14684 * Get the string value of a (global/local) variable.
14685 * Returns NULL when it doesn't exist.
14686 */
14687 char_u *
14688get_var_value(name)
14689 char_u *name;
14690{
Bram Moolenaar33570922005-01-25 22:26:29 +000014691 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692
Bram Moolenaara7043832005-01-21 11:56:39 +000014693 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694 if (v == NULL)
14695 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014696 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697}
14698
14699/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014700 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 * sourcing this script and when executing functions defined in the script.
14702 */
14703 void
14704new_script_vars(id)
14705 scid_T id;
14706{
Bram Moolenaara7043832005-01-21 11:56:39 +000014707 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014708 hashtab_T *ht;
14709 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014710
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14712 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014713 /* Re-allocating ga_data means that an ht_array pointing to
14714 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014715 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014716 for (i = 1; i <= ga_scripts.ga_len; ++i)
14717 {
14718 ht = &SCRIPT_VARS(i);
14719 if (ht->ht_mask == HT_INIT_SIZE - 1)
14720 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 sv = &SCRIPT_SV(i);
14722 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014723 }
14724
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 while (ga_scripts.ga_len < id)
14726 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14728 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 }
14731 }
14732}
14733
14734/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014735 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14736 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 */
14738 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014739init_var_dict(dict, dict_var)
14740 dict_T *dict;
14741 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742{
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 hash_init(&dict->dv_hashtab);
14744 dict->dv_refcount = 99999;
14745 dict_var->di_tv.vval.v_dict = dict;
14746 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014747 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14749 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750}
14751
14752/*
14753 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 * Frees all allocated variables and the value they contain.
14755 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 */
14757 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014758vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014759 hashtab_T *ht;
14760{
14761 vars_clear_ext(ht, TRUE);
14762}
14763
14764/*
14765 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14766 */
14767 static void
14768vars_clear_ext(ht, free_val)
14769 hashtab_T *ht;
14770 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771{
Bram Moolenaara7043832005-01-21 11:56:39 +000014772 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 hashitem_T *hi;
14774 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014777 todo = ht->ht_used;
14778 for (hi = ht->ht_array; todo > 0; ++hi)
14779 {
14780 if (!HASHITEM_EMPTY(hi))
14781 {
14782 --todo;
14783
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014785 * ht_array might change then. hash_clear() takes care of it
14786 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 v = HI2DI(hi);
14788 if (free_val)
14789 clear_tv(&v->di_tv);
14790 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14791 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014792 }
14793 }
14794 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795}
14796
Bram Moolenaara7043832005-01-21 11:56:39 +000014797/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 * Delete a variable from hashtab "ht" at item "hi".
14799 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014802delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014803 hashtab_T *ht;
14804 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805{
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014807
14808 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014809 clear_tv(&di->di_tv);
14810 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811}
14812
14813/*
14814 * List the value of one internal variable.
14815 */
14816 static void
14817list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014818 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 char_u *prefix;
14820{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014821 char_u *tofree;
14822 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014823 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014824
Bram Moolenaar33570922005-01-25 22:26:29 +000014825 s = echo_string(&v->di_tv, &tofree, numbuf);
14826 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 s == NULL ? (char_u *)"" : s);
14828 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829}
14830
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 static void
14832list_one_var_a(prefix, name, type, string)
14833 char_u *prefix;
14834 char_u *name;
14835 int type;
14836 char_u *string;
14837{
14838 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14839 if (name != NULL) /* "a:" vars don't have a name stored */
14840 msg_puts(name);
14841 msg_putchar(' ');
14842 msg_advance(22);
14843 if (type == VAR_NUMBER)
14844 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014845 else if (type == VAR_FUNC)
14846 msg_putchar('*');
14847 else if (type == VAR_LIST)
14848 {
14849 msg_putchar('[');
14850 if (*string == '[')
14851 ++string;
14852 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014853 else if (type == VAR_DICT)
14854 {
14855 msg_putchar('{');
14856 if (*string == '{')
14857 ++string;
14858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 else
14860 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014861
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863
14864 if (type == VAR_FUNC)
14865 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866}
14867
14868/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 * If the variable already exists, the value is updated.
14871 * Otherwise the variable is created.
14872 */
14873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014874set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014876 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014877 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878{
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014881 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014883 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014884 {
14885 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14886 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14887 ? name[2] : name[0]))
14888 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014889 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014890 return;
14891 }
14892 if (function_exists(name))
14893 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014894 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14895 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014896 return;
14897 }
14898 }
14899
Bram Moolenaara7043832005-01-21 11:56:39 +000014900 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014901 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014902 {
14903 EMSG2(_("E461: Illegal variable name: %s"), name);
14904 return;
14905 }
14906
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014907 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014908 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014910 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014911 if (var_check_ro(v->di_flags, name)
14912 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014913 return;
14914 if (v->di_tv.v_type != tv->v_type
14915 && !((v->di_tv.v_type == VAR_STRING
14916 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014917 && (tv->v_type == VAR_STRING
14918 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014919 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014920 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014921 return;
14922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014923
14924 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014925 * Handle setting internal v: variables separately: we don't change
14926 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014927 */
14928 if (ht == &vimvarht)
14929 {
14930 if (v->di_tv.v_type == VAR_STRING)
14931 {
14932 vim_free(v->di_tv.vval.v_string);
14933 if (copy || tv->v_type != VAR_STRING)
14934 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14935 else
14936 {
14937 /* Take over the string to avoid an extra alloc/free. */
14938 v->di_tv.vval.v_string = tv->vval.v_string;
14939 tv->vval.v_string = NULL;
14940 }
14941 }
14942 else if (v->di_tv.v_type != VAR_NUMBER)
14943 EMSG2(_(e_intern2), "set_var()");
14944 else
14945 v->di_tv.vval.v_number = get_tv_number(tv);
14946 return;
14947 }
14948
14949 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 }
14951 else /* add a new variable */
14952 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014953 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14954 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014955 if (v == NULL)
14956 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014957 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014958 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014960 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 return;
14962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014963 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014968 else
14969 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014970 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014971 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014972 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974}
14975
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014977 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14978 * Also give an error message.
14979 */
14980 static int
14981var_check_ro(flags, name)
14982 int flags;
14983 char_u *name;
14984{
14985 if (flags & DI_FLAGS_RO)
14986 {
14987 EMSG2(_(e_readonlyvar), name);
14988 return TRUE;
14989 }
14990 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14991 {
14992 EMSG2(_(e_readonlysbx), name);
14993 return TRUE;
14994 }
14995 return FALSE;
14996}
14997
14998/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014999 * Return TRUE if typeval "tv" is set to be locked (immutable).
15000 * Also give an error message, using "name".
15001 */
15002 static int
15003tv_check_lock(lock, name)
15004 int lock;
15005 char_u *name;
15006{
15007 if (lock & VAR_LOCKED)
15008 {
15009 EMSG2(_("E741: Value is locked: %s"),
15010 name == NULL ? (char_u *)_("Unknown") : name);
15011 return TRUE;
15012 }
15013 if (lock & VAR_FIXED)
15014 {
15015 EMSG2(_("E742: Cannot change value of %s"),
15016 name == NULL ? (char_u *)_("Unknown") : name);
15017 return TRUE;
15018 }
15019 return FALSE;
15020}
15021
15022/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015024 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015025 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 typval_T *from;
15030 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015032 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015033 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015034 switch (from->v_type)
15035 {
15036 case VAR_NUMBER:
15037 to->vval.v_number = from->vval.v_number;
15038 break;
15039 case VAR_STRING:
15040 case VAR_FUNC:
15041 if (from->vval.v_string == NULL)
15042 to->vval.v_string = NULL;
15043 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015044 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015045 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015046 if (from->v_type == VAR_FUNC)
15047 func_ref(to->vval.v_string);
15048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015049 break;
15050 case VAR_LIST:
15051 if (from->vval.v_list == NULL)
15052 to->vval.v_list = NULL;
15053 else
15054 {
15055 to->vval.v_list = from->vval.v_list;
15056 ++to->vval.v_list->lv_refcount;
15057 }
15058 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015059 case VAR_DICT:
15060 if (from->vval.v_dict == NULL)
15061 to->vval.v_dict = NULL;
15062 else
15063 {
15064 to->vval.v_dict = from->vval.v_dict;
15065 ++to->vval.v_dict->dv_refcount;
15066 }
15067 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015068 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015069 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015070 break;
15071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072}
15073
15074/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015075 * Make a copy of an item.
15076 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015077 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15078 * reference to an already copied list/dict can be used.
15079 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015080 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015081 static int
15082item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015083 typval_T *from;
15084 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015085 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015086 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015087{
15088 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015089 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015090
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015092 {
15093 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015094 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015095 }
15096 ++recurse;
15097
15098 switch (from->v_type)
15099 {
15100 case VAR_NUMBER:
15101 case VAR_STRING:
15102 case VAR_FUNC:
15103 copy_tv(from, to);
15104 break;
15105 case VAR_LIST:
15106 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015107 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015108 if (from->vval.v_list == NULL)
15109 to->vval.v_list = NULL;
15110 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15111 {
15112 /* use the copy made earlier */
15113 to->vval.v_list = from->vval.v_list->lv_copylist;
15114 ++to->vval.v_list->lv_refcount;
15115 }
15116 else
15117 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15118 if (to->vval.v_list == NULL)
15119 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015120 break;
15121 case VAR_DICT:
15122 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015123 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015124 if (from->vval.v_dict == NULL)
15125 to->vval.v_dict = NULL;
15126 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15127 {
15128 /* use the copy made earlier */
15129 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15130 ++to->vval.v_dict->dv_refcount;
15131 }
15132 else
15133 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15134 if (to->vval.v_dict == NULL)
15135 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015136 break;
15137 default:
15138 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015139 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015140 }
15141 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015142 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015143}
15144
15145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146 * ":echo expr1 ..." print each argument separated with a space, add a
15147 * newline at the end.
15148 * ":echon expr1 ..." print each argument plain.
15149 */
15150 void
15151ex_echo(eap)
15152 exarg_T *eap;
15153{
15154 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015156 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 char_u *p;
15158 int needclr = TRUE;
15159 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015160 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
15162 if (eap->skip)
15163 ++emsg_skip;
15164 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15165 {
15166 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 {
15169 /*
15170 * Report the invalid expression unless the expression evaluation
15171 * has been cancelled due to an aborting error, an interrupt, or an
15172 * exception.
15173 */
15174 if (!aborting())
15175 EMSG2(_(e_invexpr2), p);
15176 break;
15177 }
15178 if (!eap->skip)
15179 {
15180 if (atstart)
15181 {
15182 atstart = FALSE;
15183 /* Call msg_start() after eval1(), evaluating the expression
15184 * may cause a message to appear. */
15185 if (eap->cmdidx == CMD_echo)
15186 msg_start();
15187 }
15188 else if (eap->cmdidx == CMD_echo)
15189 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015190 p = echo_string(&rettv, &tofree, numbuf);
15191 if (p != NULL)
15192 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015194 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015196 if (*p != TAB && needclr)
15197 {
15198 /* remove any text still there from the command */
15199 msg_clr_eos();
15200 needclr = FALSE;
15201 }
15202 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 }
15204 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015205 {
15206#ifdef FEAT_MBYTE
15207 if (has_mbyte)
15208 {
15209 int i = (*mb_ptr2len_check)(p);
15210
15211 (void)msg_outtrans_len_attr(p, i, echo_attr);
15212 p += i - 1;
15213 }
15214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015216 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015219 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015221 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 arg = skipwhite(arg);
15223 }
15224 eap->nextcmd = check_nextcmd(arg);
15225
15226 if (eap->skip)
15227 --emsg_skip;
15228 else
15229 {
15230 /* remove text that may still be there from the command */
15231 if (needclr)
15232 msg_clr_eos();
15233 if (eap->cmdidx == CMD_echo)
15234 msg_end();
15235 }
15236}
15237
15238/*
15239 * ":echohl {name}".
15240 */
15241 void
15242ex_echohl(eap)
15243 exarg_T *eap;
15244{
15245 int id;
15246
15247 id = syn_name2id(eap->arg);
15248 if (id == 0)
15249 echo_attr = 0;
15250 else
15251 echo_attr = syn_id2attr(id);
15252}
15253
15254/*
15255 * ":execute expr1 ..." execute the result of an expression.
15256 * ":echomsg expr1 ..." Print a message
15257 * ":echoerr expr1 ..." Print an error
15258 * Each gets spaces around each argument and a newline at the end for
15259 * echo commands
15260 */
15261 void
15262ex_execute(eap)
15263 exarg_T *eap;
15264{
15265 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015266 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 int ret = OK;
15268 char_u *p;
15269 garray_T ga;
15270 int len;
15271 int save_did_emsg;
15272
15273 ga_init2(&ga, 1, 80);
15274
15275 if (eap->skip)
15276 ++emsg_skip;
15277 while (*arg != NUL && *arg != '|' && *arg != '\n')
15278 {
15279 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015280 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 {
15282 /*
15283 * Report the invalid expression unless the expression evaluation
15284 * has been cancelled due to an aborting error, an interrupt, or an
15285 * exception.
15286 */
15287 if (!aborting())
15288 EMSG2(_(e_invexpr2), p);
15289 ret = FAIL;
15290 break;
15291 }
15292
15293 if (!eap->skip)
15294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015295 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296 len = (int)STRLEN(p);
15297 if (ga_grow(&ga, len + 2) == FAIL)
15298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 ret = FAIL;
15301 break;
15302 }
15303 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 ga.ga_len += len;
15307 }
15308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 arg = skipwhite(arg);
15311 }
15312
15313 if (ret != FAIL && ga.ga_data != NULL)
15314 {
15315 if (eap->cmdidx == CMD_echomsg)
15316 MSG_ATTR(ga.ga_data, echo_attr);
15317 else if (eap->cmdidx == CMD_echoerr)
15318 {
15319 /* We don't want to abort following commands, restore did_emsg. */
15320 save_did_emsg = did_emsg;
15321 EMSG((char_u *)ga.ga_data);
15322 if (!force_abort)
15323 did_emsg = save_did_emsg;
15324 }
15325 else if (eap->cmdidx == CMD_execute)
15326 do_cmdline((char_u *)ga.ga_data,
15327 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15328 }
15329
15330 ga_clear(&ga);
15331
15332 if (eap->skip)
15333 --emsg_skip;
15334
15335 eap->nextcmd = check_nextcmd(arg);
15336}
15337
15338/*
15339 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15340 * "arg" points to the "&" or '+' when called, to "option" when returning.
15341 * Returns NULL when no option name found. Otherwise pointer to the char
15342 * after the option name.
15343 */
15344 static char_u *
15345find_option_end(arg, opt_flags)
15346 char_u **arg;
15347 int *opt_flags;
15348{
15349 char_u *p = *arg;
15350
15351 ++p;
15352 if (*p == 'g' && p[1] == ':')
15353 {
15354 *opt_flags = OPT_GLOBAL;
15355 p += 2;
15356 }
15357 else if (*p == 'l' && p[1] == ':')
15358 {
15359 *opt_flags = OPT_LOCAL;
15360 p += 2;
15361 }
15362 else
15363 *opt_flags = 0;
15364
15365 if (!ASCII_ISALPHA(*p))
15366 return NULL;
15367 *arg = p;
15368
15369 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15370 p += 4; /* termcap option */
15371 else
15372 while (ASCII_ISALPHA(*p))
15373 ++p;
15374 return p;
15375}
15376
15377/*
15378 * ":function"
15379 */
15380 void
15381ex_function(eap)
15382 exarg_T *eap;
15383{
15384 char_u *theline;
15385 int j;
15386 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 char_u *name = NULL;
15389 char_u *p;
15390 char_u *arg;
15391 garray_T newargs;
15392 garray_T newlines;
15393 int varargs = FALSE;
15394 int mustend = FALSE;
15395 int flags = 0;
15396 ufunc_T *fp;
15397 int indent;
15398 int nesting;
15399 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015400 dictitem_T *v;
15401 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015402 static int func_nr = 0; /* number for nameless function */
15403 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015404 hashtab_T *ht;
15405 int todo;
15406 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407
15408 /*
15409 * ":function" without argument: list functions.
15410 */
15411 if (ends_excmd(*eap->arg))
15412 {
15413 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015414 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015415 todo = func_hashtab.ht_used;
15416 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015417 {
15418 if (!HASHITEM_EMPTY(hi))
15419 {
15420 --todo;
15421 fp = HI2UF(hi);
15422 if (!isdigit(*fp->uf_name))
15423 list_func_head(fp, FALSE);
15424 }
15425 }
15426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 eap->nextcmd = check_nextcmd(eap->arg);
15428 return;
15429 }
15430
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015431 /*
15432 * Get the function name. There are these situations:
15433 * func normal function name
15434 * "name" == func, "fudi.fd_dict" == NULL
15435 * dict.func new dictionary entry
15436 * "name" == NULL, "fudi.fd_dict" set,
15437 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15438 * dict.func existing dict entry with a Funcref
15439 * "name" == fname, "fudi.fd_dict" set,
15440 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15441 * dict.func existing dict entry that's not a Funcref
15442 * "name" == NULL, "fudi.fd_dict" set,
15443 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015446 name = trans_function_name(&p, eap->skip, 0, &fudi);
15447 paren = (vim_strchr(p, '(') != NULL);
15448 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 {
15450 /*
15451 * Return on an invalid expression in braces, unless the expression
15452 * evaluation has been cancelled due to an aborting error, an
15453 * interrupt, or an exception.
15454 */
15455 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015457 if (!eap->skip && fudi.fd_newkey != NULL)
15458 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015459 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 else
15463 eap->skip = TRUE;
15464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 /* An error in a function call during evaluation of an expression in magic
15466 * braces should not cause the function not to be defined. */
15467 saved_did_emsg = did_emsg;
15468 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469
15470 /*
15471 * ":function func" with only function name: list function.
15472 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015473 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 {
15475 if (!ends_excmd(*skipwhite(p)))
15476 {
15477 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015478 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 }
15480 eap->nextcmd = check_nextcmd(p);
15481 if (eap->nextcmd != NULL)
15482 *p = NUL;
15483 if (!eap->skip && !got_int)
15484 {
15485 fp = find_func(name);
15486 if (fp != NULL)
15487 {
15488 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015489 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 {
15491 msg_putchar('\n');
15492 msg_outnum((long)(j + 1));
15493 if (j < 9)
15494 msg_putchar(' ');
15495 if (j < 99)
15496 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015497 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 out_flush(); /* show a line at a time */
15499 ui_breakcheck();
15500 }
15501 if (!got_int)
15502 {
15503 msg_putchar('\n');
15504 msg_puts((char_u *)" endfunction");
15505 }
15506 }
15507 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015508 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015510 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 }
15512
15513 /*
15514 * ":function name(arg1, arg2)" Define function.
15515 */
15516 p = skipwhite(p);
15517 if (*p != '(')
15518 {
15519 if (!eap->skip)
15520 {
15521 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015522 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 }
15524 /* attempt to continue by skipping some text */
15525 if (vim_strchr(p, '(') != NULL)
15526 p = vim_strchr(p, '(');
15527 }
15528 p = skipwhite(p + 1);
15529
15530 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15531 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15532
15533 /*
15534 * Isolate the arguments: "arg1, arg2, ...)"
15535 */
15536 while (*p != ')')
15537 {
15538 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15539 {
15540 varargs = TRUE;
15541 p += 3;
15542 mustend = TRUE;
15543 }
15544 else
15545 {
15546 arg = p;
15547 while (ASCII_ISALNUM(*p) || *p == '_')
15548 ++p;
15549 if (arg == p || isdigit(*arg)
15550 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15551 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15552 {
15553 if (!eap->skip)
15554 EMSG2(_("E125: Illegal argument: %s"), arg);
15555 break;
15556 }
15557 if (ga_grow(&newargs, 1) == FAIL)
15558 goto erret;
15559 c = *p;
15560 *p = NUL;
15561 arg = vim_strsave(arg);
15562 if (arg == NULL)
15563 goto erret;
15564 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15565 *p = c;
15566 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 if (*p == ',')
15568 ++p;
15569 else
15570 mustend = TRUE;
15571 }
15572 p = skipwhite(p);
15573 if (mustend && *p != ')')
15574 {
15575 if (!eap->skip)
15576 EMSG2(_(e_invarg2), eap->arg);
15577 break;
15578 }
15579 }
15580 ++p; /* skip the ')' */
15581
Bram Moolenaare9a41262005-01-15 22:18:47 +000015582 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 for (;;)
15584 {
15585 p = skipwhite(p);
15586 if (STRNCMP(p, "range", 5) == 0)
15587 {
15588 flags |= FC_RANGE;
15589 p += 5;
15590 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015591 else if (STRNCMP(p, "dict", 4) == 0)
15592 {
15593 flags |= FC_DICT;
15594 p += 4;
15595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 else if (STRNCMP(p, "abort", 5) == 0)
15597 {
15598 flags |= FC_ABORT;
15599 p += 5;
15600 }
15601 else
15602 break;
15603 }
15604
15605 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15606 EMSG(_(e_trailing));
15607
15608 /*
15609 * Read the body of the function, until ":endfunction" is found.
15610 */
15611 if (KeyTyped)
15612 {
15613 /* Check if the function already exists, don't let the user type the
15614 * whole function before telling him it doesn't work! For a script we
15615 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015616 if (!eap->skip && !eap->forceit)
15617 {
15618 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15619 EMSG(_(e_funcdict));
15620 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015621 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623
15624 msg_putchar('\n'); /* don't overwrite the function name */
15625 cmdline_row = msg_row;
15626 }
15627
15628 indent = 2;
15629 nesting = 0;
15630 for (;;)
15631 {
15632 msg_scroll = TRUE;
15633 need_wait_return = FALSE;
15634 if (eap->getline == NULL)
15635 theline = getcmdline(':', 0L, indent);
15636 else
15637 theline = eap->getline(':', eap->cookie, indent);
15638 if (KeyTyped)
15639 lines_left = Rows - 1;
15640 if (theline == NULL)
15641 {
15642 EMSG(_("E126: Missing :endfunction"));
15643 goto erret;
15644 }
15645
15646 if (skip_until != NULL)
15647 {
15648 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15649 * don't check for ":endfunc". */
15650 if (STRCMP(theline, skip_until) == 0)
15651 {
15652 vim_free(skip_until);
15653 skip_until = NULL;
15654 }
15655 }
15656 else
15657 {
15658 /* skip ':' and blanks*/
15659 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15660 ;
15661
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015662 /* Check for "endfunction". */
15663 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 {
15665 vim_free(theline);
15666 break;
15667 }
15668
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 * at "end". */
15671 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15672 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015673 else if (STRNCMP(p, "if", 2) == 0
15674 || STRNCMP(p, "wh", 2) == 0
15675 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 || STRNCMP(p, "try", 3) == 0)
15677 indent += 2;
15678
15679 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015680 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015682 if (*p == '!')
15683 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 p += eval_fname_script(p);
15685 if (ASCII_ISALPHA(*p))
15686 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015687 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 if (*skipwhite(p) == '(')
15689 {
15690 ++nesting;
15691 indent += 2;
15692 }
15693 }
15694 }
15695
15696 /* Check for ":append" or ":insert". */
15697 p = skip_range(p, NULL);
15698 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15699 || (p[0] == 'i'
15700 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15701 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15702 skip_until = vim_strsave((char_u *)".");
15703
15704 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15705 arg = skipwhite(skiptowhite(p));
15706 if (arg[0] == '<' && arg[1] =='<'
15707 && ((p[0] == 'p' && p[1] == 'y'
15708 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15709 || (p[0] == 'p' && p[1] == 'e'
15710 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15711 || (p[0] == 't' && p[1] == 'c'
15712 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15713 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15714 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015715 || (p[0] == 'm' && p[1] == 'z'
15716 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 ))
15718 {
15719 /* ":python <<" continues until a dot, like ":append" */
15720 p = skipwhite(arg + 2);
15721 if (*p == NUL)
15722 skip_until = vim_strsave((char_u *)".");
15723 else
15724 skip_until = vim_strsave(p);
15725 }
15726 }
15727
15728 /* Add the line to the function. */
15729 if (ga_grow(&newlines, 1) == FAIL)
15730 goto erret;
15731 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15732 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 }
15734
15735 /* Don't define the function when skipping commands or when an error was
15736 * detected. */
15737 if (eap->skip || did_emsg)
15738 goto erret;
15739
15740 /*
15741 * If there are no errors, add the function
15742 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015743 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015744 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015745 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015746 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015747 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015748 emsg_funcname("E707: Function name conflicts with variable: %s",
15749 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015750 goto erret;
15751 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015752
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015753 fp = find_func(name);
15754 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015756 if (!eap->forceit)
15757 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015758 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015759 goto erret;
15760 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015761 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015762 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015763 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015764 name);
15765 goto erret;
15766 }
15767 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015768 ga_clear_strings(&(fp->uf_args));
15769 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015770 vim_free(name);
15771 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 }
15774 else
15775 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015776 char numbuf[20];
15777
15778 fp = NULL;
15779 if (fudi.fd_newkey == NULL && !eap->forceit)
15780 {
15781 EMSG(_(e_funcdict));
15782 goto erret;
15783 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015784 if (fudi.fd_di == NULL)
15785 {
15786 /* Can't add a function to a locked dictionary */
15787 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15788 goto erret;
15789 }
15790 /* Can't change an existing function if it is locked */
15791 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15792 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015793
15794 /* Give the function a sequential number. Can only be used with a
15795 * Funcref! */
15796 vim_free(name);
15797 sprintf(numbuf, "%d", ++func_nr);
15798 name = vim_strsave((char_u *)numbuf);
15799 if (name == NULL)
15800 goto erret;
15801 }
15802
15803 if (fp == NULL)
15804 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015805 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL)
15806 {
15807 int slen, plen;
15808 char_u *scriptname;
15809
15810 /* Check that the autoload name matches the script name. */
15811 j = FAIL;
15812 if (sourcing_name != NULL)
15813 {
15814 scriptname = autoload_name(name);
15815 if (scriptname != NULL)
15816 {
15817 p = vim_strchr(scriptname, '/');
15818 plen = STRLEN(p);
15819 slen = STRLEN(sourcing_name);
15820 if (slen > plen && fnamecmp(p,
15821 sourcing_name + slen - plen) == 0)
15822 j = OK;
15823 vim_free(scriptname);
15824 }
15825 }
15826 if (j == FAIL)
15827 {
15828 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15829 goto erret;
15830 }
15831 }
15832
15833 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 if (fp == NULL)
15835 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015836
15837 if (fudi.fd_dict != NULL)
15838 {
15839 if (fudi.fd_di == NULL)
15840 {
15841 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015842 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015843 if (fudi.fd_di == NULL)
15844 {
15845 vim_free(fp);
15846 goto erret;
15847 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015848 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15849 {
15850 vim_free(fudi.fd_di);
15851 goto erret;
15852 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015853 }
15854 else
15855 /* overwrite existing dict entry */
15856 clear_tv(&fudi.fd_di->di_tv);
15857 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015858 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015859 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015860 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015861 }
15862
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015864 STRCPY(fp->uf_name, name);
15865 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015867 fp->uf_args = newargs;
15868 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015869#ifdef FEAT_PROFILE
15870 fp->uf_tml_count = NULL;
15871 fp->uf_tml_total = NULL;
15872 fp->uf_tml_self = NULL;
15873 fp->uf_profiling = FALSE;
15874 if (prof_def_func())
15875 func_do_profile(fp);
15876#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015877 fp->uf_varargs = varargs;
15878 fp->uf_flags = flags;
15879 fp->uf_calls = 0;
15880 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015881 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
15883erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 ga_clear_strings(&newargs);
15885 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015886ret_free:
15887 vim_free(skip_until);
15888 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891}
15892
15893/*
15894 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015895 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015897 * flags:
15898 * TFN_INT: internal function name OK
15899 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 * Advances "pp" to just after the function name (if no error).
15901 */
15902 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015903trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 char_u **pp;
15905 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015906 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015909 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 char_u *start;
15911 char_u *end;
15912 int lead;
15913 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015915 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015916
15917 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015918 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015920
15921 /* Check for hard coded <SNR>: already translated function ID (from a user
15922 * command). */
15923 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15924 && (*pp)[2] == (int)KE_SNR)
15925 {
15926 *pp += 3;
15927 len = get_id_len(pp) + 3;
15928 return vim_strnsave(start, len);
15929 }
15930
15931 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15932 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015934 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015936
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015937 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015938 if (end == start)
15939 {
15940 if (!skip)
15941 EMSG(_("E129: Function name required"));
15942 goto theend;
15943 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015944 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015945 {
15946 /*
15947 * Report an invalid expression in braces, unless the expression
15948 * evaluation has been cancelled due to an aborting error, an
15949 * interrupt, or an exception.
15950 */
15951 if (!aborting())
15952 {
15953 if (end != NULL)
15954 EMSG2(_(e_invarg2), start);
15955 }
15956 else
15957 *pp = find_name_end(start, NULL, NULL, TRUE);
15958 goto theend;
15959 }
15960
15961 if (lv.ll_tv != NULL)
15962 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015963 if (fdp != NULL)
15964 {
15965 fdp->fd_dict = lv.ll_dict;
15966 fdp->fd_newkey = lv.ll_newkey;
15967 lv.ll_newkey = NULL;
15968 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015970 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15971 {
15972 name = vim_strsave(lv.ll_tv->vval.v_string);
15973 *pp = end;
15974 }
15975 else
15976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015977 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15978 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015979 EMSG(_(e_funcref));
15980 else
15981 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015982 name = NULL;
15983 }
15984 goto theend;
15985 }
15986
15987 if (lv.ll_name == NULL)
15988 {
15989 /* Error found, but continue after the function name. */
15990 *pp = end;
15991 goto theend;
15992 }
15993
15994 if (lv.ll_exp_name != NULL)
15995 len = STRLEN(lv.ll_exp_name);
15996 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015997 {
15998 if (lead == 2) /* skip over "s:" */
15999 lv.ll_name += 2;
16000 len = (int)(end - lv.ll_name);
16001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016002
16003 /*
16004 * Copy the function name to allocated memory.
16005 * Accept <SID>name() inside a script, translate into <SNR>123_name().
16006 * Accept <SNR>123_name() outside a script.
16007 */
16008 if (skip)
16009 lead = 0; /* do nothing */
16010 else if (lead > 0)
16011 {
16012 lead = 3;
16013 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16014 {
16015 if (current_SID <= 0)
16016 {
16017 EMSG(_(e_usingsid));
16018 goto theend;
16019 }
16020 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16021 lead += (int)STRLEN(sid_buf);
16022 }
16023 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016024 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016025 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016026 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016027 goto theend;
16028 }
16029 name = alloc((unsigned)(len + lead + 1));
16030 if (name != NULL)
16031 {
16032 if (lead > 0)
16033 {
16034 name[0] = K_SPECIAL;
16035 name[1] = KS_EXTRA;
16036 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016037 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016038 STRCPY(name + 3, sid_buf);
16039 }
16040 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16041 name[len + lead] = NUL;
16042 }
16043 *pp = end;
16044
16045theend:
16046 clear_lval(&lv);
16047 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048}
16049
16050/*
16051 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16052 * Return 2 if "p" starts with "s:".
16053 * Return 0 otherwise.
16054 */
16055 static int
16056eval_fname_script(p)
16057 char_u *p;
16058{
16059 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16060 || STRNICMP(p + 1, "SNR>", 4) == 0))
16061 return 5;
16062 if (p[0] == 's' && p[1] == ':')
16063 return 2;
16064 return 0;
16065}
16066
16067/*
16068 * Return TRUE if "p" starts with "<SID>" or "s:".
16069 * Only works if eval_fname_script() returned non-zero for "p"!
16070 */
16071 static int
16072eval_fname_sid(p)
16073 char_u *p;
16074{
16075 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16076}
16077
16078/*
16079 * List the head of the function: "name(arg1, arg2)".
16080 */
16081 static void
16082list_func_head(fp, indent)
16083 ufunc_T *fp;
16084 int indent;
16085{
16086 int j;
16087
16088 msg_start();
16089 if (indent)
16090 MSG_PUTS(" ");
16091 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016092 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 {
16094 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016095 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 }
16097 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016098 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016100 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 {
16102 if (j)
16103 MSG_PUTS(", ");
16104 msg_puts(FUNCARG(fp, j));
16105 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016106 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 {
16108 if (j)
16109 MSG_PUTS(", ");
16110 MSG_PUTS("...");
16111 }
16112 msg_putchar(')');
16113}
16114
16115/*
16116 * Find a function by name, return pointer to it in ufuncs.
16117 * Return NULL for unknown function.
16118 */
16119 static ufunc_T *
16120find_func(name)
16121 char_u *name;
16122{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016123 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016125 hi = hash_find(&func_hashtab, name);
16126 if (!HASHITEM_EMPTY(hi))
16127 return HI2UF(hi);
16128 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129}
16130
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016131/*
16132 * Return TRUE if a function "name" exists.
16133 */
16134 static int
16135function_exists(name)
16136 char_u *name;
16137{
16138 char_u *p = name;
16139 int n = FALSE;
16140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016141 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016142 if (p != NULL)
16143 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016144 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016145 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016146 else
16147 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016148 vim_free(p);
16149 }
16150 return n;
16151}
16152
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016153/*
16154 * Return TRUE if "name" looks like a builtin function name: starts with a
16155 * lower case letter and doesn't contain a ':'.
16156 */
16157 static int
16158builtin_function(name)
16159 char_u *name;
16160{
16161 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
16162}
16163
Bram Moolenaar05159a02005-02-26 23:04:13 +000016164#if defined(FEAT_PROFILE) || defined(PROTO)
16165/*
16166 * Start profiling function "fp".
16167 */
16168 static void
16169func_do_profile(fp)
16170 ufunc_T *fp;
16171{
16172 fp->uf_tm_count = 0;
16173 profile_zero(&fp->uf_tm_self);
16174 profile_zero(&fp->uf_tm_total);
16175 if (fp->uf_tml_count == NULL)
16176 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16177 (sizeof(int) * fp->uf_lines.ga_len));
16178 if (fp->uf_tml_total == NULL)
16179 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16180 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16181 if (fp->uf_tml_self == NULL)
16182 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16183 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16184 fp->uf_tml_idx = -1;
16185 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16186 || fp->uf_tml_self == NULL)
16187 return; /* out of memory */
16188
16189 fp->uf_profiling = TRUE;
16190}
16191
16192/*
16193 * Dump the profiling results for all functions in file "fd".
16194 */
16195 void
16196func_dump_profile(fd)
16197 FILE *fd;
16198{
16199 hashitem_T *hi;
16200 int todo;
16201 ufunc_T *fp;
16202 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016203 ufunc_T **sorttab;
16204 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016205
16206 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016207 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16208
Bram Moolenaar05159a02005-02-26 23:04:13 +000016209 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16210 {
16211 if (!HASHITEM_EMPTY(hi))
16212 {
16213 --todo;
16214 fp = HI2UF(hi);
16215 if (fp->uf_profiling)
16216 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016217 if (sorttab != NULL)
16218 sorttab[st_len++] = fp;
16219
Bram Moolenaar05159a02005-02-26 23:04:13 +000016220 if (fp->uf_name[0] == K_SPECIAL)
16221 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16222 else
16223 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16224 if (fp->uf_tm_count == 1)
16225 fprintf(fd, "Called 1 time\n");
16226 else
16227 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16228 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16229 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16230 fprintf(fd, "\n");
16231 fprintf(fd, "count total (s) self (s)\n");
16232
16233 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16234 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016235 prof_func_line(fd, fp->uf_tml_count[i],
16236 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016237 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16238 }
16239 fprintf(fd, "\n");
16240 }
16241 }
16242 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016243
16244 if (sorttab != NULL && st_len > 0)
16245 {
16246 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16247 prof_total_cmp);
16248 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16249 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16250 prof_self_cmp);
16251 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16252 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016253}
Bram Moolenaar73830342005-02-28 22:48:19 +000016254
16255 static void
16256prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16257 FILE *fd;
16258 ufunc_T **sorttab;
16259 int st_len;
16260 char *title;
16261 int prefer_self; /* when equal print only self time */
16262{
16263 int i;
16264 ufunc_T *fp;
16265
16266 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16267 fprintf(fd, "count total (s) self (s) function\n");
16268 for (i = 0; i < 20 && i < st_len; ++i)
16269 {
16270 fp = sorttab[i];
16271 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16272 prefer_self);
16273 if (fp->uf_name[0] == K_SPECIAL)
16274 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16275 else
16276 fprintf(fd, " %s()\n", fp->uf_name);
16277 }
16278 fprintf(fd, "\n");
16279}
16280
16281/*
16282 * Print the count and times for one function or function line.
16283 */
16284 static void
16285prof_func_line(fd, count, total, self, prefer_self)
16286 FILE *fd;
16287 int count;
16288 proftime_T *total;
16289 proftime_T *self;
16290 int prefer_self; /* when equal print only self time */
16291{
16292 if (count > 0)
16293 {
16294 fprintf(fd, "%5d ", count);
16295 if (prefer_self && profile_equal(total, self))
16296 fprintf(fd, " ");
16297 else
16298 fprintf(fd, "%s ", profile_msg(total));
16299 if (!prefer_self && profile_equal(total, self))
16300 fprintf(fd, " ");
16301 else
16302 fprintf(fd, "%s ", profile_msg(self));
16303 }
16304 else
16305 fprintf(fd, " ");
16306}
16307
16308/*
16309 * Compare function for total time sorting.
16310 */
16311 static int
16312#ifdef __BORLANDC__
16313_RTLENTRYF
16314#endif
16315prof_total_cmp(s1, s2)
16316 const void *s1;
16317 const void *s2;
16318{
16319 ufunc_T *p1, *p2;
16320
16321 p1 = *(ufunc_T **)s1;
16322 p2 = *(ufunc_T **)s2;
16323 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16324}
16325
16326/*
16327 * Compare function for self time sorting.
16328 */
16329 static int
16330#ifdef __BORLANDC__
16331_RTLENTRYF
16332#endif
16333prof_self_cmp(s1, s2)
16334 const void *s1;
16335 const void *s2;
16336{
16337 ufunc_T *p1, *p2;
16338
16339 p1 = *(ufunc_T **)s1;
16340 p2 = *(ufunc_T **)s2;
16341 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16342}
16343
Bram Moolenaar05159a02005-02-26 23:04:13 +000016344#endif
16345
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016346/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016347 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016348 * Return TRUE if a package was loaded.
16349 */
16350 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016351script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016352 char_u *name;
16353{
16354 char_u *p;
16355 char_u *scriptname;
16356 int ret = FALSE;
16357
16358 /* If there is no colon after name[1] there is no package name. */
16359 p = vim_strchr(name, ':');
16360 if (p == NULL || p <= name + 2)
16361 return FALSE;
16362
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016363 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16364 scriptname = autoload_name(name);
16365 if (cmd_runtime(scriptname, FALSE) == OK)
16366 ret = TRUE;
16367
16368 vim_free(scriptname);
16369 return ret;
16370}
16371
16372/*
16373 * Return the autoload script name for a function or variable name.
16374 * Returns NULL when out of memory.
16375 */
16376 static char_u *
16377autoload_name(name)
16378 char_u *name;
16379{
16380 char_u *p;
16381 char_u *scriptname;
16382
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016383 /* Get the script file name: replace ':' with '/', append ".vim". */
16384 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16385 if (scriptname == NULL)
16386 return FALSE;
16387 STRCPY(scriptname, "autoload/");
16388 STRCAT(scriptname, name);
16389 *vim_strrchr(scriptname, ':') = NUL;
16390 STRCAT(scriptname, ".vim");
16391 while ((p = vim_strchr(scriptname, ':')) != NULL)
16392 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016393 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016394}
16395
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16397
16398/*
16399 * Function given to ExpandGeneric() to obtain the list of user defined
16400 * function names.
16401 */
16402 char_u *
16403get_user_func_name(xp, idx)
16404 expand_T *xp;
16405 int idx;
16406{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016407 static long_u done;
16408 static hashitem_T *hi;
16409 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410
16411 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016413 done = 0;
16414 hi = func_hashtab.ht_array;
16415 }
16416 if (done < func_hashtab.ht_used)
16417 {
16418 if (done++ > 0)
16419 ++hi;
16420 while (HASHITEM_EMPTY(hi))
16421 ++hi;
16422 fp = HI2UF(hi);
16423
16424 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16425 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426
16427 cat_func_name(IObuff, fp);
16428 if (xp->xp_context != EXPAND_USER_FUNC)
16429 {
16430 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016431 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 STRCAT(IObuff, ")");
16433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 return IObuff;
16435 }
16436 return NULL;
16437}
16438
16439#endif /* FEAT_CMDL_COMPL */
16440
16441/*
16442 * Copy the function name of "fp" to buffer "buf".
16443 * "buf" must be able to hold the function name plus three bytes.
16444 * Takes care of script-local function names.
16445 */
16446 static void
16447cat_func_name(buf, fp)
16448 char_u *buf;
16449 ufunc_T *fp;
16450{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016451 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 {
16453 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016454 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 }
16456 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016457 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458}
16459
16460/*
16461 * ":delfunction {name}"
16462 */
16463 void
16464ex_delfunction(eap)
16465 exarg_T *eap;
16466{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016467 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 char_u *p;
16469 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016470 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471
16472 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016473 name = trans_function_name(&p, eap->skip, 0, &fudi);
16474 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016476 {
16477 if (fudi.fd_dict != NULL && !eap->skip)
16478 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 if (!ends_excmd(*skipwhite(p)))
16482 {
16483 vim_free(name);
16484 EMSG(_(e_trailing));
16485 return;
16486 }
16487 eap->nextcmd = check_nextcmd(p);
16488 if (eap->nextcmd != NULL)
16489 *p = NUL;
16490
16491 if (!eap->skip)
16492 fp = find_func(name);
16493 vim_free(name);
16494
16495 if (!eap->skip)
16496 {
16497 if (fp == NULL)
16498 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016499 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 return;
16501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016502 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 {
16504 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16505 return;
16506 }
16507
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016508 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016510 /* Delete the dict item that refers to the function, it will
16511 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016512 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016514 else
16515 func_free(fp);
16516 }
16517}
16518
16519/*
16520 * Free a function and remove it from the list of functions.
16521 */
16522 static void
16523func_free(fp)
16524 ufunc_T *fp;
16525{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016526 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016527
16528 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016529 ga_clear_strings(&(fp->uf_args));
16530 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016531#ifdef FEAT_PROFILE
16532 vim_free(fp->uf_tml_count);
16533 vim_free(fp->uf_tml_total);
16534 vim_free(fp->uf_tml_self);
16535#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016536
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016537 /* remove the function from the function hashtable */
16538 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16539 if (HASHITEM_EMPTY(hi))
16540 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016541 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016542 hash_remove(&func_hashtab, hi);
16543
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016544 vim_free(fp);
16545}
16546
16547/*
16548 * Unreference a Function: decrement the reference count and free it when it
16549 * becomes zero. Only for numbered functions.
16550 */
16551 static void
16552func_unref(name)
16553 char_u *name;
16554{
16555 ufunc_T *fp;
16556
16557 if (name != NULL && isdigit(*name))
16558 {
16559 fp = find_func(name);
16560 if (fp == NULL)
16561 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016562 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016563 {
16564 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016565 * when "uf_calls" becomes zero. */
16566 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016567 func_free(fp);
16568 }
16569 }
16570}
16571
16572/*
16573 * Count a reference to a Function.
16574 */
16575 static void
16576func_ref(name)
16577 char_u *name;
16578{
16579 ufunc_T *fp;
16580
16581 if (name != NULL && isdigit(*name))
16582 {
16583 fp = find_func(name);
16584 if (fp == NULL)
16585 EMSG2(_(e_intern2), "func_ref()");
16586 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016587 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 }
16589}
16590
16591/*
16592 * Call a user function.
16593 */
16594 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016595call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 ufunc_T *fp; /* pointer to function */
16597 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016598 typval_T *argvars; /* arguments */
16599 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 linenr_T firstline; /* first line of range */
16601 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016602 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603{
Bram Moolenaar33570922005-01-25 22:26:29 +000016604 char_u *save_sourcing_name;
16605 linenr_T save_sourcing_lnum;
16606 scid_T save_current_SID;
16607 funccall_T fc;
16608 funccall_T *save_fcp = current_funccal;
16609 int save_did_emsg;
16610 static int depth = 0;
16611 dictitem_T *v;
16612 int fixvar_idx = 0; /* index in fixvar[] */
16613 int i;
16614 int ai;
16615 char_u numbuf[NUMBUFLEN];
16616 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016617#ifdef FEAT_PROFILE
16618 proftime_T wait_start;
16619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620
16621 /* If depth of calling is getting too high, don't execute the function */
16622 if (depth >= p_mfd)
16623 {
16624 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016625 rettv->v_type = VAR_NUMBER;
16626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 return;
16628 }
16629 ++depth;
16630
16631 line_breakcheck(); /* check for CTRL-C hit */
16632
Bram Moolenaar33570922005-01-25 22:26:29 +000016633 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 fc.rettv = rettv;
16636 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 fc.linenr = 0;
16638 fc.returned = FALSE;
16639 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016641 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 fc.dbg_tick = debug_tick;
16643
Bram Moolenaar33570922005-01-25 22:26:29 +000016644 /*
16645 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16646 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16647 * each argument variable and saves a lot of time.
16648 */
16649 /*
16650 * Init l: variables.
16651 */
16652 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016653 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016654 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016655 /* Set l:self to "selfdict". */
16656 v = &fc.fixvar[fixvar_idx++].var;
16657 STRCPY(v->di_key, "self");
16658 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16659 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16660 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016661 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 v->di_tv.vval.v_dict = selfdict;
16663 ++selfdict->dv_refcount;
16664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016665
Bram Moolenaar33570922005-01-25 22:26:29 +000016666 /*
16667 * Init a: variables.
16668 * Set a:0 to "argcount".
16669 * Set a:000 to a list with room for the "..." arguments.
16670 */
16671 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16672 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016673 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 v = &fc.fixvar[fixvar_idx++].var;
16675 STRCPY(v->di_key, "000");
16676 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16677 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16678 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016679 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016680 v->di_tv.vval.v_list = &fc.l_varlist;
16681 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16682 fc.l_varlist.lv_refcount = 99999;
16683
16684 /*
16685 * Set a:firstline to "firstline" and a:lastline to "lastline".
16686 * Set a:name to named arguments.
16687 * Set a:N to the "..." arguments.
16688 */
16689 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16690 (varnumber_T)firstline);
16691 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16692 (varnumber_T)lastline);
16693 for (i = 0; i < argcount; ++i)
16694 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016695 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 if (ai < 0)
16697 /* named argument a:name */
16698 name = FUNCARG(fp, i);
16699 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016700 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 /* "..." argument a:1, a:2, etc. */
16702 sprintf((char *)numbuf, "%d", ai + 1);
16703 name = numbuf;
16704 }
16705 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16706 {
16707 v = &fc.fixvar[fixvar_idx++].var;
16708 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16709 }
16710 else
16711 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016712 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16713 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016714 if (v == NULL)
16715 break;
16716 v->di_flags = DI_FLAGS_RO;
16717 }
16718 STRCPY(v->di_key, name);
16719 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16720
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016721 /* Note: the values are copied directly to avoid alloc/free.
16722 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016723 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016724 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016725
16726 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16727 {
16728 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16729 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016730 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016731 }
16732 }
16733
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 /* Don't redraw while executing the function. */
16735 ++RedrawingDisabled;
16736 save_sourcing_name = sourcing_name;
16737 save_sourcing_lnum = sourcing_lnum;
16738 sourcing_lnum = 1;
16739 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016740 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 if (sourcing_name != NULL)
16742 {
16743 if (save_sourcing_name != NULL
16744 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16745 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16746 else
16747 STRCPY(sourcing_name, "function ");
16748 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16749
16750 if (p_verbose >= 12)
16751 {
16752 ++no_wait_return;
16753 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16754 msg_str((char_u *)_("calling %s"), sourcing_name);
16755 if (p_verbose >= 14)
16756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016758 char_u numbuf[NUMBUFLEN];
16759 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760
16761 msg_puts((char_u *)"(");
16762 for (i = 0; i < argcount; ++i)
16763 {
16764 if (i > 0)
16765 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016766 if (argvars[i].v_type == VAR_NUMBER)
16767 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 else
16769 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016770 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016773 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 }
16775 }
16776 msg_puts((char_u *)")");
16777 }
16778 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16779 cmdline_row = msg_row;
16780 --no_wait_return;
16781 }
16782 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016783#ifdef FEAT_PROFILE
16784 if (do_profiling)
16785 {
16786 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16787 func_do_profile(fp);
16788 if (fp->uf_profiling
16789 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16790 {
16791 ++fp->uf_tm_count;
16792 profile_start(&fp->uf_tm_start);
16793 profile_zero(&fp->uf_tm_children);
16794 }
16795 script_prof_save(&wait_start);
16796 }
16797#endif
16798
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016800 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 save_did_emsg = did_emsg;
16802 did_emsg = FALSE;
16803
16804 /* call do_cmdline() to execute the lines */
16805 do_cmdline(NULL, get_func_line, (void *)&fc,
16806 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16807
16808 --RedrawingDisabled;
16809
16810 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016811 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016813 clear_tv(rettv);
16814 rettv->v_type = VAR_NUMBER;
16815 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 }
16817
Bram Moolenaar05159a02005-02-26 23:04:13 +000016818#ifdef FEAT_PROFILE
16819 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16820 {
16821 profile_end(&fp->uf_tm_start);
16822 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16823 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16824 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16825 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16826 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16827 {
16828 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16829 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16830 }
16831 }
16832#endif
16833
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 /* when being verbose, mention the return value */
16835 if (p_verbose >= 12)
16836 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016837 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838
16839 ++no_wait_return;
16840 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16841
16842 /* Make sure the output fits in IObuff. */
16843 sn = sourcing_name;
16844 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16845 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16846
16847 if (aborting())
16848 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016849 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016854 char_u buf[MSG_BUF_LEN];
16855 char_u numbuf[NUMBUFLEN];
16856 char_u *tofree;
16857
16858 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16859 buf, MSG_BUF_LEN);
16860 smsg((char_u *)_("%s returning %s"), sn, buf);
16861 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 }
16863 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16864 cmdline_row = msg_row;
16865 --no_wait_return;
16866 }
16867
16868 vim_free(sourcing_name);
16869 sourcing_name = save_sourcing_name;
16870 sourcing_lnum = save_sourcing_lnum;
16871 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016872#ifdef FEAT_PROFILE
16873 if (do_profiling)
16874 script_prof_restore(&wait_start);
16875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876
16877 if (p_verbose >= 12 && sourcing_name != NULL)
16878 {
16879 ++no_wait_return;
16880 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16881 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16882 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16883 cmdline_row = msg_row;
16884 --no_wait_return;
16885 }
16886
16887 did_emsg |= save_did_emsg;
16888 current_funccal = save_fcp;
16889
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 /* The a: variables typevals were not alloced, only free the allocated
16891 * variables. */
16892 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16893
16894 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 --depth;
16896}
16897
16898/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016899 * Add a number variable "name" to dict "dp" with value "nr".
16900 */
16901 static void
16902add_nr_var(dp, v, name, nr)
16903 dict_T *dp;
16904 dictitem_T *v;
16905 char *name;
16906 varnumber_T nr;
16907{
16908 STRCPY(v->di_key, name);
16909 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16910 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16911 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016912 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 v->di_tv.vval.v_number = nr;
16914}
16915
16916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 * ":return [expr]"
16918 */
16919 void
16920ex_return(eap)
16921 exarg_T *eap;
16922{
16923 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 int returning = FALSE;
16926
16927 if (current_funccal == NULL)
16928 {
16929 EMSG(_("E133: :return not inside a function"));
16930 return;
16931 }
16932
16933 if (eap->skip)
16934 ++emsg_skip;
16935
16936 eap->nextcmd = NULL;
16937 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016938 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 {
16940 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016941 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 }
16945 /* It's safer to return also on error. */
16946 else if (!eap->skip)
16947 {
16948 /*
16949 * Return unless the expression evaluation has been cancelled due to an
16950 * aborting error, an interrupt, or an exception.
16951 */
16952 if (!aborting())
16953 returning = do_return(eap, FALSE, TRUE, NULL);
16954 }
16955
16956 /* When skipping or the return gets pending, advance to the next command
16957 * in this line (!returning). Otherwise, ignore the rest of the line.
16958 * Following lines will be ignored by get_func_line(). */
16959 if (returning)
16960 eap->nextcmd = NULL;
16961 else if (eap->nextcmd == NULL) /* no argument */
16962 eap->nextcmd = check_nextcmd(arg);
16963
16964 if (eap->skip)
16965 --emsg_skip;
16966}
16967
16968/*
16969 * Return from a function. Possibly makes the return pending. Also called
16970 * for a pending return at the ":endtry" or after returning from an extra
16971 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 * FALSE when the return gets pending.
16975 */
16976 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016977do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 exarg_T *eap;
16979 int reanimate;
16980 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016981 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982{
16983 int idx;
16984 struct condstack *cstack = eap->cstack;
16985
16986 if (reanimate)
16987 /* Undo the return. */
16988 current_funccal->returned = FALSE;
16989
16990 /*
16991 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16992 * not in its finally clause (which then is to be executed next) is found.
16993 * In this case, make the ":return" pending for execution at the ":endtry".
16994 * Otherwise, return normally.
16995 */
16996 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16997 if (idx >= 0)
16998 {
16999 cstack->cs_pending[idx] = CSTP_RETURN;
17000
17001 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017002 /* A pending return again gets pending. "rettv" points to an
17003 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 else
17007 {
17008 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017009 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 {
17015 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017016 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 else
17019 EMSG(_(e_outofmem));
17020 }
17021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023
17024 if (reanimate)
17025 {
17026 /* The pending return value could be overwritten by a ":return"
17027 * without argument in a finally clause; reset the default
17028 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 current_funccal->rettv->v_type = VAR_NUMBER;
17030 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 }
17032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017033 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 }
17035 else
17036 {
17037 current_funccal->returned = TRUE;
17038
17039 /* If the return is carried out now, store the return value. For
17040 * a return immediately after reanimation, the value is already
17041 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017044 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017045 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 }
17049 }
17050
17051 return idx < 0;
17052}
17053
17054/*
17055 * Free the variable with a pending return value.
17056 */
17057 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017058discard_pending_return(rettv)
17059 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060{
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062}
17063
17064/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 * is an allocated string. Used by report_pending() for verbose messages.
17067 */
17068 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017069get_return_cmd(rettv)
17070 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017072 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017073 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017074 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017076 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017078 if (s == NULL)
17079 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017080
17081 STRCPY(IObuff, ":return ");
17082 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17083 if (STRLEN(s) + 8 >= IOSIZE)
17084 STRCPY(IObuff + IOSIZE - 4, "...");
17085 vim_free(tofree);
17086 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087}
17088
17089/*
17090 * Get next function line.
17091 * Called by do_cmdline() to get the next line.
17092 * Returns allocated string, or NULL for end of function.
17093 */
17094/* ARGSUSED */
17095 char_u *
17096get_func_line(c, cookie, indent)
17097 int c; /* not used */
17098 void *cookie;
17099 int indent; /* not used */
17100{
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017102 ufunc_T *fp = fcp->func;
17103 char_u *retval;
17104 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105
17106 /* If breakpoints have been added/deleted need to check for it. */
17107 if (fcp->dbg_tick != debug_tick)
17108 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017109 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 sourcing_lnum);
17111 fcp->dbg_tick = debug_tick;
17112 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017113#ifdef FEAT_PROFILE
17114 if (do_profiling)
17115 func_line_end(cookie);
17116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117
Bram Moolenaar05159a02005-02-26 23:04:13 +000017118 gap = &fp->uf_lines;
17119 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 retval = NULL;
17121 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17122 retval = NULL;
17123 else
17124 {
17125 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17126 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017127#ifdef FEAT_PROFILE
17128 if (do_profiling)
17129 func_line_start(cookie);
17130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 }
17132
17133 /* Did we encounter a breakpoint? */
17134 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17135 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017136 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017138 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 sourcing_lnum);
17140 fcp->dbg_tick = debug_tick;
17141 }
17142
17143 return retval;
17144}
17145
Bram Moolenaar05159a02005-02-26 23:04:13 +000017146#if defined(FEAT_PROFILE) || defined(PROTO)
17147/*
17148 * Called when starting to read a function line.
17149 * "sourcing_lnum" must be correct!
17150 * When skipping lines it may not actually be executed, but we won't find out
17151 * until later and we need to store the time now.
17152 */
17153 void
17154func_line_start(cookie)
17155 void *cookie;
17156{
17157 funccall_T *fcp = (funccall_T *)cookie;
17158 ufunc_T *fp = fcp->func;
17159
17160 if (fp->uf_profiling && sourcing_lnum >= 1
17161 && sourcing_lnum <= fp->uf_lines.ga_len)
17162 {
17163 fp->uf_tml_idx = sourcing_lnum - 1;
17164 fp->uf_tml_execed = FALSE;
17165 profile_start(&fp->uf_tml_start);
17166 profile_zero(&fp->uf_tml_children);
17167 profile_get_wait(&fp->uf_tml_wait);
17168 }
17169}
17170
17171/*
17172 * Called when actually executing a function line.
17173 */
17174 void
17175func_line_exec(cookie)
17176 void *cookie;
17177{
17178 funccall_T *fcp = (funccall_T *)cookie;
17179 ufunc_T *fp = fcp->func;
17180
17181 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17182 fp->uf_tml_execed = TRUE;
17183}
17184
17185/*
17186 * Called when done with a function line.
17187 */
17188 void
17189func_line_end(cookie)
17190 void *cookie;
17191{
17192 funccall_T *fcp = (funccall_T *)cookie;
17193 ufunc_T *fp = fcp->func;
17194
17195 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17196 {
17197 if (fp->uf_tml_execed)
17198 {
17199 ++fp->uf_tml_count[fp->uf_tml_idx];
17200 profile_end(&fp->uf_tml_start);
17201 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17202 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17203 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17204 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17205 }
17206 fp->uf_tml_idx = -1;
17207 }
17208}
17209#endif
17210
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211/*
17212 * Return TRUE if the currently active function should be ended, because a
17213 * return was encountered or an error occured. Used inside a ":while".
17214 */
17215 int
17216func_has_ended(cookie)
17217 void *cookie;
17218{
Bram Moolenaar33570922005-01-25 22:26:29 +000017219 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220
17221 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17222 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017223 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 || fcp->returned);
17225}
17226
17227/*
17228 * return TRUE if cookie indicates a function which "abort"s on errors.
17229 */
17230 int
17231func_has_abort(cookie)
17232 void *cookie;
17233{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017234 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235}
17236
17237#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17238typedef enum
17239{
17240 VAR_FLAVOUR_DEFAULT,
17241 VAR_FLAVOUR_SESSION,
17242 VAR_FLAVOUR_VIMINFO
17243} var_flavour_T;
17244
17245static var_flavour_T var_flavour __ARGS((char_u *varname));
17246
17247 static var_flavour_T
17248var_flavour(varname)
17249 char_u *varname;
17250{
17251 char_u *p = varname;
17252
17253 if (ASCII_ISUPPER(*p))
17254 {
17255 while (*(++p))
17256 if (ASCII_ISLOWER(*p))
17257 return VAR_FLAVOUR_SESSION;
17258 return VAR_FLAVOUR_VIMINFO;
17259 }
17260 else
17261 return VAR_FLAVOUR_DEFAULT;
17262}
17263#endif
17264
17265#if defined(FEAT_VIMINFO) || defined(PROTO)
17266/*
17267 * Restore global vars that start with a capital from the viminfo file
17268 */
17269 int
17270read_viminfo_varlist(virp, writing)
17271 vir_T *virp;
17272 int writing;
17273{
17274 char_u *tab;
17275 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277
17278 if (!writing && (find_viminfo_parameter('!') != NULL))
17279 {
17280 tab = vim_strchr(virp->vir_line + 1, '\t');
17281 if (tab != NULL)
17282 {
17283 *tab++ = '\0'; /* isolate the variable name */
17284 if (*tab == 'S') /* string var */
17285 is_string = TRUE;
17286
17287 tab = vim_strchr(tab, '\t');
17288 if (tab != NULL)
17289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 if (is_string)
17291 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017292 tv.v_type = VAR_STRING;
17293 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 }
17296 else
17297 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017298 tv.v_type = VAR_NUMBER;
17299 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017301 set_var(virp->vir_line + 1, &tv, FALSE);
17302 if (is_string)
17303 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 }
17305 }
17306 }
17307
17308 return viminfo_readline(virp);
17309}
17310
17311/*
17312 * Write global vars that start with a capital to the viminfo file
17313 */
17314 void
17315write_viminfo_varlist(fp)
17316 FILE *fp;
17317{
Bram Moolenaar33570922005-01-25 22:26:29 +000017318 hashitem_T *hi;
17319 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017320 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017322 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017323 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017324 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325
17326 if (find_viminfo_parameter('!') == NULL)
17327 return;
17328
17329 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017330
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 todo = globvarht.ht_used;
17332 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017334 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017336 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 this_var = HI2DI(hi);
17338 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017339 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017340 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017341 {
17342 case VAR_STRING: s = "STR"; break;
17343 case VAR_NUMBER: s = "NUM"; break;
17344 default: continue;
17345 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017346 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017347 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17348 if (p != NULL)
17349 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017350 vim_free(tofree);
17351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 }
17353 }
17354}
17355#endif
17356
17357#if defined(FEAT_SESSION) || defined(PROTO)
17358 int
17359store_session_globals(fd)
17360 FILE *fd;
17361{
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 hashitem_T *hi;
17363 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017364 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 char_u *p, *t;
17366
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 todo = globvarht.ht_used;
17368 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017372 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 this_var = HI2DI(hi);
17374 if ((this_var->di_tv.v_type == VAR_NUMBER
17375 || this_var->di_tv.v_type == VAR_STRING)
17376 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017377 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017378 /* Escape special characters with a backslash. Turn a LF and
17379 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017380 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017381 (char_u *)"\\\"\n\r");
17382 if (p == NULL) /* out of memory */
17383 break;
17384 for (t = p; *t != NUL; ++t)
17385 if (*t == '\n')
17386 *t = 'n';
17387 else if (*t == '\r')
17388 *t = 'r';
17389 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 this_var->di_key,
17391 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17392 : ' ',
17393 p,
17394 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17395 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017396 || put_eol(fd) == FAIL)
17397 {
17398 vim_free(p);
17399 return FAIL;
17400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 vim_free(p);
17402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
17404 }
17405 return OK;
17406}
17407#endif
17408
17409#endif /* FEAT_EVAL */
17410
17411#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17412
17413
17414#ifdef WIN3264
17415/*
17416 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17417 */
17418static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17419static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17420static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17421
17422/*
17423 * Get the short pathname of a file.
17424 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17425 */
17426 static int
17427get_short_pathname(fnamep, bufp, fnamelen)
17428 char_u **fnamep;
17429 char_u **bufp;
17430 int *fnamelen;
17431{
17432 int l,len;
17433 char_u *newbuf;
17434
17435 len = *fnamelen;
17436
17437 l = GetShortPathName(*fnamep, *fnamep, len);
17438 if (l > len - 1)
17439 {
17440 /* If that doesn't work (not enough space), then save the string
17441 * and try again with a new buffer big enough
17442 */
17443 newbuf = vim_strnsave(*fnamep, l);
17444 if (newbuf == NULL)
17445 return 0;
17446
17447 vim_free(*bufp);
17448 *fnamep = *bufp = newbuf;
17449
17450 l = GetShortPathName(*fnamep,*fnamep,l+1);
17451
17452 /* Really should always succeed, as the buffer is big enough */
17453 }
17454
17455 *fnamelen = l;
17456 return 1;
17457}
17458
17459/*
17460 * Create a short path name. Returns the length of the buffer it needs.
17461 * Doesn't copy over the end of the buffer passed in.
17462 */
17463 static int
17464shortpath_for_invalid_fname(fname, bufp, fnamelen)
17465 char_u **fname;
17466 char_u **bufp;
17467 int *fnamelen;
17468{
17469 char_u *s, *p, *pbuf2, *pbuf3;
17470 char_u ch;
17471 int l,len,len2,plen,slen;
17472
17473 /* Make a copy */
17474 len2 = *fnamelen;
17475 pbuf2 = vim_strnsave(*fname, len2);
17476 pbuf3 = NULL;
17477
17478 s = pbuf2 + len2 - 1; /* Find the end */
17479 slen = 1;
17480 plen = len2;
17481
17482 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017483 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 {
17485 --s;
17486 ++slen;
17487 --plen;
17488 }
17489
17490 do
17491 {
17492 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017493 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 {
17495 --s;
17496 ++slen;
17497 --plen;
17498 }
17499 if (s <= pbuf2)
17500 break;
17501
17502 /* Remeber the character that is about to be blatted */
17503 ch = *s;
17504 *s = 0; /* get_short_pathname requires a null-terminated string */
17505
17506 /* Try it in situ */
17507 p = pbuf2;
17508 if (!get_short_pathname(&p, &pbuf3, &plen))
17509 {
17510 vim_free(pbuf2);
17511 return -1;
17512 }
17513 *s = ch; /* Preserve the string */
17514 } while (plen == 0);
17515
17516 if (plen > 0)
17517 {
17518 /* Remeber the length of the new string. */
17519 *fnamelen = len = plen + slen;
17520 vim_free(*bufp);
17521 if (len > len2)
17522 {
17523 /* If there's not enough space in the currently allocated string,
17524 * then copy it to a buffer big enough.
17525 */
17526 *fname= *bufp = vim_strnsave(p, len);
17527 if (*fname == NULL)
17528 return -1;
17529 }
17530 else
17531 {
17532 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17533 *fname = *bufp = pbuf2;
17534 if (p != pbuf2)
17535 strncpy(*fname, p, plen);
17536 pbuf2 = NULL;
17537 }
17538 /* Concat the next bit */
17539 strncpy(*fname + plen, s, slen);
17540 (*fname)[len] = '\0';
17541 }
17542 vim_free(pbuf3);
17543 vim_free(pbuf2);
17544 return 0;
17545}
17546
17547/*
17548 * Get a pathname for a partial path.
17549 */
17550 static int
17551shortpath_for_partial(fnamep, bufp, fnamelen)
17552 char_u **fnamep;
17553 char_u **bufp;
17554 int *fnamelen;
17555{
17556 int sepcount, len, tflen;
17557 char_u *p;
17558 char_u *pbuf, *tfname;
17559 int hasTilde;
17560
17561 /* Count up the path seperators from the RHS.. so we know which part
17562 * of the path to return.
17563 */
17564 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017565 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 if (vim_ispathsep(*p))
17567 ++sepcount;
17568
17569 /* Need full path first (use expand_env() to remove a "~/") */
17570 hasTilde = (**fnamep == '~');
17571 if (hasTilde)
17572 pbuf = tfname = expand_env_save(*fnamep);
17573 else
17574 pbuf = tfname = FullName_save(*fnamep, FALSE);
17575
17576 len = tflen = STRLEN(tfname);
17577
17578 if (!get_short_pathname(&tfname, &pbuf, &len))
17579 return -1;
17580
17581 if (len == 0)
17582 {
17583 /* Don't have a valid filename, so shorten the rest of the
17584 * path if we can. This CAN give us invalid 8.3 filenames, but
17585 * there's not a lot of point in guessing what it might be.
17586 */
17587 len = tflen;
17588 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17589 return -1;
17590 }
17591
17592 /* Count the paths backward to find the beginning of the desired string. */
17593 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017594 {
17595#ifdef FEAT_MBYTE
17596 if (has_mbyte)
17597 p -= mb_head_off(tfname, p);
17598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 if (vim_ispathsep(*p))
17600 {
17601 if (sepcount == 0 || (hasTilde && sepcount == 1))
17602 break;
17603 else
17604 sepcount --;
17605 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 if (hasTilde)
17608 {
17609 --p;
17610 if (p >= tfname)
17611 *p = '~';
17612 else
17613 return -1;
17614 }
17615 else
17616 ++p;
17617
17618 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17619 vim_free(*bufp);
17620 *fnamelen = (int)STRLEN(p);
17621 *bufp = pbuf;
17622 *fnamep = p;
17623
17624 return 0;
17625}
17626#endif /* WIN3264 */
17627
17628/*
17629 * Adjust a filename, according to a string of modifiers.
17630 * *fnamep must be NUL terminated when called. When returning, the length is
17631 * determined by *fnamelen.
17632 * Returns valid flags.
17633 * When there is an error, *fnamep is set to NULL.
17634 */
17635 int
17636modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17637 char_u *src; /* string with modifiers */
17638 int *usedlen; /* characters after src that are used */
17639 char_u **fnamep; /* file name so far */
17640 char_u **bufp; /* buffer for allocated file name or NULL */
17641 int *fnamelen; /* length of fnamep */
17642{
17643 int valid = 0;
17644 char_u *tail;
17645 char_u *s, *p, *pbuf;
17646 char_u dirname[MAXPATHL];
17647 int c;
17648 int has_fullname = 0;
17649#ifdef WIN3264
17650 int has_shortname = 0;
17651#endif
17652
17653repeat:
17654 /* ":p" - full path/file_name */
17655 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17656 {
17657 has_fullname = 1;
17658
17659 valid |= VALID_PATH;
17660 *usedlen += 2;
17661
17662 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17663 if ((*fnamep)[0] == '~'
17664#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17665 && ((*fnamep)[1] == '/'
17666# ifdef BACKSLASH_IN_FILENAME
17667 || (*fnamep)[1] == '\\'
17668# endif
17669 || (*fnamep)[1] == NUL)
17670
17671#endif
17672 )
17673 {
17674 *fnamep = expand_env_save(*fnamep);
17675 vim_free(*bufp); /* free any allocated file name */
17676 *bufp = *fnamep;
17677 if (*fnamep == NULL)
17678 return -1;
17679 }
17680
17681 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017682 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 {
17684 if (vim_ispathsep(*p)
17685 && p[1] == '.'
17686 && (p[2] == NUL
17687 || vim_ispathsep(p[2])
17688 || (p[2] == '.'
17689 && (p[3] == NUL || vim_ispathsep(p[3])))))
17690 break;
17691 }
17692
17693 /* FullName_save() is slow, don't use it when not needed. */
17694 if (*p != NUL || !vim_isAbsName(*fnamep))
17695 {
17696 *fnamep = FullName_save(*fnamep, *p != NUL);
17697 vim_free(*bufp); /* free any allocated file name */
17698 *bufp = *fnamep;
17699 if (*fnamep == NULL)
17700 return -1;
17701 }
17702
17703 /* Append a path separator to a directory. */
17704 if (mch_isdir(*fnamep))
17705 {
17706 /* Make room for one or two extra characters. */
17707 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17708 vim_free(*bufp); /* free any allocated file name */
17709 *bufp = *fnamep;
17710 if (*fnamep == NULL)
17711 return -1;
17712 add_pathsep(*fnamep);
17713 }
17714 }
17715
17716 /* ":." - path relative to the current directory */
17717 /* ":~" - path relative to the home directory */
17718 /* ":8" - shortname path - postponed till after */
17719 while (src[*usedlen] == ':'
17720 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17721 {
17722 *usedlen += 2;
17723 if (c == '8')
17724 {
17725#ifdef WIN3264
17726 has_shortname = 1; /* Postpone this. */
17727#endif
17728 continue;
17729 }
17730 pbuf = NULL;
17731 /* Need full path first (use expand_env() to remove a "~/") */
17732 if (!has_fullname)
17733 {
17734 if (c == '.' && **fnamep == '~')
17735 p = pbuf = expand_env_save(*fnamep);
17736 else
17737 p = pbuf = FullName_save(*fnamep, FALSE);
17738 }
17739 else
17740 p = *fnamep;
17741
17742 has_fullname = 0;
17743
17744 if (p != NULL)
17745 {
17746 if (c == '.')
17747 {
17748 mch_dirname(dirname, MAXPATHL);
17749 s = shorten_fname(p, dirname);
17750 if (s != NULL)
17751 {
17752 *fnamep = s;
17753 if (pbuf != NULL)
17754 {
17755 vim_free(*bufp); /* free any allocated file name */
17756 *bufp = pbuf;
17757 pbuf = NULL;
17758 }
17759 }
17760 }
17761 else
17762 {
17763 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17764 /* Only replace it when it starts with '~' */
17765 if (*dirname == '~')
17766 {
17767 s = vim_strsave(dirname);
17768 if (s != NULL)
17769 {
17770 *fnamep = s;
17771 vim_free(*bufp);
17772 *bufp = s;
17773 }
17774 }
17775 }
17776 vim_free(pbuf);
17777 }
17778 }
17779
17780 tail = gettail(*fnamep);
17781 *fnamelen = (int)STRLEN(*fnamep);
17782
17783 /* ":h" - head, remove "/file_name", can be repeated */
17784 /* Don't remove the first "/" or "c:\" */
17785 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17786 {
17787 valid |= VALID_HEAD;
17788 *usedlen += 2;
17789 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017790 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 --tail;
17792 *fnamelen = (int)(tail - *fnamep);
17793#ifdef VMS
17794 if (*fnamelen > 0)
17795 *fnamelen += 1; /* the path separator is part of the path */
17796#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017797 while (tail > s && !after_pathsep(s, tail))
17798 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 }
17800
17801 /* ":8" - shortname */
17802 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17803 {
17804 *usedlen += 2;
17805#ifdef WIN3264
17806 has_shortname = 1;
17807#endif
17808 }
17809
17810#ifdef WIN3264
17811 /* Check shortname after we have done 'heads' and before we do 'tails'
17812 */
17813 if (has_shortname)
17814 {
17815 pbuf = NULL;
17816 /* Copy the string if it is shortened by :h */
17817 if (*fnamelen < (int)STRLEN(*fnamep))
17818 {
17819 p = vim_strnsave(*fnamep, *fnamelen);
17820 if (p == 0)
17821 return -1;
17822 vim_free(*bufp);
17823 *bufp = *fnamep = p;
17824 }
17825
17826 /* Split into two implementations - makes it easier. First is where
17827 * there isn't a full name already, second is where there is.
17828 */
17829 if (!has_fullname && !vim_isAbsName(*fnamep))
17830 {
17831 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17832 return -1;
17833 }
17834 else
17835 {
17836 int l;
17837
17838 /* Simple case, already have the full-name
17839 * Nearly always shorter, so try first time. */
17840 l = *fnamelen;
17841 if (!get_short_pathname(fnamep, bufp, &l))
17842 return -1;
17843
17844 if (l == 0)
17845 {
17846 /* Couldn't find the filename.. search the paths.
17847 */
17848 l = *fnamelen;
17849 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17850 return -1;
17851 }
17852 *fnamelen = l;
17853 }
17854 }
17855#endif /* WIN3264 */
17856
17857 /* ":t" - tail, just the basename */
17858 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17859 {
17860 *usedlen += 2;
17861 *fnamelen -= (int)(tail - *fnamep);
17862 *fnamep = tail;
17863 }
17864
17865 /* ":e" - extension, can be repeated */
17866 /* ":r" - root, without extension, can be repeated */
17867 while (src[*usedlen] == ':'
17868 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17869 {
17870 /* find a '.' in the tail:
17871 * - for second :e: before the current fname
17872 * - otherwise: The last '.'
17873 */
17874 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17875 s = *fnamep - 2;
17876 else
17877 s = *fnamep + *fnamelen - 1;
17878 for ( ; s > tail; --s)
17879 if (s[0] == '.')
17880 break;
17881 if (src[*usedlen + 1] == 'e') /* :e */
17882 {
17883 if (s > tail)
17884 {
17885 *fnamelen += (int)(*fnamep - (s + 1));
17886 *fnamep = s + 1;
17887#ifdef VMS
17888 /* cut version from the extension */
17889 s = *fnamep + *fnamelen - 1;
17890 for ( ; s > *fnamep; --s)
17891 if (s[0] == ';')
17892 break;
17893 if (s > *fnamep)
17894 *fnamelen = s - *fnamep;
17895#endif
17896 }
17897 else if (*fnamep <= tail)
17898 *fnamelen = 0;
17899 }
17900 else /* :r */
17901 {
17902 if (s > tail) /* remove one extension */
17903 *fnamelen = (int)(s - *fnamep);
17904 }
17905 *usedlen += 2;
17906 }
17907
17908 /* ":s?pat?foo?" - substitute */
17909 /* ":gs?pat?foo?" - global substitute */
17910 if (src[*usedlen] == ':'
17911 && (src[*usedlen + 1] == 's'
17912 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17913 {
17914 char_u *str;
17915 char_u *pat;
17916 char_u *sub;
17917 int sep;
17918 char_u *flags;
17919 int didit = FALSE;
17920
17921 flags = (char_u *)"";
17922 s = src + *usedlen + 2;
17923 if (src[*usedlen + 1] == 'g')
17924 {
17925 flags = (char_u *)"g";
17926 ++s;
17927 }
17928
17929 sep = *s++;
17930 if (sep)
17931 {
17932 /* find end of pattern */
17933 p = vim_strchr(s, sep);
17934 if (p != NULL)
17935 {
17936 pat = vim_strnsave(s, (int)(p - s));
17937 if (pat != NULL)
17938 {
17939 s = p + 1;
17940 /* find end of substitution */
17941 p = vim_strchr(s, sep);
17942 if (p != NULL)
17943 {
17944 sub = vim_strnsave(s, (int)(p - s));
17945 str = vim_strnsave(*fnamep, *fnamelen);
17946 if (sub != NULL && str != NULL)
17947 {
17948 *usedlen = (int)(p + 1 - src);
17949 s = do_string_sub(str, pat, sub, flags);
17950 if (s != NULL)
17951 {
17952 *fnamep = s;
17953 *fnamelen = (int)STRLEN(s);
17954 vim_free(*bufp);
17955 *bufp = s;
17956 didit = TRUE;
17957 }
17958 }
17959 vim_free(sub);
17960 vim_free(str);
17961 }
17962 vim_free(pat);
17963 }
17964 }
17965 /* after using ":s", repeat all the modifiers */
17966 if (didit)
17967 goto repeat;
17968 }
17969 }
17970
17971 return valid;
17972}
17973
17974/*
17975 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
17976 * "flags" can be "g" to do a global substitute.
17977 * Returns an allocated string, NULL for error.
17978 */
17979 char_u *
17980do_string_sub(str, pat, sub, flags)
17981 char_u *str;
17982 char_u *pat;
17983 char_u *sub;
17984 char_u *flags;
17985{
17986 int sublen;
17987 regmatch_T regmatch;
17988 int i;
17989 int do_all;
17990 char_u *tail;
17991 garray_T ga;
17992 char_u *ret;
17993 char_u *save_cpo;
17994
17995 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
17996 save_cpo = p_cpo;
17997 p_cpo = (char_u *)"";
17998
17999 ga_init2(&ga, 1, 200);
18000
18001 do_all = (flags[0] == 'g');
18002
18003 regmatch.rm_ic = p_ic;
18004 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18005 if (regmatch.regprog != NULL)
18006 {
18007 tail = str;
18008 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18009 {
18010 /*
18011 * Get some space for a temporary buffer to do the substitution
18012 * into. It will contain:
18013 * - The text up to where the match is.
18014 * - The substituted text.
18015 * - The text after the match.
18016 */
18017 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18018 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18019 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18020 {
18021 ga_clear(&ga);
18022 break;
18023 }
18024
18025 /* copy the text up to where the match is */
18026 i = (int)(regmatch.startp[0] - tail);
18027 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18028 /* add the substituted text */
18029 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18030 + ga.ga_len + i, TRUE, TRUE, FALSE);
18031 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 /* avoid getting stuck on a match with an empty string */
18033 if (tail == regmatch.endp[0])
18034 {
18035 if (*tail == NUL)
18036 break;
18037 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18038 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 }
18040 else
18041 {
18042 tail = regmatch.endp[0];
18043 if (*tail == NUL)
18044 break;
18045 }
18046 if (!do_all)
18047 break;
18048 }
18049
18050 if (ga.ga_data != NULL)
18051 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18052
18053 vim_free(regmatch.regprog);
18054 }
18055
18056 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18057 ga_clear(&ga);
18058 p_cpo = save_cpo;
18059
18060 return ret;
18061}
18062
18063#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */