blob: 42556b53722bb277f3c9fd651eaa441a7ca11cf1 [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 {
9045 /* set curwin to be our win, temporarily */
9046 oldcurwin = curwin;
9047 curwin = win;
9048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 /* restore previous notion of curwin */
9052 curwin = oldcurwin;
9053 }
9054 else
9055 {
9056 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009057 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 }
9062
9063 --emsg_off;
9064}
9065
9066/*
9067 * "glob()" function
9068 */
9069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009071 typval_T *argvars;
9072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 expand_T xpc;
9075
9076 ExpandInit(&xpc);
9077 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->v_type = VAR_STRING;
9079 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9081 ExpandCleanup(&xpc);
9082}
9083
9084/*
9085 * "globpath()" function
9086 */
9087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 typval_T *argvars;
9090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092 char_u buf1[NUMBUFLEN];
9093
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->v_type = VAR_STRING;
9095 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
9096 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
9099/*
9100 * "has()" function
9101 */
9102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009104 typval_T *argvars;
9105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 int i;
9108 char_u *name;
9109 int n = FALSE;
9110 static char *(has_list[]) =
9111 {
9112#ifdef AMIGA
9113 "amiga",
9114# ifdef FEAT_ARP
9115 "arp",
9116# endif
9117#endif
9118#ifdef __BEOS__
9119 "beos",
9120#endif
9121#ifdef MSDOS
9122# ifdef DJGPP
9123 "dos32",
9124# else
9125 "dos16",
9126# endif
9127#endif
9128#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9129 "mac",
9130#endif
9131#if defined(MACOS_X_UNIX)
9132 "macunix",
9133#endif
9134#ifdef OS2
9135 "os2",
9136#endif
9137#ifdef __QNX__
9138 "qnx",
9139#endif
9140#ifdef RISCOS
9141 "riscos",
9142#endif
9143#ifdef UNIX
9144 "unix",
9145#endif
9146#ifdef VMS
9147 "vms",
9148#endif
9149#ifdef WIN16
9150 "win16",
9151#endif
9152#ifdef WIN32
9153 "win32",
9154#endif
9155#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9156 "win32unix",
9157#endif
9158#ifdef WIN64
9159 "win64",
9160#endif
9161#ifdef EBCDIC
9162 "ebcdic",
9163#endif
9164#ifndef CASE_INSENSITIVE_FILENAME
9165 "fname_case",
9166#endif
9167#ifdef FEAT_ARABIC
9168 "arabic",
9169#endif
9170#ifdef FEAT_AUTOCMD
9171 "autocmd",
9172#endif
9173#ifdef FEAT_BEVAL
9174 "balloon_eval",
9175#endif
9176#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9177 "builtin_terms",
9178# ifdef ALL_BUILTIN_TCAPS
9179 "all_builtin_terms",
9180# endif
9181#endif
9182#ifdef FEAT_BYTEOFF
9183 "byte_offset",
9184#endif
9185#ifdef FEAT_CINDENT
9186 "cindent",
9187#endif
9188#ifdef FEAT_CLIENTSERVER
9189 "clientserver",
9190#endif
9191#ifdef FEAT_CLIPBOARD
9192 "clipboard",
9193#endif
9194#ifdef FEAT_CMDL_COMPL
9195 "cmdline_compl",
9196#endif
9197#ifdef FEAT_CMDHIST
9198 "cmdline_hist",
9199#endif
9200#ifdef FEAT_COMMENTS
9201 "comments",
9202#endif
9203#ifdef FEAT_CRYPT
9204 "cryptv",
9205#endif
9206#ifdef FEAT_CSCOPE
9207 "cscope",
9208#endif
9209#ifdef DEBUG
9210 "debug",
9211#endif
9212#ifdef FEAT_CON_DIALOG
9213 "dialog_con",
9214#endif
9215#ifdef FEAT_GUI_DIALOG
9216 "dialog_gui",
9217#endif
9218#ifdef FEAT_DIFF
9219 "diff",
9220#endif
9221#ifdef FEAT_DIGRAPHS
9222 "digraphs",
9223#endif
9224#ifdef FEAT_DND
9225 "dnd",
9226#endif
9227#ifdef FEAT_EMACS_TAGS
9228 "emacs_tags",
9229#endif
9230 "eval", /* always present, of course! */
9231#ifdef FEAT_EX_EXTRA
9232 "ex_extra",
9233#endif
9234#ifdef FEAT_SEARCH_EXTRA
9235 "extra_search",
9236#endif
9237#ifdef FEAT_FKMAP
9238 "farsi",
9239#endif
9240#ifdef FEAT_SEARCHPATH
9241 "file_in_path",
9242#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009243#if defined(UNIX) && !defined(USE_SYSTEM)
9244 "filterpipe",
9245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#ifdef FEAT_FIND_ID
9247 "find_in_path",
9248#endif
9249#ifdef FEAT_FOLDING
9250 "folding",
9251#endif
9252#ifdef FEAT_FOOTER
9253 "footer",
9254#endif
9255#if !defined(USE_SYSTEM) && defined(UNIX)
9256 "fork",
9257#endif
9258#ifdef FEAT_GETTEXT
9259 "gettext",
9260#endif
9261#ifdef FEAT_GUI
9262 "gui",
9263#endif
9264#ifdef FEAT_GUI_ATHENA
9265# ifdef FEAT_GUI_NEXTAW
9266 "gui_neXtaw",
9267# else
9268 "gui_athena",
9269# endif
9270#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009271#ifdef FEAT_GUI_KDE
9272 "gui_kde",
9273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#ifdef FEAT_GUI_GTK
9275 "gui_gtk",
9276# ifdef HAVE_GTK2
9277 "gui_gtk2",
9278# endif
9279#endif
9280#ifdef FEAT_GUI_MAC
9281 "gui_mac",
9282#endif
9283#ifdef FEAT_GUI_MOTIF
9284 "gui_motif",
9285#endif
9286#ifdef FEAT_GUI_PHOTON
9287 "gui_photon",
9288#endif
9289#ifdef FEAT_GUI_W16
9290 "gui_win16",
9291#endif
9292#ifdef FEAT_GUI_W32
9293 "gui_win32",
9294#endif
9295#ifdef FEAT_HANGULIN
9296 "hangul_input",
9297#endif
9298#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9299 "iconv",
9300#endif
9301#ifdef FEAT_INS_EXPAND
9302 "insert_expand",
9303#endif
9304#ifdef FEAT_JUMPLIST
9305 "jumplist",
9306#endif
9307#ifdef FEAT_KEYMAP
9308 "keymap",
9309#endif
9310#ifdef FEAT_LANGMAP
9311 "langmap",
9312#endif
9313#ifdef FEAT_LIBCALL
9314 "libcall",
9315#endif
9316#ifdef FEAT_LINEBREAK
9317 "linebreak",
9318#endif
9319#ifdef FEAT_LISP
9320 "lispindent",
9321#endif
9322#ifdef FEAT_LISTCMDS
9323 "listcmds",
9324#endif
9325#ifdef FEAT_LOCALMAP
9326 "localmap",
9327#endif
9328#ifdef FEAT_MENU
9329 "menu",
9330#endif
9331#ifdef FEAT_SESSION
9332 "mksession",
9333#endif
9334#ifdef FEAT_MODIFY_FNAME
9335 "modify_fname",
9336#endif
9337#ifdef FEAT_MOUSE
9338 "mouse",
9339#endif
9340#ifdef FEAT_MOUSESHAPE
9341 "mouseshape",
9342#endif
9343#if defined(UNIX) || defined(VMS)
9344# ifdef FEAT_MOUSE_DEC
9345 "mouse_dec",
9346# endif
9347# ifdef FEAT_MOUSE_GPM
9348 "mouse_gpm",
9349# endif
9350# ifdef FEAT_MOUSE_JSB
9351 "mouse_jsbterm",
9352# endif
9353# ifdef FEAT_MOUSE_NET
9354 "mouse_netterm",
9355# endif
9356# ifdef FEAT_MOUSE_PTERM
9357 "mouse_pterm",
9358# endif
9359# ifdef FEAT_MOUSE_XTERM
9360 "mouse_xterm",
9361# endif
9362#endif
9363#ifdef FEAT_MBYTE
9364 "multi_byte",
9365#endif
9366#ifdef FEAT_MBYTE_IME
9367 "multi_byte_ime",
9368#endif
9369#ifdef FEAT_MULTI_LANG
9370 "multi_lang",
9371#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009372#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009373#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009374 "mzscheme",
9375#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377#ifdef FEAT_OLE
9378 "ole",
9379#endif
9380#ifdef FEAT_OSFILETYPE
9381 "osfiletype",
9382#endif
9383#ifdef FEAT_PATH_EXTRA
9384 "path_extra",
9385#endif
9386#ifdef FEAT_PERL
9387#ifndef DYNAMIC_PERL
9388 "perl",
9389#endif
9390#endif
9391#ifdef FEAT_PYTHON
9392#ifndef DYNAMIC_PYTHON
9393 "python",
9394#endif
9395#endif
9396#ifdef FEAT_POSTSCRIPT
9397 "postscript",
9398#endif
9399#ifdef FEAT_PRINTER
9400 "printer",
9401#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00009402#ifdef FEAT_PROFILE
9403 "profile",
9404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405#ifdef FEAT_QUICKFIX
9406 "quickfix",
9407#endif
9408#ifdef FEAT_RIGHTLEFT
9409 "rightleft",
9410#endif
9411#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9412 "ruby",
9413#endif
9414#ifdef FEAT_SCROLLBIND
9415 "scrollbind",
9416#endif
9417#ifdef FEAT_CMDL_INFO
9418 "showcmd",
9419 "cmdline_info",
9420#endif
9421#ifdef FEAT_SIGNS
9422 "signs",
9423#endif
9424#ifdef FEAT_SMARTINDENT
9425 "smartindent",
9426#endif
9427#ifdef FEAT_SNIFF
9428 "sniff",
9429#endif
9430#ifdef FEAT_STL_OPT
9431 "statusline",
9432#endif
9433#ifdef FEAT_SUN_WORKSHOP
9434 "sun_workshop",
9435#endif
9436#ifdef FEAT_NETBEANS_INTG
9437 "netbeans_intg",
9438#endif
9439#ifdef FEAT_SYN_HL
9440 "syntax",
9441#endif
9442#if defined(USE_SYSTEM) || !defined(UNIX)
9443 "system",
9444#endif
9445#ifdef FEAT_TAG_BINS
9446 "tag_binary",
9447#endif
9448#ifdef FEAT_TAG_OLDSTATIC
9449 "tag_old_static",
9450#endif
9451#ifdef FEAT_TAG_ANYWHITE
9452 "tag_any_white",
9453#endif
9454#ifdef FEAT_TCL
9455# ifndef DYNAMIC_TCL
9456 "tcl",
9457# endif
9458#endif
9459#ifdef TERMINFO
9460 "terminfo",
9461#endif
9462#ifdef FEAT_TERMRESPONSE
9463 "termresponse",
9464#endif
9465#ifdef FEAT_TEXTOBJ
9466 "textobjects",
9467#endif
9468#ifdef HAVE_TGETENT
9469 "tgetent",
9470#endif
9471#ifdef FEAT_TITLE
9472 "title",
9473#endif
9474#ifdef FEAT_TOOLBAR
9475 "toolbar",
9476#endif
9477#ifdef FEAT_USR_CMDS
9478 "user-commands", /* was accidentally included in 5.4 */
9479 "user_commands",
9480#endif
9481#ifdef FEAT_VIMINFO
9482 "viminfo",
9483#endif
9484#ifdef FEAT_VERTSPLIT
9485 "vertsplit",
9486#endif
9487#ifdef FEAT_VIRTUALEDIT
9488 "virtualedit",
9489#endif
9490#ifdef FEAT_VISUAL
9491 "visual",
9492#endif
9493#ifdef FEAT_VISUALEXTRA
9494 "visualextra",
9495#endif
9496#ifdef FEAT_VREPLACE
9497 "vreplace",
9498#endif
9499#ifdef FEAT_WILDIGN
9500 "wildignore",
9501#endif
9502#ifdef FEAT_WILDMENU
9503 "wildmenu",
9504#endif
9505#ifdef FEAT_WINDOWS
9506 "windows",
9507#endif
9508#ifdef FEAT_WAK
9509 "winaltkeys",
9510#endif
9511#ifdef FEAT_WRITEBACKUP
9512 "writebackup",
9513#endif
9514#ifdef FEAT_XIM
9515 "xim",
9516#endif
9517#ifdef FEAT_XFONTSET
9518 "xfontset",
9519#endif
9520#ifdef USE_XSMP
9521 "xsmp",
9522#endif
9523#ifdef USE_XSMP_INTERACT
9524 "xsmp_interact",
9525#endif
9526#ifdef FEAT_XCLIPBOARD
9527 "xterm_clipboard",
9528#endif
9529#ifdef FEAT_XTERM_SAVE
9530 "xterm_save",
9531#endif
9532#if defined(UNIX) && defined(FEAT_X11)
9533 "X11",
9534#endif
9535 NULL
9536 };
9537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 for (i = 0; has_list[i] != NULL; ++i)
9540 if (STRICMP(name, has_list[i]) == 0)
9541 {
9542 n = TRUE;
9543 break;
9544 }
9545
9546 if (n == FALSE)
9547 {
9548 if (STRNICMP(name, "patch", 5) == 0)
9549 n = has_patch(atoi((char *)name + 5));
9550 else if (STRICMP(name, "vim_starting") == 0)
9551 n = (starting != 0);
9552#ifdef DYNAMIC_TCL
9553 else if (STRICMP(name, "tcl") == 0)
9554 n = tcl_enabled(FALSE);
9555#endif
9556#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9557 else if (STRICMP(name, "iconv") == 0)
9558 n = iconv_enabled(FALSE);
9559#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009560#ifdef DYNAMIC_MZSCHEME
9561 else if (STRICMP(name, "mzscheme") == 0)
9562 n = mzscheme_enabled(FALSE);
9563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#ifdef DYNAMIC_RUBY
9565 else if (STRICMP(name, "ruby") == 0)
9566 n = ruby_enabled(FALSE);
9567#endif
9568#ifdef DYNAMIC_PYTHON
9569 else if (STRICMP(name, "python") == 0)
9570 n = python_enabled(FALSE);
9571#endif
9572#ifdef DYNAMIC_PERL
9573 else if (STRICMP(name, "perl") == 0)
9574 n = perl_enabled(FALSE);
9575#endif
9576#ifdef FEAT_GUI
9577 else if (STRICMP(name, "gui_running") == 0)
9578 n = (gui.in_use || gui.starting);
9579# ifdef FEAT_GUI_W32
9580 else if (STRICMP(name, "gui_win32s") == 0)
9581 n = gui_is_win32s();
9582# endif
9583# ifdef FEAT_BROWSE
9584 else if (STRICMP(name, "browse") == 0)
9585 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9586# endif
9587#endif
9588#ifdef FEAT_SYN_HL
9589 else if (STRICMP(name, "syntax_items") == 0)
9590 n = syntax_present(curbuf);
9591#endif
9592#if defined(WIN3264)
9593 else if (STRICMP(name, "win95") == 0)
9594 n = mch_windows95();
9595#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009596#ifdef FEAT_NETBEANS_INTG
9597 else if (STRICMP(name, "netbeans_enabled") == 0)
9598 n = usingNetbeans;
9599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 }
9601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 * "has_key()" function
9607 */
9608 static void
9609f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612{
9613 rettv->vval.v_number = 0;
9614 if (argvars[0].v_type != VAR_DICT)
9615 {
9616 EMSG(_(e_dictreq));
9617 return;
9618 }
9619 if (argvars[0].vval.v_dict == NULL)
9620 return;
9621
9622 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009623 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624}
9625
9626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 * "hasmapto()" function
9628 */
9629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009631 typval_T *argvars;
9632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 char_u *name;
9635 char_u *mode;
9636 char_u buf[NUMBUFLEN];
9637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009639 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 mode = (char_u *)"nvo";
9641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643
9644 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
9649
9650/*
9651 * "histadd()" function
9652 */
9653/*ARGSUSED*/
9654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 typval_T *argvars;
9657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659#ifdef FEAT_CMDHIST
9660 int histype;
9661 char_u *str;
9662 char_u buf[NUMBUFLEN];
9663#endif
9664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 if (check_restricted() || check_secure())
9667 return;
9668#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (histype >= 0)
9671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (*str != NUL)
9674 {
9675 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 return;
9678 }
9679 }
9680#endif
9681}
9682
9683/*
9684 * "histdel()" function
9685 */
9686/*ARGSUSED*/
9687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 typval_T *argvars;
9690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
9692#ifdef FEAT_CMDHIST
9693 int n;
9694 char_u buf[NUMBUFLEN];
9695
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009696 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009699 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9702 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 else
9704 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9706 get_tv_string_buf(&argvars[1], buf));
9707 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710#endif
9711}
9712
9713/*
9714 * "histget()" function
9715 */
9716/*ARGSUSED*/
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722#ifdef FEAT_CMDHIST
9723 int type;
9724 int idx;
9725
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009727 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 idx = get_history_idx(type);
9729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 idx = (int)get_tv_number(&argvars[1]);
9731 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736}
9737
9738/*
9739 * "histnr()" function
9740 */
9741/*ARGSUSED*/
9742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009744 typval_T *argvars;
9745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747 int i;
9748
9749#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (i >= HIST_CMD && i < HIST_COUNT)
9752 i = get_history_idx(i);
9753 else
9754#endif
9755 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 * "highlightID(name)" function
9761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009771 * "highlight_exists()" function
9772 */
9773 static void
9774f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009777{
9778 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9779}
9780
9781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 * "hostname()" function
9783 */
9784/*ARGSUSED*/
9785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 typval_T *argvars;
9788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790 char_u hostname[256];
9791
9792 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->v_type = VAR_STRING;
9794 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795}
9796
9797/*
9798 * iconv() function
9799 */
9800/*ARGSUSED*/
9801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009803 typval_T *argvars;
9804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806#ifdef FEAT_MBYTE
9807 char_u buf1[NUMBUFLEN];
9808 char_u buf2[NUMBUFLEN];
9809 char_u *from, *to, *str;
9810 vimconv_T vimconv;
9811#endif
9812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 rettv->v_type = VAR_STRING;
9814 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 str = get_tv_string(&argvars[0]);
9818 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9819 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 vimconv.vc_type = CONV_NONE;
9821 convert_setup(&vimconv, from, to);
9822
9823 /* If the encodings are equal, no conversion needed. */
9824 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
9829 convert_setup(&vimconv, NULL, NULL);
9830 vim_free(from);
9831 vim_free(to);
9832#endif
9833}
9834
9835/*
9836 * "indent()" function
9837 */
9838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009840 typval_T *argvars;
9841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842{
9843 linenr_T lnum;
9844
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850}
9851
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009852/*
9853 * "index()" function
9854 */
9855 static void
9856f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009857 typval_T *argvars;
9858 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009859{
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 list_T *l;
9861 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009862 long idx = 0;
9863 int ic = FALSE;
9864
9865 rettv->vval.v_number = -1;
9866 if (argvars[0].v_type != VAR_LIST)
9867 {
9868 EMSG(_(e_listreq));
9869 return;
9870 }
9871 l = argvars[0].vval.v_list;
9872 if (l != NULL)
9873 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009874 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009876 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009877 /* Start at specified item. Use the cached index that list_find()
9878 * sets, so that a negative number also works. */
9879 item = list_find(l, get_tv_number(&argvars[2]));
9880 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009881 if (argvars[3].v_type != VAR_UNKNOWN)
9882 ic = get_tv_number(&argvars[3]);
9883 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884
Bram Moolenaar758711c2005-02-02 23:11:38 +00009885 for ( ; item != NULL; item = item->li_next, ++idx)
9886 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887 {
9888 rettv->vval.v_number = idx;
9889 break;
9890 }
9891 }
9892}
9893
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894static int inputsecret_flag = 0;
9895
9896/*
9897 * "input()" function
9898 * Also handles inputsecret() when inputsecret is set.
9899 */
9900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 typval_T *argvars;
9903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 char_u *p = NULL;
9907 int c;
9908 char_u buf[NUMBUFLEN];
9909 int cmd_silent_save = cmd_silent;
9910
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
9913#ifdef NO_CONSOLE_INPUT
9914 /* While starting up, there is no place to enter text. */
9915 if (no_console_input())
9916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 return;
9919 }
9920#endif
9921
9922 cmd_silent = FALSE; /* Want to see the prompt. */
9923 if (prompt != NULL)
9924 {
9925 /* Only the part of the message after the last NL is considered as
9926 * prompt for the command line */
9927 p = vim_strrchr(prompt, '\n');
9928 if (p == NULL)
9929 p = prompt;
9930 else
9931 {
9932 ++p;
9933 c = *p;
9934 *p = NUL;
9935 msg_start();
9936 msg_clr_eos();
9937 msg_puts_attr(prompt, echo_attr);
9938 msg_didout = FALSE;
9939 msg_starthere();
9940 *p = c;
9941 }
9942 cmdline_row = msg_row;
9943 }
9944
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009945 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9950
9951 /* since the user typed this, no need to wait for return */
9952 need_wait_return = FALSE;
9953 msg_didout = FALSE;
9954 cmd_silent = cmd_silent_save;
9955}
9956
9957/*
9958 * "inputdialog()" function
9959 */
9960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 typval_T *argvars;
9963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965#if defined(FEAT_GUI_TEXTDIALOG)
9966 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9967 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9968 {
9969 char_u *message;
9970 char_u buf[NUMBUFLEN];
9971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009973 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 IObuff[IOSIZE - 1] = NUL;
9977 }
9978 else
9979 IObuff[0] = NUL;
9980 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9981 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 else
9984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009985 if (argvars[1].v_type != VAR_UNKNOWN
9986 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_string = vim_strsave(
9988 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994 else
9995#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997}
9998
9999static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10000
10001/*
10002 * "inputrestore()" function
10003 */
10004/*ARGSUSED*/
10005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010007 typval_T *argvars;
10008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010 if (ga_userinput.ga_len > 0)
10011 {
10012 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10014 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 }
10017 else if (p_verbose > 1)
10018 {
10019 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022}
10023
10024/*
10025 * "inputsave()" function
10026 */
10027/*ARGSUSED*/
10028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010030 typval_T *argvars;
10031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032{
10033 /* Add an entry to the stack of typehead storage. */
10034 if (ga_grow(&ga_userinput, 1) == OK)
10035 {
10036 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10037 + ga_userinput.ga_len);
10038 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 }
10041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043}
10044
10045/*
10046 * "inputsecret()" function
10047 */
10048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
10053 ++cmdline_star;
10054 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 --cmdline_star;
10057 --inputsecret_flag;
10058}
10059
10060/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010061 * "insert()" function
10062 */
10063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010067{
10068 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010069 listitem_T *item;
10070 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010071
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010072 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010073 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010074 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010075 else if ((l = argvars[0].vval.v_list) != NULL
10076 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010077 {
10078 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010080
Bram Moolenaar758711c2005-02-02 23:11:38 +000010081 if (before == l->lv_len)
10082 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010083 else
10084 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010085 item = list_find(l, before);
10086 if (item == NULL)
10087 {
10088 EMSGN(_(e_listidx), before);
10089 l = NULL;
10090 }
10091 }
10092 if (l != NULL)
10093 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010094 list_insert_tv(l, &argvars[1], item);
10095 ++l->lv_refcount;
10096 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010097 }
10098 }
10099}
10100
10101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 * "isdirectory()" function
10103 */
10104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010106 typval_T *argvars;
10107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110}
10111
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010112/*
10113 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10114 * it refers to a List or Dictionary that is locked.
10115 */
10116 static int
10117tv_islocked(tv)
10118 typval_T *tv;
10119{
10120 return (tv->v_lock & VAR_LOCKED)
10121 || (tv->v_type == VAR_LIST
10122 && tv->vval.v_list != NULL
10123 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10124 || (tv->v_type == VAR_DICT
10125 && tv->vval.v_dict != NULL
10126 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10127}
10128
10129/*
10130 * "islocked()" function
10131 */
10132 static void
10133f_islocked(argvars, rettv)
10134 typval_T *argvars;
10135 typval_T *rettv;
10136{
10137 lval_T lv;
10138 char_u *end;
10139 dictitem_T *di;
10140
10141 rettv->vval.v_number = -1;
10142 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
10143 if (end != NULL && lv.ll_name != NULL)
10144 {
10145 if (*end != NUL)
10146 EMSG(_(e_trailing));
10147 else
10148 {
10149 if (lv.ll_tv == NULL)
10150 {
10151 if (check_changedtick(lv.ll_name))
10152 rettv->vval.v_number = 1; /* always locked */
10153 else
10154 {
10155 di = find_var(lv.ll_name, NULL);
10156 if (di != NULL)
10157 {
10158 /* Consider a variable locked when:
10159 * 1. the variable itself is locked
10160 * 2. the value of the variable is locked.
10161 * 3. the List or Dict value is locked.
10162 */
10163 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10164 || tv_islocked(&di->di_tv));
10165 }
10166 }
10167 }
10168 else if (lv.ll_range)
10169 EMSG(_("E745: Range not allowed"));
10170 else if (lv.ll_newkey != NULL)
10171 EMSG2(_(e_dictkey), lv.ll_newkey);
10172 else if (lv.ll_list != NULL)
10173 /* List item. */
10174 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10175 else
10176 /* Dictionary item. */
10177 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10178 }
10179 }
10180
10181 clear_lval(&lv);
10182}
10183
Bram Moolenaar33570922005-01-25 22:26:29 +000010184static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010185
10186/*
10187 * Turn a dict into a list:
10188 * "what" == 0: list of keys
10189 * "what" == 1: list of values
10190 * "what" == 2: list of items
10191 */
10192 static void
10193dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010194 typval_T *argvars;
10195 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010196 int what;
10197{
Bram Moolenaar33570922005-01-25 22:26:29 +000010198 list_T *l;
10199 list_T *l2;
10200 dictitem_T *di;
10201 hashitem_T *hi;
10202 listitem_T *li;
10203 listitem_T *li2;
10204 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010205 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010206
10207 rettv->vval.v_number = 0;
10208 if (argvars[0].v_type != VAR_DICT)
10209 {
10210 EMSG(_(e_dictreq));
10211 return;
10212 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010213 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010214 return;
10215
10216 l = list_alloc();
10217 if (l == NULL)
10218 return;
10219 rettv->v_type = VAR_LIST;
10220 rettv->vval.v_list = l;
10221 ++l->lv_refcount;
10222
Bram Moolenaar33570922005-01-25 22:26:29 +000010223 todo = d->dv_hashtab.ht_used;
10224 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010226 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010227 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010228 --todo;
10229 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010230
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010231 li = listitem_alloc();
10232 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010233 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010234 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010235
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010236 if (what == 0)
10237 {
10238 /* keys() */
10239 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010240 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010241 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10242 }
10243 else if (what == 1)
10244 {
10245 /* values() */
10246 copy_tv(&di->di_tv, &li->li_tv);
10247 }
10248 else
10249 {
10250 /* items() */
10251 l2 = list_alloc();
10252 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010253 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010254 li->li_tv.vval.v_list = l2;
10255 if (l2 == NULL)
10256 break;
10257 ++l2->lv_refcount;
10258
10259 li2 = listitem_alloc();
10260 if (li2 == NULL)
10261 break;
10262 list_append(l2, li2);
10263 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010264 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010265 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10266
10267 li2 = listitem_alloc();
10268 if (li2 == NULL)
10269 break;
10270 list_append(l2, li2);
10271 copy_tv(&di->di_tv, &li2->li_tv);
10272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010273 }
10274 }
10275}
10276
10277/*
10278 * "items(dict)" function
10279 */
10280 static void
10281f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010282 typval_T *argvars;
10283 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010284{
10285 dict_list(argvars, rettv, 2);
10286}
10287
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010289 * "join()" function
10290 */
10291 static void
10292f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *argvars;
10294 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010295{
10296 garray_T ga;
10297 char_u *sep;
10298
10299 rettv->vval.v_number = 0;
10300 if (argvars[0].v_type != VAR_LIST)
10301 {
10302 EMSG(_(e_listreq));
10303 return;
10304 }
10305 if (argvars[0].vval.v_list == NULL)
10306 return;
10307 if (argvars[1].v_type == VAR_UNKNOWN)
10308 sep = (char_u *)" ";
10309 else
10310 sep = get_tv_string(&argvars[1]);
10311
10312 ga_init2(&ga, (int)sizeof(char), 80);
10313 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10314 ga_append(&ga, NUL);
10315
10316 rettv->v_type = VAR_STRING;
10317 rettv->vval.v_string = (char_u *)ga.ga_data;
10318}
10319
10320/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010321 * "keys()" function
10322 */
10323 static void
10324f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010325 typval_T *argvars;
10326 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010327{
10328 dict_list(argvars, rettv, 0);
10329}
10330
10331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 * "last_buffer_nr()" function.
10333 */
10334/*ARGSUSED*/
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 typval_T *argvars;
10338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
10340 int n = 0;
10341 buf_T *buf;
10342
10343 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10344 if (n < buf->b_fnum)
10345 n = buf->b_fnum;
10346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010348}
10349
10350/*
10351 * "len()" function
10352 */
10353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010354f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010355 typval_T *argvars;
10356 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010357{
10358 switch (argvars[0].v_type)
10359 {
10360 case VAR_STRING:
10361 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 rettv->vval.v_number = (varnumber_T)STRLEN(
10363 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010364 break;
10365 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010367 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010368 case VAR_DICT:
10369 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10370 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010372 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010373 break;
10374 }
10375}
10376
Bram Moolenaar33570922005-01-25 22:26:29 +000010377static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010378
10379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010380libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010381 typval_T *argvars;
10382 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010383 int type;
10384{
10385#ifdef FEAT_LIBCALL
10386 char_u *string_in;
10387 char_u **string_result;
10388 int nr_result;
10389#endif
10390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010391 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010392 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010394 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396
10397 if (check_restricted() || check_secure())
10398 return;
10399
10400#ifdef FEAT_LIBCALL
10401 /* The first two args must be strings, otherwise its meaningless */
10402 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10403 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010404 string_in = NULL;
10405 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010406 string_in = argvars[2].vval.v_string;
10407 if (type == VAR_NUMBER)
10408 string_result = NULL;
10409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010411 if (mch_libcall(argvars[0].vval.v_string,
10412 argvars[1].vval.v_string,
10413 string_in,
10414 argvars[2].vval.v_number,
10415 string_result,
10416 &nr_result) == OK
10417 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010419 }
10420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421}
10422
10423/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010424 * "libcall()" function
10425 */
10426 static void
10427f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010428 typval_T *argvars;
10429 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010430{
10431 libcall_common(argvars, rettv, VAR_STRING);
10432}
10433
10434/*
10435 * "libcallnr()" function
10436 */
10437 static void
10438f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010439 typval_T *argvars;
10440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010441{
10442 libcall_common(argvars, rettv, VAR_NUMBER);
10443}
10444
10445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 * "line(string)" function
10447 */
10448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010450 typval_T *argvars;
10451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452{
10453 linenr_T lnum = 0;
10454 pos_T *fp;
10455
10456 fp = var2fpos(&argvars[0], TRUE);
10457 if (fp != NULL)
10458 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460}
10461
10462/*
10463 * "line2byte(lnum)" function
10464 */
10465/*ARGSUSED*/
10466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 typval_T *argvars;
10469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470{
10471#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473#else
10474 linenr_T lnum;
10475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010480 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10481 if (rettv->vval.v_number >= 0)
10482 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483#endif
10484}
10485
10486/*
10487 * "lispindent(lnum)" function
10488 */
10489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010491 typval_T *argvars;
10492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493{
10494#ifdef FEAT_LISP
10495 pos_T pos;
10496 linenr_T lnum;
10497
10498 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10501 {
10502 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 curwin->w_cursor = pos;
10505 }
10506 else
10507#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509}
10510
10511/*
10512 * "localtime()" function
10513 */
10514/*ARGSUSED*/
10515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T *argvars;
10518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521}
10522
Bram Moolenaar33570922005-01-25 22:26:29 +000010523static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524
10525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 typval_T *argvars;
10528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 int exact;
10530{
10531 char_u *keys;
10532 char_u *which;
10533 char_u buf[NUMBUFLEN];
10534 char_u *keys_buf = NULL;
10535 char_u *rhs;
10536 int mode;
10537 garray_T ga;
10538
10539 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 rettv->v_type = VAR_STRING;
10541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (*keys == NUL)
10545 return;
10546
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 else
10550 which = (char_u *)"";
10551 mode = get_map_mode(&which, 0);
10552
10553 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10554 rhs = check_map(keys, mode, exact);
10555 vim_free(keys_buf);
10556 if (rhs != NULL)
10557 {
10558 ga_init(&ga);
10559 ga.ga_itemsize = 1;
10560 ga.ga_growsize = 40;
10561
10562 while (*rhs != NUL)
10563 ga_concat(&ga, str2special(&rhs, FALSE));
10564
10565 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 }
10568}
10569
10570/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010571 * "map()" function
10572 */
10573 static void
10574f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *argvars;
10576 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577{
10578 filter_map(argvars, rettv, TRUE);
10579}
10580
10581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010582 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 */
10584 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010585f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010589 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590}
10591
10592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010593 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 */
10595 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010596f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010597 typval_T *argvars;
10598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010600 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601}
10602
Bram Moolenaar33570922005-01-25 22:26:29 +000010603static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604
10605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010606find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 typval_T *argvars;
10608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 int type;
10610{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611 char_u *str = NULL;
10612 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 char_u *pat;
10614 regmatch_T regmatch;
10615 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010616 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 char_u *save_cpo;
10618 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010619 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010620 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010621 list_T *l = NULL;
10622 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010624 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625
10626 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10627 save_cpo = p_cpo;
10628 p_cpo = (char_u *)"";
10629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010630 rettv->vval.v_number = -1;
10631 if (type == 3)
10632 {
10633 /* return empty list when there are no matches */
10634 if ((rettv->vval.v_list = list_alloc()) == NULL)
10635 goto theend;
10636 rettv->v_type = VAR_LIST;
10637 ++rettv->vval.v_list->lv_refcount;
10638 }
10639 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010641 rettv->v_type = VAR_STRING;
10642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010645 if (argvars[0].v_type == VAR_LIST)
10646 {
10647 if ((l = argvars[0].vval.v_list) == NULL)
10648 goto theend;
10649 li = l->lv_first;
10650 }
10651 else
10652 expr = str = get_tv_string(&argvars[0]);
10653
10654 pat = get_tv_string_buf(&argvars[1], patbuf);
10655
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010659 if (l != NULL)
10660 {
10661 li = list_find(l, start);
10662 if (li == NULL)
10663 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010664 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010665 }
10666 else
10667 {
10668 if (start < 0)
10669 start = 0;
10670 if (start > (long)STRLEN(str))
10671 goto theend;
10672 str += start;
10673 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010674
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010675 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678
10679 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10680 if (regmatch.regprog != NULL)
10681 {
10682 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010683
10684 while (1)
10685 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010686 if (l != NULL)
10687 {
10688 if (li == NULL)
10689 {
10690 match = FALSE;
10691 break;
10692 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010693 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010694 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010695 if (str == NULL)
10696 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010697 }
10698
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010699 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010700
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010701 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010702 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010703 if (l == NULL && !match)
10704 break;
10705
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010706 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010707 if (l != NULL)
10708 {
10709 li = li->li_next;
10710 ++idx;
10711 }
10712 else
10713 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010714#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010715 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010716#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010717 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010718#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010719 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010720 }
10721
10722 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010724 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010725 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010726 int i;
10727
10728 /* return list with matched string and submatches */
10729 for (i = 0; i < NSUBEXP; ++i)
10730 {
10731 if (regmatch.endp[i] == NULL)
10732 break;
10733 li = listitem_alloc();
10734 if (li == NULL)
10735 break;
10736 li->li_tv.v_type = VAR_STRING;
10737 li->li_tv.v_lock = 0;
10738 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10739 (int)(regmatch.endp[i] - regmatch.startp[i]));
10740 list_append(rettv->vval.v_list, li);
10741 }
10742 }
10743 else if (type == 2)
10744 {
10745 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010746 if (l != NULL)
10747 copy_tv(&li->li_tv, rettv);
10748 else
10749 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010751 }
10752 else if (l != NULL)
10753 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 else
10755 {
10756 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 (varnumber_T)(regmatch.startp[0] - str);
10759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 }
10764 }
10765 vim_free(regmatch.regprog);
10766 }
10767
10768theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010769 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 p_cpo = save_cpo;
10771}
10772
10773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774 * "match()" function
10775 */
10776 static void
10777f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010778 typval_T *argvars;
10779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010780{
10781 find_some_match(argvars, rettv, 1);
10782}
10783
10784/*
10785 * "matchend()" function
10786 */
10787 static void
10788f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *argvars;
10790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010791{
10792 find_some_match(argvars, rettv, 0);
10793}
10794
10795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010796 * "matchlist()" function
10797 */
10798 static void
10799f_matchlist(argvars, rettv)
10800 typval_T *argvars;
10801 typval_T *rettv;
10802{
10803 find_some_match(argvars, rettv, 3);
10804}
10805
10806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010807 * "matchstr()" function
10808 */
10809 static void
10810f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813{
10814 find_some_match(argvars, rettv, 2);
10815}
10816
Bram Moolenaar33570922005-01-25 22:26:29 +000010817static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010818
10819 static void
10820max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010821 typval_T *argvars;
10822 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010823 int domax;
10824{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010825 long n = 0;
10826 long i;
10827
10828 if (argvars[0].v_type == VAR_LIST)
10829 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 list_T *l;
10831 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010833 l = argvars[0].vval.v_list;
10834 if (l != NULL)
10835 {
10836 li = l->lv_first;
10837 if (li != NULL)
10838 {
10839 n = get_tv_number(&li->li_tv);
10840 while (1)
10841 {
10842 li = li->li_next;
10843 if (li == NULL)
10844 break;
10845 i = get_tv_number(&li->li_tv);
10846 if (domax ? i > n : i < n)
10847 n = i;
10848 }
10849 }
10850 }
10851 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 else if (argvars[0].v_type == VAR_DICT)
10853 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010855 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010856 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010857 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858
10859 d = argvars[0].vval.v_dict;
10860 if (d != NULL)
10861 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010862 todo = d->dv_hashtab.ht_used;
10863 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010865 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010867 --todo;
10868 i = get_tv_number(&HI2DI(hi)->di_tv);
10869 if (first)
10870 {
10871 n = i;
10872 first = FALSE;
10873 }
10874 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010875 n = i;
10876 }
10877 }
10878 }
10879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010880 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010881 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010882 rettv->vval.v_number = n;
10883}
10884
10885/*
10886 * "max()" function
10887 */
10888 static void
10889f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010890 typval_T *argvars;
10891 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010892{
10893 max_min(argvars, rettv, TRUE);
10894}
10895
10896/*
10897 * "min()" function
10898 */
10899 static void
10900f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010901 typval_T *argvars;
10902 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010903{
10904 max_min(argvars, rettv, FALSE);
10905}
10906
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010907static int mkdir_recurse __ARGS((char_u *dir, int prot));
10908
10909/*
10910 * Create the directory in which "dir" is located, and higher levels when
10911 * needed.
10912 */
10913 static int
10914mkdir_recurse(dir, prot)
10915 char_u *dir;
10916 int prot;
10917{
10918 char_u *p;
10919 char_u *updir;
10920 int r = FAIL;
10921
10922 /* Get end of directory name in "dir".
10923 * We're done when it's "/" or "c:/". */
10924 p = gettail_sep(dir);
10925 if (p <= get_past_head(dir))
10926 return OK;
10927
10928 /* If the directory exists we're done. Otherwise: create it.*/
10929 updir = vim_strnsave(dir, (int)(p - dir));
10930 if (updir == NULL)
10931 return FAIL;
10932 if (mch_isdir(updir))
10933 r = OK;
10934 else if (mkdir_recurse(updir, prot) == OK)
10935 r = vim_mkdir_emsg(updir, prot);
10936 vim_free(updir);
10937 return r;
10938}
10939
10940#ifdef vim_mkdir
10941/*
10942 * "mkdir()" function
10943 */
10944 static void
10945f_mkdir(argvars, rettv)
10946 typval_T *argvars;
10947 typval_T *rettv;
10948{
10949 char_u *dir;
10950 char_u buf[NUMBUFLEN];
10951 int prot = 0755;
10952
10953 rettv->vval.v_number = FAIL;
10954 if (check_restricted() || check_secure())
10955 return;
10956
10957 dir = get_tv_string_buf(&argvars[0], buf);
10958 if (argvars[1].v_type != VAR_UNKNOWN)
10959 {
10960 if (argvars[2].v_type != VAR_UNKNOWN)
10961 prot = get_tv_number(&argvars[2]);
10962 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
10963 mkdir_recurse(dir, prot);
10964 }
10965 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
10966}
10967#endif
10968
Bram Moolenaar0d660222005-01-07 21:51:51 +000010969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 * "mode()" function
10971 */
10972/*ARGSUSED*/
10973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010975 typval_T *argvars;
10976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978 char_u buf[2];
10979
10980#ifdef FEAT_VISUAL
10981 if (VIsual_active)
10982 {
10983 if (VIsual_select)
10984 buf[0] = VIsual_mode + 's' - 'v';
10985 else
10986 buf[0] = VIsual_mode;
10987 }
10988 else
10989#endif
10990 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10991 buf[0] = 'r';
10992 else if (State & INSERT)
10993 {
10994 if (State & REPLACE_FLAG)
10995 buf[0] = 'R';
10996 else
10997 buf[0] = 'i';
10998 }
10999 else if (State & CMDLINE)
11000 buf[0] = 'c';
11001 else
11002 buf[0] = 'n';
11003
11004 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 rettv->vval.v_string = vim_strsave(buf);
11006 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011010 * "nextnonblank()" function
11011 */
11012 static void
11013f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *argvars;
11015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011016{
11017 linenr_T lnum;
11018
11019 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11020 {
11021 if (lnum > curbuf->b_ml.ml_line_count)
11022 {
11023 lnum = 0;
11024 break;
11025 }
11026 if (*skipwhite(ml_get(lnum)) != NUL)
11027 break;
11028 }
11029 rettv->vval.v_number = lnum;
11030}
11031
11032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 * "nr2char()" function
11034 */
11035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 typval_T *argvars;
11038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
11040 char_u buf[NUMBUFLEN];
11041
11042#ifdef FEAT_MBYTE
11043 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 else
11046#endif
11047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 buf[1] = NUL;
11050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 rettv->v_type = VAR_STRING;
11052 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053}
11054
11055/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011056 * "prevnonblank()" function
11057 */
11058 static void
11059f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011060 typval_T *argvars;
11061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011062{
11063 linenr_T lnum;
11064
11065 lnum = get_tv_lnum(argvars);
11066 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11067 lnum = 0;
11068 else
11069 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11070 --lnum;
11071 rettv->vval.v_number = lnum;
11072}
11073
Bram Moolenaar8c711452005-01-14 21:53:12 +000011074/*
11075 * "range()" function
11076 */
11077 static void
11078f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 typval_T *argvars;
11080 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011081{
11082 long start;
11083 long end;
11084 long stride = 1;
11085 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011086 list_T *l;
11087 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011088
11089 start = get_tv_number(&argvars[0]);
11090 if (argvars[1].v_type == VAR_UNKNOWN)
11091 {
11092 end = start - 1;
11093 start = 0;
11094 }
11095 else
11096 {
11097 end = get_tv_number(&argvars[1]);
11098 if (argvars[2].v_type != VAR_UNKNOWN)
11099 stride = get_tv_number(&argvars[2]);
11100 }
11101
11102 rettv->vval.v_number = 0;
11103 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011104 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011105 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011106 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011107 else
11108 {
11109 l = list_alloc();
11110 if (l != NULL)
11111 {
11112 rettv->v_type = VAR_LIST;
11113 rettv->vval.v_list = l;
11114 ++l->lv_refcount;
11115
11116 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11117 {
11118 li = listitem_alloc();
11119 if (li == NULL)
11120 break;
11121 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011122 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011123 li->li_tv.vval.v_number = i;
11124 list_append(l, li);
11125 }
11126 }
11127 }
11128}
11129
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011130/*
11131 * "readfile()" function
11132 */
11133 static void
11134f_readfile(argvars, rettv)
11135 typval_T *argvars;
11136 typval_T *rettv;
11137{
11138 int binary = FALSE;
11139 char_u *fname;
11140 FILE *fd;
11141 list_T *l;
11142 listitem_T *li;
11143#define FREAD_SIZE 200 /* optimized for text lines */
11144 char_u buf[FREAD_SIZE];
11145 int readlen; /* size of last fread() */
11146 int buflen; /* nr of valid chars in buf[] */
11147 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11148 int tolist; /* first byte in buf[] still to be put in list */
11149 int chop; /* how many CR to chop off */
11150 char_u *prev = NULL; /* previously read bytes, if any */
11151 int prevlen = 0; /* length of "prev" if not NULL */
11152 char_u *s;
11153 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011154 long maxline = MAXLNUM;
11155 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011157 if (argvars[1].v_type != VAR_UNKNOWN)
11158 {
11159 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11160 binary = TRUE;
11161 if (argvars[2].v_type != VAR_UNKNOWN)
11162 maxline = get_tv_number(&argvars[2]);
11163 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011164
11165 l = list_alloc();
11166 if (l == NULL)
11167 return;
11168 rettv->v_type = VAR_LIST;
11169 rettv->vval.v_list = l;
11170 l->lv_refcount = 1;
11171
11172 /* Always open the file in binary mode, library functions have a mind of
11173 * their own about CR-LF conversion. */
11174 fname = get_tv_string(&argvars[0]);
11175 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11176 {
11177 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11178 return;
11179 }
11180
11181 filtd = 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011182 while (cnt < maxline)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011183 {
11184 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11185 buflen = filtd + readlen;
11186 tolist = 0;
11187 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11188 {
11189 if (buf[filtd] == '\n' || readlen <= 0)
11190 {
11191 /* Only when in binary mode add an empty list item when the
11192 * last line ends in a '\n'. */
11193 if (!binary && readlen == 0 && filtd == 0)
11194 break;
11195
11196 /* Found end-of-line or end-of-file: add a text line to the
11197 * list. */
11198 chop = 0;
11199 if (!binary)
11200 while (filtd - chop - 1 >= tolist
11201 && buf[filtd - chop - 1] == '\r')
11202 ++chop;
11203 len = filtd - tolist - chop;
11204 if (prev == NULL)
11205 s = vim_strnsave(buf + tolist, len);
11206 else
11207 {
11208 s = alloc((unsigned)(prevlen + len + 1));
11209 if (s != NULL)
11210 {
11211 mch_memmove(s, prev, prevlen);
11212 vim_free(prev);
11213 prev = NULL;
11214 mch_memmove(s + prevlen, buf + tolist, len);
11215 s[prevlen + len] = NUL;
11216 }
11217 }
11218 tolist = filtd + 1;
11219
11220 li = listitem_alloc();
11221 if (li == NULL)
11222 {
11223 vim_free(s);
11224 break;
11225 }
11226 li->li_tv.v_type = VAR_STRING;
11227 li->li_tv.v_lock = 0;
11228 li->li_tv.vval.v_string = s;
11229 list_append(l, li);
11230
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011231 if (++cnt >= maxline)
11232 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011233 if (readlen <= 0)
11234 break;
11235 }
11236 else if (buf[filtd] == NUL)
11237 buf[filtd] = '\n';
11238 }
11239 if (readlen <= 0)
11240 break;
11241
11242 if (tolist == 0)
11243 {
11244 /* "buf" is full, need to move text to an allocated buffer */
11245 if (prev == NULL)
11246 {
11247 prev = vim_strnsave(buf, buflen);
11248 prevlen = buflen;
11249 }
11250 else
11251 {
11252 s = alloc((unsigned)(prevlen + buflen));
11253 if (s != NULL)
11254 {
11255 mch_memmove(s, prev, prevlen);
11256 mch_memmove(s + prevlen, buf, buflen);
11257 vim_free(prev);
11258 prev = s;
11259 prevlen += buflen;
11260 }
11261 }
11262 filtd = 0;
11263 }
11264 else
11265 {
11266 mch_memmove(buf, buf + tolist, buflen - tolist);
11267 filtd -= tolist;
11268 }
11269 }
11270
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011271 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011272 fclose(fd);
11273}
11274
11275
Bram Moolenaar0d660222005-01-07 21:51:51 +000011276#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11277static void make_connection __ARGS((void));
11278static int check_connection __ARGS((void));
11279
11280 static void
11281make_connection()
11282{
11283 if (X_DISPLAY == NULL
11284# ifdef FEAT_GUI
11285 && !gui.in_use
11286# endif
11287 )
11288 {
11289 x_force_connect = TRUE;
11290 setup_term_clip();
11291 x_force_connect = FALSE;
11292 }
11293}
11294
11295 static int
11296check_connection()
11297{
11298 make_connection();
11299 if (X_DISPLAY == NULL)
11300 {
11301 EMSG(_("E240: No connection to Vim server"));
11302 return FAIL;
11303 }
11304 return OK;
11305}
11306#endif
11307
11308#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011309static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011310
11311 static void
11312remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011313 typval_T *argvars;
11314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011315 int expr;
11316{
11317 char_u *server_name;
11318 char_u *keys;
11319 char_u *r = NULL;
11320 char_u buf[NUMBUFLEN];
11321# ifdef WIN32
11322 HWND w;
11323# else
11324 Window w;
11325# endif
11326
11327 if (check_restricted() || check_secure())
11328 return;
11329
11330# ifdef FEAT_X11
11331 if (check_connection() == FAIL)
11332 return;
11333# endif
11334
11335 server_name = get_tv_string(&argvars[0]);
11336 keys = get_tv_string_buf(&argvars[1], buf);
11337# ifdef WIN32
11338 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
11339# else
11340 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
11341 < 0)
11342# endif
11343 {
11344 if (r != NULL)
11345 EMSG(r); /* sending worked but evaluation failed */
11346 else
11347 EMSG2(_("E241: Unable to send to %s"), server_name);
11348 return;
11349 }
11350
11351 rettv->vval.v_string = r;
11352
11353 if (argvars[2].v_type != VAR_UNKNOWN)
11354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356 char_u str[30];
11357
11358 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 v.di_tv.v_type = VAR_STRING;
11360 v.di_tv.vval.v_string = vim_strsave(str);
11361 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
11362 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363 }
11364}
11365#endif
11366
11367/*
11368 * "remote_expr()" function
11369 */
11370/*ARGSUSED*/
11371 static void
11372f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375{
11376 rettv->v_type = VAR_STRING;
11377 rettv->vval.v_string = NULL;
11378#ifdef FEAT_CLIENTSERVER
11379 remote_common(argvars, rettv, TRUE);
11380#endif
11381}
11382
11383/*
11384 * "remote_foreground()" function
11385 */
11386/*ARGSUSED*/
11387 static void
11388f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391{
11392 rettv->vval.v_number = 0;
11393#ifdef FEAT_CLIENTSERVER
11394# ifdef WIN32
11395 /* On Win32 it's done in this application. */
11396 serverForeground(get_tv_string(&argvars[0]));
11397# else
11398 /* Send a foreground() expression to the server. */
11399 argvars[1].v_type = VAR_STRING;
11400 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
11401 argvars[2].v_type = VAR_UNKNOWN;
11402 remote_common(argvars, rettv, TRUE);
11403 vim_free(argvars[1].vval.v_string);
11404# endif
11405#endif
11406}
11407
11408/*ARGSUSED*/
11409 static void
11410f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011413{
11414#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011415 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011416 char_u *s = NULL;
11417# ifdef WIN32
11418 int n = 0;
11419# endif
11420
11421 if (check_restricted() || check_secure())
11422 {
11423 rettv->vval.v_number = -1;
11424 return;
11425 }
11426# ifdef WIN32
11427 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11428 if (n == 0)
11429 rettv->vval.v_number = -1;
11430 else
11431 {
11432 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11433 rettv->vval.v_number = (s != NULL);
11434 }
11435# else
11436 rettv->vval.v_number = 0;
11437 if (check_connection() == FAIL)
11438 return;
11439
11440 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11441 serverStrToWin(get_tv_string(&argvars[0])), &s);
11442# endif
11443
11444 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11445 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011446 v.di_tv.v_type = VAR_STRING;
11447 v.di_tv.vval.v_string = vim_strsave(s);
11448 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11449 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011450 }
11451#else
11452 rettv->vval.v_number = -1;
11453#endif
11454}
11455
11456/*ARGSUSED*/
11457 static void
11458f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 typval_T *argvars;
11460 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011461{
11462 char_u *r = NULL;
11463
11464#ifdef FEAT_CLIENTSERVER
11465 if (!check_restricted() && !check_secure())
11466 {
11467# ifdef WIN32
11468 /* The server's HWND is encoded in the 'id' parameter */
11469 int n = 0;
11470
11471 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11472 if (n != 0)
11473 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11474 if (r == NULL)
11475# else
11476 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11477 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11478# endif
11479 EMSG(_("E277: Unable to read a server reply"));
11480 }
11481#endif
11482 rettv->v_type = VAR_STRING;
11483 rettv->vval.v_string = r;
11484}
11485
11486/*
11487 * "remote_send()" function
11488 */
11489/*ARGSUSED*/
11490 static void
11491f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494{
11495 rettv->v_type = VAR_STRING;
11496 rettv->vval.v_string = NULL;
11497#ifdef FEAT_CLIENTSERVER
11498 remote_common(argvars, rettv, FALSE);
11499#endif
11500}
11501
11502/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011503 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011504 */
11505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011507 typval_T *argvars;
11508 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011509{
Bram Moolenaar33570922005-01-25 22:26:29 +000011510 list_T *l;
11511 listitem_T *item, *item2;
11512 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011513 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011514 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011515 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 dict_T *d;
11517 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011518
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011519 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011520 if (argvars[0].v_type == VAR_DICT)
11521 {
11522 if (argvars[2].v_type != VAR_UNKNOWN)
11523 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011524 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011525 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011526 {
11527 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011528 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011529 if (di == NULL)
11530 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011531 else
11532 {
11533 *rettv = di->di_tv;
11534 init_tv(&di->di_tv);
11535 dictitem_remove(d, di);
11536 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011537 }
11538 }
11539 else if (argvars[0].v_type != VAR_LIST)
11540 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011541 else if ((l = argvars[0].vval.v_list) != NULL
11542 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011545 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 if (item == NULL)
11547 EMSGN(_(e_listidx), idx);
11548 else
11549 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011550 if (argvars[2].v_type == VAR_UNKNOWN)
11551 {
11552 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011553 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011554 *rettv = item->li_tv;
11555 vim_free(item);
11556 }
11557 else
11558 {
11559 /* Remove range of items, return list with values. */
11560 end = get_tv_number(&argvars[2]);
11561 item2 = list_find(l, end);
11562 if (item2 == NULL)
11563 EMSGN(_(e_listidx), end);
11564 else
11565 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011566 int cnt = 0;
11567
11568 for (li = item; li != NULL; li = li->li_next)
11569 {
11570 ++cnt;
11571 if (li == item2)
11572 break;
11573 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011574 if (li == NULL) /* didn't find "item2" after "item" */
11575 EMSG(_(e_invrange));
11576 else
11577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011578 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011579 l = list_alloc();
11580 if (l != NULL)
11581 {
11582 rettv->v_type = VAR_LIST;
11583 rettv->vval.v_list = l;
11584 l->lv_first = item;
11585 l->lv_last = item2;
11586 l->lv_refcount = 1;
11587 item->li_prev = NULL;
11588 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011589 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011590 }
11591 }
11592 }
11593 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011594 }
11595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596}
11597
11598/*
11599 * "rename({from}, {to})" function
11600 */
11601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 typval_T *argvars;
11604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605{
11606 char_u buf[NUMBUFLEN];
11607
11608 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11612 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613}
11614
11615/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011616 * "repeat()" function
11617 */
11618/*ARGSUSED*/
11619 static void
11620f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 typval_T *argvars;
11622 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011623{
11624 char_u *p;
11625 int n;
11626 int slen;
11627 int len;
11628 char_u *r;
11629 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011630 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011631
11632 n = get_tv_number(&argvars[1]);
11633 if (argvars[0].v_type == VAR_LIST)
11634 {
11635 l = list_alloc();
11636 if (l != NULL && argvars[0].vval.v_list != NULL)
11637 {
11638 l->lv_refcount = 1;
11639 while (n-- > 0)
11640 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11641 break;
11642 }
11643 rettv->v_type = VAR_LIST;
11644 rettv->vval.v_list = l;
11645 }
11646 else
11647 {
11648 p = get_tv_string(&argvars[0]);
11649 rettv->v_type = VAR_STRING;
11650 rettv->vval.v_string = NULL;
11651
11652 slen = (int)STRLEN(p);
11653 len = slen * n;
11654 if (len <= 0)
11655 return;
11656
11657 r = alloc(len + 1);
11658 if (r != NULL)
11659 {
11660 for (i = 0; i < n; i++)
11661 mch_memmove(r + i * slen, p, (size_t)slen);
11662 r[len] = NUL;
11663 }
11664
11665 rettv->vval.v_string = r;
11666 }
11667}
11668
11669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 * "resolve()" function
11671 */
11672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 typval_T *argvars;
11675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676{
11677 char_u *p;
11678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680#ifdef FEAT_SHORTCUT
11681 {
11682 char_u *v = NULL;
11683
11684 v = mch_resolve_shortcut(p);
11685 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 }
11690#else
11691# ifdef HAVE_READLINK
11692 {
11693 char_u buf[MAXPATHL + 1];
11694 char_u *cpy;
11695 int len;
11696 char_u *remain = NULL;
11697 char_u *q;
11698 int is_relative_to_current = FALSE;
11699 int has_trailing_pathsep = FALSE;
11700 int limit = 100;
11701
11702 p = vim_strsave(p);
11703
11704 if (p[0] == '.' && (vim_ispathsep(p[1])
11705 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11706 is_relative_to_current = TRUE;
11707
11708 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011709 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 has_trailing_pathsep = TRUE;
11711
11712 q = getnextcomp(p);
11713 if (*q != NUL)
11714 {
11715 /* Separate the first path component in "p", and keep the
11716 * remainder (beginning with the path separator). */
11717 remain = vim_strsave(q - 1);
11718 q[-1] = NUL;
11719 }
11720
11721 for (;;)
11722 {
11723 for (;;)
11724 {
11725 len = readlink((char *)p, (char *)buf, MAXPATHL);
11726 if (len <= 0)
11727 break;
11728 buf[len] = NUL;
11729
11730 if (limit-- == 0)
11731 {
11732 vim_free(p);
11733 vim_free(remain);
11734 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 goto fail;
11737 }
11738
11739 /* Ensure that the result will have a trailing path separator
11740 * if the argument has one. */
11741 if (remain == NULL && has_trailing_pathsep)
11742 add_pathsep(buf);
11743
11744 /* Separate the first path component in the link value and
11745 * concatenate the remainders. */
11746 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11747 if (*q != NUL)
11748 {
11749 if (remain == NULL)
11750 remain = vim_strsave(q - 1);
11751 else
11752 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011753 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 if (cpy != NULL)
11755 {
11756 STRCAT(cpy, remain);
11757 vim_free(remain);
11758 remain = cpy;
11759 }
11760 }
11761 q[-1] = NUL;
11762 }
11763
11764 q = gettail(p);
11765 if (q > p && *q == NUL)
11766 {
11767 /* Ignore trailing path separator. */
11768 q[-1] = NUL;
11769 q = gettail(p);
11770 }
11771 if (q > p && !mch_isFullName(buf))
11772 {
11773 /* symlink is relative to directory of argument */
11774 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11775 if (cpy != NULL)
11776 {
11777 STRCPY(cpy, p);
11778 STRCPY(gettail(cpy), buf);
11779 vim_free(p);
11780 p = cpy;
11781 }
11782 }
11783 else
11784 {
11785 vim_free(p);
11786 p = vim_strsave(buf);
11787 }
11788 }
11789
11790 if (remain == NULL)
11791 break;
11792
11793 /* Append the first path component of "remain" to "p". */
11794 q = getnextcomp(remain + 1);
11795 len = q - remain - (*q != NUL);
11796 cpy = vim_strnsave(p, STRLEN(p) + len);
11797 if (cpy != NULL)
11798 {
11799 STRNCAT(cpy, remain, len);
11800 vim_free(p);
11801 p = cpy;
11802 }
11803 /* Shorten "remain". */
11804 if (*q != NUL)
11805 STRCPY(remain, q - 1);
11806 else
11807 {
11808 vim_free(remain);
11809 remain = NULL;
11810 }
11811 }
11812
11813 /* If the result is a relative path name, make it explicitly relative to
11814 * the current directory if and only if the argument had this form. */
11815 if (!vim_ispathsep(*p))
11816 {
11817 if (is_relative_to_current
11818 && *p != NUL
11819 && !(p[0] == '.'
11820 && (p[1] == NUL
11821 || vim_ispathsep(p[1])
11822 || (p[1] == '.'
11823 && (p[2] == NUL
11824 || vim_ispathsep(p[2]))))))
11825 {
11826 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011827 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 if (cpy != NULL)
11829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 vim_free(p);
11831 p = cpy;
11832 }
11833 }
11834 else if (!is_relative_to_current)
11835 {
11836 /* Strip leading "./". */
11837 q = p;
11838 while (q[0] == '.' && vim_ispathsep(q[1]))
11839 q += 2;
11840 if (q > p)
11841 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11842 }
11843 }
11844
11845 /* Ensure that the result will have no trailing path separator
11846 * if the argument had none. But keep "/" or "//". */
11847 if (!has_trailing_pathsep)
11848 {
11849 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011850 if (after_pathsep(p, q))
11851 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 }
11853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 }
11856# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858# endif
11859#endif
11860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862
11863#ifdef HAVE_READLINK
11864fail:
11865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867}
11868
11869/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011870 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 */
11872 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 typval_T *argvars;
11875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 list_T *l;
11878 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879
Bram Moolenaar0d660222005-01-07 21:51:51 +000011880 rettv->vval.v_number = 0;
11881 if (argvars[0].v_type != VAR_LIST)
11882 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011883 else if ((l = argvars[0].vval.v_list) != NULL
11884 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885 {
11886 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011887 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011888 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011889 while (li != NULL)
11890 {
11891 ni = li->li_prev;
11892 list_append(l, li);
11893 li = ni;
11894 }
11895 rettv->vval.v_list = l;
11896 rettv->v_type = VAR_LIST;
11897 ++l->lv_refcount;
11898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899}
11900
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011901#define SP_NOMOVE 1 /* don't move cursor */
11902#define SP_REPEAT 2 /* repeat to find outer pair */
11903#define SP_RETCOUNT 4 /* return matchcount */
11904
Bram Moolenaar33570922005-01-25 22:26:29 +000011905static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906
11907/*
11908 * Get flags for a search function.
11909 * Possibly sets "p_ws".
11910 * Returns BACKWARD, FORWARD or zero (for an error).
11911 */
11912 static int
11913get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011914 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011915 int *flagsp;
11916{
11917 int dir = FORWARD;
11918 char_u *flags;
11919 char_u nbuf[NUMBUFLEN];
11920 int mask;
11921
11922 if (varp->v_type != VAR_UNKNOWN)
11923 {
11924 flags = get_tv_string_buf(varp, nbuf);
11925 while (*flags != NUL)
11926 {
11927 switch (*flags)
11928 {
11929 case 'b': dir = BACKWARD; break;
11930 case 'w': p_ws = TRUE; break;
11931 case 'W': p_ws = FALSE; break;
11932 default: mask = 0;
11933 if (flagsp != NULL)
11934 switch (*flags)
11935 {
11936 case 'n': mask = SP_NOMOVE; break;
11937 case 'r': mask = SP_REPEAT; break;
11938 case 'm': mask = SP_RETCOUNT; break;
11939 }
11940 if (mask == 0)
11941 {
11942 EMSG2(_(e_invarg2), flags);
11943 dir = 0;
11944 }
11945 else
11946 *flagsp |= mask;
11947 }
11948 if (dir == 0)
11949 break;
11950 ++flags;
11951 }
11952 }
11953 return dir;
11954}
11955
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956/*
11957 * "search()" function
11958 */
11959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011961 typval_T *argvars;
11962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963{
11964 char_u *pat;
11965 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011966 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 int save_p_ws = p_ws;
11968 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011969 int flags = 0;
11970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011971 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011974 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11975 if (dir == 0)
11976 goto theend;
11977 if ((flags & ~SP_NOMOVE) != 0)
11978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011980 goto theend;
11981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011983 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11985 SEARCH_KEEP, RE_SEARCH) != FAIL)
11986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 curwin->w_cursor = pos;
11989 /* "/$" will put the cursor after the end of the line, may need to
11990 * correct that here */
11991 check_cursor();
11992 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011993
11994 /* If 'n' flag is used: restore cursor position. */
11995 if (flags & SP_NOMOVE)
11996 curwin->w_cursor = save_cursor;
11997theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 p_ws = save_p_ws;
11999}
12000
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001/*
12002 * "searchpair()" function
12003 */
12004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012006 typval_T *argvars;
12007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
12009 char_u *spat, *mpat, *epat;
12010 char_u *skip;
12011 char_u *pat, *pat2, *pat3;
12012 pos_T pos;
12013 pos_T firstpos;
12014 pos_T save_cursor;
12015 pos_T save_pos;
12016 int save_p_ws = p_ws;
12017 char_u *save_cpo;
12018 int dir;
12019 int flags = 0;
12020 char_u nbuf1[NUMBUFLEN];
12021 char_u nbuf2[NUMBUFLEN];
12022 char_u nbuf3[NUMBUFLEN];
12023 int n;
12024 int r;
12025 int nest = 1;
12026 int err;
12027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
12030 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12031 save_cpo = p_cpo;
12032 p_cpo = (char_u *)"";
12033
12034 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 spat = get_tv_string(&argvars[0]);
12036 mpat = get_tv_string_buf(&argvars[1], nbuf1);
12037 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038
12039 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12040 * start/middle/end (pat3, for the top pair). */
12041 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12042 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12043 if (pat2 == NULL || pat3 == NULL)
12044 goto theend;
12045 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12046 if (*mpat == NUL)
12047 STRCPY(pat3, pat2);
12048 else
12049 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12050 spat, epat, mpat);
12051
12052 /* Handle the optional fourth argument: flags */
12053 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012054 if (dir == 0)
12055 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056
12057 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012058 if (argvars[3].v_type == VAR_UNKNOWN
12059 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 skip = (char_u *)"";
12061 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063
12064 save_cursor = curwin->w_cursor;
12065 pos = curwin->w_cursor;
12066 firstpos.lnum = 0;
12067 pat = pat3;
12068 for (;;)
12069 {
12070 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12071 SEARCH_KEEP, RE_SEARCH);
12072 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12073 /* didn't find it or found the first match again: FAIL */
12074 break;
12075
12076 if (firstpos.lnum == 0)
12077 firstpos = pos;
12078
12079 /* If the skip pattern matches, ignore this match. */
12080 if (*skip != NUL)
12081 {
12082 save_pos = curwin->w_cursor;
12083 curwin->w_cursor = pos;
12084 r = eval_to_bool(skip, &err, NULL, FALSE);
12085 curwin->w_cursor = save_pos;
12086 if (err)
12087 {
12088 /* Evaluating {skip} caused an error, break here. */
12089 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 break;
12092 }
12093 if (r)
12094 continue;
12095 }
12096
12097 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12098 {
12099 /* Found end when searching backwards or start when searching
12100 * forward: nested pair. */
12101 ++nest;
12102 pat = pat2; /* nested, don't search for middle */
12103 }
12104 else
12105 {
12106 /* Found end when searching forward or start when searching
12107 * backward: end of (nested) pair; or found middle in outer pair. */
12108 if (--nest == 1)
12109 pat = pat3; /* outer level, search for middle */
12110 }
12111
12112 if (nest == 0)
12113 {
12114 /* Found the match: return matchcount or line number. */
12115 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 curwin->w_cursor = pos;
12120 if (!(flags & SP_REPEAT))
12121 break;
12122 nest = 1; /* search for next unmatched */
12123 }
12124 }
12125
12126 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 curwin->w_cursor = save_cursor;
12129
12130theend:
12131 vim_free(pat2);
12132 vim_free(pat3);
12133 p_ws = save_p_ws;
12134 p_cpo = save_cpo;
12135}
12136
Bram Moolenaar0d660222005-01-07 21:51:51 +000012137/*ARGSUSED*/
12138 static void
12139f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 typval_T *argvars;
12141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143#ifdef FEAT_CLIENTSERVER
12144 char_u buf[NUMBUFLEN];
12145 char_u *server = get_tv_string(&argvars[0]);
12146 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148 rettv->vval.v_number = -1;
12149 if (check_restricted() || check_secure())
12150 return;
12151# ifdef FEAT_X11
12152 if (check_connection() == FAIL)
12153 return;
12154# endif
12155
12156 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158 EMSG(_("E258: Unable to send to client"));
12159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 rettv->vval.v_number = 0;
12162#else
12163 rettv->vval.v_number = -1;
12164#endif
12165}
12166
12167/*ARGSUSED*/
12168 static void
12169f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012170 typval_T *argvars;
12171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172{
12173 char_u *r = NULL;
12174
12175#ifdef FEAT_CLIENTSERVER
12176# ifdef WIN32
12177 r = serverGetVimNames();
12178# else
12179 make_connection();
12180 if (X_DISPLAY != NULL)
12181 r = serverGetVimNames(X_DISPLAY);
12182# endif
12183#endif
12184 rettv->v_type = VAR_STRING;
12185 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186}
12187
12188/*
12189 * "setbufvar()" function
12190 */
12191/*ARGSUSED*/
12192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 typval_T *argvars;
12195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196{
12197 buf_T *buf;
12198#ifdef FEAT_AUTOCMD
12199 aco_save_T aco;
12200#else
12201 buf_T *save_curbuf;
12202#endif
12203 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 char_u nbuf[NUMBUFLEN];
12206
12207 if (check_restricted() || check_secure())
12208 return;
12209 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012210 buf = get_buf_tv(&argvars[0]);
12211 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 varp = &argvars[2];
12213
12214 if (buf != NULL && varname != NULL && varp != NULL)
12215 {
12216 /* set curbuf to be our buf, temporarily */
12217#ifdef FEAT_AUTOCMD
12218 aucmd_prepbuf(&aco, buf);
12219#else
12220 save_curbuf = curbuf;
12221 curbuf = buf;
12222#endif
12223
12224 if (*varname == '&')
12225 {
12226 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 set_option_value(varname, get_tv_number(varp),
12228 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 }
12230 else
12231 {
12232 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12233 if (bufvarname != NULL)
12234 {
12235 STRCPY(bufvarname, "b:");
12236 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012237 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 vim_free(bufvarname);
12239 }
12240 }
12241
12242 /* reset notion of buffer */
12243#ifdef FEAT_AUTOCMD
12244 aucmd_restbuf(&aco);
12245#else
12246 curbuf = save_curbuf;
12247#endif
12248 }
12249 --emsg_off;
12250}
12251
12252/*
12253 * "setcmdpos()" function
12254 */
12255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 typval_T *argvars;
12258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = set_cmdline_pos(
12261 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262}
12263
12264/*
12265 * "setline()" function
12266 */
12267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012269 typval_T *argvars;
12270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271{
12272 linenr_T lnum;
12273 char_u *line;
12274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 lnum = get_tv_lnum(argvars);
12276 line = get_tv_string(&argvars[1]);
12277 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278
12279 if (lnum >= 1
12280 && lnum <= curbuf->b_ml.ml_line_count
12281 && u_savesub(lnum) == OK
12282 && ml_replace(lnum, line, TRUE) == OK)
12283 {
12284 changed_bytes(lnum, 0);
12285 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 }
12288}
12289
12290/*
12291 * "setreg()" function
12292 */
12293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 typval_T *argvars;
12296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297{
12298 int regname;
12299 char_u *strregname;
12300 char_u *stropt;
12301 int append;
12302 char_u yank_type;
12303 long block_len;
12304
12305 block_len = -1;
12306 yank_type = MAUTO;
12307 append = FALSE;
12308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309 strregname = get_tv_string(argvars);
12310 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311
12312 regname = (strregname == NULL ? '"' : *strregname);
12313 if (regname == 0 || regname == '@')
12314 regname = '"';
12315 else if (regname == '=')
12316 return;
12317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 switch (*stropt)
12322 {
12323 case 'a': case 'A': /* append */
12324 append = TRUE;
12325 break;
12326 case 'v': case 'c': /* character-wise selection */
12327 yank_type = MCHAR;
12328 break;
12329 case 'V': case 'l': /* line-wise selection */
12330 yank_type = MLINE;
12331 break;
12332#ifdef FEAT_VISUAL
12333 case 'b': case Ctrl_V: /* block-wise selection */
12334 yank_type = MBLOCK;
12335 if (VIM_ISDIGIT(stropt[1]))
12336 {
12337 ++stropt;
12338 block_len = getdigits(&stropt) - 1;
12339 --stropt;
12340 }
12341 break;
12342#endif
12343 }
12344 }
12345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349}
12350
12351
12352/*
12353 * "setwinvar(expr)" function
12354 */
12355/*ARGSUSED*/
12356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012358 typval_T *argvars;
12359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360{
12361 win_T *win;
12362#ifdef FEAT_WINDOWS
12363 win_T *save_curwin;
12364#endif
12365 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 char_u nbuf[NUMBUFLEN];
12368
12369 if (check_restricted() || check_secure())
12370 return;
12371 ++emsg_off;
12372 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 varp = &argvars[2];
12375
12376 if (win != NULL && varname != NULL && varp != NULL)
12377 {
12378#ifdef FEAT_WINDOWS
12379 /* set curwin to be our win, temporarily */
12380 save_curwin = curwin;
12381 curwin = win;
12382 curbuf = curwin->w_buffer;
12383#endif
12384
12385 if (*varname == '&')
12386 {
12387 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 set_option_value(varname, get_tv_number(varp),
12389 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 }
12391 else
12392 {
12393 winvarname = alloc((unsigned)STRLEN(varname) + 3);
12394 if (winvarname != NULL)
12395 {
12396 STRCPY(winvarname, "w:");
12397 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012398 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 vim_free(winvarname);
12400 }
12401 }
12402
12403#ifdef FEAT_WINDOWS
12404 /* Restore current window, if it's still valid (autocomands can make
12405 * it invalid). */
12406 if (win_valid(save_curwin))
12407 {
12408 curwin = save_curwin;
12409 curbuf = curwin->w_buffer;
12410 }
12411#endif
12412 }
12413 --emsg_off;
12414}
12415
12416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012417 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 */
12419 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012420f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 typval_T *argvars;
12422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012424 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425
Bram Moolenaar0d660222005-01-07 21:51:51 +000012426 p = get_tv_string(&argvars[0]);
12427 rettv->vval.v_string = vim_strsave(p);
12428 simplify_filename(rettv->vval.v_string); /* simplify in place */
12429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430}
12431
Bram Moolenaar0d660222005-01-07 21:51:51 +000012432static int
12433#ifdef __BORLANDC__
12434 _RTLENTRYF
12435#endif
12436 item_compare __ARGS((const void *s1, const void *s2));
12437static int
12438#ifdef __BORLANDC__
12439 _RTLENTRYF
12440#endif
12441 item_compare2 __ARGS((const void *s1, const void *s2));
12442
12443static int item_compare_ic;
12444static char_u *item_compare_func;
12445#define ITEM_COMPARE_FAIL 999
12446
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012448 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450 static int
12451#ifdef __BORLANDC__
12452_RTLENTRYF
12453#endif
12454item_compare(s1, s2)
12455 const void *s1;
12456 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012458 char_u *p1, *p2;
12459 char_u *tofree1, *tofree2;
12460 int res;
12461 char_u numbuf1[NUMBUFLEN];
12462 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12465 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012466 if (item_compare_ic)
12467 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012469 res = STRCMP(p1, p2);
12470 vim_free(tofree1);
12471 vim_free(tofree2);
12472 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473}
12474
12475 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476#ifdef __BORLANDC__
12477_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479item_compare2(s1, s2)
12480 const void *s1;
12481 const void *s2;
12482{
12483 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T rettv;
12485 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012488 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12489 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12491 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492
12493 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12494 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012495 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496 clear_tv(&argv[0]);
12497 clear_tv(&argv[1]);
12498
12499 if (res == FAIL)
12500 res = ITEM_COMPARE_FAIL;
12501 else
12502 res = get_tv_number(&rettv);
12503 clear_tv(&rettv);
12504 return res;
12505}
12506
12507/*
12508 * "sort({list})" function
12509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 typval_T *argvars;
12513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514{
Bram Moolenaar33570922005-01-25 22:26:29 +000012515 list_T *l;
12516 listitem_T *li;
12517 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518 long len;
12519 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521 rettv->vval.v_number = 0;
12522 if (argvars[0].v_type != VAR_LIST)
12523 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 else
12525 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012527 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528 return;
12529 rettv->vval.v_list = l;
12530 rettv->v_type = VAR_LIST;
12531 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 len = list_len(l);
12534 if (len <= 1)
12535 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 item_compare_ic = FALSE;
12538 item_compare_func = NULL;
12539 if (argvars[1].v_type != VAR_UNKNOWN)
12540 {
12541 if (argvars[1].v_type == VAR_FUNC)
12542 item_compare_func = argvars[0].vval.v_string;
12543 else
12544 {
12545 i = get_tv_number(&argvars[1]);
12546 if (i == 1)
12547 item_compare_ic = TRUE;
12548 else
12549 item_compare_func = get_tv_string(&argvars[1]);
12550 }
12551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012554 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555 if (ptrs == NULL)
12556 return;
12557 i = 0;
12558 for (li = l->lv_first; li != NULL; li = li->li_next)
12559 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561 /* test the compare function */
12562 if (item_compare_func != NULL
12563 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12564 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012565 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567 {
12568 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 item_compare_func == NULL ? item_compare : item_compare2);
12571
12572 /* Clear the List and append the items in the sorted order. */
12573 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012574 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012575 for (i = 0; i < len; ++i)
12576 list_append(l, ptrs[i]);
12577 }
12578
12579 vim_free(ptrs);
12580 }
12581}
12582
12583 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012584f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587{
12588 char_u *str;
12589 char_u *end;
12590 char_u *pat;
12591 regmatch_T regmatch;
12592 char_u patbuf[NUMBUFLEN];
12593 char_u *save_cpo;
12594 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 listitem_T *ni;
12596 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597 colnr_T col = 0;
12598
12599 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12600 save_cpo = p_cpo;
12601 p_cpo = (char_u *)"";
12602
12603 str = get_tv_string(&argvars[0]);
12604 if (argvars[1].v_type == VAR_UNKNOWN)
12605 pat = (char_u *)"[\\x01- ]\\+";
12606 else
12607 pat = get_tv_string_buf(&argvars[1], patbuf);
12608
12609 l = list_alloc();
12610 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012612 rettv->v_type = VAR_LIST;
12613 rettv->vval.v_list = l;
12614 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12617 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 regmatch.rm_ic = FALSE;
12620 while (*str != NUL)
12621 {
12622 match = vim_regexec_nl(&regmatch, str, col);
12623 if (match)
12624 end = regmatch.startp[0];
12625 else
12626 end = str + STRLEN(str);
12627 if (end > str)
12628 {
12629 ni = listitem_alloc();
12630 if (ni == NULL)
12631 break;
12632 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012633 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12635 list_append(l, ni);
12636 }
12637 if (!match)
12638 break;
12639 /* Advance to just after the match. */
12640 if (regmatch.endp[0] > str)
12641 col = 0;
12642 else
12643 {
12644 /* Don't get stuck at the same match. */
12645#ifdef FEAT_MBYTE
12646 col = mb_ptr2len_check(regmatch.endp[0]);
12647#else
12648 col = 1;
12649#endif
12650 }
12651 str = regmatch.endp[0];
12652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658}
12659
12660#ifdef HAVE_STRFTIME
12661/*
12662 * "strftime({format}[, {time}])" function
12663 */
12664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *argvars;
12667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668{
12669 char_u result_buf[256];
12670 struct tm *curtime;
12671 time_t seconds;
12672 char_u *p;
12673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012677 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 seconds = time(NULL);
12679 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 curtime = localtime(&seconds);
12682 /* MSVC returns NULL for an invalid value of seconds. */
12683 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 else
12686 {
12687# ifdef FEAT_MBYTE
12688 vimconv_T conv;
12689 char_u *enc;
12690
12691 conv.vc_type = CONV_NONE;
12692 enc = enc_locale();
12693 convert_setup(&conv, p_enc, enc);
12694 if (conv.vc_type != CONV_NONE)
12695 p = string_convert(&conv, p, NULL);
12696# endif
12697 if (p != NULL)
12698 (void)strftime((char *)result_buf, sizeof(result_buf),
12699 (char *)p, curtime);
12700 else
12701 result_buf[0] = NUL;
12702
12703# ifdef FEAT_MBYTE
12704 if (conv.vc_type != CONV_NONE)
12705 vim_free(p);
12706 convert_setup(&conv, enc, p_enc);
12707 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 else
12710# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712
12713# ifdef FEAT_MBYTE
12714 /* Release conversion descriptors */
12715 convert_setup(&conv, NULL, NULL);
12716 vim_free(enc);
12717# endif
12718 }
12719}
12720#endif
12721
12722/*
12723 * "stridx()" function
12724 */
12725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *argvars;
12728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729{
12730 char_u buf[NUMBUFLEN];
12731 char_u *needle;
12732 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012738 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12739 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 if (argvars[2].v_type != VAR_UNKNOWN)
12742 {
12743 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012744 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012746 if (start_idx >= 0)
12747 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012748 }
12749
12750 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12751 if (pos != NULL)
12752 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753}
12754
12755/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012756 * "string()" function
12757 */
12758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012760 typval_T *argvars;
12761 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012762{
12763 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012764 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012767 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012768 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770}
12771
12772/*
12773 * "strlen()" function
12774 */
12775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012777 typval_T *argvars;
12778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 rettv->vval.v_number = (varnumber_T)(STRLEN(
12781 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782}
12783
12784/*
12785 * "strpart()" function
12786 */
12787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *argvars;
12790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791{
12792 char_u *p;
12793 int n;
12794 int len;
12795 int slen;
12796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 slen = (int)STRLEN(p);
12799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 else
12804 len = slen - n; /* default len: all bytes that are available. */
12805
12806 /*
12807 * Only return the overlap between the specified part and the actual
12808 * string.
12809 */
12810 if (n < 0)
12811 {
12812 len += n;
12813 n = 0;
12814 }
12815 else if (n > slen)
12816 n = slen;
12817 if (len < 0)
12818 len = 0;
12819 else if (n + len > slen)
12820 len = slen - n;
12821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 rettv->v_type = VAR_STRING;
12823 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827 * "strridx()" function
12828 */
12829 static void
12830f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012831 typval_T *argvars;
12832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012833{
12834 char_u buf[NUMBUFLEN];
12835 char_u *needle;
12836 char_u *haystack;
12837 char_u *rest;
12838 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012839 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012840
12841 needle = get_tv_string(&argvars[1]);
12842 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012843 haystack_len = STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000012844 if (argvars[2].v_type != VAR_UNKNOWN)
12845 {
12846 /* Third argument: upper limit for index */
12847 end_idx = get_tv_number(&argvars[2]);
12848 if (end_idx < 0)
12849 {
12850 /* can never find a match */
12851 rettv->vval.v_number = -1;
12852 return;
12853 }
12854 }
12855 else
12856 end_idx = haystack_len;
12857
Bram Moolenaar0d660222005-01-07 21:51:51 +000012858 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000012859 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000012861 lastmatch = haystack + end_idx;
12862 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012863 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012864 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865 for (rest = haystack; *rest != '\0'; ++rest)
12866 {
12867 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012868 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012869 break;
12870 lastmatch = rest;
12871 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012872 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873
12874 if (lastmatch == NULL)
12875 rettv->vval.v_number = -1;
12876 else
12877 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12878}
12879
12880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 * "strtrans()" function
12882 */
12883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *argvars;
12886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890}
12891
12892/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012893 * "submatch()" function
12894 */
12895 static void
12896f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *argvars;
12898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012899{
12900 rettv->v_type = VAR_STRING;
12901 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12902}
12903
12904/*
12905 * "substitute()" function
12906 */
12907 static void
12908f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012909 typval_T *argvars;
12910 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012911{
12912 char_u patbuf[NUMBUFLEN];
12913 char_u subbuf[NUMBUFLEN];
12914 char_u flagsbuf[NUMBUFLEN];
12915
12916 rettv->v_type = VAR_STRING;
12917 rettv->vval.v_string = do_string_sub(
12918 get_tv_string(&argvars[0]),
12919 get_tv_string_buf(&argvars[1], patbuf),
12920 get_tv_string_buf(&argvars[2], subbuf),
12921 get_tv_string_buf(&argvars[3], flagsbuf));
12922}
12923
12924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 * "synID(line, col, trans)" function
12926 */
12927/*ARGSUSED*/
12928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932{
12933 int id = 0;
12934#ifdef FEAT_SYN_HL
12935 long line;
12936 long col;
12937 int trans;
12938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 line = get_tv_lnum(argvars);
12940 col = get_tv_number(&argvars[1]) - 1;
12941 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
12943 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12944 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12945 id = syn_get_id(line, col, trans);
12946#endif
12947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949}
12950
12951/*
12952 * "synIDattr(id, what [, mode])" function
12953 */
12954/*ARGSUSED*/
12955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 typval_T *argvars;
12958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959{
12960 char_u *p = NULL;
12961#ifdef FEAT_SYN_HL
12962 int id;
12963 char_u *what;
12964 char_u *mode;
12965 char_u modebuf[NUMBUFLEN];
12966 int modec;
12967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 id = get_tv_number(&argvars[0]);
12969 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 modec = TOLOWER_ASC(mode[0]);
12974 if (modec != 't' && modec != 'c'
12975#ifdef FEAT_GUI
12976 && modec != 'g'
12977#endif
12978 )
12979 modec = 0; /* replace invalid with current */
12980 }
12981 else
12982 {
12983#ifdef FEAT_GUI
12984 if (gui.in_use)
12985 modec = 'g';
12986 else
12987#endif
12988 if (t_colors > 1)
12989 modec = 'c';
12990 else
12991 modec = 't';
12992 }
12993
12994
12995 switch (TOLOWER_ASC(what[0]))
12996 {
12997 case 'b':
12998 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12999 p = highlight_color(id, what, modec);
13000 else /* bold */
13001 p = highlight_has_attr(id, HL_BOLD, modec);
13002 break;
13003
13004 case 'f': /* fg[#] */
13005 p = highlight_color(id, what, modec);
13006 break;
13007
13008 case 'i':
13009 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13010 p = highlight_has_attr(id, HL_INVERSE, modec);
13011 else /* italic */
13012 p = highlight_has_attr(id, HL_ITALIC, modec);
13013 break;
13014
13015 case 'n': /* name */
13016 p = get_highlight_name(NULL, id - 1);
13017 break;
13018
13019 case 'r': /* reverse */
13020 p = highlight_has_attr(id, HL_INVERSE, modec);
13021 break;
13022
13023 case 's': /* standout */
13024 p = highlight_has_attr(id, HL_STANDOUT, modec);
13025 break;
13026
13027 case 'u': /* underline */
13028 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13029 break;
13030 }
13031
13032 if (p != NULL)
13033 p = vim_strsave(p);
13034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->v_type = VAR_STRING;
13036 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037}
13038
13039/*
13040 * "synIDtrans(id)" function
13041 */
13042/*ARGSUSED*/
13043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
13048 int id;
13049
13050#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013051 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052
13053 if (id > 0)
13054 id = syn_get_final_id(id);
13055 else
13056#endif
13057 id = 0;
13058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060}
13061
13062/*
13063 * "system()" function
13064 */
13065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013067 typval_T *argvars;
13068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013070 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013072 char_u *infile = NULL;
13073 char_u buf[NUMBUFLEN];
13074 int err = FALSE;
13075 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013077 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013078 {
13079 /*
13080 * Write the string to a temp file, to be used for input of the shell
13081 * command.
13082 */
13083 if ((infile = vim_tempname('i')) == NULL)
13084 {
13085 EMSG(_(e_notmp));
13086 return;
13087 }
13088
13089 fd = mch_fopen((char *)infile, WRITEBIN);
13090 if (fd == NULL)
13091 {
13092 EMSG2(_(e_notopen), infile);
13093 goto done;
13094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013096 if (fwrite(p, STRLEN(p), 1, fd) != 1)
13097 err = TRUE;
13098 if (fclose(fd) != 0)
13099 err = TRUE;
13100 if (err)
13101 {
13102 EMSG(_("E677: Error writing temp file"));
13103 goto done;
13104 }
13105 }
13106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013108
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109#ifdef USE_CR
13110 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013111 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 {
13113 char_u *s;
13114
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013115 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 {
13117 if (*s == CAR)
13118 *s = NL;
13119 }
13120 }
13121#else
13122# ifdef USE_CRNL
13123 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013124 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 {
13126 char_u *s, *d;
13127
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013128 d = res;
13129 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 {
13131 if (s[0] == CAR && s[1] == NL)
13132 ++s;
13133 *d++ = *s;
13134 }
13135 *d = NUL;
13136 }
13137# endif
13138#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000013139
13140done:
13141 if (infile != NULL)
13142 {
13143 mch_remove(infile);
13144 vim_free(infile);
13145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->v_type = VAR_STRING;
13147 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148}
13149
13150/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000013151 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000013152 */
13153 static void
13154f_taglist(argvars, rettv)
13155 typval_T *argvars;
13156 typval_T *rettv;
13157{
13158 char_u *tag_pattern;
13159 list_T *l;
13160
13161 tag_pattern = get_tv_string(&argvars[0]);
13162
13163 rettv->vval.v_number = FALSE;
13164
13165 l = list_alloc();
13166 if (l != NULL)
13167 {
13168 if (get_tags(l, tag_pattern) != FAIL)
13169 {
13170 rettv->vval.v_list = l;
13171 rettv->v_type = VAR_LIST;
13172 ++l->lv_refcount;
13173 }
13174 else
13175 list_free(l);
13176 }
13177}
13178
13179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 * "tempname()" function
13181 */
13182/*ARGSUSED*/
13183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *argvars;
13186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187{
13188 static int x = 'A';
13189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190 rettv->v_type = VAR_STRING;
13191 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192
13193 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13194 * names. Skip 'I' and 'O', they are used for shell redirection. */
13195 do
13196 {
13197 if (x == 'Z')
13198 x = '0';
13199 else if (x == '9')
13200 x = 'A';
13201 else
13202 {
13203#ifdef EBCDIC
13204 if (x == 'I')
13205 x = 'J';
13206 else if (x == 'R')
13207 x = 'S';
13208 else
13209#endif
13210 ++x;
13211 }
13212 } while (x == 'I' || x == 'O');
13213}
13214
13215/*
13216 * "tolower(string)" function
13217 */
13218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013220 typval_T *argvars;
13221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222{
13223 char_u *p;
13224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 p = vim_strsave(get_tv_string(&argvars[0]));
13226 rettv->v_type = VAR_STRING;
13227 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
13229 if (p != NULL)
13230 while (*p != NUL)
13231 {
13232#ifdef FEAT_MBYTE
13233 int l;
13234
13235 if (enc_utf8)
13236 {
13237 int c, lc;
13238
13239 c = utf_ptr2char(p);
13240 lc = utf_tolower(c);
13241 l = utf_ptr2len_check(p);
13242 /* TODO: reallocate string when byte count changes. */
13243 if (utf_char2len(lc) == l)
13244 utf_char2bytes(lc, p);
13245 p += l;
13246 }
13247 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13248 p += l; /* skip multi-byte character */
13249 else
13250#endif
13251 {
13252 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
13253 ++p;
13254 }
13255 }
13256}
13257
13258/*
13259 * "toupper(string)" function
13260 */
13261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 typval_T *argvars;
13264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265{
13266 char_u *p;
13267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268 p = vim_strsave(get_tv_string(&argvars[0]));
13269 rettv->v_type = VAR_STRING;
13270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271
13272 if (p != NULL)
13273 while (*p != NUL)
13274 {
13275#ifdef FEAT_MBYTE
13276 int l;
13277
13278 if (enc_utf8)
13279 {
13280 int c, uc;
13281
13282 c = utf_ptr2char(p);
13283 uc = utf_toupper(c);
13284 l = utf_ptr2len_check(p);
13285 /* TODO: reallocate string when byte count changes. */
13286 if (utf_char2len(uc) == l)
13287 utf_char2bytes(uc, p);
13288 p += l;
13289 }
13290 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
13291 p += l; /* skip multi-byte character */
13292 else
13293#endif
13294 {
13295 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
13296 p++;
13297 }
13298 }
13299}
13300
13301/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000013302 * "tr(string, fromstr, tostr)" function
13303 */
13304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013306 typval_T *argvars;
13307 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013308{
13309 char_u *instr;
13310 char_u *fromstr;
13311 char_u *tostr;
13312 char_u *p;
13313#ifdef FEAT_MBYTE
13314 int inlen;
13315 int fromlen;
13316 int tolen;
13317 int idx;
13318 char_u *cpstr;
13319 int cplen;
13320 int first = TRUE;
13321#endif
13322 char_u buf[NUMBUFLEN];
13323 char_u buf2[NUMBUFLEN];
13324 garray_T ga;
13325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 instr = get_tv_string(&argvars[0]);
13327 fromstr = get_tv_string_buf(&argvars[1], buf);
13328 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013329
13330 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 rettv->v_type = VAR_STRING;
13332 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013333 ga_init2(&ga, (int)sizeof(char), 80);
13334
13335#ifdef FEAT_MBYTE
13336 if (!has_mbyte)
13337#endif
13338 /* not multi-byte: fromstr and tostr must be the same length */
13339 if (STRLEN(fromstr) != STRLEN(tostr))
13340 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013341#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000013342error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013343#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000013344 EMSG2(_(e_invarg2), fromstr);
13345 ga_clear(&ga);
13346 return;
13347 }
13348
13349 /* fromstr and tostr have to contain the same number of chars */
13350 while (*instr != NUL)
13351 {
13352#ifdef FEAT_MBYTE
13353 if (has_mbyte)
13354 {
13355 inlen = mb_ptr2len_check(instr);
13356 cpstr = instr;
13357 cplen = inlen;
13358 idx = 0;
13359 for (p = fromstr; *p != NUL; p += fromlen)
13360 {
13361 fromlen = mb_ptr2len_check(p);
13362 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
13363 {
13364 for (p = tostr; *p != NUL; p += tolen)
13365 {
13366 tolen = mb_ptr2len_check(p);
13367 if (idx-- == 0)
13368 {
13369 cplen = tolen;
13370 cpstr = p;
13371 break;
13372 }
13373 }
13374 if (*p == NUL) /* tostr is shorter than fromstr */
13375 goto error;
13376 break;
13377 }
13378 ++idx;
13379 }
13380
13381 if (first && cpstr == instr)
13382 {
13383 /* Check that fromstr and tostr have the same number of
13384 * (multi-byte) characters. Done only once when a character
13385 * of instr doesn't appear in fromstr. */
13386 first = FALSE;
13387 for (p = tostr; *p != NUL; p += tolen)
13388 {
13389 tolen = mb_ptr2len_check(p);
13390 --idx;
13391 }
13392 if (idx != 0)
13393 goto error;
13394 }
13395
13396 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000013397 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000013398 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013399
13400 instr += inlen;
13401 }
13402 else
13403#endif
13404 {
13405 /* When not using multi-byte chars we can do it faster. */
13406 p = vim_strchr(fromstr, *instr);
13407 if (p != NULL)
13408 ga_append(&ga, tostr[p - fromstr]);
13409 else
13410 ga_append(&ga, *instr);
13411 ++instr;
13412 }
13413 }
13414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000013416}
13417
13418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 * "type(expr)" function
13420 */
13421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013426 int n;
13427
13428 switch (argvars[0].v_type)
13429 {
13430 case VAR_NUMBER: n = 0; break;
13431 case VAR_STRING: n = 1; break;
13432 case VAR_FUNC: n = 2; break;
13433 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013434 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013435 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
13436 }
13437 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438}
13439
13440/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013441 * "values(dict)" function
13442 */
13443 static void
13444f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 typval_T *argvars;
13446 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013447{
13448 dict_list(argvars, rettv, 1);
13449}
13450
13451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 * "virtcol(string)" function
13453 */
13454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459 colnr_T vcol = 0;
13460 pos_T *fp;
13461
13462 fp = var2fpos(&argvars[0], FALSE);
13463 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13464 {
13465 getvvcol(curwin, fp, NULL, NULL, &vcol);
13466 ++vcol;
13467 }
13468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470}
13471
13472/*
13473 * "visualmode()" function
13474 */
13475/*ARGSUSED*/
13476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480{
13481#ifdef FEAT_VISUAL
13482 char_u str[2];
13483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013484 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 str[0] = curbuf->b_visual_mode_eval;
13486 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488
13489 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013490 if ((argvars[0].v_type == VAR_NUMBER
13491 && argvars[0].vval.v_number != 0)
13492 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 curbuf->b_visual_mode_eval = NUL;
13495#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497#endif
13498}
13499
13500/*
13501 * "winbufnr(nr)" function
13502 */
13503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 typval_T *argvars;
13506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507{
13508 win_T *wp;
13509
13510 wp = find_win_by_nr(&argvars[0]);
13511 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
13517/*
13518 * "wincol()" function
13519 */
13520/*ARGSUSED*/
13521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *argvars;
13524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525{
13526 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528}
13529
13530/*
13531 * "winheight(nr)" function
13532 */
13533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *argvars;
13536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537{
13538 win_T *wp;
13539
13540 wp = find_win_by_nr(&argvars[0]);
13541 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545}
13546
13547/*
13548 * "winline()" function
13549 */
13550/*ARGSUSED*/
13551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 typval_T *argvars;
13554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
13556 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558}
13559
13560/*
13561 * "winnr()" function
13562 */
13563/* ARGSUSED */
13564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013566 typval_T *argvars;
13567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568{
13569 int nr = 1;
13570#ifdef FEAT_WINDOWS
13571 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013572 win_T *twin = curwin;
13573 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013575 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013578 if (STRCMP(arg, "$") == 0)
13579 twin = lastwin;
13580 else if (STRCMP(arg, "#") == 0)
13581 {
13582 twin = prevwin;
13583 if (prevwin == NULL)
13584 nr = 0;
13585 }
13586 else
13587 {
13588 EMSG2(_(e_invexpr2), arg);
13589 nr = 0;
13590 }
13591 }
13592
13593 if (nr > 0)
13594 for (wp = firstwin; wp != twin; wp = wp->w_next)
13595 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598}
13599
13600/*
13601 * "winrestcmd()" function
13602 */
13603/* ARGSUSED */
13604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013606 typval_T *argvars;
13607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608{
13609#ifdef FEAT_WINDOWS
13610 win_T *wp;
13611 int winnr = 1;
13612 garray_T ga;
13613 char_u buf[50];
13614
13615 ga_init2(&ga, (int)sizeof(char), 70);
13616 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13617 {
13618 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13619 ga_concat(&ga, buf);
13620# ifdef FEAT_VERTSPLIT
13621 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13622 ga_concat(&ga, buf);
13623# endif
13624 ++winnr;
13625 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013626 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633}
13634
13635/*
13636 * "winwidth(nr)" function
13637 */
13638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642{
13643 win_T *wp;
13644
13645 wp = find_win_by_nr(&argvars[0]);
13646 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 else
13649#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653#endif
13654}
13655
13656 static win_T *
13657find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659{
13660#ifdef FEAT_WINDOWS
13661 win_T *wp;
13662#endif
13663 int nr;
13664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
13667#ifdef FEAT_WINDOWS
13668 if (nr == 0)
13669 return curwin;
13670
13671 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13672 if (--nr <= 0)
13673 break;
13674 return wp;
13675#else
13676 if (nr == 0 || nr == 1)
13677 return curwin;
13678 return NULL;
13679#endif
13680}
13681
13682/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013683 * "writefile()" function
13684 */
13685 static void
13686f_writefile(argvars, rettv)
13687 typval_T *argvars;
13688 typval_T *rettv;
13689{
13690 int binary = FALSE;
13691 char_u *fname;
13692 FILE *fd;
13693 listitem_T *li;
13694 char_u *s;
13695 int ret = 0;
13696 int c;
13697
13698 if (argvars[0].v_type != VAR_LIST)
13699 {
13700 EMSG2(_(e_listarg), "writefile()");
13701 return;
13702 }
13703 if (argvars[0].vval.v_list == NULL)
13704 return;
13705
13706 if (argvars[2].v_type != VAR_UNKNOWN
13707 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13708 binary = TRUE;
13709
13710 /* Always open the file in binary mode, library functions have a mind of
13711 * their own about CR-LF conversion. */
13712 fname = get_tv_string(&argvars[1]);
13713 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13714 {
13715 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13716 ret = -1;
13717 }
13718 else
13719 {
13720 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13721 li = li->li_next)
13722 {
13723 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13724 {
13725 if (*s == '\n')
13726 c = putc(NUL, fd);
13727 else
13728 c = putc(*s, fd);
13729 if (c == EOF)
13730 {
13731 ret = -1;
13732 break;
13733 }
13734 }
13735 if (!binary || li->li_next != NULL)
13736 if (putc('\n', fd) == EOF)
13737 {
13738 ret = -1;
13739 break;
13740 }
13741 if (ret < 0)
13742 {
13743 EMSG(_(e_write));
13744 break;
13745 }
13746 }
13747 fclose(fd);
13748 }
13749
13750 rettv->vval.v_number = ret;
13751}
13752
13753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754 * Translate a String variable into a position.
13755 */
13756 static pos_T *
13757var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 int lnum; /* TRUE when $ is last line */
13760{
13761 char_u *name;
13762 static pos_T pos;
13763 pos_T *pp;
13764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 if (name[0] == '.') /* cursor */
13767 return &curwin->w_cursor;
13768 if (name[0] == '\'') /* mark */
13769 {
13770 pp = getmark(name[1], FALSE);
13771 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13772 return NULL;
13773 return pp;
13774 }
13775 if (name[0] == '$') /* last column or line */
13776 {
13777 if (lnum)
13778 {
13779 pos.lnum = curbuf->b_ml.ml_line_count;
13780 pos.col = 0;
13781 }
13782 else
13783 {
13784 pos.lnum = curwin->w_cursor.lnum;
13785 pos.col = (colnr_T)STRLEN(ml_get_curline());
13786 }
13787 return &pos;
13788 }
13789 return NULL;
13790}
13791
13792/*
13793 * Get the length of an environment variable name.
13794 * Advance "arg" to the first character after the name.
13795 * Return 0 for error.
13796 */
13797 static int
13798get_env_len(arg)
13799 char_u **arg;
13800{
13801 char_u *p;
13802 int len;
13803
13804 for (p = *arg; vim_isIDc(*p); ++p)
13805 ;
13806 if (p == *arg) /* no name found */
13807 return 0;
13808
13809 len = (int)(p - *arg);
13810 *arg = p;
13811 return len;
13812}
13813
13814/*
13815 * Get the length of the name of a function or internal variable.
13816 * "arg" is advanced to the first non-white character after the name.
13817 * Return 0 if something is wrong.
13818 */
13819 static int
13820get_id_len(arg)
13821 char_u **arg;
13822{
13823 char_u *p;
13824 int len;
13825
13826 /* Find the end of the name. */
13827 for (p = *arg; eval_isnamec(*p); ++p)
13828 ;
13829 if (p == *arg) /* no name found */
13830 return 0;
13831
13832 len = (int)(p - *arg);
13833 *arg = skipwhite(p);
13834
13835 return len;
13836}
13837
13838/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013839 * Get the length of the name of a variable or function.
13840 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013842 * Return -1 if curly braces expansion failed.
13843 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 * If the name contains 'magic' {}'s, expand them and return the
13845 * expanded name in an allocated string via 'alias' - caller must free.
13846 */
13847 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013848get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 char_u **arg;
13850 char_u **alias;
13851 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013852 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853{
13854 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 char_u *p;
13856 char_u *expr_start;
13857 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
13859 *alias = NULL; /* default to no alias */
13860
13861 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13862 && (*arg)[2] == (int)KE_SNR)
13863 {
13864 /* hard coded <SNR>, already translated */
13865 *arg += 3;
13866 return get_id_len(arg) + 3;
13867 }
13868 len = eval_fname_script(*arg);
13869 if (len > 0)
13870 {
13871 /* literal "<SID>", "s:" or "<SNR>" */
13872 *arg += len;
13873 }
13874
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013876 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 if (expr_start != NULL)
13880 {
13881 char_u *temp_string;
13882
13883 if (!evaluate)
13884 {
13885 len += (int)(p - *arg);
13886 *arg = skipwhite(p);
13887 return len;
13888 }
13889
13890 /*
13891 * Include any <SID> etc in the expanded string:
13892 * Thus the -len here.
13893 */
13894 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13895 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013896 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 *alias = temp_string;
13898 *arg = skipwhite(p);
13899 return (int)STRLEN(temp_string);
13900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901
13902 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013903 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 EMSG2(_(e_invexpr2), *arg);
13905
13906 return len;
13907}
13908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909/*
13910 * Find the end of a variable or function name, taking care of magic braces.
13911 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13912 * start and end of the first magic braces item.
13913 * Return a pointer to just after the name. Equal to "arg" if there is no
13914 * valid name.
13915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 char_u *arg;
13919 char_u **expr_start;
13920 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013921 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 int mb_nest = 0;
13924 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 char_u *p;
13926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929 *expr_start = NULL;
13930 *expr_end = NULL;
13931 }
13932
13933 for (p = arg; *p != NUL
13934 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013935 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013936 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 || mb_nest != 0
13938 || br_nest != 0); ++p)
13939 {
13940 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 if (*p == '[')
13943 ++br_nest;
13944 else if (*p == ']')
13945 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013947 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 if (*p == '{')
13950 {
13951 mb_nest++;
13952 if (expr_start != NULL && *expr_start == NULL)
13953 *expr_start = p;
13954 }
13955 else if (*p == '}')
13956 {
13957 mb_nest--;
13958 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13959 *expr_end = p;
13960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 }
13963
13964 return p;
13965}
13966
13967/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013968 * Expands out the 'magic' {}'s in a variable/function name.
13969 * Note that this can call itself recursively, to deal with
13970 * constructs like foo{bar}{baz}{bam}
13971 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13972 * "in_start" ^
13973 * "expr_start" ^
13974 * "expr_end" ^
13975 * "in_end" ^
13976 *
13977 * Returns a new allocated string, which the caller must free.
13978 * Returns NULL for failure.
13979 */
13980 static char_u *
13981make_expanded_name(in_start, expr_start, expr_end, in_end)
13982 char_u *in_start;
13983 char_u *expr_start;
13984 char_u *expr_end;
13985 char_u *in_end;
13986{
13987 char_u c1;
13988 char_u *retval = NULL;
13989 char_u *temp_result;
13990 char_u *nextcmd = NULL;
13991
13992 if (expr_end == NULL || in_end == NULL)
13993 return NULL;
13994 *expr_start = NUL;
13995 *expr_end = NUL;
13996 c1 = *in_end;
13997 *in_end = NUL;
13998
13999 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14000 if (temp_result != NULL && nextcmd == NULL)
14001 {
14002 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14003 + (in_end - expr_end) + 1));
14004 if (retval != NULL)
14005 {
14006 STRCPY(retval, in_start);
14007 STRCAT(retval, temp_result);
14008 STRCAT(retval, expr_end + 1);
14009 }
14010 }
14011 vim_free(temp_result);
14012
14013 *in_end = c1; /* put char back for error messages */
14014 *expr_start = '{';
14015 *expr_end = '}';
14016
14017 if (retval != NULL)
14018 {
14019 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
14020 if (expr_start != NULL)
14021 {
14022 /* Further expansion! */
14023 temp_result = make_expanded_name(retval, expr_start,
14024 expr_end, temp_result);
14025 vim_free(retval);
14026 retval = temp_result;
14027 }
14028 }
14029
14030 return retval;
14031}
14032
14033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014035 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 */
14037 static int
14038eval_isnamec(c)
14039 int c;
14040{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014041 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042}
14043
14044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 * Set number v: variable to "val".
14046 */
14047 void
14048set_vim_var_nr(idx, val)
14049 int idx;
14050 long val;
14051{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014052 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053}
14054
14055/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014056 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 */
14058 long
14059get_vim_var_nr(idx)
14060 int idx;
14061{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014062 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063}
14064
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014065#if defined(FEAT_AUTOCMD) || defined(PROTO)
14066/*
14067 * Get string v: variable value. Uses a static buffer, can only be used once.
14068 */
14069 char_u *
14070get_vim_var_str(idx)
14071 int idx;
14072{
14073 return get_tv_string(&vimvars[idx].vv_tv);
14074}
14075#endif
14076
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077/*
14078 * Set v:count, v:count1 and v:prevcount.
14079 */
14080 void
14081set_vcount(count, count1)
14082 long count;
14083 long count1;
14084{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014085 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
14086 vimvars[VV_COUNT].vv_nr = count;
14087 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088}
14089
14090/*
14091 * Set string v: variable to a copy of "val".
14092 */
14093 void
14094set_vim_var_string(idx, val, len)
14095 int idx;
14096 char_u *val;
14097 int len; /* length of "val" to use or -1 (whole string) */
14098{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014099 /* Need to do this (at least) once, since we can't initialize a union.
14100 * Will always be invoked when "v:progname" is set. */
14101 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
14102
Bram Moolenaare9a41262005-01-15 22:18:47 +000014103 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014105 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014107 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000014109 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110}
14111
14112/*
14113 * Set v:register if needed.
14114 */
14115 void
14116set_reg_var(c)
14117 int c;
14118{
14119 char_u regname;
14120
14121 if (c == 0 || c == ' ')
14122 regname = '"';
14123 else
14124 regname = c;
14125 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000014126 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 set_vim_var_string(VV_REG, &regname, 1);
14128}
14129
14130/*
14131 * Get or set v:exception. If "oldval" == NULL, return the current value.
14132 * Otherwise, restore the value to "oldval" and return NULL.
14133 * Must always be called in pairs to save and restore v:exception! Does not
14134 * take care of memory allocations.
14135 */
14136 char_u *
14137v_exception(oldval)
14138 char_u *oldval;
14139{
14140 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014141 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142
Bram Moolenaare9a41262005-01-15 22:18:47 +000014143 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 return NULL;
14145}
14146
14147/*
14148 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
14149 * Otherwise, restore the value to "oldval" and return NULL.
14150 * Must always be called in pairs to save and restore v:throwpoint! Does not
14151 * take care of memory allocations.
14152 */
14153 char_u *
14154v_throwpoint(oldval)
14155 char_u *oldval;
14156{
14157 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014158 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159
Bram Moolenaare9a41262005-01-15 22:18:47 +000014160 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 return NULL;
14162}
14163
14164#if defined(FEAT_AUTOCMD) || defined(PROTO)
14165/*
14166 * Set v:cmdarg.
14167 * If "eap" != NULL, use "eap" to generate the value and return the old value.
14168 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
14169 * Must always be called in pairs!
14170 */
14171 char_u *
14172set_cmdarg(eap, oldarg)
14173 exarg_T *eap;
14174 char_u *oldarg;
14175{
14176 char_u *oldval;
14177 char_u *newval;
14178 unsigned len;
14179
Bram Moolenaare9a41262005-01-15 22:18:47 +000014180 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014181 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014183 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000014184 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014185 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 }
14187
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014188 if (eap->force_bin == FORCE_BIN)
14189 len = 6;
14190 else if (eap->force_bin == FORCE_NOBIN)
14191 len = 8;
14192 else
14193 len = 0;
14194 if (eap->force_ff != 0)
14195 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
14196# ifdef FEAT_MBYTE
14197 if (eap->force_enc != 0)
14198 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
14199# endif
14200
14201 newval = alloc(len + 1);
14202 if (newval == NULL)
14203 return NULL;
14204
14205 if (eap->force_bin == FORCE_BIN)
14206 sprintf((char *)newval, " ++bin");
14207 else if (eap->force_bin == FORCE_NOBIN)
14208 sprintf((char *)newval, " ++nobin");
14209 else
14210 *newval = NUL;
14211 if (eap->force_ff != 0)
14212 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
14213 eap->cmd + eap->force_ff);
14214# ifdef FEAT_MBYTE
14215 if (eap->force_enc != 0)
14216 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
14217 eap->cmd + eap->force_enc);
14218# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000014219 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000014220 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221}
14222#endif
14223
14224/*
14225 * Get the value of internal variable "name".
14226 * Return OK or FAIL.
14227 */
14228 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014229get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 char_u *name;
14231 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000014232 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014233 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234{
14235 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *tv = NULL;
14237 typval_T atv;
14238 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240
14241 /* truncate the name, so that we can use strcmp() */
14242 cc = name[len];
14243 name[len] = NUL;
14244
14245 /*
14246 * Check for "b:changedtick".
14247 */
14248 if (STRCMP(name, "b:changedtick") == 0)
14249 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000014250 atv.v_type = VAR_NUMBER;
14251 atv.vval.v_number = curbuf->b_changedtick;
14252 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 }
14254
14255 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 * Check for user-defined variables.
14257 */
14258 else
14259 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014260 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014262 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 }
14264
Bram Moolenaare9a41262005-01-15 22:18:47 +000014265 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014267 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014268 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 ret = FAIL;
14270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014272 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273
14274 name[len] = cc;
14275
14276 return ret;
14277}
14278
14279/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014280 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
14281 * Also handle function call with Funcref variable: func(expr)
14282 * Can all be combined: dict.func(expr)[idx]['func'](expr)
14283 */
14284 static int
14285handle_subscript(arg, rettv, evaluate, verbose)
14286 char_u **arg;
14287 typval_T *rettv;
14288 int evaluate; /* do more than finding the end */
14289 int verbose; /* give error messages */
14290{
14291 int ret = OK;
14292 dict_T *selfdict = NULL;
14293 char_u *s;
14294 int len;
14295
14296 while (ret == OK
14297 && (**arg == '['
14298 || (**arg == '.' && rettv->v_type == VAR_DICT)
14299 || (**arg == '(' && rettv->v_type == VAR_FUNC))
14300 && !vim_iswhite(*(*arg - 1)))
14301 {
14302 if (**arg == '(')
14303 {
14304 s = rettv->vval.v_string;
14305
14306 /* Invoke the function. Recursive! */
14307 ret = get_func_tv(s, STRLEN(s), rettv, arg,
14308 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
14309 &len, evaluate, selfdict);
14310
14311 /* Stop the expression evaluation when immediately aborting on
14312 * error, or when an interrupt occurred or an exception was thrown
14313 * but not caught. */
14314 if (aborting())
14315 {
14316 if (ret == OK)
14317 clear_tv(rettv);
14318 ret = FAIL;
14319 }
14320 dict_unref(selfdict);
14321 selfdict = NULL;
14322 }
14323 else /* **arg == '[' || **arg == '.' */
14324 {
14325 dict_unref(selfdict);
14326 if (rettv->v_type == VAR_DICT)
14327 {
14328 selfdict = rettv->vval.v_dict;
14329 if (selfdict != NULL)
14330 ++selfdict->dv_refcount;
14331 }
14332 else
14333 selfdict = NULL;
14334 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
14335 {
14336 clear_tv(rettv);
14337 ret = FAIL;
14338 }
14339 }
14340 }
14341 dict_unref(selfdict);
14342 return ret;
14343}
14344
14345/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014346 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
14347 * value).
14348 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351{
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014353}
14354
14355/*
14356 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 * The string "s" must have been allocated, it is consumed.
14358 * Return NULL for out of memory, the variable otherwise.
14359 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 char_u *s;
14363{
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv = alloc_tv();
14367 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 rettv->v_type = VAR_STRING;
14370 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 }
14372 else
14373 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375}
14376
14377/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014378 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 */
14380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
14384 if (varp != NULL)
14385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014386 switch (varp->v_type)
14387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014388 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014389 func_unref(varp->vval.v_string);
14390 /*FALLTHROUGH*/
14391 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014392 vim_free(varp->vval.v_string);
14393 break;
14394 case VAR_LIST:
14395 list_unref(varp->vval.v_list);
14396 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014397 case VAR_DICT:
14398 dict_unref(varp->vval.v_dict);
14399 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014400 case VAR_NUMBER:
14401 case VAR_UNKNOWN:
14402 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014403 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014404 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014405 break;
14406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 vim_free(varp);
14408 }
14409}
14410
14411/*
14412 * Free the memory for a variable value and set the value to NULL or 0.
14413 */
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417{
14418 if (varp != NULL)
14419 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014420 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014422 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014423 func_unref(varp->vval.v_string);
14424 /*FALLTHROUGH*/
14425 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 vim_free(varp->vval.v_string);
14427 varp->vval.v_string = NULL;
14428 break;
14429 case VAR_LIST:
14430 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014431 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014433 case VAR_DICT:
14434 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014435 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014436 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014438 varp->vval.v_number = 0;
14439 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440 case VAR_UNKNOWN:
14441 break;
14442 default:
14443 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014445 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 }
14447}
14448
14449/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450 * Set the value of a variable to NULL without freeing items.
14451 */
14452 static void
14453init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014454 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455{
14456 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458}
14459
14460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 * Get the number value of a variable.
14462 * If it is a String variable, uses vim_str2nr().
14463 */
14464 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014468 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014470 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 case VAR_NUMBER:
14473 n = (long)(varp->vval.v_number);
14474 break;
14475 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014477 break;
14478 case VAR_STRING:
14479 if (varp->vval.v_string != NULL)
14480 vim_str2nr(varp->vval.v_string, NULL, NULL,
14481 TRUE, TRUE, &n, NULL);
14482 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014483 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014484 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014485 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014486 case VAR_DICT:
14487 EMSG(_("E728: Using a Dictionary as a number"));
14488 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014489 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014490 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014491 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014493 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494}
14495
14496/*
14497 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14498 */
14499 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502{
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 linenr_T lnum;
14505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 if (lnum == 0) /* no valid number, try using line() */
14508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv.v_type = VAR_NUMBER;
14510 f_line(argvars, &rettv);
14511 lnum = rettv.vval.v_number;
14512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 }
14514 return lnum;
14515}
14516
14517/*
14518 * Get the string value of a variable.
14519 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014520 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14521 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 * If the String variable has never been set, return an empty string.
14523 * Never returns NULL;
14524 */
14525 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528{
14529 static char_u mybuf[NUMBUFLEN];
14530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532}
14533
14534 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014536 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014537 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014539 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014541 case VAR_NUMBER:
14542 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14543 return buf;
14544 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014545 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014546 break;
14547 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014548 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014549 break;
14550 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014551 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014552 break;
14553 case VAR_STRING:
14554 if (varp->vval.v_string != NULL)
14555 return varp->vval.v_string;
14556 break;
14557 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014561 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562}
14563
14564/*
14565 * Find variable "name" in the list of variables.
14566 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014567 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014568 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014569 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014572find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578
Bram Moolenaara7043832005-01-21 11:56:39 +000014579 ht = find_var_ht(name, &varname);
14580 if (htp != NULL)
14581 *htp = ht;
14582 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014584 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585}
14586
14587/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014589 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014592find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014594 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014595 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000014596{
Bram Moolenaar33570922005-01-25 22:26:29 +000014597 hashitem_T *hi;
14598
14599 if (*varname == NUL)
14600 {
14601 /* Must be something like "s:", otherwise "ht" would be NULL. */
14602 switch (varname[-2])
14603 {
14604 case 's': return &SCRIPT_SV(current_SID).sv_var;
14605 case 'g': return &globvars_var;
14606 case 'v': return &vimvars_var;
14607 case 'b': return &curbuf->b_bufvar;
14608 case 'w': return &curwin->w_winvar;
14609 case 'l': return &current_funccal->l_vars_var;
14610 case 'a': return &current_funccal->l_avars_var;
14611 }
14612 return NULL;
14613 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014614
14615 hi = hash_find(ht, varname);
14616 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014617 {
14618 /* For global variables we may try auto-loading the script. If it
14619 * worked find the variable again. */
14620 if (ht == &globvarht && !writing
14621 && script_autoload(varname) && !aborting())
14622 hi = hash_find(ht, varname);
14623 if (HASHITEM_EMPTY(hi))
14624 return NULL;
14625 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014627}
14628
14629/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014630 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014631 * Set "varname" to the start of name without ':'.
14632 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014634find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 char_u *name;
14636 char_u **varname;
14637{
14638 if (name[1] != ':')
14639 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014640 /* The name must not start with a colon. */
14641 if (name[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 return NULL;
14643 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014644
14645 /* "version" is "v:version" in all scopes */
14646 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14647 return &compat_hashtab;
14648
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 return &globvarht; /* global variable */
14651 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 }
14653 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014654 if (*name == 'g') /* global variable */
14655 return &globvarht;
14656 /* There must be no ':' in the rest of the name, unless g: is used */
14657 if (vim_strchr(name + 2, ':') != NULL)
14658 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014662 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 if (*name == 'v') /* v: variable */
14664 return &vimvarht;
14665 if (*name == 'a' && current_funccal != NULL) /* function argument */
14666 return &current_funccal->l_avars.dv_hashtab;
14667 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14668 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 if (*name == 's' /* script variable */
14670 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14671 return &SCRIPT_VARS(current_SID);
14672 return NULL;
14673}
14674
14675/*
14676 * Get the string value of a (global/local) variable.
14677 * Returns NULL when it doesn't exist.
14678 */
14679 char_u *
14680get_var_value(name)
14681 char_u *name;
14682{
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684
Bram Moolenaara7043832005-01-21 11:56:39 +000014685 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 if (v == NULL)
14687 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689}
14690
14691/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014692 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 * sourcing this script and when executing functions defined in the script.
14694 */
14695 void
14696new_script_vars(id)
14697 scid_T id;
14698{
Bram Moolenaara7043832005-01-21 11:56:39 +000014699 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014700 hashtab_T *ht;
14701 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014702
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14704 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014705 /* Re-allocating ga_data means that an ht_array pointing to
14706 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014708 for (i = 1; i <= ga_scripts.ga_len; ++i)
14709 {
14710 ht = &SCRIPT_VARS(i);
14711 if (ht->ht_mask == HT_INIT_SIZE - 1)
14712 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014713 sv = &SCRIPT_SV(i);
14714 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014715 }
14716
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717 while (ga_scripts.ga_len < id)
14718 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014719 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14720 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 }
14723 }
14724}
14725
14726/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14728 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 */
14730 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014731init_var_dict(dict, dict_var)
14732 dict_T *dict;
14733 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734{
Bram Moolenaar33570922005-01-25 22:26:29 +000014735 hash_init(&dict->dv_hashtab);
14736 dict->dv_refcount = 99999;
14737 dict_var->di_tv.vval.v_dict = dict;
14738 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014739 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014740 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14741 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742}
14743
14744/*
14745 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 * Frees all allocated variables and the value they contain.
14747 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 */
14749 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014750vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014751 hashtab_T *ht;
14752{
14753 vars_clear_ext(ht, TRUE);
14754}
14755
14756/*
14757 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14758 */
14759 static void
14760vars_clear_ext(ht, free_val)
14761 hashtab_T *ht;
14762 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763{
Bram Moolenaara7043832005-01-21 11:56:39 +000014764 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014765 hashitem_T *hi;
14766 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767
Bram Moolenaar33570922005-01-25 22:26:29 +000014768 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014769 todo = ht->ht_used;
14770 for (hi = ht->ht_array; todo > 0; ++hi)
14771 {
14772 if (!HASHITEM_EMPTY(hi))
14773 {
14774 --todo;
14775
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014777 * ht_array might change then. hash_clear() takes care of it
14778 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014779 v = HI2DI(hi);
14780 if (free_val)
14781 clear_tv(&v->di_tv);
14782 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14783 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014784 }
14785 }
14786 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787}
14788
Bram Moolenaara7043832005-01-21 11:56:39 +000014789/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 * Delete a variable from hashtab "ht" at item "hi".
14791 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014794delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 hashtab_T *ht;
14796 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797{
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014799
14800 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 clear_tv(&di->di_tv);
14802 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803}
14804
14805/*
14806 * List the value of one internal variable.
14807 */
14808 static void
14809list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 char_u *prefix;
14812{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014813 char_u *tofree;
14814 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014816
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 s = echo_string(&v->di_tv, &tofree, numbuf);
14818 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014819 s == NULL ? (char_u *)"" : s);
14820 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821}
14822
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 static void
14824list_one_var_a(prefix, name, type, string)
14825 char_u *prefix;
14826 char_u *name;
14827 int type;
14828 char_u *string;
14829{
14830 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14831 if (name != NULL) /* "a:" vars don't have a name stored */
14832 msg_puts(name);
14833 msg_putchar(' ');
14834 msg_advance(22);
14835 if (type == VAR_NUMBER)
14836 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014837 else if (type == VAR_FUNC)
14838 msg_putchar('*');
14839 else if (type == VAR_LIST)
14840 {
14841 msg_putchar('[');
14842 if (*string == '[')
14843 ++string;
14844 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014845 else if (type == VAR_DICT)
14846 {
14847 msg_putchar('{');
14848 if (*string == '{')
14849 ++string;
14850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 else
14852 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014855
14856 if (type == VAR_FUNC)
14857 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858}
14859
14860/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 * If the variable already exists, the value is updated.
14863 * Otherwise the variable is created.
14864 */
14865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014868 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870{
Bram Moolenaar33570922005-01-25 22:26:29 +000014871 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014875 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014876 {
14877 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14878 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14879 ? name[2] : name[0]))
14880 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014881 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014882 return;
14883 }
14884 if (function_exists(name))
14885 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014886 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14887 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014888 return;
14889 }
14890 }
14891
Bram Moolenaara7043832005-01-21 11:56:39 +000014892 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014894 {
14895 EMSG2(_("E461: Illegal variable name: %s"), name);
14896 return;
14897 }
14898
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014899 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014900 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014902 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014903 if (var_check_ro(v->di_flags, name)
14904 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014905 return;
14906 if (v->di_tv.v_type != tv->v_type
14907 && !((v->di_tv.v_type == VAR_STRING
14908 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 && (tv->v_type == VAR_STRING
14910 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014911 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014912 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014913 return;
14914 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014915
14916 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014917 * Handle setting internal v: variables separately: we don't change
14918 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014919 */
14920 if (ht == &vimvarht)
14921 {
14922 if (v->di_tv.v_type == VAR_STRING)
14923 {
14924 vim_free(v->di_tv.vval.v_string);
14925 if (copy || tv->v_type != VAR_STRING)
14926 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14927 else
14928 {
14929 /* Take over the string to avoid an extra alloc/free. */
14930 v->di_tv.vval.v_string = tv->vval.v_string;
14931 tv->vval.v_string = NULL;
14932 }
14933 }
14934 else if (v->di_tv.v_type != VAR_NUMBER)
14935 EMSG2(_(e_intern2), "set_var()");
14936 else
14937 v->di_tv.vval.v_number = get_tv_number(tv);
14938 return;
14939 }
14940
14941 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 }
14943 else /* add a new variable */
14944 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014945 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14946 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014947 if (v == NULL)
14948 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014950 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014952 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 return;
14954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014955 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014960 else
14961 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014962 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014963 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966}
14967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014968/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014969 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14970 * Also give an error message.
14971 */
14972 static int
14973var_check_ro(flags, name)
14974 int flags;
14975 char_u *name;
14976{
14977 if (flags & DI_FLAGS_RO)
14978 {
14979 EMSG2(_(e_readonlyvar), name);
14980 return TRUE;
14981 }
14982 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14983 {
14984 EMSG2(_(e_readonlysbx), name);
14985 return TRUE;
14986 }
14987 return FALSE;
14988}
14989
14990/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014991 * Return TRUE if typeval "tv" is set to be locked (immutable).
14992 * Also give an error message, using "name".
14993 */
14994 static int
14995tv_check_lock(lock, name)
14996 int lock;
14997 char_u *name;
14998{
14999 if (lock & VAR_LOCKED)
15000 {
15001 EMSG2(_("E741: Value is locked: %s"),
15002 name == NULL ? (char_u *)_("Unknown") : name);
15003 return TRUE;
15004 }
15005 if (lock & VAR_FIXED)
15006 {
15007 EMSG2(_("E742: Cannot change value of %s"),
15008 name == NULL ? (char_u *)_("Unknown") : name);
15009 return TRUE;
15010 }
15011 return FALSE;
15012}
15013
15014/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015016 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015017 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015020copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000015021 typval_T *from;
15022 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015024 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015025 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015026 switch (from->v_type)
15027 {
15028 case VAR_NUMBER:
15029 to->vval.v_number = from->vval.v_number;
15030 break;
15031 case VAR_STRING:
15032 case VAR_FUNC:
15033 if (from->vval.v_string == NULL)
15034 to->vval.v_string = NULL;
15035 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015036 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015037 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015038 if (from->v_type == VAR_FUNC)
15039 func_ref(to->vval.v_string);
15040 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015041 break;
15042 case VAR_LIST:
15043 if (from->vval.v_list == NULL)
15044 to->vval.v_list = NULL;
15045 else
15046 {
15047 to->vval.v_list = from->vval.v_list;
15048 ++to->vval.v_list->lv_refcount;
15049 }
15050 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015051 case VAR_DICT:
15052 if (from->vval.v_dict == NULL)
15053 to->vval.v_dict = NULL;
15054 else
15055 {
15056 to->vval.v_dict = from->vval.v_dict;
15057 ++to->vval.v_dict->dv_refcount;
15058 }
15059 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015061 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015062 break;
15063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064}
15065
15066/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000015067 * Make a copy of an item.
15068 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015069 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
15070 * reference to an already copied list/dict can be used.
15071 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015072 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015073 static int
15074item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000015075 typval_T *from;
15076 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015077 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015078 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015079{
15080 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015081 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015082
Bram Moolenaar33570922005-01-25 22:26:29 +000015083 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015084 {
15085 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015086 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015087 }
15088 ++recurse;
15089
15090 switch (from->v_type)
15091 {
15092 case VAR_NUMBER:
15093 case VAR_STRING:
15094 case VAR_FUNC:
15095 copy_tv(from, to);
15096 break;
15097 case VAR_LIST:
15098 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015099 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015100 if (from->vval.v_list == NULL)
15101 to->vval.v_list = NULL;
15102 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
15103 {
15104 /* use the copy made earlier */
15105 to->vval.v_list = from->vval.v_list->lv_copylist;
15106 ++to->vval.v_list->lv_refcount;
15107 }
15108 else
15109 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
15110 if (to->vval.v_list == NULL)
15111 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015112 break;
15113 case VAR_DICT:
15114 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015115 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015116 if (from->vval.v_dict == NULL)
15117 to->vval.v_dict = NULL;
15118 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
15119 {
15120 /* use the copy made earlier */
15121 to->vval.v_dict = from->vval.v_dict->dv_copydict;
15122 ++to->vval.v_dict->dv_refcount;
15123 }
15124 else
15125 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
15126 if (to->vval.v_dict == NULL)
15127 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015128 break;
15129 default:
15130 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015131 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015132 }
15133 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015134 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015135}
15136
15137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 * ":echo expr1 ..." print each argument separated with a space, add a
15139 * newline at the end.
15140 * ":echon expr1 ..." print each argument plain.
15141 */
15142 void
15143ex_echo(eap)
15144 exarg_T *eap;
15145{
15146 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015148 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 char_u *p;
15150 int needclr = TRUE;
15151 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015152 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153
15154 if (eap->skip)
15155 ++emsg_skip;
15156 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
15157 {
15158 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015159 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 {
15161 /*
15162 * Report the invalid expression unless the expression evaluation
15163 * has been cancelled due to an aborting error, an interrupt, or an
15164 * exception.
15165 */
15166 if (!aborting())
15167 EMSG2(_(e_invexpr2), p);
15168 break;
15169 }
15170 if (!eap->skip)
15171 {
15172 if (atstart)
15173 {
15174 atstart = FALSE;
15175 /* Call msg_start() after eval1(), evaluating the expression
15176 * may cause a message to appear. */
15177 if (eap->cmdidx == CMD_echo)
15178 msg_start();
15179 }
15180 else if (eap->cmdidx == CMD_echo)
15181 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015182 p = echo_string(&rettv, &tofree, numbuf);
15183 if (p != NULL)
15184 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015186 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015188 if (*p != TAB && needclr)
15189 {
15190 /* remove any text still there from the command */
15191 msg_clr_eos();
15192 needclr = FALSE;
15193 }
15194 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 }
15196 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015197 {
15198#ifdef FEAT_MBYTE
15199 if (has_mbyte)
15200 {
15201 int i = (*mb_ptr2len_check)(p);
15202
15203 (void)msg_outtrans_len_attr(p, i, echo_attr);
15204 p += i - 1;
15205 }
15206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015208 (void)msg_outtrans_len_attr(p, 1, echo_attr);
15209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015211 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015213 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 arg = skipwhite(arg);
15215 }
15216 eap->nextcmd = check_nextcmd(arg);
15217
15218 if (eap->skip)
15219 --emsg_skip;
15220 else
15221 {
15222 /* remove text that may still be there from the command */
15223 if (needclr)
15224 msg_clr_eos();
15225 if (eap->cmdidx == CMD_echo)
15226 msg_end();
15227 }
15228}
15229
15230/*
15231 * ":echohl {name}".
15232 */
15233 void
15234ex_echohl(eap)
15235 exarg_T *eap;
15236{
15237 int id;
15238
15239 id = syn_name2id(eap->arg);
15240 if (id == 0)
15241 echo_attr = 0;
15242 else
15243 echo_attr = syn_id2attr(id);
15244}
15245
15246/*
15247 * ":execute expr1 ..." execute the result of an expression.
15248 * ":echomsg expr1 ..." Print a message
15249 * ":echoerr expr1 ..." Print an error
15250 * Each gets spaces around each argument and a newline at the end for
15251 * echo commands
15252 */
15253 void
15254ex_execute(eap)
15255 exarg_T *eap;
15256{
15257 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 int ret = OK;
15260 char_u *p;
15261 garray_T ga;
15262 int len;
15263 int save_did_emsg;
15264
15265 ga_init2(&ga, 1, 80);
15266
15267 if (eap->skip)
15268 ++emsg_skip;
15269 while (*arg != NUL && *arg != '|' && *arg != '\n')
15270 {
15271 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 {
15274 /*
15275 * Report the invalid expression unless the expression evaluation
15276 * has been cancelled due to an aborting error, an interrupt, or an
15277 * exception.
15278 */
15279 if (!aborting())
15280 EMSG2(_(e_invexpr2), p);
15281 ret = FAIL;
15282 break;
15283 }
15284
15285 if (!eap->skip)
15286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 len = (int)STRLEN(p);
15289 if (ga_grow(&ga, len + 2) == FAIL)
15290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 ret = FAIL;
15293 break;
15294 }
15295 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 ga.ga_len += len;
15299 }
15300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 arg = skipwhite(arg);
15303 }
15304
15305 if (ret != FAIL && ga.ga_data != NULL)
15306 {
15307 if (eap->cmdidx == CMD_echomsg)
15308 MSG_ATTR(ga.ga_data, echo_attr);
15309 else if (eap->cmdidx == CMD_echoerr)
15310 {
15311 /* We don't want to abort following commands, restore did_emsg. */
15312 save_did_emsg = did_emsg;
15313 EMSG((char_u *)ga.ga_data);
15314 if (!force_abort)
15315 did_emsg = save_did_emsg;
15316 }
15317 else if (eap->cmdidx == CMD_execute)
15318 do_cmdline((char_u *)ga.ga_data,
15319 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
15320 }
15321
15322 ga_clear(&ga);
15323
15324 if (eap->skip)
15325 --emsg_skip;
15326
15327 eap->nextcmd = check_nextcmd(arg);
15328}
15329
15330/*
15331 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
15332 * "arg" points to the "&" or '+' when called, to "option" when returning.
15333 * Returns NULL when no option name found. Otherwise pointer to the char
15334 * after the option name.
15335 */
15336 static char_u *
15337find_option_end(arg, opt_flags)
15338 char_u **arg;
15339 int *opt_flags;
15340{
15341 char_u *p = *arg;
15342
15343 ++p;
15344 if (*p == 'g' && p[1] == ':')
15345 {
15346 *opt_flags = OPT_GLOBAL;
15347 p += 2;
15348 }
15349 else if (*p == 'l' && p[1] == ':')
15350 {
15351 *opt_flags = OPT_LOCAL;
15352 p += 2;
15353 }
15354 else
15355 *opt_flags = 0;
15356
15357 if (!ASCII_ISALPHA(*p))
15358 return NULL;
15359 *arg = p;
15360
15361 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
15362 p += 4; /* termcap option */
15363 else
15364 while (ASCII_ISALPHA(*p))
15365 ++p;
15366 return p;
15367}
15368
15369/*
15370 * ":function"
15371 */
15372 void
15373ex_function(eap)
15374 exarg_T *eap;
15375{
15376 char_u *theline;
15377 int j;
15378 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 char_u *name = NULL;
15381 char_u *p;
15382 char_u *arg;
15383 garray_T newargs;
15384 garray_T newlines;
15385 int varargs = FALSE;
15386 int mustend = FALSE;
15387 int flags = 0;
15388 ufunc_T *fp;
15389 int indent;
15390 int nesting;
15391 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015392 dictitem_T *v;
15393 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015394 static int func_nr = 0; /* number for nameless function */
15395 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015396 hashtab_T *ht;
15397 int todo;
15398 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399
15400 /*
15401 * ":function" without argument: list functions.
15402 */
15403 if (ends_excmd(*eap->arg))
15404 {
15405 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015406 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000015407 todo = func_hashtab.ht_used;
15408 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015409 {
15410 if (!HASHITEM_EMPTY(hi))
15411 {
15412 --todo;
15413 fp = HI2UF(hi);
15414 if (!isdigit(*fp->uf_name))
15415 list_func_head(fp, FALSE);
15416 }
15417 }
15418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 eap->nextcmd = check_nextcmd(eap->arg);
15420 return;
15421 }
15422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015423 /*
15424 * Get the function name. There are these situations:
15425 * func normal function name
15426 * "name" == func, "fudi.fd_dict" == NULL
15427 * dict.func new dictionary entry
15428 * "name" == NULL, "fudi.fd_dict" set,
15429 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
15430 * dict.func existing dict entry with a Funcref
15431 * "name" == fname, "fudi.fd_dict" set,
15432 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15433 * dict.func existing dict entry that's not a Funcref
15434 * "name" == NULL, "fudi.fd_dict" set,
15435 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
15436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015438 name = trans_function_name(&p, eap->skip, 0, &fudi);
15439 paren = (vim_strchr(p, '(') != NULL);
15440 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 {
15442 /*
15443 * Return on an invalid expression in braces, unless the expression
15444 * evaluation has been cancelled due to an aborting error, an
15445 * interrupt, or an exception.
15446 */
15447 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015448 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015449 if (!eap->skip && fudi.fd_newkey != NULL)
15450 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015451 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 else
15455 eap->skip = TRUE;
15456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 /* An error in a function call during evaluation of an expression in magic
15458 * braces should not cause the function not to be defined. */
15459 saved_did_emsg = did_emsg;
15460 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461
15462 /*
15463 * ":function func" with only function name: list function.
15464 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015465 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 {
15467 if (!ends_excmd(*skipwhite(p)))
15468 {
15469 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015470 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 }
15472 eap->nextcmd = check_nextcmd(p);
15473 if (eap->nextcmd != NULL)
15474 *p = NUL;
15475 if (!eap->skip && !got_int)
15476 {
15477 fp = find_func(name);
15478 if (fp != NULL)
15479 {
15480 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015481 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 {
15483 msg_putchar('\n');
15484 msg_outnum((long)(j + 1));
15485 if (j < 9)
15486 msg_putchar(' ');
15487 if (j < 99)
15488 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015489 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 out_flush(); /* show a line at a time */
15491 ui_breakcheck();
15492 }
15493 if (!got_int)
15494 {
15495 msg_putchar('\n');
15496 msg_puts((char_u *)" endfunction");
15497 }
15498 }
15499 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015500 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015502 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 }
15504
15505 /*
15506 * ":function name(arg1, arg2)" Define function.
15507 */
15508 p = skipwhite(p);
15509 if (*p != '(')
15510 {
15511 if (!eap->skip)
15512 {
15513 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015514 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 }
15516 /* attempt to continue by skipping some text */
15517 if (vim_strchr(p, '(') != NULL)
15518 p = vim_strchr(p, '(');
15519 }
15520 p = skipwhite(p + 1);
15521
15522 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15523 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15524
15525 /*
15526 * Isolate the arguments: "arg1, arg2, ...)"
15527 */
15528 while (*p != ')')
15529 {
15530 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15531 {
15532 varargs = TRUE;
15533 p += 3;
15534 mustend = TRUE;
15535 }
15536 else
15537 {
15538 arg = p;
15539 while (ASCII_ISALNUM(*p) || *p == '_')
15540 ++p;
15541 if (arg == p || isdigit(*arg)
15542 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15543 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15544 {
15545 if (!eap->skip)
15546 EMSG2(_("E125: Illegal argument: %s"), arg);
15547 break;
15548 }
15549 if (ga_grow(&newargs, 1) == FAIL)
15550 goto erret;
15551 c = *p;
15552 *p = NUL;
15553 arg = vim_strsave(arg);
15554 if (arg == NULL)
15555 goto erret;
15556 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15557 *p = c;
15558 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 if (*p == ',')
15560 ++p;
15561 else
15562 mustend = TRUE;
15563 }
15564 p = skipwhite(p);
15565 if (mustend && *p != ')')
15566 {
15567 if (!eap->skip)
15568 EMSG2(_(e_invarg2), eap->arg);
15569 break;
15570 }
15571 }
15572 ++p; /* skip the ')' */
15573
Bram Moolenaare9a41262005-01-15 22:18:47 +000015574 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 for (;;)
15576 {
15577 p = skipwhite(p);
15578 if (STRNCMP(p, "range", 5) == 0)
15579 {
15580 flags |= FC_RANGE;
15581 p += 5;
15582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015583 else if (STRNCMP(p, "dict", 4) == 0)
15584 {
15585 flags |= FC_DICT;
15586 p += 4;
15587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 else if (STRNCMP(p, "abort", 5) == 0)
15589 {
15590 flags |= FC_ABORT;
15591 p += 5;
15592 }
15593 else
15594 break;
15595 }
15596
15597 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15598 EMSG(_(e_trailing));
15599
15600 /*
15601 * Read the body of the function, until ":endfunction" is found.
15602 */
15603 if (KeyTyped)
15604 {
15605 /* Check if the function already exists, don't let the user type the
15606 * whole function before telling him it doesn't work! For a script we
15607 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015608 if (!eap->skip && !eap->forceit)
15609 {
15610 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15611 EMSG(_(e_funcdict));
15612 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015613 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615
15616 msg_putchar('\n'); /* don't overwrite the function name */
15617 cmdline_row = msg_row;
15618 }
15619
15620 indent = 2;
15621 nesting = 0;
15622 for (;;)
15623 {
15624 msg_scroll = TRUE;
15625 need_wait_return = FALSE;
15626 if (eap->getline == NULL)
15627 theline = getcmdline(':', 0L, indent);
15628 else
15629 theline = eap->getline(':', eap->cookie, indent);
15630 if (KeyTyped)
15631 lines_left = Rows - 1;
15632 if (theline == NULL)
15633 {
15634 EMSG(_("E126: Missing :endfunction"));
15635 goto erret;
15636 }
15637
15638 if (skip_until != NULL)
15639 {
15640 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15641 * don't check for ":endfunc". */
15642 if (STRCMP(theline, skip_until) == 0)
15643 {
15644 vim_free(skip_until);
15645 skip_until = NULL;
15646 }
15647 }
15648 else
15649 {
15650 /* skip ':' and blanks*/
15651 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15652 ;
15653
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015654 /* Check for "endfunction". */
15655 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 {
15657 vim_free(theline);
15658 break;
15659 }
15660
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015661 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 * at "end". */
15663 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15664 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015665 else if (STRNCMP(p, "if", 2) == 0
15666 || STRNCMP(p, "wh", 2) == 0
15667 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 || STRNCMP(p, "try", 3) == 0)
15669 indent += 2;
15670
15671 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015672 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015674 if (*p == '!')
15675 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 p += eval_fname_script(p);
15677 if (ASCII_ISALPHA(*p))
15678 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015679 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 if (*skipwhite(p) == '(')
15681 {
15682 ++nesting;
15683 indent += 2;
15684 }
15685 }
15686 }
15687
15688 /* Check for ":append" or ":insert". */
15689 p = skip_range(p, NULL);
15690 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15691 || (p[0] == 'i'
15692 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15693 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15694 skip_until = vim_strsave((char_u *)".");
15695
15696 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15697 arg = skipwhite(skiptowhite(p));
15698 if (arg[0] == '<' && arg[1] =='<'
15699 && ((p[0] == 'p' && p[1] == 'y'
15700 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15701 || (p[0] == 'p' && p[1] == 'e'
15702 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15703 || (p[0] == 't' && p[1] == 'c'
15704 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15705 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15706 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015707 || (p[0] == 'm' && p[1] == 'z'
15708 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 ))
15710 {
15711 /* ":python <<" continues until a dot, like ":append" */
15712 p = skipwhite(arg + 2);
15713 if (*p == NUL)
15714 skip_until = vim_strsave((char_u *)".");
15715 else
15716 skip_until = vim_strsave(p);
15717 }
15718 }
15719
15720 /* Add the line to the function. */
15721 if (ga_grow(&newlines, 1) == FAIL)
15722 goto erret;
15723 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15724 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 }
15726
15727 /* Don't define the function when skipping commands or when an error was
15728 * detected. */
15729 if (eap->skip || did_emsg)
15730 goto erret;
15731
15732 /*
15733 * If there are no errors, add the function
15734 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015735 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015736 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015737 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000015738 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015739 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015740 emsg_funcname("E707: Function name conflicts with variable: %s",
15741 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015742 goto erret;
15743 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015744
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015745 fp = find_func(name);
15746 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015748 if (!eap->forceit)
15749 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015750 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015751 goto erret;
15752 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015753 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015754 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015755 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015756 name);
15757 goto erret;
15758 }
15759 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015760 ga_clear_strings(&(fp->uf_args));
15761 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015762 vim_free(name);
15763 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 }
15766 else
15767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015768 char numbuf[20];
15769
15770 fp = NULL;
15771 if (fudi.fd_newkey == NULL && !eap->forceit)
15772 {
15773 EMSG(_(e_funcdict));
15774 goto erret;
15775 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015776 if (fudi.fd_di == NULL)
15777 {
15778 /* Can't add a function to a locked dictionary */
15779 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15780 goto erret;
15781 }
15782 /* Can't change an existing function if it is locked */
15783 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15784 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015785
15786 /* Give the function a sequential number. Can only be used with a
15787 * Funcref! */
15788 vim_free(name);
15789 sprintf(numbuf, "%d", ++func_nr);
15790 name = vim_strsave((char_u *)numbuf);
15791 if (name == NULL)
15792 goto erret;
15793 }
15794
15795 if (fp == NULL)
15796 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015797 if (fudi.fd_dict == NULL && vim_strchr(name, ':') != NULL)
15798 {
15799 int slen, plen;
15800 char_u *scriptname;
15801
15802 /* Check that the autoload name matches the script name. */
15803 j = FAIL;
15804 if (sourcing_name != NULL)
15805 {
15806 scriptname = autoload_name(name);
15807 if (scriptname != NULL)
15808 {
15809 p = vim_strchr(scriptname, '/');
15810 plen = STRLEN(p);
15811 slen = STRLEN(sourcing_name);
15812 if (slen > plen && fnamecmp(p,
15813 sourcing_name + slen - plen) == 0)
15814 j = OK;
15815 vim_free(scriptname);
15816 }
15817 }
15818 if (j == FAIL)
15819 {
15820 EMSG2(_("E746: Function name does not match script file name: %s"), name);
15821 goto erret;
15822 }
15823 }
15824
15825 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 if (fp == NULL)
15827 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015828
15829 if (fudi.fd_dict != NULL)
15830 {
15831 if (fudi.fd_di == NULL)
15832 {
15833 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015834 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015835 if (fudi.fd_di == NULL)
15836 {
15837 vim_free(fp);
15838 goto erret;
15839 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015840 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15841 {
15842 vim_free(fudi.fd_di);
15843 goto erret;
15844 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015845 }
15846 else
15847 /* overwrite existing dict entry */
15848 clear_tv(&fudi.fd_di->di_tv);
15849 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015850 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015851 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015852 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015853 }
15854
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015856 STRCPY(fp->uf_name, name);
15857 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015859 fp->uf_args = newargs;
15860 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000015861#ifdef FEAT_PROFILE
15862 fp->uf_tml_count = NULL;
15863 fp->uf_tml_total = NULL;
15864 fp->uf_tml_self = NULL;
15865 fp->uf_profiling = FALSE;
15866 if (prof_def_func())
15867 func_do_profile(fp);
15868#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015869 fp->uf_varargs = varargs;
15870 fp->uf_flags = flags;
15871 fp->uf_calls = 0;
15872 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015873 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874
15875erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 ga_clear_strings(&newargs);
15877 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015878ret_free:
15879 vim_free(skip_until);
15880 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883}
15884
15885/*
15886 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015887 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015889 * flags:
15890 * TFN_INT: internal function name OK
15891 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 * Advances "pp" to just after the function name (if no error).
15893 */
15894 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015895trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 char_u **pp;
15897 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015898 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015901 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 char_u *start;
15903 char_u *end;
15904 int lead;
15905 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015908
15909 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015912
15913 /* Check for hard coded <SNR>: already translated function ID (from a user
15914 * command). */
15915 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15916 && (*pp)[2] == (int)KE_SNR)
15917 {
15918 *pp += 3;
15919 len = get_id_len(pp) + 3;
15920 return vim_strnsave(start, len);
15921 }
15922
15923 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15924 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015926 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015928
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015929 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015930 if (end == start)
15931 {
15932 if (!skip)
15933 EMSG(_("E129: Function name required"));
15934 goto theend;
15935 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015936 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015937 {
15938 /*
15939 * Report an invalid expression in braces, unless the expression
15940 * evaluation has been cancelled due to an aborting error, an
15941 * interrupt, or an exception.
15942 */
15943 if (!aborting())
15944 {
15945 if (end != NULL)
15946 EMSG2(_(e_invarg2), start);
15947 }
15948 else
15949 *pp = find_name_end(start, NULL, NULL, TRUE);
15950 goto theend;
15951 }
15952
15953 if (lv.ll_tv != NULL)
15954 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015955 if (fdp != NULL)
15956 {
15957 fdp->fd_dict = lv.ll_dict;
15958 fdp->fd_newkey = lv.ll_newkey;
15959 lv.ll_newkey = NULL;
15960 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015962 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15963 {
15964 name = vim_strsave(lv.ll_tv->vval.v_string);
15965 *pp = end;
15966 }
15967 else
15968 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015969 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15970 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015971 EMSG(_(e_funcref));
15972 else
15973 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015974 name = NULL;
15975 }
15976 goto theend;
15977 }
15978
15979 if (lv.ll_name == NULL)
15980 {
15981 /* Error found, but continue after the function name. */
15982 *pp = end;
15983 goto theend;
15984 }
15985
15986 if (lv.ll_exp_name != NULL)
15987 len = STRLEN(lv.ll_exp_name);
15988 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015989 {
15990 if (lead == 2) /* skip over "s:" */
15991 lv.ll_name += 2;
15992 len = (int)(end - lv.ll_name);
15993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015994
15995 /*
15996 * Copy the function name to allocated memory.
15997 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15998 * Accept <SNR>123_name() outside a script.
15999 */
16000 if (skip)
16001 lead = 0; /* do nothing */
16002 else if (lead > 0)
16003 {
16004 lead = 3;
16005 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
16006 {
16007 if (current_SID <= 0)
16008 {
16009 EMSG(_(e_usingsid));
16010 goto theend;
16011 }
16012 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
16013 lead += (int)STRLEN(sid_buf);
16014 }
16015 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016016 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016017 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016018 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016019 goto theend;
16020 }
16021 name = alloc((unsigned)(len + lead + 1));
16022 if (name != NULL)
16023 {
16024 if (lead > 0)
16025 {
16026 name[0] = K_SPECIAL;
16027 name[1] = KS_EXTRA;
16028 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000016029 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016030 STRCPY(name + 3, sid_buf);
16031 }
16032 mch_memmove(name + lead, lv.ll_name, (size_t)len);
16033 name[len + lead] = NUL;
16034 }
16035 *pp = end;
16036
16037theend:
16038 clear_lval(&lv);
16039 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040}
16041
16042/*
16043 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
16044 * Return 2 if "p" starts with "s:".
16045 * Return 0 otherwise.
16046 */
16047 static int
16048eval_fname_script(p)
16049 char_u *p;
16050{
16051 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
16052 || STRNICMP(p + 1, "SNR>", 4) == 0))
16053 return 5;
16054 if (p[0] == 's' && p[1] == ':')
16055 return 2;
16056 return 0;
16057}
16058
16059/*
16060 * Return TRUE if "p" starts with "<SID>" or "s:".
16061 * Only works if eval_fname_script() returned non-zero for "p"!
16062 */
16063 static int
16064eval_fname_sid(p)
16065 char_u *p;
16066{
16067 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
16068}
16069
16070/*
16071 * List the head of the function: "name(arg1, arg2)".
16072 */
16073 static void
16074list_func_head(fp, indent)
16075 ufunc_T *fp;
16076 int indent;
16077{
16078 int j;
16079
16080 msg_start();
16081 if (indent)
16082 MSG_PUTS(" ");
16083 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016084 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 {
16086 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016087 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 }
16089 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016090 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016092 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 {
16094 if (j)
16095 MSG_PUTS(", ");
16096 msg_puts(FUNCARG(fp, j));
16097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016098 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 {
16100 if (j)
16101 MSG_PUTS(", ");
16102 MSG_PUTS("...");
16103 }
16104 msg_putchar(')');
16105}
16106
16107/*
16108 * Find a function by name, return pointer to it in ufuncs.
16109 * Return NULL for unknown function.
16110 */
16111 static ufunc_T *
16112find_func(name)
16113 char_u *name;
16114{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016115 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016117 hi = hash_find(&func_hashtab, name);
16118 if (!HASHITEM_EMPTY(hi))
16119 return HI2UF(hi);
16120 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121}
16122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016123/*
16124 * Return TRUE if a function "name" exists.
16125 */
16126 static int
16127function_exists(name)
16128 char_u *name;
16129{
16130 char_u *p = name;
16131 int n = FALSE;
16132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016133 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016134 if (p != NULL)
16135 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016136 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016137 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016138 else
16139 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016140 vim_free(p);
16141 }
16142 return n;
16143}
16144
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016145/*
16146 * Return TRUE if "name" looks like a builtin function name: starts with a
16147 * lower case letter and doesn't contain a ':'.
16148 */
16149 static int
16150builtin_function(name)
16151 char_u *name;
16152{
16153 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
16154}
16155
Bram Moolenaar05159a02005-02-26 23:04:13 +000016156#if defined(FEAT_PROFILE) || defined(PROTO)
16157/*
16158 * Start profiling function "fp".
16159 */
16160 static void
16161func_do_profile(fp)
16162 ufunc_T *fp;
16163{
16164 fp->uf_tm_count = 0;
16165 profile_zero(&fp->uf_tm_self);
16166 profile_zero(&fp->uf_tm_total);
16167 if (fp->uf_tml_count == NULL)
16168 fp->uf_tml_count = (int *)alloc_clear((unsigned)
16169 (sizeof(int) * fp->uf_lines.ga_len));
16170 if (fp->uf_tml_total == NULL)
16171 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
16172 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16173 if (fp->uf_tml_self == NULL)
16174 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
16175 (sizeof(proftime_T) * fp->uf_lines.ga_len));
16176 fp->uf_tml_idx = -1;
16177 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
16178 || fp->uf_tml_self == NULL)
16179 return; /* out of memory */
16180
16181 fp->uf_profiling = TRUE;
16182}
16183
16184/*
16185 * Dump the profiling results for all functions in file "fd".
16186 */
16187 void
16188func_dump_profile(fd)
16189 FILE *fd;
16190{
16191 hashitem_T *hi;
16192 int todo;
16193 ufunc_T *fp;
16194 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000016195 ufunc_T **sorttab;
16196 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016197
16198 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000016199 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
16200
Bram Moolenaar05159a02005-02-26 23:04:13 +000016201 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
16202 {
16203 if (!HASHITEM_EMPTY(hi))
16204 {
16205 --todo;
16206 fp = HI2UF(hi);
16207 if (fp->uf_profiling)
16208 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016209 if (sorttab != NULL)
16210 sorttab[st_len++] = fp;
16211
Bram Moolenaar05159a02005-02-26 23:04:13 +000016212 if (fp->uf_name[0] == K_SPECIAL)
16213 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
16214 else
16215 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
16216 if (fp->uf_tm_count == 1)
16217 fprintf(fd, "Called 1 time\n");
16218 else
16219 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
16220 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
16221 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
16222 fprintf(fd, "\n");
16223 fprintf(fd, "count total (s) self (s)\n");
16224
16225 for (i = 0; i < fp->uf_lines.ga_len; ++i)
16226 {
Bram Moolenaar73830342005-02-28 22:48:19 +000016227 prof_func_line(fd, fp->uf_tml_count[i],
16228 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016229 fprintf(fd, "%s\n", FUNCLINE(fp, i));
16230 }
16231 fprintf(fd, "\n");
16232 }
16233 }
16234 }
Bram Moolenaar73830342005-02-28 22:48:19 +000016235
16236 if (sorttab != NULL && st_len > 0)
16237 {
16238 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16239 prof_total_cmp);
16240 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
16241 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
16242 prof_self_cmp);
16243 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
16244 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016245}
Bram Moolenaar73830342005-02-28 22:48:19 +000016246
16247 static void
16248prof_sort_list(fd, sorttab, st_len, title, prefer_self)
16249 FILE *fd;
16250 ufunc_T **sorttab;
16251 int st_len;
16252 char *title;
16253 int prefer_self; /* when equal print only self time */
16254{
16255 int i;
16256 ufunc_T *fp;
16257
16258 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
16259 fprintf(fd, "count total (s) self (s) function\n");
16260 for (i = 0; i < 20 && i < st_len; ++i)
16261 {
16262 fp = sorttab[i];
16263 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
16264 prefer_self);
16265 if (fp->uf_name[0] == K_SPECIAL)
16266 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
16267 else
16268 fprintf(fd, " %s()\n", fp->uf_name);
16269 }
16270 fprintf(fd, "\n");
16271}
16272
16273/*
16274 * Print the count and times for one function or function line.
16275 */
16276 static void
16277prof_func_line(fd, count, total, self, prefer_self)
16278 FILE *fd;
16279 int count;
16280 proftime_T *total;
16281 proftime_T *self;
16282 int prefer_self; /* when equal print only self time */
16283{
16284 if (count > 0)
16285 {
16286 fprintf(fd, "%5d ", count);
16287 if (prefer_self && profile_equal(total, self))
16288 fprintf(fd, " ");
16289 else
16290 fprintf(fd, "%s ", profile_msg(total));
16291 if (!prefer_self && profile_equal(total, self))
16292 fprintf(fd, " ");
16293 else
16294 fprintf(fd, "%s ", profile_msg(self));
16295 }
16296 else
16297 fprintf(fd, " ");
16298}
16299
16300/*
16301 * Compare function for total time sorting.
16302 */
16303 static int
16304#ifdef __BORLANDC__
16305_RTLENTRYF
16306#endif
16307prof_total_cmp(s1, s2)
16308 const void *s1;
16309 const void *s2;
16310{
16311 ufunc_T *p1, *p2;
16312
16313 p1 = *(ufunc_T **)s1;
16314 p2 = *(ufunc_T **)s2;
16315 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
16316}
16317
16318/*
16319 * Compare function for self time sorting.
16320 */
16321 static int
16322#ifdef __BORLANDC__
16323_RTLENTRYF
16324#endif
16325prof_self_cmp(s1, s2)
16326 const void *s1;
16327 const void *s2;
16328{
16329 ufunc_T *p1, *p2;
16330
16331 p1 = *(ufunc_T **)s1;
16332 p2 = *(ufunc_T **)s2;
16333 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
16334}
16335
Bram Moolenaar05159a02005-02-26 23:04:13 +000016336#endif
16337
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016338/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016339 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016340 * Return TRUE if a package was loaded.
16341 */
16342 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016343script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016344 char_u *name;
16345{
16346 char_u *p;
16347 char_u *scriptname;
16348 int ret = FALSE;
16349
16350 /* If there is no colon after name[1] there is no package name. */
16351 p = vim_strchr(name, ':');
16352 if (p == NULL || p <= name + 2)
16353 return FALSE;
16354
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016355 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
16356 scriptname = autoload_name(name);
16357 if (cmd_runtime(scriptname, FALSE) == OK)
16358 ret = TRUE;
16359
16360 vim_free(scriptname);
16361 return ret;
16362}
16363
16364/*
16365 * Return the autoload script name for a function or variable name.
16366 * Returns NULL when out of memory.
16367 */
16368 static char_u *
16369autoload_name(name)
16370 char_u *name;
16371{
16372 char_u *p;
16373 char_u *scriptname;
16374
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016375 /* Get the script file name: replace ':' with '/', append ".vim". */
16376 scriptname = alloc((unsigned)(STRLEN(name) + 14));
16377 if (scriptname == NULL)
16378 return FALSE;
16379 STRCPY(scriptname, "autoload/");
16380 STRCAT(scriptname, name);
16381 *vim_strrchr(scriptname, ':') = NUL;
16382 STRCAT(scriptname, ".vim");
16383 while ((p = vim_strchr(scriptname, ':')) != NULL)
16384 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016385 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000016386}
16387
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
16389
16390/*
16391 * Function given to ExpandGeneric() to obtain the list of user defined
16392 * function names.
16393 */
16394 char_u *
16395get_user_func_name(xp, idx)
16396 expand_T *xp;
16397 int idx;
16398{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016399 static long_u done;
16400 static hashitem_T *hi;
16401 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402
16403 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016405 done = 0;
16406 hi = func_hashtab.ht_array;
16407 }
16408 if (done < func_hashtab.ht_used)
16409 {
16410 if (done++ > 0)
16411 ++hi;
16412 while (HASHITEM_EMPTY(hi))
16413 ++hi;
16414 fp = HI2UF(hi);
16415
16416 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
16417 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
16419 cat_func_name(IObuff, fp);
16420 if (xp->xp_context != EXPAND_USER_FUNC)
16421 {
16422 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016423 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 STRCAT(IObuff, ")");
16425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 return IObuff;
16427 }
16428 return NULL;
16429}
16430
16431#endif /* FEAT_CMDL_COMPL */
16432
16433/*
16434 * Copy the function name of "fp" to buffer "buf".
16435 * "buf" must be able to hold the function name plus three bytes.
16436 * Takes care of script-local function names.
16437 */
16438 static void
16439cat_func_name(buf, fp)
16440 char_u *buf;
16441 ufunc_T *fp;
16442{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016443 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 {
16445 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016446 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 }
16448 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016449 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450}
16451
16452/*
16453 * ":delfunction {name}"
16454 */
16455 void
16456ex_delfunction(eap)
16457 exarg_T *eap;
16458{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016459 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 char_u *p;
16461 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463
16464 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016465 name = trans_function_name(&p, eap->skip, 0, &fudi);
16466 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016468 {
16469 if (fudi.fd_dict != NULL && !eap->skip)
16470 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 if (!ends_excmd(*skipwhite(p)))
16474 {
16475 vim_free(name);
16476 EMSG(_(e_trailing));
16477 return;
16478 }
16479 eap->nextcmd = check_nextcmd(p);
16480 if (eap->nextcmd != NULL)
16481 *p = NUL;
16482
16483 if (!eap->skip)
16484 fp = find_func(name);
16485 vim_free(name);
16486
16487 if (!eap->skip)
16488 {
16489 if (fp == NULL)
16490 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000016491 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 return;
16493 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016494 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 {
16496 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
16497 return;
16498 }
16499
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016500 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016502 /* Delete the dict item that refers to the function, it will
16503 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016504 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016506 else
16507 func_free(fp);
16508 }
16509}
16510
16511/*
16512 * Free a function and remove it from the list of functions.
16513 */
16514 static void
16515func_free(fp)
16516 ufunc_T *fp;
16517{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016518 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016519
16520 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016521 ga_clear_strings(&(fp->uf_args));
16522 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000016523#ifdef FEAT_PROFILE
16524 vim_free(fp->uf_tml_count);
16525 vim_free(fp->uf_tml_total);
16526 vim_free(fp->uf_tml_self);
16527#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016528
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016529 /* remove the function from the function hashtable */
16530 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
16531 if (HASHITEM_EMPTY(hi))
16532 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016534 hash_remove(&func_hashtab, hi);
16535
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016536 vim_free(fp);
16537}
16538
16539/*
16540 * Unreference a Function: decrement the reference count and free it when it
16541 * becomes zero. Only for numbered functions.
16542 */
16543 static void
16544func_unref(name)
16545 char_u *name;
16546{
16547 ufunc_T *fp;
16548
16549 if (name != NULL && isdigit(*name))
16550 {
16551 fp = find_func(name);
16552 if (fp == NULL)
16553 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016554 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016555 {
16556 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016557 * when "uf_calls" becomes zero. */
16558 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016559 func_free(fp);
16560 }
16561 }
16562}
16563
16564/*
16565 * Count a reference to a Function.
16566 */
16567 static void
16568func_ref(name)
16569 char_u *name;
16570{
16571 ufunc_T *fp;
16572
16573 if (name != NULL && isdigit(*name))
16574 {
16575 fp = find_func(name);
16576 if (fp == NULL)
16577 EMSG2(_(e_intern2), "func_ref()");
16578 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016579 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 }
16581}
16582
16583/*
16584 * Call a user function.
16585 */
16586 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000016587call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 ufunc_T *fp; /* pointer to function */
16589 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000016590 typval_T *argvars; /* arguments */
16591 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 linenr_T firstline; /* first line of range */
16593 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000016594 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595{
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 char_u *save_sourcing_name;
16597 linenr_T save_sourcing_lnum;
16598 scid_T save_current_SID;
16599 funccall_T fc;
16600 funccall_T *save_fcp = current_funccal;
16601 int save_did_emsg;
16602 static int depth = 0;
16603 dictitem_T *v;
16604 int fixvar_idx = 0; /* index in fixvar[] */
16605 int i;
16606 int ai;
16607 char_u numbuf[NUMBUFLEN];
16608 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016609#ifdef FEAT_PROFILE
16610 proftime_T wait_start;
16611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612
16613 /* If depth of calling is getting too high, don't execute the function */
16614 if (depth >= p_mfd)
16615 {
16616 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617 rettv->v_type = VAR_NUMBER;
16618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 return;
16620 }
16621 ++depth;
16622
16623 line_breakcheck(); /* check for CTRL-C hit */
16624
Bram Moolenaar33570922005-01-25 22:26:29 +000016625 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016627 fc.rettv = rettv;
16628 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 fc.linenr = 0;
16630 fc.returned = FALSE;
16631 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016633 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 fc.dbg_tick = debug_tick;
16635
Bram Moolenaar33570922005-01-25 22:26:29 +000016636 /*
16637 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
16638 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
16639 * each argument variable and saves a lot of time.
16640 */
16641 /*
16642 * Init l: variables.
16643 */
16644 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000016645 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016646 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 /* Set l:self to "selfdict". */
16648 v = &fc.fixvar[fixvar_idx++].var;
16649 STRCPY(v->di_key, "self");
16650 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
16651 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
16652 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016653 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 v->di_tv.vval.v_dict = selfdict;
16655 ++selfdict->dv_refcount;
16656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016657
Bram Moolenaar33570922005-01-25 22:26:29 +000016658 /*
16659 * Init a: variables.
16660 * Set a:0 to "argcount".
16661 * Set a:000 to a list with room for the "..." arguments.
16662 */
16663 init_var_dict(&fc.l_avars, &fc.l_avars_var);
16664 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016665 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000016666 v = &fc.fixvar[fixvar_idx++].var;
16667 STRCPY(v->di_key, "000");
16668 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16669 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16670 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016671 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016672 v->di_tv.vval.v_list = &fc.l_varlist;
16673 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
16674 fc.l_varlist.lv_refcount = 99999;
16675
16676 /*
16677 * Set a:firstline to "firstline" and a:lastline to "lastline".
16678 * Set a:name to named arguments.
16679 * Set a:N to the "..." arguments.
16680 */
16681 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
16682 (varnumber_T)firstline);
16683 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
16684 (varnumber_T)lastline);
16685 for (i = 0; i < argcount; ++i)
16686 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016687 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016688 if (ai < 0)
16689 /* named argument a:name */
16690 name = FUNCARG(fp, i);
16691 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016692 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016693 /* "..." argument a:1, a:2, etc. */
16694 sprintf((char *)numbuf, "%d", ai + 1);
16695 name = numbuf;
16696 }
16697 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
16698 {
16699 v = &fc.fixvar[fixvar_idx++].var;
16700 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16701 }
16702 else
16703 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016704 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16705 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000016706 if (v == NULL)
16707 break;
16708 v->di_flags = DI_FLAGS_RO;
16709 }
16710 STRCPY(v->di_key, name);
16711 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
16712
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016713 /* Note: the values are copied directly to avoid alloc/free.
16714 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016715 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016716 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016717
16718 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16719 {
16720 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16721 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016722 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016723 }
16724 }
16725
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 /* Don't redraw while executing the function. */
16727 ++RedrawingDisabled;
16728 save_sourcing_name = sourcing_name;
16729 save_sourcing_lnum = sourcing_lnum;
16730 sourcing_lnum = 1;
16731 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016732 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 if (sourcing_name != NULL)
16734 {
16735 if (save_sourcing_name != NULL
16736 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16737 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16738 else
16739 STRCPY(sourcing_name, "function ");
16740 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16741
16742 if (p_verbose >= 12)
16743 {
16744 ++no_wait_return;
16745 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16746 msg_str((char_u *)_("calling %s"), sourcing_name);
16747 if (p_verbose >= 14)
16748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016750 char_u numbuf[NUMBUFLEN];
16751 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752
16753 msg_puts((char_u *)"(");
16754 for (i = 0; i < argcount; ++i)
16755 {
16756 if (i > 0)
16757 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016758 if (argvars[i].v_type == VAR_NUMBER)
16759 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 else
16761 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016762 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016765 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 }
16767 }
16768 msg_puts((char_u *)")");
16769 }
16770 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16771 cmdline_row = msg_row;
16772 --no_wait_return;
16773 }
16774 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000016775#ifdef FEAT_PROFILE
16776 if (do_profiling)
16777 {
16778 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
16779 func_do_profile(fp);
16780 if (fp->uf_profiling
16781 || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16782 {
16783 ++fp->uf_tm_count;
16784 profile_start(&fp->uf_tm_start);
16785 profile_zero(&fp->uf_tm_children);
16786 }
16787 script_prof_save(&wait_start);
16788 }
16789#endif
16790
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016792 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 save_did_emsg = did_emsg;
16794 did_emsg = FALSE;
16795
16796 /* call do_cmdline() to execute the lines */
16797 do_cmdline(NULL, get_func_line, (void *)&fc,
16798 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16799
16800 --RedrawingDisabled;
16801
16802 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016803 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016805 clear_tv(rettv);
16806 rettv->v_type = VAR_NUMBER;
16807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 }
16809
Bram Moolenaar05159a02005-02-26 23:04:13 +000016810#ifdef FEAT_PROFILE
16811 if (fp->uf_profiling || (save_fcp != NULL && &save_fcp->func->uf_profiling))
16812 {
16813 profile_end(&fp->uf_tm_start);
16814 profile_sub_wait(&wait_start, &fp->uf_tm_start);
16815 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
16816 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
16817 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
16818 if (save_fcp != NULL && &save_fcp->func->uf_profiling)
16819 {
16820 profile_add(&save_fcp->func->uf_tm_children, &fp->uf_tm_start);
16821 profile_add(&save_fcp->func->uf_tml_children, &fp->uf_tm_start);
16822 }
16823 }
16824#endif
16825
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 /* when being verbose, mention the return value */
16827 if (p_verbose >= 12)
16828 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016829 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830
16831 ++no_wait_return;
16832 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16833
16834 /* Make sure the output fits in IObuff. */
16835 sn = sourcing_name;
16836 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16837 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16838
16839 if (aborting())
16840 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016843 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016846 char_u buf[MSG_BUF_LEN];
16847 char_u numbuf[NUMBUFLEN];
16848 char_u *tofree;
16849
16850 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16851 buf, MSG_BUF_LEN);
16852 smsg((char_u *)_("%s returning %s"), sn, buf);
16853 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 }
16855 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16856 cmdline_row = msg_row;
16857 --no_wait_return;
16858 }
16859
16860 vim_free(sourcing_name);
16861 sourcing_name = save_sourcing_name;
16862 sourcing_lnum = save_sourcing_lnum;
16863 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016864#ifdef FEAT_PROFILE
16865 if (do_profiling)
16866 script_prof_restore(&wait_start);
16867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868
16869 if (p_verbose >= 12 && sourcing_name != NULL)
16870 {
16871 ++no_wait_return;
16872 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16873 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16874 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16875 cmdline_row = msg_row;
16876 --no_wait_return;
16877 }
16878
16879 did_emsg |= save_did_emsg;
16880 current_funccal = save_fcp;
16881
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 /* The a: variables typevals were not alloced, only free the allocated
16883 * variables. */
16884 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16885
16886 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 --depth;
16888}
16889
16890/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 * Add a number variable "name" to dict "dp" with value "nr".
16892 */
16893 static void
16894add_nr_var(dp, v, name, nr)
16895 dict_T *dp;
16896 dictitem_T *v;
16897 char *name;
16898 varnumber_T nr;
16899{
16900 STRCPY(v->di_key, name);
16901 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16902 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16903 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016904 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016905 v->di_tv.vval.v_number = nr;
16906}
16907
16908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 * ":return [expr]"
16910 */
16911 void
16912ex_return(eap)
16913 exarg_T *eap;
16914{
16915 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 int returning = FALSE;
16918
16919 if (current_funccal == NULL)
16920 {
16921 EMSG(_("E133: :return not inside a function"));
16922 return;
16923 }
16924
16925 if (eap->skip)
16926 ++emsg_skip;
16927
16928 eap->nextcmd = NULL;
16929 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 {
16932 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 }
16937 /* It's safer to return also on error. */
16938 else if (!eap->skip)
16939 {
16940 /*
16941 * Return unless the expression evaluation has been cancelled due to an
16942 * aborting error, an interrupt, or an exception.
16943 */
16944 if (!aborting())
16945 returning = do_return(eap, FALSE, TRUE, NULL);
16946 }
16947
16948 /* When skipping or the return gets pending, advance to the next command
16949 * in this line (!returning). Otherwise, ignore the rest of the line.
16950 * Following lines will be ignored by get_func_line(). */
16951 if (returning)
16952 eap->nextcmd = NULL;
16953 else if (eap->nextcmd == NULL) /* no argument */
16954 eap->nextcmd = check_nextcmd(arg);
16955
16956 if (eap->skip)
16957 --emsg_skip;
16958}
16959
16960/*
16961 * Return from a function. Possibly makes the return pending. Also called
16962 * for a pending return at the ":endtry" or after returning from an extra
16963 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016964 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016965 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 * FALSE when the return gets pending.
16967 */
16968 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 exarg_T *eap;
16971 int reanimate;
16972 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974{
16975 int idx;
16976 struct condstack *cstack = eap->cstack;
16977
16978 if (reanimate)
16979 /* Undo the return. */
16980 current_funccal->returned = FALSE;
16981
16982 /*
16983 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16984 * not in its finally clause (which then is to be executed next) is found.
16985 * In this case, make the ":return" pending for execution at the ":endtry".
16986 * Otherwise, return normally.
16987 */
16988 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16989 if (idx >= 0)
16990 {
16991 cstack->cs_pending[idx] = CSTP_RETURN;
16992
16993 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016994 /* A pending return again gets pending. "rettv" points to an
16995 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016997 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 else
16999 {
17000 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 {
17007 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017008 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017009 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 else
17011 EMSG(_(e_outofmem));
17012 }
17013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017014 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015
17016 if (reanimate)
17017 {
17018 /* The pending return value could be overwritten by a ":return"
17019 * without argument in a finally clause; reset the default
17020 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017021 current_funccal->rettv->v_type = VAR_NUMBER;
17022 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 }
17024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 }
17027 else
17028 {
17029 current_funccal->returned = TRUE;
17030
17031 /* If the return is carried out now, store the return value. For
17032 * a return immediately after reanimation, the value is already
17033 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017039 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 }
17041 }
17042
17043 return idx < 0;
17044}
17045
17046/*
17047 * Free the variable with a pending return value.
17048 */
17049 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050discard_pending_return(rettv)
17051 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052{
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054}
17055
17056/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 * is an allocated string. Used by report_pending() for verbose messages.
17059 */
17060 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061get_return_cmd(rettv)
17062 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017064 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017066 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017068 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017070 if (s == NULL)
17071 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017072
17073 STRCPY(IObuff, ":return ");
17074 STRNCPY(IObuff + 8, s, IOSIZE - 8);
17075 if (STRLEN(s) + 8 >= IOSIZE)
17076 STRCPY(IObuff + IOSIZE - 4, "...");
17077 vim_free(tofree);
17078 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079}
17080
17081/*
17082 * Get next function line.
17083 * Called by do_cmdline() to get the next line.
17084 * Returns allocated string, or NULL for end of function.
17085 */
17086/* ARGSUSED */
17087 char_u *
17088get_func_line(c, cookie, indent)
17089 int c; /* not used */
17090 void *cookie;
17091 int indent; /* not used */
17092{
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017094 ufunc_T *fp = fcp->func;
17095 char_u *retval;
17096 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097
17098 /* If breakpoints have been added/deleted need to check for it. */
17099 if (fcp->dbg_tick != debug_tick)
17100 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017101 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 sourcing_lnum);
17103 fcp->dbg_tick = debug_tick;
17104 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017105#ifdef FEAT_PROFILE
17106 if (do_profiling)
17107 func_line_end(cookie);
17108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaar05159a02005-02-26 23:04:13 +000017110 gap = &fp->uf_lines;
17111 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 retval = NULL;
17113 else if (fcp->returned || fcp->linenr >= gap->ga_len)
17114 retval = NULL;
17115 else
17116 {
17117 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
17118 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017119#ifdef FEAT_PROFILE
17120 if (do_profiling)
17121 func_line_start(cookie);
17122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 }
17124
17125 /* Did we encounter a breakpoint? */
17126 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
17127 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017128 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017130 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 sourcing_lnum);
17132 fcp->dbg_tick = debug_tick;
17133 }
17134
17135 return retval;
17136}
17137
Bram Moolenaar05159a02005-02-26 23:04:13 +000017138#if defined(FEAT_PROFILE) || defined(PROTO)
17139/*
17140 * Called when starting to read a function line.
17141 * "sourcing_lnum" must be correct!
17142 * When skipping lines it may not actually be executed, but we won't find out
17143 * until later and we need to store the time now.
17144 */
17145 void
17146func_line_start(cookie)
17147 void *cookie;
17148{
17149 funccall_T *fcp = (funccall_T *)cookie;
17150 ufunc_T *fp = fcp->func;
17151
17152 if (fp->uf_profiling && sourcing_lnum >= 1
17153 && sourcing_lnum <= fp->uf_lines.ga_len)
17154 {
17155 fp->uf_tml_idx = sourcing_lnum - 1;
17156 fp->uf_tml_execed = FALSE;
17157 profile_start(&fp->uf_tml_start);
17158 profile_zero(&fp->uf_tml_children);
17159 profile_get_wait(&fp->uf_tml_wait);
17160 }
17161}
17162
17163/*
17164 * Called when actually executing a function line.
17165 */
17166 void
17167func_line_exec(cookie)
17168 void *cookie;
17169{
17170 funccall_T *fcp = (funccall_T *)cookie;
17171 ufunc_T *fp = fcp->func;
17172
17173 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17174 fp->uf_tml_execed = TRUE;
17175}
17176
17177/*
17178 * Called when done with a function line.
17179 */
17180 void
17181func_line_end(cookie)
17182 void *cookie;
17183{
17184 funccall_T *fcp = (funccall_T *)cookie;
17185 ufunc_T *fp = fcp->func;
17186
17187 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
17188 {
17189 if (fp->uf_tml_execed)
17190 {
17191 ++fp->uf_tml_count[fp->uf_tml_idx];
17192 profile_end(&fp->uf_tml_start);
17193 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
17194 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
17195 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
17196 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
17197 }
17198 fp->uf_tml_idx = -1;
17199 }
17200}
17201#endif
17202
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203/*
17204 * Return TRUE if the currently active function should be ended, because a
17205 * return was encountered or an error occured. Used inside a ":while".
17206 */
17207 int
17208func_has_ended(cookie)
17209 void *cookie;
17210{
Bram Moolenaar33570922005-01-25 22:26:29 +000017211 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212
17213 /* Ignore the "abort" flag if the abortion behavior has been changed due to
17214 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017215 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 || fcp->returned);
17217}
17218
17219/*
17220 * return TRUE if cookie indicates a function which "abort"s on errors.
17221 */
17222 int
17223func_has_abort(cookie)
17224 void *cookie;
17225{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017226 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227}
17228
17229#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
17230typedef enum
17231{
17232 VAR_FLAVOUR_DEFAULT,
17233 VAR_FLAVOUR_SESSION,
17234 VAR_FLAVOUR_VIMINFO
17235} var_flavour_T;
17236
17237static var_flavour_T var_flavour __ARGS((char_u *varname));
17238
17239 static var_flavour_T
17240var_flavour(varname)
17241 char_u *varname;
17242{
17243 char_u *p = varname;
17244
17245 if (ASCII_ISUPPER(*p))
17246 {
17247 while (*(++p))
17248 if (ASCII_ISLOWER(*p))
17249 return VAR_FLAVOUR_SESSION;
17250 return VAR_FLAVOUR_VIMINFO;
17251 }
17252 else
17253 return VAR_FLAVOUR_DEFAULT;
17254}
17255#endif
17256
17257#if defined(FEAT_VIMINFO) || defined(PROTO)
17258/*
17259 * Restore global vars that start with a capital from the viminfo file
17260 */
17261 int
17262read_viminfo_varlist(virp, writing)
17263 vir_T *virp;
17264 int writing;
17265{
17266 char_u *tab;
17267 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000017268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
17270 if (!writing && (find_viminfo_parameter('!') != NULL))
17271 {
17272 tab = vim_strchr(virp->vir_line + 1, '\t');
17273 if (tab != NULL)
17274 {
17275 *tab++ = '\0'; /* isolate the variable name */
17276 if (*tab == 'S') /* string var */
17277 is_string = TRUE;
17278
17279 tab = vim_strchr(tab, '\t');
17280 if (tab != NULL)
17281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 if (is_string)
17283 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017284 tv.v_type = VAR_STRING;
17285 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 }
17288 else
17289 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017290 tv.v_type = VAR_NUMBER;
17291 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017293 set_var(virp->vir_line + 1, &tv, FALSE);
17294 if (is_string)
17295 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 }
17297 }
17298 }
17299
17300 return viminfo_readline(virp);
17301}
17302
17303/*
17304 * Write global vars that start with a capital to the viminfo file
17305 */
17306 void
17307write_viminfo_varlist(fp)
17308 FILE *fp;
17309{
Bram Moolenaar33570922005-01-25 22:26:29 +000017310 hashitem_T *hi;
17311 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017312 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017313 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017314 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017316 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317
17318 if (find_viminfo_parameter('!') == NULL)
17319 return;
17320
17321 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000017322
Bram Moolenaar33570922005-01-25 22:26:29 +000017323 todo = globvarht.ht_used;
17324 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017326 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017328 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 this_var = HI2DI(hi);
17330 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000017333 {
17334 case VAR_STRING: s = "STR"; break;
17335 case VAR_NUMBER: s = "NUM"; break;
17336 default: continue;
17337 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017338 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017339 p = echo_string(&this_var->di_tv, &tofree, numbuf);
17340 if (p != NULL)
17341 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000017342 vim_free(tofree);
17343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 }
17345 }
17346}
17347#endif
17348
17349#if defined(FEAT_SESSION) || defined(PROTO)
17350 int
17351store_session_globals(fd)
17352 FILE *fd;
17353{
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 hashitem_T *hi;
17355 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000017356 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 char_u *p, *t;
17358
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 todo = globvarht.ht_used;
17360 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017364 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 this_var = HI2DI(hi);
17366 if ((this_var->di_tv.v_type == VAR_NUMBER
17367 || this_var->di_tv.v_type == VAR_STRING)
17368 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000017369 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017370 /* Escape special characters with a backslash. Turn a LF and
17371 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000017373 (char_u *)"\\\"\n\r");
17374 if (p == NULL) /* out of memory */
17375 break;
17376 for (t = p; *t != NUL; ++t)
17377 if (*t == '\n')
17378 *t = 'n';
17379 else if (*t == '\r')
17380 *t = 'r';
17381 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 this_var->di_key,
17383 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17384 : ' ',
17385 p,
17386 (this_var->di_tv.v_type == VAR_STRING) ? '"'
17387 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000017388 || put_eol(fd) == FAIL)
17389 {
17390 vim_free(p);
17391 return FAIL;
17392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 vim_free(p);
17394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 }
17396 }
17397 return OK;
17398}
17399#endif
17400
17401#endif /* FEAT_EVAL */
17402
17403#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
17404
17405
17406#ifdef WIN3264
17407/*
17408 * Functions for ":8" filename modifier: get 8.3 version of a filename.
17409 */
17410static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17411static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
17412static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
17413
17414/*
17415 * Get the short pathname of a file.
17416 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
17417 */
17418 static int
17419get_short_pathname(fnamep, bufp, fnamelen)
17420 char_u **fnamep;
17421 char_u **bufp;
17422 int *fnamelen;
17423{
17424 int l,len;
17425 char_u *newbuf;
17426
17427 len = *fnamelen;
17428
17429 l = GetShortPathName(*fnamep, *fnamep, len);
17430 if (l > len - 1)
17431 {
17432 /* If that doesn't work (not enough space), then save the string
17433 * and try again with a new buffer big enough
17434 */
17435 newbuf = vim_strnsave(*fnamep, l);
17436 if (newbuf == NULL)
17437 return 0;
17438
17439 vim_free(*bufp);
17440 *fnamep = *bufp = newbuf;
17441
17442 l = GetShortPathName(*fnamep,*fnamep,l+1);
17443
17444 /* Really should always succeed, as the buffer is big enough */
17445 }
17446
17447 *fnamelen = l;
17448 return 1;
17449}
17450
17451/*
17452 * Create a short path name. Returns the length of the buffer it needs.
17453 * Doesn't copy over the end of the buffer passed in.
17454 */
17455 static int
17456shortpath_for_invalid_fname(fname, bufp, fnamelen)
17457 char_u **fname;
17458 char_u **bufp;
17459 int *fnamelen;
17460{
17461 char_u *s, *p, *pbuf2, *pbuf3;
17462 char_u ch;
17463 int l,len,len2,plen,slen;
17464
17465 /* Make a copy */
17466 len2 = *fnamelen;
17467 pbuf2 = vim_strnsave(*fname, len2);
17468 pbuf3 = NULL;
17469
17470 s = pbuf2 + len2 - 1; /* Find the end */
17471 slen = 1;
17472 plen = len2;
17473
17474 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017475 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 {
17477 --s;
17478 ++slen;
17479 --plen;
17480 }
17481
17482 do
17483 {
17484 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017485 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 {
17487 --s;
17488 ++slen;
17489 --plen;
17490 }
17491 if (s <= pbuf2)
17492 break;
17493
17494 /* Remeber the character that is about to be blatted */
17495 ch = *s;
17496 *s = 0; /* get_short_pathname requires a null-terminated string */
17497
17498 /* Try it in situ */
17499 p = pbuf2;
17500 if (!get_short_pathname(&p, &pbuf3, &plen))
17501 {
17502 vim_free(pbuf2);
17503 return -1;
17504 }
17505 *s = ch; /* Preserve the string */
17506 } while (plen == 0);
17507
17508 if (plen > 0)
17509 {
17510 /* Remeber the length of the new string. */
17511 *fnamelen = len = plen + slen;
17512 vim_free(*bufp);
17513 if (len > len2)
17514 {
17515 /* If there's not enough space in the currently allocated string,
17516 * then copy it to a buffer big enough.
17517 */
17518 *fname= *bufp = vim_strnsave(p, len);
17519 if (*fname == NULL)
17520 return -1;
17521 }
17522 else
17523 {
17524 /* Transfer pbuf2 to being the main buffer (it's big enough) */
17525 *fname = *bufp = pbuf2;
17526 if (p != pbuf2)
17527 strncpy(*fname, p, plen);
17528 pbuf2 = NULL;
17529 }
17530 /* Concat the next bit */
17531 strncpy(*fname + plen, s, slen);
17532 (*fname)[len] = '\0';
17533 }
17534 vim_free(pbuf3);
17535 vim_free(pbuf2);
17536 return 0;
17537}
17538
17539/*
17540 * Get a pathname for a partial path.
17541 */
17542 static int
17543shortpath_for_partial(fnamep, bufp, fnamelen)
17544 char_u **fnamep;
17545 char_u **bufp;
17546 int *fnamelen;
17547{
17548 int sepcount, len, tflen;
17549 char_u *p;
17550 char_u *pbuf, *tfname;
17551 int hasTilde;
17552
17553 /* Count up the path seperators from the RHS.. so we know which part
17554 * of the path to return.
17555 */
17556 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017557 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 if (vim_ispathsep(*p))
17559 ++sepcount;
17560
17561 /* Need full path first (use expand_env() to remove a "~/") */
17562 hasTilde = (**fnamep == '~');
17563 if (hasTilde)
17564 pbuf = tfname = expand_env_save(*fnamep);
17565 else
17566 pbuf = tfname = FullName_save(*fnamep, FALSE);
17567
17568 len = tflen = STRLEN(tfname);
17569
17570 if (!get_short_pathname(&tfname, &pbuf, &len))
17571 return -1;
17572
17573 if (len == 0)
17574 {
17575 /* Don't have a valid filename, so shorten the rest of the
17576 * path if we can. This CAN give us invalid 8.3 filenames, but
17577 * there's not a lot of point in guessing what it might be.
17578 */
17579 len = tflen;
17580 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
17581 return -1;
17582 }
17583
17584 /* Count the paths backward to find the beginning of the desired string. */
17585 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017586 {
17587#ifdef FEAT_MBYTE
17588 if (has_mbyte)
17589 p -= mb_head_off(tfname, p);
17590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 if (vim_ispathsep(*p))
17592 {
17593 if (sepcount == 0 || (hasTilde && sepcount == 1))
17594 break;
17595 else
17596 sepcount --;
17597 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 if (hasTilde)
17600 {
17601 --p;
17602 if (p >= tfname)
17603 *p = '~';
17604 else
17605 return -1;
17606 }
17607 else
17608 ++p;
17609
17610 /* Copy in the string - p indexes into tfname - allocated at pbuf */
17611 vim_free(*bufp);
17612 *fnamelen = (int)STRLEN(p);
17613 *bufp = pbuf;
17614 *fnamep = p;
17615
17616 return 0;
17617}
17618#endif /* WIN3264 */
17619
17620/*
17621 * Adjust a filename, according to a string of modifiers.
17622 * *fnamep must be NUL terminated when called. When returning, the length is
17623 * determined by *fnamelen.
17624 * Returns valid flags.
17625 * When there is an error, *fnamep is set to NULL.
17626 */
17627 int
17628modify_fname(src, usedlen, fnamep, bufp, fnamelen)
17629 char_u *src; /* string with modifiers */
17630 int *usedlen; /* characters after src that are used */
17631 char_u **fnamep; /* file name so far */
17632 char_u **bufp; /* buffer for allocated file name or NULL */
17633 int *fnamelen; /* length of fnamep */
17634{
17635 int valid = 0;
17636 char_u *tail;
17637 char_u *s, *p, *pbuf;
17638 char_u dirname[MAXPATHL];
17639 int c;
17640 int has_fullname = 0;
17641#ifdef WIN3264
17642 int has_shortname = 0;
17643#endif
17644
17645repeat:
17646 /* ":p" - full path/file_name */
17647 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
17648 {
17649 has_fullname = 1;
17650
17651 valid |= VALID_PATH;
17652 *usedlen += 2;
17653
17654 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
17655 if ((*fnamep)[0] == '~'
17656#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
17657 && ((*fnamep)[1] == '/'
17658# ifdef BACKSLASH_IN_FILENAME
17659 || (*fnamep)[1] == '\\'
17660# endif
17661 || (*fnamep)[1] == NUL)
17662
17663#endif
17664 )
17665 {
17666 *fnamep = expand_env_save(*fnamep);
17667 vim_free(*bufp); /* free any allocated file name */
17668 *bufp = *fnamep;
17669 if (*fnamep == NULL)
17670 return -1;
17671 }
17672
17673 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017674 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 {
17676 if (vim_ispathsep(*p)
17677 && p[1] == '.'
17678 && (p[2] == NUL
17679 || vim_ispathsep(p[2])
17680 || (p[2] == '.'
17681 && (p[3] == NUL || vim_ispathsep(p[3])))))
17682 break;
17683 }
17684
17685 /* FullName_save() is slow, don't use it when not needed. */
17686 if (*p != NUL || !vim_isAbsName(*fnamep))
17687 {
17688 *fnamep = FullName_save(*fnamep, *p != NUL);
17689 vim_free(*bufp); /* free any allocated file name */
17690 *bufp = *fnamep;
17691 if (*fnamep == NULL)
17692 return -1;
17693 }
17694
17695 /* Append a path separator to a directory. */
17696 if (mch_isdir(*fnamep))
17697 {
17698 /* Make room for one or two extra characters. */
17699 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17700 vim_free(*bufp); /* free any allocated file name */
17701 *bufp = *fnamep;
17702 if (*fnamep == NULL)
17703 return -1;
17704 add_pathsep(*fnamep);
17705 }
17706 }
17707
17708 /* ":." - path relative to the current directory */
17709 /* ":~" - path relative to the home directory */
17710 /* ":8" - shortname path - postponed till after */
17711 while (src[*usedlen] == ':'
17712 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
17713 {
17714 *usedlen += 2;
17715 if (c == '8')
17716 {
17717#ifdef WIN3264
17718 has_shortname = 1; /* Postpone this. */
17719#endif
17720 continue;
17721 }
17722 pbuf = NULL;
17723 /* Need full path first (use expand_env() to remove a "~/") */
17724 if (!has_fullname)
17725 {
17726 if (c == '.' && **fnamep == '~')
17727 p = pbuf = expand_env_save(*fnamep);
17728 else
17729 p = pbuf = FullName_save(*fnamep, FALSE);
17730 }
17731 else
17732 p = *fnamep;
17733
17734 has_fullname = 0;
17735
17736 if (p != NULL)
17737 {
17738 if (c == '.')
17739 {
17740 mch_dirname(dirname, MAXPATHL);
17741 s = shorten_fname(p, dirname);
17742 if (s != NULL)
17743 {
17744 *fnamep = s;
17745 if (pbuf != NULL)
17746 {
17747 vim_free(*bufp); /* free any allocated file name */
17748 *bufp = pbuf;
17749 pbuf = NULL;
17750 }
17751 }
17752 }
17753 else
17754 {
17755 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17756 /* Only replace it when it starts with '~' */
17757 if (*dirname == '~')
17758 {
17759 s = vim_strsave(dirname);
17760 if (s != NULL)
17761 {
17762 *fnamep = s;
17763 vim_free(*bufp);
17764 *bufp = s;
17765 }
17766 }
17767 }
17768 vim_free(pbuf);
17769 }
17770 }
17771
17772 tail = gettail(*fnamep);
17773 *fnamelen = (int)STRLEN(*fnamep);
17774
17775 /* ":h" - head, remove "/file_name", can be repeated */
17776 /* Don't remove the first "/" or "c:\" */
17777 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
17778 {
17779 valid |= VALID_HEAD;
17780 *usedlen += 2;
17781 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017782 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 --tail;
17784 *fnamelen = (int)(tail - *fnamep);
17785#ifdef VMS
17786 if (*fnamelen > 0)
17787 *fnamelen += 1; /* the path separator is part of the path */
17788#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017789 while (tail > s && !after_pathsep(s, tail))
17790 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 }
17792
17793 /* ":8" - shortname */
17794 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
17795 {
17796 *usedlen += 2;
17797#ifdef WIN3264
17798 has_shortname = 1;
17799#endif
17800 }
17801
17802#ifdef WIN3264
17803 /* Check shortname after we have done 'heads' and before we do 'tails'
17804 */
17805 if (has_shortname)
17806 {
17807 pbuf = NULL;
17808 /* Copy the string if it is shortened by :h */
17809 if (*fnamelen < (int)STRLEN(*fnamep))
17810 {
17811 p = vim_strnsave(*fnamep, *fnamelen);
17812 if (p == 0)
17813 return -1;
17814 vim_free(*bufp);
17815 *bufp = *fnamep = p;
17816 }
17817
17818 /* Split into two implementations - makes it easier. First is where
17819 * there isn't a full name already, second is where there is.
17820 */
17821 if (!has_fullname && !vim_isAbsName(*fnamep))
17822 {
17823 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
17824 return -1;
17825 }
17826 else
17827 {
17828 int l;
17829
17830 /* Simple case, already have the full-name
17831 * Nearly always shorter, so try first time. */
17832 l = *fnamelen;
17833 if (!get_short_pathname(fnamep, bufp, &l))
17834 return -1;
17835
17836 if (l == 0)
17837 {
17838 /* Couldn't find the filename.. search the paths.
17839 */
17840 l = *fnamelen;
17841 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17842 return -1;
17843 }
17844 *fnamelen = l;
17845 }
17846 }
17847#endif /* WIN3264 */
17848
17849 /* ":t" - tail, just the basename */
17850 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17851 {
17852 *usedlen += 2;
17853 *fnamelen -= (int)(tail - *fnamep);
17854 *fnamep = tail;
17855 }
17856
17857 /* ":e" - extension, can be repeated */
17858 /* ":r" - root, without extension, can be repeated */
17859 while (src[*usedlen] == ':'
17860 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17861 {
17862 /* find a '.' in the tail:
17863 * - for second :e: before the current fname
17864 * - otherwise: The last '.'
17865 */
17866 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17867 s = *fnamep - 2;
17868 else
17869 s = *fnamep + *fnamelen - 1;
17870 for ( ; s > tail; --s)
17871 if (s[0] == '.')
17872 break;
17873 if (src[*usedlen + 1] == 'e') /* :e */
17874 {
17875 if (s > tail)
17876 {
17877 *fnamelen += (int)(*fnamep - (s + 1));
17878 *fnamep = s + 1;
17879#ifdef VMS
17880 /* cut version from the extension */
17881 s = *fnamep + *fnamelen - 1;
17882 for ( ; s > *fnamep; --s)
17883 if (s[0] == ';')
17884 break;
17885 if (s > *fnamep)
17886 *fnamelen = s - *fnamep;
17887#endif
17888 }
17889 else if (*fnamep <= tail)
17890 *fnamelen = 0;
17891 }
17892 else /* :r */
17893 {
17894 if (s > tail) /* remove one extension */
17895 *fnamelen = (int)(s - *fnamep);
17896 }
17897 *usedlen += 2;
17898 }
17899
17900 /* ":s?pat?foo?" - substitute */
17901 /* ":gs?pat?foo?" - global substitute */
17902 if (src[*usedlen] == ':'
17903 && (src[*usedlen + 1] == 's'
17904 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17905 {
17906 char_u *str;
17907 char_u *pat;
17908 char_u *sub;
17909 int sep;
17910 char_u *flags;
17911 int didit = FALSE;
17912
17913 flags = (char_u *)"";
17914 s = src + *usedlen + 2;
17915 if (src[*usedlen + 1] == 'g')
17916 {
17917 flags = (char_u *)"g";
17918 ++s;
17919 }
17920
17921 sep = *s++;
17922 if (sep)
17923 {
17924 /* find end of pattern */
17925 p = vim_strchr(s, sep);
17926 if (p != NULL)
17927 {
17928 pat = vim_strnsave(s, (int)(p - s));
17929 if (pat != NULL)
17930 {
17931 s = p + 1;
17932 /* find end of substitution */
17933 p = vim_strchr(s, sep);
17934 if (p != NULL)
17935 {
17936 sub = vim_strnsave(s, (int)(p - s));
17937 str = vim_strnsave(*fnamep, *fnamelen);
17938 if (sub != NULL && str != NULL)
17939 {
17940 *usedlen = (int)(p + 1 - src);
17941 s = do_string_sub(str, pat, sub, flags);
17942 if (s != NULL)
17943 {
17944 *fnamep = s;
17945 *fnamelen = (int)STRLEN(s);
17946 vim_free(*bufp);
17947 *bufp = s;
17948 didit = TRUE;
17949 }
17950 }
17951 vim_free(sub);
17952 vim_free(str);
17953 }
17954 vim_free(pat);
17955 }
17956 }
17957 /* after using ":s", repeat all the modifiers */
17958 if (didit)
17959 goto repeat;
17960 }
17961 }
17962
17963 return valid;
17964}
17965
17966/*
17967 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
17968 * "flags" can be "g" to do a global substitute.
17969 * Returns an allocated string, NULL for error.
17970 */
17971 char_u *
17972do_string_sub(str, pat, sub, flags)
17973 char_u *str;
17974 char_u *pat;
17975 char_u *sub;
17976 char_u *flags;
17977{
17978 int sublen;
17979 regmatch_T regmatch;
17980 int i;
17981 int do_all;
17982 char_u *tail;
17983 garray_T ga;
17984 char_u *ret;
17985 char_u *save_cpo;
17986
17987 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
17988 save_cpo = p_cpo;
17989 p_cpo = (char_u *)"";
17990
17991 ga_init2(&ga, 1, 200);
17992
17993 do_all = (flags[0] == 'g');
17994
17995 regmatch.rm_ic = p_ic;
17996 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17997 if (regmatch.regprog != NULL)
17998 {
17999 tail = str;
18000 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
18001 {
18002 /*
18003 * Get some space for a temporary buffer to do the substitution
18004 * into. It will contain:
18005 * - The text up to where the match is.
18006 * - The substituted text.
18007 * - The text after the match.
18008 */
18009 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
18010 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
18011 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
18012 {
18013 ga_clear(&ga);
18014 break;
18015 }
18016
18017 /* copy the text up to where the match is */
18018 i = (int)(regmatch.startp[0] - tail);
18019 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
18020 /* add the substituted text */
18021 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
18022 + ga.ga_len + i, TRUE, TRUE, FALSE);
18023 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 /* avoid getting stuck on a match with an empty string */
18025 if (tail == regmatch.endp[0])
18026 {
18027 if (*tail == NUL)
18028 break;
18029 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
18030 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 }
18032 else
18033 {
18034 tail = regmatch.endp[0];
18035 if (*tail == NUL)
18036 break;
18037 }
18038 if (!do_all)
18039 break;
18040 }
18041
18042 if (ga.ga_data != NULL)
18043 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
18044
18045 vim_free(regmatch.regprog);
18046 }
18047
18048 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
18049 ga_clear(&ga);
18050 p_cpo = save_cpo;
18051
18052 return ret;
18053}
18054
18055#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */